/*
Author: Arturas Piksrys(arturas@ring.lt)
*/
var PhotoSlider = {
    
    init: function(settings){
        this.images = new Array();
        this.images["thumbs"] = new Array();
        this.images["big"] = new Array();
        this.holder = $(settings.holder);
        this.urls = new Array();
        this.urls["thumbs"] = settings.thumbs_url;
        this.urls["big"] = settings.big_url;
        if(settings.object_url){
            this.urls["object"] = settings.object_url;
        }
        this.slb = 0;
    },
    
    
    set: function(dir_index, image_id, title){
        var image = new Image();
        image.src = this.urls["thumbs"].replace(/#image_id#/g, image_id).replace(/#dir_index#/g, dir_index);
        image.src = this.urls["thumbs"].replace(/#image_id#/g, image_id).replace(/#dir_index#/g, dir_index);
        this.images["thumbs"].push(image);
		if(image_id > 0){
			var str = '<a href="'+ this.urls["big"].replace(/#image_id#/g, image_id).replace(/#dir_index#/g, dir_index) +'" rel="gallery[image]" class="lightview" title="'+title+'"><img src="'+ this.urls["object"].replace(/#image_id#/g, image_id).replace(/#dir_index#/g, dir_index) +'" border="0" alt="" /></a>';
		}else{
			var str = '<img src="'+ this.urls["big"] +'" alt="" />';
		}
		this.images["big"].push(str);
        this.max_photos = 4;
    },
    
    setProjectPhotos: function(id){
        var image = new Image();
        image.src = this.urls["thumbs"].replace(/#id#/g, id);
        this.images["thumbs"].push(image);
        
        var str = '<a href="/files/prbig/'+ id+ '/image.png" class="lightview" rel="gallery[image]" title=""><img src="'+ this.urls["big"].replace(/#id#/g, id) +'" border="0" alt="" /></a>';
        this.images["big"].push(str);
        this.max_photos = 4;
    },
    
    draw: function(){
        var span = document.createElement('span');
        span.setAttribute('id', 'big');
        span.innerHTML = this.images["big"][0];
		
		var hidden_span = document.createElement('span');
		hidden_span.setAttribute('id', 'hidden_photos');
		
		hidden_span.innerHTML = '';
		for(var i=1; i <= this.images["big"].length; i++){
			hidden_span.innerHTML += this.images["big"][i];
		}
		
		
        this.holder.appendChild(span);
        this.holder.appendChild(hidden_span);

        this.ul = document.createElement('ul');
        
        this.createThumbs(0);
        this.holder.appendChild(this.ul);
        
    },
    
    createThumbs: function(seq){
        if(this.images["big"].length == 1) return;
         
        if(this.images["big"].length > this.max_photos){
            var to_left_a = document.createElement('a');
            to_left_a.setAttribute("href", "javascript:PhotoSlider.prev();");
            var left_img = new Image();
            left_img.src = '/images/back.gif';
            left_img.style.border = 0;
            left_img.style.margin = '13 2 0 0';
            to_left_a.appendChild(left_img);
            var li = document.createElement('li');
            li.appendChild(to_left_a);
            this.ul.appendChild(li);
         }
        
        for(var i = seq; i < (this.max_photos + seq); i++){
            if(this.images["thumbs"][i]){
                var li = document.createElement('li');
                var a = document.createElement('a');
                a.setAttribute("href", "javascript:PhotoSlider.changePhoto("+ i +");");
                a.appendChild(this.images["thumbs"][i]);
                li.appendChild(a);
                this.ul.appendChild(li);
             }
        }
        
        
        if(this.images["big"].length > this.max_photos){
            var to_right_a = document.createElement('a');
            to_right_a.setAttribute("href", "javascript:PhotoSlider.next();");
            to_right_a.className = 'to_right';
            var to_right_img = new Image();
            to_right_img.src = '/images/next.gif';
            to_right_img.style.border = 0;
            to_right_img.style.margin = '13 0 0 2';
            to_right_a.appendChild(to_right_img);
            var li = document.createElement('li');
            li.appendChild(to_right_a);
            this.ul.appendChild(li);
        }
    },
    
    changePhoto: function(seq){
        $("big").innerHTML = '';
        $("big").innerHTML = this.images["big"][seq];
		Lightview.updateViews()
    },
    
    next: function(){
        var max = this.images["thumbs"].length - this.max_photos;
        if(this.slb >= max) return;
        this.slb++;
        this.ul.innerHTML = '';
        this.createThumbs(this.slb);
    },
    
    prev: function(){
       if(this.slb <= 0) return;
       this.slb--;
       this.ul.innerHTML = '';
       this.createThumbs(this.slb);
    }
}