
/* 
Zawartosc tego dokumentu jest chronina prawami autorskimi i
 nie moze byc kopiowana ani wykorzystywana do wlasnych celow. 
Jesli chcesz wykorzystac zawartosc tego skryptu skataktuj sie 
z wlascicielem.
                                          www.flusso.pl                                          */


Infobox = function(object){
	this.gallery   = ('Gallery'	  in object)?object['Gallery']      :null;
	this.box       = ('Box'	      in object)?object['Box']          :null;
	
	if(this.gallery) this.addEvent();
	
	return this;
}

Infobox.prototype.addEvent = function(){
	var self = this;
	var a_image = self.gallery.getByTagName("a");  //pobranie wszystkich a znajdujacych sie w ul bedacym contenerem zdiec 
							
		for(var key=0;key<a_image.length;key++){
			MyEvent.add(a_image[key],"mousemove",function(e){self.move(e);});
			MyEvent.add(a_image[key],"mouseover",function(e){self.show(e)});
			MyEvent.add(a_image[key],"mouseout" ,function(e){self.hidden(e)});
		}
}

Infobox.prototype.move = function(e){
	var self = this,Top = 0,Left = 0;
		if(!document.body.getScroll||document.body.getSize)flusso_Abstract.extend(document.body);
					
		var CP  = MyEvent.getCursorPosition(e);
		var SP  = document.body.getScroll();
	//	var SS  = new Array(document.body.scrollWidth,document.body.scrollHeight);
		var SD  = document.body.getSize();
		var ES  = Explorer.getSize();
		var IBS = new Array(self.box.offsetWidth,self.box.offsetHeight);			
			//alert(CP+"  "+SP);
			Top  = CP.y+10;
			Left = CP.x+10; 
			
			if(Explorer.IE){
				Top  -= (Top >=(ES[1]+SP[1]-IBS[1]))?IBS[1]+15:0;
				Left -= (Left>=(ES[0]+SP[0]-IBS[0]))?IBS[0]+15:0;
			}else{
		
				Top -= (Top >=((ES[1]+SP[1]-IBS[1])-((ES[0]<SD[0])?16:0)))?IBS[1]+15:0;
				Left-= (Left>=((ES[0]+SP[0]-IBS[0])-((ES[1]<SD[1])?16:0)))?IBS[0]+15:0;		
			}
			
            self.box.setMyAttributes({top: (Top) +"px",left: (Left) +"px"});
}

Infobox.prototype.show = function(e){
	var self = this;
	
	var target = MyEvent.getTarget(e);
	
		target = (target.nodeName == 'IMG')?target.getParent():target;	
		if(target.title!="") self.box.setMyAttributes({display:"block"});
		self.box.innerHTML = target.title;
		
		target.title = "";
}

Infobox.prototype.hidden = function(e){
	var self = this;
	var target = MyEvent.getTarget(e);
		target = (target.nodeName == 'IMG')?target.getParent():target;
		target.title = self.box.innerHTML;
		self.box.setMyAttributes({display:"none"});
		
}

Infobox.init = function(id){

	var Dest = (typeof id =='string')?Element.getByID(id):id;	
	
	var Box = Infobox.createBox();
	
	var C = Element.getByID('center');
		C.parentNode.insertBefore(Box,C);
	
	var Info = new Infobox({Gallery:Dest,
							Box: Box
							});
}

Infobox.createBox = function(){

	var Info = Element.createFromStr("<div id='"+Infobox.BX+"' style='display:none;position:absolute;top:0px;z-index:99;left:0px;'>Brak opisu</div>");

	return Info;					
}
	
Infobox.BX = "gallery_info_box";   //id elementu z i opisem




Gallery = function(object){
     this.gallery       = ('Gallery'	  in object)?object['Gallery']      :null;
     this.background    = ('BackGround'   in object)?object['BackGround']   :null;
	 this.container     = ('PicContainer' in object)?object['PicContainer'] :null;
	 this.content       = ('PicContent'   in object)?object['PicContent']   :null;
	 this.next          = ('ImageNext'    in object)?object['ImageNext']    :null;
	 this.previous   	= ('ImagePrevious'in object)?object['ImagePrevious']:null;
	 this.description   = ('Description'  in object)?object['Description']  :null;
     this.counter       = ('Counter'      in object)?object['Counter']      :null;
	 this.close         = ('Close'        in object)?object['Close']        :null;
	 this.loader        = ('Loader'       in object)?object['Loader']       :null;
	 this.image  		= null;
	 this.ajax 			= new MyAjax("../../pages/gallery_zoom.php",'POST',true);
	 
	 if(this.gallery != null) this.addEvent(); 

	 return this;
}

