/*
	
	couple own features added - such as dynamic text corresponding for images, extracted from database
	and alternate images view in fullsize mode
	http://zmolo.com

	thanks to Lokesh Dhakar - http://www.huddletogether.com for the base
*/

// If you would like to use a custom loading image or close button reference them in the next two lines.
var loadingImage = '/im/clock1.gif';
var currentfade = 100;
var transDirection=0;
//


function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function getClear(obj){
	obj.value='';
}


/*updates security code picture*/
function refIm(imagename){
	var ima=document.getElementById(imagename);
	var src=ima.src;
	var regs=	/\&tst\=.*/;
	if (src.match(regs))
	{
		newshit="&tst="+Math.random();
		src = src.replace(regs,newshit);
	} else {
		src=src+"&tst="+Math.random();
	}
	ima.src = src;
	return false;
}
// Core code from - quirksmode.org

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



// Core code from - quirksmode.org
// Edit for Firefox by pHaez


function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

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


//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.
//

function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}	
	key = String.fromCharCode(keycode).toLowerCase();
	if(key == 'x'){ hideLightbox(); }
	if(keycode == 37){
		var objPrev = document.getElementById('prevButton');
		objPrev.onclick();
	}
	if(keycode == 39){ 
		var objNext = document.getElementById('nextButton');
		objNext.onclick();
	}
}


//
// listenKey()
//
function listenKey () {	document.onkeydown = getKey; }
	

//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//
function showLightbox(objLink)
{
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objImage = document.getElementById('lightboxImage');
	var objLoadingImage = document.getElementById('loadingImage');
	var objText=document.getElementById('text');
	//objText.innerHTML=objLink.getAttribute("id");
	var objScript = document.getElementById ? document.getElementById("ajaxObj") : null;
	var currentID = objLink.getAttribute("id");
	var sstr="/get_ajax.php?act=gallery&id="+currentID+"&timestamp="+Math.random();
	objScript.src=sstr;


	var allIDS = new Array();
	allIDS = document.forms['photoform'].allims.value.split(",");
	
	var ccid;

	for (m=0;m<allIDS.length;m++ ){
			if (allIDS[m] == currentID)
			{
				ccid=m;
			}
	}

	
	
	var objPrev = document.getElementById('prevButton');
	if(ccid > 0) {
		objPrev.style.display = '';
		objPrev.onclick = function () {
			ind = ccid-1;
			var previndex= document.getElementById(allIDS[ind]);
			return previndex.onclick();
			
		}
	} else {
		objPrev.style.display = 'none';
	}
	

	var objNext = document.getElementById('nextButton');

	if(ccid < (allIDS.length-1)) {
		objNext.style.display = '';
		objNext.onclick = function () {
			ind = ccid+1;
			var nextindex= document.getElementById(allIDS[ind]);
			return nextindex.onclick();
			
		}
	}else {
		objNext.style.display = 'none';
	}


	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// center loadingImage if it exists
	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	// preload image
	imgPreload = new Image();

	imgPreload.onload=function(){
		objImage.src = objLink.href;

		// center lightbox and make sure that the top and left values are not negative
		// and the image placed outside the viewport
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 80 - imgPreload.height) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 80 - imgPreload.width) / 2);

		//objText.style.width=imgPreload.width-20+"px";
		
		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


		// A small pause between the image loading and displaying is required with IE,
		// this prevents the previous image displaying for a short burst causing flicker.
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			pause(100);
		} 

		if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }

		// Hide select boxes as they will 'peek' through the image in IE
		selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
                selects[i].style.visibility = "hidden";
        }

	
		objLightbox.style.display = 'block';

		// After image is loaded, update the overlay height as the new image might have
		// increased the overall page height.
		arrayPageSize = getPageSize();
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		
		// Check for 'x' keypress
		listenKey();

		onloadDone=1; 
		currentfade=0;

		moveNavButts(objImage.width,objImage.height,20);
		objImage.className = 'transparent';
		fadeObj(objImage,1);
		return false;
	}

	
	imgPreload.src = objLink.href;
	

}

function moveNavButts (imgW,imgH,margin){
	var prev = document.getElementById('prevButton');
	var next = document.getElementById('nextButton');
	
	prev.style.marginTop=parseInt(imgH/2)+30+'px';
	prev.style.marginLeft='-35px';
	next.style.marginTop=parseInt(imgH/2)+30+'px';
	next.style.marginLeft=parseInt(imgW+2*margin-5)+'px';	  
}





//
// hideLightbox()
//
function hideLightbox()
{
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';

	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}

	// disable keypress listener
	document.onkeypress = '';
}



