/*
	Kopia ze Znajomi.dev.te.interia.pl 2007-05-02
*/
myPos = 0;
thumb = null;
thumb2 = null;
function SlideMove(objName,itemsCnt,itemsWidth,align) {
    this.objName = objName || '';
    this.itemsCnt = itemsCnt || 0;
    this.itemsWidth = itemsWidth || 0;
    this.align = align || 'vertical';
    this.moveElements = 1;
    this.visibleElements = 1;
    this.move = false;
    this.click = true;
    this.step = 10;
    this.speed = 20;
    this.margin = 0;
    this.doEnd = false;
    this.up = 1;
    this.down = 2;
    this.endMode = 0;
	this.pack = 0;    
    this.init = function() {
        //lewo
        var left = $(this.objName+'left');
        left.onclick = new Function('Gallery.doL();');
        //prawo
        var right = $(this.objName+'right');
        right.onclick = new Function('Gallery.doR();');
        thumb = $(this.objName+'show');
        thumb2 = $('galItems');
        //this.setPos(0);
        this.disableButton('left');
    }
    
    this.getElemPosition = function() {
    	var len = this.itemsWidth * this.visibleElements;
    	var pos = myPos;
    	var ve = this.visibleElements;
    	var iw = this.itemsWidth;	
    	var dz = Math.floor(Math.abs(pos/iw)+1);
    	this.down = Math.floor(dz+1);    	
    	this.up = Math.abs(pos/iw)+1;
    }
    
    this.checkButtons = function() {
    	if (this.endMode == 0) {
    		this.enableButton('right');
    	}
    	if (this.endMode == 1) {
	    	var z = this.itemsWidth *(this.itemsCnt-this.visibleElements);
	    	if (-1*z == myPos) {
	    		this.disableButton('right');
	    	} else {
	    		this.enableButton('right');
	    	}
    	}
    	if (myPos == 0) {
    		this.disableButton('left');
    	} else {
    		this.enableButton('left');
    	}
    }
    this.setPos = function(pos) {
		$('galItems').scrollLeft -= pos;
		myPos += pos;
    }
    this.getPos = function() {
        return myPos;
    }
    this.setSpeed = function (step,speed) {
        this.step = step;
        this.speed = speed;
    }
    
    this.moveLeft = function() {
        if (this.getPos() < 0 ) {
            this.move = true;
            this.doMove(1);
            this.disableButton('left');
        } else {
            this.move = false;
            this.disableButton('left');
        }
    }
    
    this.doL = function() {
    	if (!Gallery.move) {
    		Gallery.moveLeft();Gallery.doEndLeft(0);
    	}
    	if (this.up == 1 && this.endMode==0 && prv != 0) {
    		getMorePics('prev');
    	}
    }
    
    this.doR = function() {
    	if (!Gallery.move) {
    		Gallery.moveRight();Gallery.doEndRight(0);
    	}
    	if (this.down == this.itemsCnt-1 && this.endMode==0 && next != 0) {
    		getMorePics('next');
    	}
    }
    this.moveRight = function() {
        if (Math.abs(this.getPos()) < (this.itemsWidth*(this.itemsCnt-this.visibleElements)) ) {   
            this.move = true;
            this.doMove(-1);
        	this.disableButton('right');
        } else {
            this.move = false;
            this.disableButton('right');
        }
    }
    this.doEndLeft = function(par) {
        if (myPos%(this.itemsWidth*3) == 0) {
            this.move=false;
            this.checkButtons();
            this.getElemPosition();
            return;
        } else {
            this.setPos(this.step);
            setTimeout('Gallery.doEndLeft('+myPos+')',this.speed);
        }
    }
    this.doEndRight = function(par) {
        if (myPos % (this.itemsWidth*3) == 0 || myPos == -(galWidth)) {
            this.move=false;
            this.checkButtons();
            this.getElemPosition();
            return;
            
        } else {
            this.setPos(-1*this.step);
            setTimeout('Gallery.doEndRight('+myPos+')',this.speed);
        }
    }
    this.doMove = function(way) {
    	this.move = true;
        this.setPos(way*this.step);
    }
    this.disableButton = function(btId) {
        $(this.objName+btId).className = 'disable';
    }
    
    this.enableButton = function(btId) {
        $(this.objName+btId).className = 'top';
    }
    
    this.setVisibility = function(visible) {
    	this.visibleElements = visible;
    	this.getElemPosition();
    }
}

function getMorePics(direct) {
	if (direct == 'next') {
		var url = '/zdjecia/dociag,'+next+','+gal;
	} else {
		var url = '/zdjecia/dociag,'+prv+','+gal;
	}
	Gallery.move = true;
	advAJAX.get({
   		url: url,
   		onLoading : setLoad(),
   		onSuccess : function(obj) { regenerateList(obj.responseText,direct); },
		onError: function(obj){}   		
	});
}
function regenerateList(t,direct) {
	eval('var data = '+t);
	if (direct == 'next') {
		next = data['next'];
	} else {
		prv = data['prv'];
	}	
	gal = data['gal'];
	Gallery.itemsCnt += data['cnt'];
	Gallery.checkButtons();
	Gallery.endMode  = data['endNext'];
	Gallery.pack = data['pack'];
	reGenerateTable(data['data'],direct);
	if (direct == 'next') {
		var s = (-1*myPos);
		unsetLoad();
		$('galItems').scrollLeft=s;
		Gallery.doR();
	} else {
		//var s = (myPos+360);
		unsetLoad();
		//$('galItems').scrollLeft=s;
		Gallery.doL();
	}
}


function reGenerateTable(tb,direct) {
	var html = '';
	
	for(var i=0;i<tb.length;i++) {
		html += makeHtml(tb[i]['id'],tb[i]['url'],i)
	}
	if (direct=='next') {
		$('Galleryshow').innerHTML += html;
	} else {
		Gallery.setPos(-720); //tyle ile elementow w paczce * 120
		Gallery.getElemPosition();
		z = $('Galleryshow').innerHTML;
		$('Galleryshow').innerHTML = html+z;
	}
}


/*
# przeniesione do inc_photo_header.tpl!
function makeHtml(id,url,no) {
	var h = '';
	h = '<table><tr><td><a href="/dla_rodziny/galerie/zdjecie,'+id+','+Gallery.pack+','+(no+1)+'"><img src="'+url+'"/></a></td></tr></table>';
	return h;
}*/

function setLoad() {
	Gallery.disableButton('left');
	Gallery.disableButton('right');
	$('GalleryshowL').style.display='block';
	//$('GalleryshowL').style.left='50%';
}
function unsetLoad() {
	$('GalleryshowL').style.display='none';
	Gallery.move = false;
}

function d(str) {
	$('d').innerHTML += str + '<br />';
}