
var loadingImage = '/im/clock1.gif';
var currentfade = 100;
var transDirection=0;
var mdX=mdY=null;
var threshold=5;
var ajaxObj= null;
var tipIsOn=0;
var arrangeAdGo=0;
var movedLangs=0;



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





function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}


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();
	}
}



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


function showLightbox(objLink)
{
	// prep objects
	var fadeObj = document.getElementById('fade');
	var objLightbox = document.getElementById('lightbox');
	var objImage = document.getElementById('lightboxImage');
	var objLoadingImage = document.getElementById('loadingImage');
	var objText=document.getElementById('fadeImageText');
	//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();

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

	if (typeof(document.forms['photoform']) !=='undefined')
	{	 
		allIDS = document.forms['photoform'].allims.value.split(",");
		
		var ccid;

		for (m=0;m<allIDS.length;m++ ){
				if (allIDS[m] == currentID)
				{
					ccid=m;
				}
		}
		
		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';
		}
		

		

		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';
		}
	}  else {
		 objNext.style.display = 'none';
		 objPrev.style.display = 'none';
	}


	var arrayPageSize = wholePage();
	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 fade to take up whole page and show
	fadeObj.style.height = (arrayPageSize[1] + 'px');
	fadeObj.style.top = (arrayPageScroll[1] + 'px');
	fadeObj.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 fade height as the new image might have
		// increased the overall page height.
		arrayPageSize = wholePage();
		fadeObj.style.height = (arrayPageSize[1] + 'px');
		
		// Check for 'x' keypress
		listenKey();

		onloadDone=1; 
		currentfade=0;

		moveNavButts(objImage.width,objImage.height,20);
		setTextBlockSize(objImage.width);
		$(objImage).css('opacity',.1);
		fadeObjectAction(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='-55px';
	next.style.marginTop=parseInt(imgH/2)+30+'px';
	next.style.marginLeft=parseInt(imgW+2*margin-5)+'px';	  
}





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

	// hide lightbox and fade
	fadeObj.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 buildImageBox()
{

   	var objBody = document.getElementsByTagName("body").item(0);
	var arrayPageSize = wholePage();
	var arrayPageScroll = getPageScroll();
	var imgPreloader = new Image();
	var fadeObj = document.getElementById('fade');
	
	imgPreloader.onload=function(){
		var fadeObj = document.getElementById('fade');
		var objLoadingImageLink = document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.onclick = function () {hideLightbox(); return false;}
		fadeObj.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(){};	
		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, fadeObj.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','fadeImageText');
	objLightbox.appendChild(objText);

	
	
	
	
}


// fade in, fade out
function fadeObjectAction(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){

	if (direction)
	{	
		$("#"+objID).fadeTo('400',1);
	
	} else {
		
	   $("#"+objID).fadeTo('400',.1);

	}
}


function setTextBlockSize(width) {
	// get objects
	objtext = document.getElementById('fadeImageText');
	objtext.style.width = width+'px';
}


function getAjaxed(src){
	if (ajaxObj == null)
	{
		ajaxObj = document.getElementById ? document.getElementById('ajaxObj') : null
	}
	ajaxObj.src=src;
}


