//<![CDATA[


var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}


var imageBox = Class.create();

imageBox.prototype = {
	
	initialize: function() {
		this.pageSize	= getPageSize();
		this.delay		= 1;
		this.delayTime	= 30;
	},


	/* Wyzwala zdarzenie po okreslonym czasie, zwieksza licznik opoznienia */
	schEvt: function(evt,delay) {
		setTimeout(evt,this.delay); this.delay+=delay;
	},


	/* Wlacza wyswietlanie obiektu */
	showObject: function(objName) {
		var obj = document.getElementById(objName);
		obj.style.display = 'block';
	},


	/* Wylacza wyswietlanie obiektu */
	hideObject: function(objName) {
		var obj = document.getElementById(objName);
		obj.style.display = 'none';
	},


	/* Rozjasnia obiekt */
	fadeIn: function(objName,fromOpacity,toOpacity,posMiddle) {
		var obj = document.getElementById(objName);
		var ib	= this;
		if (fromOpacity<toOpacity) {
			if (fromOpacity==0)	{
				objW	= isNaN(parseInt(obj.style.width)) ? 0 : parseInt(obj.style.width);
				objH	= isNaN(parseInt(obj.style.height)) ? 0 : parseInt(obj.style.height);

				if (objW==0 && objH==0 && posMiddle==true) {
					this.schEvt('ib.setW("'+objName+'",1);',1);
					this.schEvt('ib.setH("'+objName+'",1);',1);
				}

				this.showObject(objName);
			}
			for (newOpacity=fromOpacity; newOpacity<toOpacity; newOpacity+=10) {
				this.schEvt('ib.setOpacity("'+objName+'",'+newOpacity+');',this.delayTime);
			}
			this.schEvt('ib.setOpacity("'+objName+'",'+toOpacity+');',this.delayTime);
		}
	},


	/* Sciemnia obiekt */
	fadeOut: function(objName,fromOpacity,toOpacity) {
		var obj = document.getElementById(objName);
		if (fromOpacity>toOpacity) {
			for (newOpacity=fromOpacity; newOpacity>toOpacity; newOpacity-=10) {
				this.schEvt('ib.setOpacity("'+objName+'",'+newOpacity+');',this.delayTime);
			}
			this.schEvt('ib.setOpacity("'+objName+'",'+toOpacity+');',this.delayTime);
			if (toOpacity==0) {
				this.schEvt('ib.hideObject("'+objName+'");',this.delayTime);
			}
		}
	},


	/* Ustawia przezroczystosc obiektu */
	setOpacity: function(objName,newOpacity) {
		var obj						= document.getElementById(objName);
		obj.style.filter			= 'alpha(opacity: '+ newOpacity +')';
		obj.style.opacity			= newOpacity>0 ? newOpacity/100 : 0;
		obj.style['-moz-opacity']	= newOpacity>0 ? newOpacity/100 : 0;
	},


	/* Ustala rozmiar nowego obiektu */
	getSize: function(objName,iHTML) {
		var obj	= document.getElementById('imageContainer');
		this.setHTML(objName,'');

		o = document.createElement('div');
		o.style.position	= 'absolute';
		o.innerHTML			= iHTML;

		obj.appendChild(o);
		oW = o.offsetWidth;
		oH = o.offsetHeight;
		obj.removeChild(o);

		ret = new Array(oW,oH);
		return ret;
	},


	/* Zmienia rozmiar obiektu */
	resizeTo: function(objName,toW,toH) {
		ib		= this;
		obj		= document.getElementById(objName);
		objW	= isNaN(parseInt(obj.style.width)) ? 0 : parseInt(obj.style.width);
		objH	= isNaN(parseInt(obj.style.height)) ? 0 : parseInt(obj.style.height);

		if (objW<toW) {
			for (w=objW; w<toW; w+=2) {this.schEvt('ib.setW("'+objName+'",'+w+');',3);}
		} else if (objW>toW) {
			for (w=objW; w>toW+1; w-=2) {this.schEvt('ib.setW("'+objName+'",'+w+');',3);}
		}
		this.schEvt('ib.setW("'+objName+'",'+toW+');',50);

		if (objH<toH) {
			for (h=objH; h<toH; h+=2) {this.schEvt('ib.setH("'+objName+'",'+h+');',3);}
		} else if (objH>toH) {
			for (h=objH; h>toH+1; h-=2) {this.schEvt('ib.setH("'+objName+'",'+h+');',3);}
		}
		this.schEvt('ib.setH("'+objName+'",'+toH+');',100);
	},


	/* Ustawia szerokosc obiektu i pozycjonuje go w poziomie */
	setW: function(objName,w) {
		var obj	= document.getElementById(objName);
		if (document.documentElement) {
			obj.style.marginLeft	= Math.floor((this.pageSize[0]-w)/2)+'px';
		} else {
			obj.style.left			= Math.floor((this.pageSize[0]-w)/2)+'px';
		}
		obj.style.width = w+'px';
	},


	/* Ustawia wysokosc obiektu i pozycjonuje go w pionie */
	setH: function(objName,h) {
		var obj	= document.getElementById(objName);

		obj.style.position		= this.pageSize[3]-h<0 ? 'absolute' : 'fixed';
		if (document.documentElement) {
			obj.style.marginTop	= this.pageSize[3]-h<0 ? (((this.pageSize[3]-h)/2)+this.pageSize[4]<0 ? '0px' : Math.floor(((this.pageSize[3]-h)/2)+this.pageSize[4])+'px') : Math.floor((this.pageSize[3]-h)/2)+'px';
		} else {
			obj.style.top		= this.pageSize[3]-h<0 ? (((this.pageSize[3]-h)/2)+this.pageSize[4]<0 ? '0px' : Math.floor(((this.pageSize[3]-h)/2)+this.pageSize[4])+'px') : Math.floor((this.pageSize[3]-h)/2)+'px';
		}
		obj.style.height = h+10+'px';
	},


	/* Wstawia kod HTML do obiektu */
	setHTML: function(objName,htmlCode) {
		var obj	= document.getElementById(objName);
		obj.innerHTML = htmlCode;
	},


	/* Ustala pozycje przycisku zamykajacego */
	setClosePosition: function() {
		obj		 = document.getElementById('imageContainer');
		objClose = document.getElementById('imageContainerClose');

		objClose.style.position			= obj.style.position;
		if (document.documentElement) {
			objClose.style.marginTop	= parseInt(obj.style.marginTop)+11 + 'px';
			objClose.style.marginLeft	= parseInt(obj.style.marginLeft) + parseInt(obj.style.width) - 23 + 'px';
		} else {
			objClose.style.top			= parseInt(obj.style.top)+11 + 'px';
			objClose.style.left			= parseInt(obj.style.left) + parseInt(obj.style.width) - 23 + 'px';
		}
	},


	/* Laduje zdjecie galerii */
	loadGalleryPhoto: function(galeriaID) {
		ib = this;
		advAJAX.get({
			url: '/ajax.php', 
			parameters: {
				'rType':'PobierzZdjecie',
				'galeriaID':galeriaID
			}, 
			onSuccess : function(retObj) {
				iHTML = retObj.responseText;

				ib.setHTML('imageContainer','');
				ib.fadeIn('imageBox',0,50);
				ib.fadeIn('imageContainer',0,100,true);

				newSize = ib.getSize('imageContainer',iHTML);

				ib.resizeTo('imageContainer',newSize[0],newSize[1]);
				ib.schEvt('ib.setClosePosition();',1);
				ib.schEvt('ib.setHTML("imageContainer",iHTML);',1);
			}
		});
	},


	/* Zmienia zdjecie galerii */
	changeGalleryPhoto: function(galeriaID) {
		ib = this;
		advAJAX.get({
			url: '/ajax.php', 
			parameters: {
				'rType':'PobierzZdjecie',
				'galeriaID':galeriaID
			}, 
			onSuccess : function(retObj) { 
				iHTML	= retObj.responseText;
				newSize = ib.getSize('imageContainer',iHTML);

				ib.setHTML('imageContainer','');
				ib.resizeTo('imageContainer',newSize[0],newSize[1]);
				ib.schEvt('ib.setClosePosition();',1);
				ib.schEvt('ib.setHTML("imageContainer",iHTML);',1);
			}
		});
	},


	/* Pokazuje pojedyncze zdjecie */
	showImageBox: function(iHTML) {
		ib = new imageBox();
		ib.setHTML('imageContainer','');
		ib.fadeIn('imageBox',0,50);
		ib.fadeIn('imageContainer',0,100,true);

		newSize = ib.getSize('imageContainer',iHTML);

		ib.resizeTo('imageContainer',newSize[0],newSize[1]);
		ib.schEvt('ib.setClosePosition();',1);
		ib.schEvt('ib.setHTML("imageContainer",iHTML);');
	},


	/* Wylacza podglad zdjecia */
	hideImageBox: function() {
		this.fadeOut('imageContainerClose',10,0);
		this.fadeOut('imageContainer',100,0);
		this.fadeOut('imageBox',50,0);
	},


	/* Pokazuje przycisk zamkniecia podgladu */
	showCloseButton: function() {
		this.showObject('imageContainerClose');
		this.setOpacity('imageContainerClose',100);
	},


	/* Ukrywa przycisk zamkniecia podgladu */
	hideCloseButton: function() {
		this.hideObject('imageContainerClose');
		this.setOpacity('imageContainerClose',0);
	},


	/* Pokazuje wersje do porównania */
	compareVersions: function(versionUrl,elementData,versionData,elementFields) {
		ib = this;
		divW = this.pageSize[2]-100;
		divH = this.pageSize[3]-100;
		advAJAX.get({
			url: versionUrl, 
			parameters: {
				'rType':'compareVersions',
				'dbTbl':elementData.dbTbl,'elN':elementData.elN,'elID':elementData.elID,
				'dbTblV':versionData.dbTbl,'vN':versionData.elN,'vID':versionData.elID,
				'n':elementFields.elN,'t':elementFields.elT,'b':elementFields.elB,
				'pW':divW,'pH':divH
			}, 
			onSuccess : function(retObj) {
				iHTML = retObj.responseText;

				ib.setHTML('imageContainer','');
				ib.fadeIn('imageBox',0,50);
				ib.fadeIn('imageContainer',0,100,true);

				ib.resizeTo('imageContainer',divW,divH);
				ib.schEvt('ib.setClosePosition();',1);
				ib.schEvt('ib.setHTML("imageContainer",iHTML);',1);
			}
		});
	}
}




