window.PG = new photoGallery();

function photoGallery()
{
/* lib function */

	var that = this

	photoGallery.prototype.msgAlert = function(msg)
		{
		var that = this;

		if(!this.msgDiv)
			{
			this.msgDiv = document.createElement('div');
			this.msgDiv.style.cssText = "background-color: #EEEEEE; padding: 5px; position: absolute; left: 0px; top: 0px; z-index: 100000; color: #000000;"
			this.msgDiv.onclick = function(){ document.body.removeChild(that.msgDiv); that.msgDiv = null; }
			document.body.appendChild(this.msgDiv);
			}

		this.msgDiv.innerHTML = msg;
		}


	photoGallery.prototype.getDomElement = function(e)
		{
		if(typeof e == "object" && e) { return e; }
		else if(typeof e == "string" && document.getElementById(e)) {return document.getElementById(e); }
		else { return null; } // wrong elementtype
		}

	photoGallery.prototype.getDomElementsByClassName = function(clsNm, t, p)
		{
		var tag = t || "*";
		var parent = this.getDomElement(p) || document;
		var elems = parent.getElementsByTagName(tag);
		var elemsCount = elems.length;
		var elem;
		var returnElems = [];

		for(var i=0; i<elemsCount; i++)
			{
			elem = elems[i];
			if(this.testClassName(elem, clsNm))
				{ returnElems.push(elem) }
			}
         return returnElems;
		}


	/* manipulate Class Names */
	photoGallery.prototype.testClassName = function(e, clsNm)
		{
		var oclsNm = this.getDomElement(e).className;
		var re = new RegExp("(^|\\s)" + clsNm + "(\\s|$)");
		if(oclsNm.match(re)) { return true }
		else { return false }
		}

	photoGallery.prototype.addClassName = function(e, cnm)
		{
		var ocnm = this.getDomElement(e).className;
		if(this.testClassName(e, cnm)) { return false }
		if(ocnm && ocnm != "") {cnm = ocnm + " " + cnm}
		this.getDomElement(e).className = cnm;
		}

	photoGallery.prototype.removeClassName = function(e, cnm)
		{
		var ocnm = this.getDomElement(e).className;
		var re = new RegExp(cnm);
		if(this.testClassName(e, cnm))
			{
			ocnm = ocnm.replace(re, '');
			ocnm = ocnm.replace(/^ +| +$/, '');
			this.getDomElement(e).className = ocnm;
			}
		}

	photoGallery.prototype.getCssStyle = function(e, cssproperty)
		{
		e = this.getDomElement(e);
		if(document.defaultView && document.defaultView.getComputedStyle)
			{
			return document.defaultView.getComputedStyle(e, "")[cssproperty];
			}
		else if(e.currentStyle)
			{ 
			if(cssproperty == "opacity") { cssproperty = 'filter'; }
			
			var compStyle = e.currentStyle[cssproperty];
			
			// f.....g IE -9 can return "inherit", so test parent Elements
			while(e.parentNode && compStyle == "inherit")
				{ compStyle = e.currentStyle[cssproperty]; e = e.parentNode;  }

			// still "inherit"? then hard code it :-( (list to be grown)
			if(compStyle == "inherit")
				{
				switch(cssproperty)
					{	
					case "visibility": compStyle = "visible"; break;
					}		
				}

			if(cssproperty == "filter")
				{
				if(compStyle == '') { compStyle = '100'; }
				else
					{
					compStyle = compStyle.replace(/[a-z|\(|=|\)]/ig, '');
					compStyle = parseInt(compStyle, 10)/100;
					}
				}
			
			return compStyle;
			}
		}

	photoGallery.prototype.getCssStyleDecimal = function(e, cssproperty)
		{
		e = this.getDomElement(e);
		if(e.currentStyle)
			{ 
			if(cssproperty=="width")
				{ return e.offsetWidth; }
			else if(cssproperty=="height")
				{ return e.offsetHeight; }
			else 
				{ return parseInt(e.currentStyle[cssproperty], 10); }
			}
		else if(document.defaultView && document.defaultView.getComputedStyle)
			{
			return parseInt(document.defaultView.getComputedStyle(e, "")[cssproperty], 10);
			}
		}

	/* window/document properties */
	photoGallery.prototype.getWindowInnerWidth = function()
		{
		var wiw = 0;
		if (document.documentElement && document.documentElement.clientWidth)
			{ wiw = document.documentElement.clientWidth; }
		else if (document.body)
			{ wiw = document.body.clientWidth; }

		return wiw;
		}

	photoGallery.prototype.getWindowInnerHeight = function()
		{
		var wih = 0;
		if (document.documentElement && document.documentElement.clientHeight)
			{ wih = document.documentElement.clientHeight; }
		else if (document.body)
			{ wih = document.body.clientHeight; }

		return wih;
		}

	photoGallery.prototype.getDocumentWidth = function()
		{
		var dw = 0;
		if(document.documentElement && document.documentElement.scrollWidth)
			{ dw = document.documentElement.scrollWidth }
		else if(document.body && document.body.scrollWidth)
			{ dw = document.body.scrollWidth; }

		return dw;
		}

	photoGallery.prototype.getDocumentHeight = function()
		{
		var dh = 0;
		if(document.documentElement && document.documentElement.scrollHeight)
			{ dh = document.documentElement.scrollHeight }
		else if(document.body && document.body.scrollHeight)
			{ dh = document.body.scrollHeight; }

		return dh;
		}

	photoGallery.prototype.getScrollX = function()
		{
		var sx = 0;
		if (window.pageXOffset)
			{ sx = window.pageXOffset; }
		else if (document.documentElement && document.documentElement.scrollLeft)
			{ sx = document.documentElement.scrollLeft; }
		else if(document.body)
			{ sx = document.body.scrollLeft; }
		return sx;		
		}

	photoGallery.prototype.getScrollY = function()
		{
		var sy = 0;
		if (window.pageYOffset)
			{ sy = window.pageYOffset; }
		else if (document.documentElement && document.documentElement.scrollTop)
			{ sy = document.documentElement.scrollTop; }
		else if(document.body)
			{ sy = document.body.scrollTop; }
		return sy;		
		}

	/* overlay dialog */
	photoGallery.prototype.overlayDialog = function(odProps)
		{
		return new function(odps, parentFn)
			{
			var that = this;
			var windowResizeOverlayDialogFollowTimer;
		
			this.overlayClassName = odps.olClass 		? odps.olClass		: "";
			this.overlayPosition = 	odps.olPos 			? odps.olPos		: "absolute";
			this.overlayZindex = 	odps.olZ 			? odps.olZ			: 10000;
			this.overlayCanClose = 	odps.olClose 		? odps.olClose		: "1";
		
			this.dialogClassName = 	odps.dlClass 		? odps.dlClass		: "";
			this.dialogPosition = 	odps.dlPos 			? odps.dlPos		: "absolute";
			this.dialogZindex = 	odps.dlZ 			? odps.dlZ 			: this.overlayZindex + 1;
	
			this.dialogSrc = 		odps.dlSrc;	
			this.dialogSrcMode = 	odps.dlSrcMode 		? odps.dlSrcMode 	: "innerHTML";
	
			this.closeClassName = 	odps.clClass 		? odps.clClass 		: "";
			this.closeShow = 		odps.clShow 		? odps.clShow 		: "1";
	
	
			this.frameWidth =		odps.frmW			? odps.frmW			: 500;
			this.frameHeight =		odps.frmH			? odps.frmH			: 300;
			this.frameScrolling =	odps.frmScroll		? odps.frmScroll	: "auto";
			this.frameFrmBorder =	odps.frmFrmBorder	? odps.frmFrmBorder	: "0";
			this.frameBorder =		odps.frmBorder		? odps.frmBorder	: "0";



			if(parentFn.getDomElement(this.dialogSrc))
				{
				this.dialogSrc = parentFn.getDomElement(this.dialogSrc);
				var dialogSrcDisplay = parentFn.getCssStyle(this.dialogSrc, "display");
				var dialogSrcVisibility = parentFn.getCssStyle(this.dialogSrc, "visibility");
				
				var srcParent = this.dialogSrc.parentNode;
				var srcNext = this.dialogSrc.nextSibling;
				}

			this.overlayDialogSetPosition = function()
				{
				that.dialogDiv.style.top = Math.max(0, parentFn.getWindowInnerHeight()/2 - this.dialogDiv.offsetHeight/2 + parentFn.getScrollY()) + "px";
				that.dialogDiv.style.left = Math.max(0, parentFn.getWindowInnerWidth()/2 - this.dialogDiv.offsetWidth/2 + parentFn.getScrollX()) + "px";

				that.overlayDiv.style.position = that.overlayPosition;
				that.overlayDiv.style.width = parentFn.getWindowInnerWidth() + "px";
				that.overlayDiv.style.height = parentFn.getWindowInnerHeight() + "px";
				that.overlayDiv.style.width = Math.max(parentFn.getWindowInnerWidth(), parentFn.getDocumentWidth()) + "px";
				that.overlayDiv.style.height = Math.max(parentFn.getWindowInnerHeight(), parentFn.getDocumentHeight()) + "px";
				}

			this.windowResizeOverlayDialogFollow = function()
				{
				if(windowResizeOverlayDialogFollowTimer!="")
					{ window.clearTimeout(windowResizeOverlayDialogFollowTimer); windowResizeOverlayDialogFollowTimer = ""; }
				
				that.overlayDiv.style.width = "100%";
				that.overlayDiv.style.height = "100%";
				that.overlayDiv.style.position = "fixed";
				
				windowResizeOverlayDialogFollowTimer = window.setTimeout( function(){that.overlayDialogSetPosition(); }, 5)
				}

			this.overlayDialogRemove = function()
				{
				if(this.dialogSrcMode == "moveSrc")
					{
					this.dialogDiv.childNodes[0].style.display = dialogSrcDisplay;
					this.dialogDiv.childNodes[0].style.visibility = dialogSrcVisibility;
					if(srcNext) { srcParent.insertBefore(this.dialogDiv.childNodes[0].cloneNode(true), srcNext) }
					else { srcParent.appendChild(this.dialogDiv.childNodes[0].cloneNode(true)); }
					}
				document.body.removeChild(this.overlayDiv); this.overlayDiv = null;
				document.body.removeChild(this.dialogDiv); this.dialogDiv = null;
				parentFn.removeEvent(window, 'resize', that.windowResizeOverlayDialogFollow);
				}

			this.overlayDialogStart = function()
				{
				if(this.dialogSrcMode == "moveSrc" || this.dialogSrcMode == "cloneSrc")
					{
					if(dialogSrcDisplay != "block") { this.dialogDiv.childNodes[0].style.display = "block"; }
					if(dialogSrcVisibility != "visible") { this.dialogDiv.childNodes[0].style.visibility = "visible"; }
					}
				this.overlayDialogSetPosition();
				this.dialogDiv.appendChild(this.closeDiv);
				}
			
			this.overlayDialogLoadFrame = function()
				{
				this.dialogFrameDiv = document.createElement('div');
				this.dialogFrameDiv.style.width = this.frameWidth + "px";
				this.dialogFrameDiv.style.height = this.frameHeight + "px";
				
				this.dialogFrame = document.createElement('iframe');
				this.dialogFrameDiv.appendChild(this.dialogFrame);


				this.dialogFrame.width = "100%";
				this.dialogFrame.height = "100%";
				this.dialogFrame.scrolling = this.frameScrolling;
				this.dialogFrame.frameBorder = this.frameFrmBorder;
				this.dialogFrame.border = this.frameBorder;
				this.dialogFrame.src = this.dialogSrc;
				
				this.dialogDiv.appendChild(this.dialogFrameDiv);
				}
			
	
			/* create overlay */
			this.overlayDiv = document.createElement('div');
			this.overlayDiv.innerHTML = "&nbsp;";

			if(this.overlayCanClose == "1") { this.overlayDiv.onclick = function(){that.overlayDialogRemove()} }

			if(this.overlayClassName) 
				{ this.overlayDiv.className = this.overlayClassName; }
			else
				{
				this.overlayDiv.style.cssText = "position: " + this.overlayPosition + "; left: 0px; top: 0px; display: block; z-index: " + this.overlayZindex + "; filter: alpha(opacity=20);";
				this.overlayDiv.style.backgroundColor = "#000000";
				this.overlayDiv.style.opacity = "0.2";
				}

			/* create dialog */
			this.dialogDiv = document.createElement('div');
			
			if(this.dialogClassName) 
				{ this.dialogDiv.className = this.dialogClassName; }
			else
				{
				this.dialogDiv.style.cssText = "position: " + this.dialogPosition + "; left: 10px; top: 10px; display: block; z-index: " + this.overlayZindex + 1  + ";";
				this.dialogDiv.style.backgroundColor = "#FFFFFF";
				this.dialogDiv.style.border = "2px solid #AAAAAA";
				this.dialogDiv.style.padding = "5px";
				}
			
			/* create close button */
			if(this.closeShow == '1') 
				{
				this.closeDiv = document.createElement('div');
				this.closeDiv.onclick = function(){that.overlayDialogRemove()}
	
				if(this.closeClassName) 
					{ this.closeDiv.className = this.closeClassName; }
				else
					{
					this.closeDiv.style.cssText = "position: " + this.dialogPosition + "; right: -26px; top: -26px; display: block; z-index: " + this.overlayZindex + 2  + "; cursor: pointer;";
					this.closeDiv.style.padding = "1px 3px 3px 3px";
					this.closeDiv.style.border = "2px solid #AAAAAA";
					this.closeDiv.style.color = "#AAAAAA";
					this.closeDiv.style.fontSize = "14px";
					this.closeDiv.style.fontWeight = "bold";
					this.closeDiv.style.lineHeight = "14px";
					this.closeDiv.style.verticalAlign = "middle";
					this.closeDiv.style.backgroundColor = "#FFFFFF";
					this.closeDiv.innerHTML = "x"
					}
				}

			document.body.appendChild(this.overlayDiv);
			document.body.appendChild(this.dialogDiv);


			switch(this.dialogSrcMode)
				{
				case "innerHTML": this.dialogDiv.innerHTML = this.dialogSrc.innerHTML; this.overlayDialogStart(); break;
				case "frame": this.overlayDialogLoadFrame(); this.overlayDialogStart(); break;
				case "moveSrc": this.dialogDiv.appendChild(this.dialogSrc); this.overlayDialogStart(); break;
				case "cloneSrc": this.dialogDiv.appendChild(this.dialogSrc.cloneNode(true)); this.overlayDialogStart(); break;
				}

			parentFn.addEvent(window, 'resize', that.windowResizeOverlayDialogFollow );

			}(odProps, this)
		}

	photoGallery.prototype.addEvent = function(e, eventType, fn2do)
		{ 
		if (this.getDomElement(e).addEventListener) { this.getDomElement(e).addEventListener(eventType, fn2do, false) }
		else if (this.getDomElement(e).attachEvent) { this.getDomElement(e).attachEvent("on"+eventType, fn2do); }
		else { return false } 
		}
	
	photoGallery.prototype.removeEvent = function(e, eventType, fn2do)
		{ 
		if (this.getDomElement(e).removeEventListener) { this.getDomElement(e).removeEventListener(eventType, fn2do, false) }
		else if (this.getDomElement(e).detachEvent) { this.getDomElement(e).detachEvent("on"+eventType, fn2do); }
		else { return false } 
		}



/* gallery vars & functions */
this.slides = [];
this.slidesCurrent = 0;
this.slidesMax = 0;

this.pgHandleNext = document.createElement('div');
this.pgHandlePrev = document.createElement('div');
this.pgHandleNext.className = "pgHandleNext";
this.pgHandlePrev.className = "pgHandlePrev";
this.pgHandleNext.onclick = function(){PG.stepGallery(1)};
this.pgHandlePrev.onclick = function(){PG.stepGallery(-1)};

photoGallery.prototype.makeGallery = function()
	{
	this.galWrapper = this.getDomElement('js_gallerywrapper');
	this.galBoxes = this.getDomElementsByClassName('galleryBox', 'div', this.galwrapper)
	this.galBoxesCount = this.galBoxes.length;
	this.slidesMax = this.galBoxesCount - 1;
	
	for(var i=0; i<this.galBoxesCount; i++)
		{
		this.galBox = this.getDomElementsByClassName('galleryImage', 'div', this.galBoxes[i])[0];
		this.galImg = this.galBox.getElementsByTagName('img')[0];
		this.slides.push( { 
							slideURL: this.galImg.getAttribute('galSlideURL'),
							origURL: this.galImg.getAttribute('galOrigURL'),
							origText: this.galImg.getAttribute('galOrigTxt'),
							slideCredit: this.galImg.getAttribute('galCredit'),
							//slideCredit: '',
							//slideText: ''
							slideText: this.galImg.getAttribute('title')
						} );
		this.addEvent(this.galImg, 'click', function(si){ return function(){PG.preShowGallery(si)}; }(i));
		}
	}

photoGallery.prototype.makeSlide = function()
	{
	this.stage = document.createElement('div');
	this.stage.className = "pgSlideStage";

	this.stageImgWrapper = document.createElement('div');
	this.stageImgWrapper.className = "pgSlideImgWrapper";

	this.stageImg = document.createElement('img');
	this.stageImg.className = "pgSlideImg";

	this.stageTxt = document.createElement('div');
	this.stageTxt.className = "pgSlideText";

	this.stageCredit = document.createElement('div');
	this.stageCredit.className = "pgSlideCredit";

	this.stageDownload = document.createElement('div');
	this.stageDownload.className = "pgSlideDownload";

	this.stageStopFloat = document.createElement('div');
	this.stageStopFloat.className = "stopFloat";
	
	this.stageImg.src = this.slides[this.slidesCurrent].slideURL;
	//this.stageImgWrapper.appendChild(this.stageImg);
	
	this.stageImgWrapper.style.backgroundImage = "url("+this.slides[this.slidesCurrent].slideURL+")";
	
	this.stageTxt.innerHTML = this.slides[this.slidesCurrent].slideText;
	this.stageCredit.innerHTML = this.slides[this.slidesCurrent].slideCredit;
	this.stageDownload.innerHTML = '<a href="'+this.slides[this.slidesCurrent].origURL+'" target="_blank">'+this.slides[this.slidesCurrent].origText+'</a>';

	this.stage.appendChild(this.stageImgWrapper);
	this.stage.appendChild(this.stageTxt);
	this.stage.appendChild(this.stageCredit);
	this.stage.appendChild(this.stageDownload);
	this.stage.appendChild(this.stageStopFloat);
	}


photoGallery.prototype.preShowGallery = function(sldnmbr)
	{
	this.slidesCurrent = sldnmbr;
	this.makeSlide();
	this.showGallery();
	this.addEvent(this.stageImg, 'load', function(){ that.reAdjustGallery(); })
	}

photoGallery.prototype.showGallery = function()
	{
	this.galleryPopup = this.overlayDialog({
		olClass: 'pgOverlay',
		olClose: '1',
		dlSrcMode: 'cloneSrc',
		dlSrc: this.stage,
		dlClass: 'pgOlDialog',
		clClass: 'pgOlClose'
		})
	this.galleryPopup.dialogDiv.appendChild(this.pgHandleNext);
	this.galleryPopup.dialogDiv.appendChild(this.pgHandlePrev);
	}

photoGallery.prototype.stepGallery = function(stp)
	{
	this.slidesCurrent += stp;
	if(this.slidesCurrent > this.slidesMax) {this.slidesCurrent=0;}
	if(this.slidesCurrent < 0) {this.slidesCurrent=this.slidesMax;}
	this.makeSlide();
	this.showStepGallery();

	this.addEvent(this.stageImg, 'load', function(){ that.reAdjustGallery(); })
	}

photoGallery.prototype.showStepGallery = function()
	{
	while(this.galleryPopup.dialogDiv.hasChildNodes())
		{ this.galleryPopup.dialogDiv.removeChild(this.galleryPopup.dialogDiv.childNodes[0]); }
	
	this.galleryPopup.dialogDiv.appendChild(this.stage);
	this.galleryPopup.dialogDiv.appendChild(this.galleryPopup.closeDiv);
	this.galleryPopup.dialogDiv.appendChild(this.pgHandleNext);
	this.galleryPopup.dialogDiv.appendChild(this.pgHandlePrev);
	this.galleryPopup.overlayDialogSetPosition();
	}

photoGallery.prototype.reAdjustGallery = function()
	{
	this.removeEvent(this.stageImg, 'load', function(){ that.readjustGallery(); })
	this.galleryPopup.overlayDialogSetPosition();
	}

return true
}
		