Gallery.prototype.addEvent = function(){
	var self = this;
	var a_image = self.gallery.getByTagName("a");  //pobranie wszystkich a znajdujacych sie w ul bedacym contenerem zdiec 
							
		for(var key=0;key<a_image.length;key++){
			MyEvent.add(a_image[key],"click",function(e){  
								self.show(e); MyEvent.clear(e); });  //nadnie zdazenia click na kazde a zawierajace zdiecie 
		}
			
		MyEvent.add(window,"resize",function(e){ if(self.background)Gallery.setFullSize(self.background)});	
		
	//	MyEvent.add(window,"scroll",function(e){	//poprawienie beledu wystepujacego w IE ze scroll'em;	
			
		//	if(self.background&&Explorer.IE){
				//self.background.setMyAttributes({top:"0px",left:"0px",width:"0px",height:"0px"});
			//	if(!document.body.getSize)flusso_Abstract.extend(document.body);
			//	var ScrollPosition = document.body.getScroll();
					//ScrollPosition[0] += document.body.clientWidth;
					//ScrollPosition[1] += document.body.clientHeight;
				//self.background.setMyAttributes({width:ScrollPosition[0]+"px",height:ScrollPosition[1]+"px"});
		//	}	
		//});	
			
		MyEvent.add(window,"scroll",function(e){
			if(self.container.getMyAttribute("display")!= 'none') Gallery.setPositionCenterDiv(self.container);				
		});

		MyEvent.add(window,"resize",function(e){
			if(self.container.getMyAttribute("display")!= 'none') Gallery.setPositionCenterDiv(self.container);
		});	
			
		if(self.close)	MyEvent.add(self.close,"click",function(e){  
								self.clear(e); MyEvent.clear(e); }); //dodanie zdarzenia na przycisk close;
};


Gallery.prototype.showImage = function(id){
	var self = this;
		 
		if(self.image&&self.loader){try{self.content.replaceChild(self.loader,self.image);}catch(e){return;};}
		else self.loader.insertTo(self.content);
	     	
		self.loader.setCenterElement(self.content);
	    	
		MyEvent.insert(self.previous,"click",function(e){MyEvent.clear(e);});
		MyEvent.insert(self.next,    "click",function(e){MyEvent.clear(e);});
	     
		this.ajax.completeandcorect = function(text){
			var myImage = new Image()
				eval("var attributes = "+text);		
				if(self.description) self.description.innerHTML=attributes["description"];
				if(self.counter) self.counter.innerHTML = " zdjęcie "+attributes['nr']+"/"+attributes['count']+" ";
				
				self.content.removeChild(self.loader);
				
				myImage.onload = function(){
					var size = new Array(this.width,this.height);
						self.image = Element.createFromStr("<img  src='"+this.src+"' />");
						self.image.setOpacity(0);
						self.content.setMyAttributes({height:self.content.getHeight()+"px"});
							
						var fx = new Effects(self.content);
						    fx.start().setSpeed(10).setStep(6).reSizeTo(null,size[1]).addFunction(
										function(){
											var fx1 = new Effects(self.image);
												fx1.start().setSpeed(10).setStep(2).changeOpacity(0,1).stop();
												self.content.appendChild(self.image);
												if(self.background){
												    var FullSizeDocument = document.body.getSize();
													var widthB  = parseInt(self.background.getMyAttribute("width"));
													var heightB = parseInt(self.background.getMyAttribute("height"));
												
												if(widthB != FullSizeDocument[0]||heightB !=FullSizeDocument[1])
												Gallery.setFullSize(self.background);}
										}).stop();
				}
				myImage.src = attributes["photo"];  
				
				if(attributes["prev"]!=''){ self.previous.deleteClassName('disabled');
					MyEvent.insert(self.previous,"click",function(e){ MyEvent.clear(e);  self.changeImage(attributes["prev"])});
				}else self.previous.addClassName('disabled');
													
				if(attributes["next"]!=''){ self.next.deleteClassName('disabled');
					MyEvent.insert(self.next,    "click",function(e){ MyEvent.clear(e);  self.changeImage(attributes["next"])});
				}else self.next.addClassName('disabled');
			}
			this.ajax.send({id:id});
};

Gallery.prototype.show = function(e){
	var self = this;
		if(self.background.getMyAttribute("display") != 'none') return;
	var target = MyEvent.getTarget(e);
		target = (target.nodeName == 'IMG')?target.getParent():target;
		Gallery.setFullSize(self.background);
		self.background.setMyAttributes({display:"block"});
		self.container.setMyAttributes({display:"block"});
		Gallery.setPositionCenterDiv(self.container);
		self.showImage(target.id.substr(3));	
};

Gallery.prototype.changeImage = function(id){
	var self = this;
		self.showImage(id);
};
						