function getPageSize() {
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight-20;
		currentScroll = self.scrollY;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight-20;
		currentScroll = document.documentElement.scrollTop;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight-20;
		currentScroll = document.body.scrollTop;
	}

	pageHeight = yScroll < windowHeight ? windowHeight : yScroll;
	pageWidth = xScroll < windowWidth ? xScroll : windowWidth;

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,currentScroll) 
	return arrayPageSize;
}


function sIB(imSrc,imW,imH) {
	iHTML = '<img src="'+imSrc+'" width="'+parseInt(imW)+'" height="'+parseInt(imH)+'" onmouseover="sCl()" onmouseout="hCl()">';
	ib	  = new imageBox();
	ib.showImageBox(iHTML);
}


function hIB() {
	ib = new imageBox();
	ib.hideImageBox();
}


function lGPh(galeriaID) {
	ib = new imageBox();
	ib.loadGalleryPhoto(galeriaID);
}


function cGPh(galeriaID) {
	ib = new imageBox();
	ib.changeGalleryPhoto(galeriaID);
}


function sCl() {
	ib = new imageBox();
	ib.showCloseButton();
}


function hCl() {
	ib = new imageBox();
	ib.hideCloseButton();
}


function cV(versionUrl,elementData,versionData,elementFields) {
	ib = new imageBox();
	ib.compareVersions(versionUrl,elementData,versionData,elementFields);
}



//]]>