function buildFader()
{
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "fdpop")){
			anchor.onclick = function () {showLightbox(this); return false;}
		}
	}


	/*iframe object to process DB*/
	var objBody = document.getElementsByTagName("body").item(0);
	var fr=document.createElement('iframe');
	fr.src='javascript:false;';
	fr.name="ajaxObj";
	fr.id="ajaxObj";
	fr.style.position='absolute';
	fr.style.margin=fr.style.padding='0px';
	fr.style.zIndex=1;
	fr.style.display='none';
	fr.style.width=fr.style.height='0px';
	objBody.appendChild(fr);	


	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {hideLightbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// preload and create loader image
	var imgPreloader = new Image();
	
	// if loader image found, create link to hide lightbox and create loadingimage
	imgPreloader.onload=function(){

		var objLoadingImageLink = document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.onclick = function () {hideLightbox(); return false;}
		objOverlay.appendChild(objLoadingImageLink);
		
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = loadingImage;
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '201';
		objLoadingImageLink.appendChild(objLoadingImage);

		imgPreloader.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs

		return false;
	}

	imgPreloader.src = loadingImage;

	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';	
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);

	var objLinkn = document.createElement("a");
	objLinkn.setAttribute('href','#');
	objLinkn.setAttribute('title','Next');
	objLinkn.setAttribute('id','nextButton');
	objLightbox.appendChild(objLinkn);


	var objLinkn = document.createElement("a");
	objLinkn.setAttribute('href','#');
	objLinkn.setAttribute('title','PREVIOUS');
	objLinkn.setAttribute('id','prevButton');
	objLightbox.appendChild(objLinkn);

	var objLink = document.createElement("a");
	objLink.setAttribute('href','#');
	objLink.setAttribute('title','Click to close');
	objLink.onclick = function () {hideLightbox(); return false;}
	objLightbox.appendChild(objLink);


	// preload and create close button image
	var objCloseButton = document.createElement("a");
	objCloseButton.setAttribute('id','closeButton');
	objCloseButton.setAttribute('title','CLOSE');
	objCloseButton.setAttribute('alt','CLOSE');
	objCloseButton.style.position = 'absolute';
	objLink.appendChild(objCloseButton);


	var objImage = document.createElement("img");
	objImage.setAttribute('id','lightboxImage');
	objLink.appendChild(objImage);

	var objText = document.createElement("div");
	objText.setAttribute('id','text');
	objLightbox.appendChild(objText);

	
	
	
	
}


//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}


function setvalue(name,value){
	var obj = document.getElementById ? document.getElementById(name) : null;
	obj.innerHTML = value;
}



// fade in, fade out
function fadeObj(obj,direction){
	var objPassed=document.getElementById(obj.getAttribute("id"));
	var iid=obj.getAttribute('id');
	eval("window.Oppa"+iid+"= function() {goFade(objPassed,direction,iid);}");

	if (!direction)
	{  
		if (eval(isdefined("downTimer"+iid)))
		{
			eval("clearTimeout(downTimer"+iid+")");
		}
	} else {
		if (eval(isdefined("upTimer"+iid)))
		{
			eval("clearTimeout(upTimer"+iid+")");
		}
	}

	goFade(objPassed,direction,iid);
}



function goFade(OP,direction,objID){
	var step=10;
	var duration=30;
	if (direction)
	{	
		if (currentfade < 100)
		{
			if (OP.filters)
			{ 
				OP.filters[0].opacity= currentfade+step;
			} else {
				OP.style.opacity =  (currentfade+step)/100;
				OP.style.MozOpacity =  (currentfade+step)/100;
			}
			currentfade+=step;
			eval("downTimer"+objID+"="+setTimeout("Oppa"+objID+"()",duration));
		} else {
			eval("clearTimeout(downTimer"+objID+")");
		}
			/*window.alert(currentfade); */
	} else {
		if (currentfade > 0)
		{
			if (OP.filters)
			{ 
				OP.filters[0].opacity= currentfade-step;
			} else {
				OP.style.opacity =  (currentfade-step)/100;
				OP.style.MozOpacity =  (currentfade-step)/100;
			}
			currentfade-=step;
			eval("upTimer"+objID+"="+setTimeout("Oppa"+objID+"()",duration));
		}   else {
			eval("clearTimeout(upTimer"+objID+")");
		}
	}
}



function arrangeElements(){
	var screenPro= getPageSize();
	var ratio = screenPro[2]/screenPro[3];
	var bgImage=  document.getElementById ? document.getElementById("background") : null;
	
	if (ratio > 1.42)
	{
		bgImage.style.width =  screenPro[2]+"px";
		bgImage.style.height = "auto";
		/*window.alert("1"+"/"+ratio); */
	} else {
		bgImage.style.height =  screenPro[3]+"px";
		bgImage.style.width = "auto";
		/*window.alert("2"+"/"+ratio);	*/
	}
		

	var brd =  document.getElementById ? document.getElementById("brd") : null;
	brd.style.left = (screenPro[2] - 700)>0 ? parseInt((screenPro[2] - 700)/2)+"px" : "0px;"
	/*window.alert(screenPro[0]+"/"+screenPro[1]+"/"+screenPro[2]+"/"+screenPro[3]+"/"+brd.style.left);	*/

	
	var zmolo =  document.getElementById ? document.getElementById("zmolo") : null;
	zmolo.style.left = (screenPro[2] - 700)>0 ? parseInt((screenPro[2] - 700)/2)+670+"px" : "670px;"
	zmolo.style.top = screenPro[3]>650 ? screenPro[3]-40+"px" : "650px;"

	if (!document.getElementsByTagName){ return; }
	var divs = document.getElementsByTagName("div");

	// loop through all anchor tags
	for (var i=0; i<divs.length; i++){
		var div = divs[i];
		var defvalue=0;
		if (div.getAttribute("rel") == "transform" || div.getAttribute("rel") == "movetop"){
			if (div.getAttribute("default"))
			{
				defvalue=div.getAttribute("default");
			}
			strechElement(div,div.getAttribute("rel"),defvalue,screenPro[3]);
		}
	}

}

function resize(){
	var t=setTimeout("arrangeElements()",500); /*Explorer tweak*/
	clearTimeout("t");
}


function strechElement (obj,action,def,screenHeight){
	var totalH=590;
	delta = (screenHeight - totalH)>0 ? parseInt((screenHeight - totalH)/2) : 0;
	
	if (action == 'transform')
	{
		obj.style.height=parseInt(def)+delta+'px';
	}  else if(action == 'movetop'){
		obj.style.marginTop=parseInt(def)+delta+'px';
	}

}

addLoadEvent(buildFader);	// run buildFader onLoad
window.onresize= resize;