Gallery.prototype.clear = function(){
	var self = this;
		self.background.setMyAttributes({display:"none"});
		self.container.setMyAttributes({display:"none"});
};
						
						
Gallery.init = function(id){		
	var Dest = (typeof id =='string')?Element.getByID(id):id;	
	var BG   = Gallery.createBackGround();
	var PC   = Gallery.createCenterDiv();
	var LO   = Gallery.createLoader();
	
	var C = Element.getByID('center');
		C.parentNode.insertBefore(PC,C);
		C.parentNode.insertBefore(BG,C);
		
		
			
	var gal = new Gallery({ Gallery   	 : Dest,
							BackGround	 : BG,
		                    PicContainer : PC,
							PicContent   : Element.getByID(Gallery.Pc),
							ImagePrevious: Element.getByID(Gallery.IP),				
							ImageNext	 : Element.getByID(Gallery.IN),
							Counter      : Element.getByID(Gallery.CR),
							Description  : Element.getByID(Gallery.DN),
							Close        : Element.getByID(Gallery.CL),
							Loader       : LO
							});				
		};

		
Gallery.setPositionCenterDiv = function(DivCenter){
	if(!DivCenter) return;
	var ExplorerSize = Explorer.getSize();
	var Width  = ExplorerSize[0];
	var Height = ExplorerSize[1];	
	
		if(Width-21>DivCenter.offsetWidth)	DivCenter.setCenterLeftExplorer();
		else DivCenter.setMyAttributes({left:"0px"});
		if(Height-21>DivCenter.offsetHeight) DivCenter.setUpExplorer(12);			
}		
		
Gallery.setFullSize = function(element){
	var FullSizeDocument = null,ScrollPosition = null;
		if((!document.body.getSize)||(!document.body.getScroll))flusso_Abstract.extend(document.body);
		
		element.setMyAttributes({top:"0px",left:"0px",width:"0px",height:"0px"});
	    FullSizeDocument = document.body.getSize();
		ScrollPosition = document.body.getScroll();
		//poprawienie beledu wystepujacego w IE ,bledu zwiaznego ze scroll'em
		if(Explorer.IE){
			FullSizeDocument[0] = document.body.clientWidth;
			FullSizeDocument[1] = document.body.clientHeight;	
			//element.setMyAttributes({left:ScrollPosition[0]+"px",top:ScrollPosition[1]+"px"});
		}
		
		
		
	    element.setMyAttributes({width:FullSizeDocument[0]+"px",height:FullSizeDocument[1]+"px"});
}
	

Gallery.createLoader = function(){
	var Loader = Element.createFromStr("<div id='"+Gallery.LD+"' style='position:absolute; border: none;'><img src='../../gfx/img/loading.gif' style='border: none' width='32' height='32' alt='' /></div>");
	return Loader;
};
	
Gallery.createBackGround = function(){
	var BackGround = Element.createFromStr("<div id='"+Gallery.BG+"' style='display:none;position:absolute;top:0;left:0;'></div>");
		BackGround.setOpacity(0.6);
	return BackGround;
};		
		
Gallery.createCenterDiv  = function(){
	var Title = Element.getByID(Gallery.Title);
	var DivCenter = Element.createFromStr(""+
	"<div id='"+Gallery.PC+"' style='display:none'><p id='"+Gallery.CL+"'><a href='#' >zamknij</a></p><h3>"+((Title)?Title.innerHTML:"&nbsp;")+"</h3>"+
          "<p id='"+Gallery.PM+"'><a id='"+Gallery.IP+"' href='#' class='pages_previous'> poprzednie</a><span id='"+Gallery.CR+"'> zdjęcie 0/0 </span>"+
          "<a id='"+Gallery.IN+"' href='#'  class='pages_next'>następne </a></p><div id='"+Gallery.Pc+"' style='position:relative;'></div><div id='"+Gallery.DN+"'></div></div>");

	return DivCenter;
};	

//nazewnictwo id poszczegolnych divow
Gallery.BG = "div_background";     //div zakrywajacy tlo 
Gallery.PC = "div_container";      //div rysowany na srodku ekranu 
Gallery.PM = "pic_menu";      	  //p z odnosnikami do stron nastepnej i poprzedniej
Gallery.Pc = "div_content";        //div ze dzieciem 
Gallery.IP = "a_previous";         // selektor a poprzednie zdiecie  
Gallery.IN = "a_next";             // selektor a nastepne zdiecie
Gallery.CR = "span_counter"        // span w ktorym podawany jest nr zdiecia
Gallery.DN = "div_description"     // div z opisem zdiecia		
Gallery.CL = "close_window_bar"             //selektor a zamykjacy galerie
Gallery.LD = "Loader"		       //id loadera;
Gallery.Title = "titleGallery"     // id znacznika  znajdujacego sie gdzies na stronie w ktorym zawarta jest informacja o tytule danej galerii znacznik musie znajdowac sie przed wywolanie m funkcji Gallery.init 
								   //najlepiej zeby znajdowal sie w znaczniku ul w ktoreym najduja sie zdiecia; w przypadku gdy nie bedzie takiego znacznika nie zostanie wypisany tytul galerii
