var zaufaliHandler = new Class({	
	initialize: function(elImg, elTitle, elDesc, elLoading, elPrev, elNext) {
		this.img=elImg;
		this.title=elTitle;
		this.desc=elDesc;
		this.loading=elLoading;
		this.prev=elPrev;
		this.next=elNext;

		this.imgIDs=new Array();
		this.imgAddr=null;
		this.imgListAddr=null;

		//szybkosc zmian ogloszen
		this.switchTime=5000;

		this.currID=0;

		this.ajax=null;

		//eventy
		this.prev.addEvent('click', this.clickPrev.bind(this));
		this.next.addEvent('click', this.clickNext.bind(this));

	},



	run: function() {
		$clear(this.updateTimer);
		$clear(this.runTimer);
		$clear(this.loadingTimer);
		
		this.currID=(this.currID+1<this.imgIDs.length)? this.currID+1:0;
		this.setLoading(true);
		this.updateTimer=this.updateAd.delay(600, this);
		this.runTimer=this.run.delay(this.switchTime, this);
	},
	
	updateAd:function() {
		this.setTitle(this.imgIDs[this.currID][1]);
		this.setDesc(this.imgIDs[this.currID][2]);
                this.img.getParent().set('href', 'index.php5?show=szczegoly&id='+this.imgIDs[this.currID][0]);
		this.setImg(this.imgAddr+this.imgIDs[this.currID][0]+".jpg");
                 
	 },

	clickPrev:function(){
		$clear(this.updateTimer);
		$clear(this.runTimer);
		if ( this.currID==0 ){
			this.currID=this.imgIDs.length-2;
		}
		else {
			this.currID=this.currID-2;
		}
		this.run();
         },

	clickNext:function(){
		$clear(this.updateTimer);
		$clear(this.runTimer);
		if ( this.currID==this.imgIDs.length ){
			this.currID=0;
		}
		else {
//			this.currID=this.currID2;
		}
		this.run();
         },
	//ustawia adres, z ktorego beda pobierane obrazki
	setImgAddress: function (addr) {
		this.imgAddr=addr;
	},

	//link do skryptu, ktory zwroci liste id
	setImgListAddress: function(link) {
		this.imgListAddr=link;
	},

	setTitle:function (txt){
			 this.title.set('text', txt);
		 },
	
	setDesc:function (txt){
			 this.desc.set('text', txt);
	 },
	
	//ustawia nowy link w obrazku
	setImg: function (link) {
//		this.setLoading(true);
		$clear(this.loadingTimer);
		this.img.removeEvent('load', this.loadingFunction);
		this.loadingFunction=function() {this.loadingTimer=this.setLoading.delay(50,this, false); }.bind(this);
		this.img.addEvent('load', this.loadingFunction);
		this.img.set('src', link);
	},

	setLoading: function(set){				
		var start=(set)? 1:0;
		var stp=(set)? 0:1;

		this.loading.fade(stp,start);

	},
	//sciagnie z serwera liste id
	getImgList: function() {
		if ( !$defined(this.ajax) )
			this.ajax= new ajaxObject(this.imgListAddr, this.loadImgIDs.bind(this));

		this.ajax.update();

	},

	//zaladuje sciagniete ID zdjec
	loadImgIDs: function(text,stat, xml) {
			var ads=text.split('#');

			for (i=0; i<ads.length; i++){
				if ( ads[i].indexOf(',') >0)
				this.imgIDs.push(ads[i].split(','));
			}

			this.run();	
		    }


});

//start reklamy
var objZaufaj = new zaufaliHandler( $('zaufaliImg'), $('zaufaliTytul'),$('zaufaliOpis'), $('zaufaliLoading'), $('zaufaliPoprz'), $('zaufaliNast'));

objZaufaj.setImgListAddress('lib/prezentacja.php5');
objZaufaj.setImgAddress('db/portfolio/prezentacja/');
objZaufaj.getImgList();


