	/*************************************************************

		Script		: Lightpage
		Version		: 1.0
		Authors		: Moon Kyoung
		Desc		: Function based barebone Lightbox
		Licence		: Open Source MIT Licence

	**************************************************************/



	//window.onresize = lightpagePosChild;

	var lightpageCheckScrollPosInterval = null;

	var CONFIG_LIGHTPAGE_PATH = "lightpage path; do not ";
	var CONFIG_IMAGES = new Array("jpg","jpeg","gif","bmp");
	var CONFIG_USE_LOADING = null;
	var CONFIG_POS_WATCH_INTERVAL = 15; //Position watcher check interval; default is 15;
	var CONFIG_OPACITY = 0; //Overlay opacity value; 1 = 100%; 0.1 = 10%; default is 70%;
	
	

	
	//Create Environment for lightpage
	function lightpageIni(){


		
		//Hide Select element
		hideSelect(true);


		var tBody;
		var overlayDiv;
		var overlayChild
		

		//Get the body ref.
		var targetBody = $(document.body);
		targetBody.style.height = "100%";
		targetBody.style.width = "100%";


		//Make overlay div
		var overlayDiv = document.createElement('div');
		overlayDiv.id = 'lightpageOverlay';
		overlayDiv.style.display = "none";
		overlayDiv.style.position = "absolute";
		overlayDiv.style.top = "0";
		overlayDiv.style.left = "0";
		overlayDiv.style.width = "100%";
		overlayDiv.style.height = "100%";
		overlayDiv.style.zIndex = "999";
		overlayDiv.style.backgroundColor = "#000";
	

		

		//Make content div
		var overlayChild = document.createElement('div');
		overlayChild.id = "lightpageContent";
		overlayChild.style.position = "absolute";
		overlayChild.style.backgroundColor="white";
		overlayChild.style.zIndex = "9999";
		overlayDiv.style.display = "inline";

			
		//Apened overlay and content divs into body
		targetBody.appendChild(overlayDiv);
		targetBody.appendChild(overlayChild);	

		$("lightpageContent").addEvent('click',function(){
			lightpageClose();
		});

		//opacity
		var op = new Fx.Tween( $("lightpageOverlay") );

		op.start("opacity",CONFIG_OPACITY,CONFIG_OPACITY);


	}


	function lightpagePOST(page,formName){
			
			

			var formData = lightpage_getFormData(formName);

			//Check if there is a opened lightpage, then close it 
			var isOpened = $("lightpageOverlay");
			if(isOpened!=null){ lightpageClose(); }

			lightpageIni();


			new Ajax(page,{
				method : "post"
				,data : formData
				,onRequest: function(){
					if(CONFIG_USE_LOADING!=null){
						$("lightpageContent").innerHTML = "<img src='"+CONFIG_USE_LOADING+"'>";
					}
				}
				,onComplete: function(){
					//$("lightpageContent").innerHTML = this.response.text;
					lightpageCheckScrollPosInterval = setInterval("lightpageCheckScrollPos();",CONFIG_POS_WATCH_INTERVAL);			
					lightpagePosChild();						
				}
			}).request();

	}

	function lightpageGET(page,width,height){


		//Check if there is a opened lightpage, then close it 
		var isOpened = $("lightpageOverlay");
		if(isOpened!=null){ lightpageClose(); }

		lightpageIni(); 

	

		var url = null;
		var pageExtention = lightpage_getFileExtention(page);

		var isImage = isInArray(pageExtention,CONFIG_IMAGES);
		
		if(isImage == true){
			url = CONFIG_LIGHTPAGE_PATH + "/prepages/imageViewer/imageViewer.php?img="+page;
		}else{
			url = page;
		}



		new Request.HTML({
			url:url
			
			,onRequest: function(){
				if(CONFIG_USE_LOADING!=null){


					$("lightpageContent").innerHTML = "<img src='"+CONFIG_USE_LOADING+"'>";
				}
			}
			
			,onComplete: function(){
					$("lightpageContent").style.width = width+"px";
					$("lightpageContent").style.height = height+"px";
				$("lightpageContent").innerHTML = this.response.text;
			
				lightpageCheckScrollPosInterval = setInterval("lightpageCheckScrollPos();",CONFIG_POS_WATCH_INTERVAL);			
				lightpagePosChild();	
			}
		}).get();

		$("lightpageOverlay").addEvent('click',function(e){
			lightpageClose();
		});

	}
	
	//Close opened lightpage
	function lightpageClose(){

	
		var lightpageOverlayDiv = $("lightpageOverlay");
		var lightpageContent = $("lightpageContent");

		lightpageOverlayDiv.parentNode.removeChild(lightpageOverlayDiv);
		lightpageContent.parentNode.removeChild(lightpageContent);
		lightpageCheckScrollPosInterval = null;
		hideSelect(false);
		

	}


	/* ------------------------------------------------ Position Releated ------------------------------------------------- */

	function lightpageCheckScrollPos(){
		
		if(lightpageCheckScrollPosInterval!=null){

			var doExecute = $("lightpageOverlay");

			if(doExecute!=null){			
				
				var scrollWidth = getScrollWidth();
				var scrollHeight = getScrollHeight();
			
				doExecute.style.top = scrollHeight+"px";
				doExecute.style.left = scrollWidth+"px";

				lightpagePosChild();
	
			}
		}
	}

	function lightpagePosChild(){
		
		var overlayContent,overlay;	
		var bodyWidth,bodyHeight;
		var overlayContentWidth, overlayContentHeight;
		var sWidth,sHeight;

		overlayContent = $('lightpageContent');
		overlay = $("lightpageOverlay");

		if(overlayContent!=null){
	
			if (self.innerHeight) 
			{
				bodyWidth = self.innerWidth;
				bodyHeight = self.innerHeight;
			}
			else if (document.documentElement && document.documentElement.clientHeight)
			
			{
				bodyWidth = document.documentElement.clientWidth;
				bodyHeight = document.documentElement.clientHeight;
			}
			else if (document.body) 
			{
				bodyWidth = document.body.clientWidth;
				bodyHeight = document.body.clientHeight;
			}
			
			overlayContentWidth = overlayContent.offsetWidth;
			overlayContentHeight = overlayContent.offsetHeight;
				
			sWidth = getScrollWidth();
			sHeight = getScrollHeight();
				
			overlayContent.style.top = ( (parseInt(bodyHeight / 2))  - (overlayContentHeight / 2) ) + sHeight+"px";
			overlayContent.style.left = ((bodyWidth / 2) - (overlayContentWidth / 2)) + sWidth+"px";
		
		}

	}

	 

	/* ------------------------------------------------ Helper ------------------------------------------------------------- */

	function lightpage_getFormData(formName){
	
		

			//var targetForm = document.forms[formName];
			var targetForm = document.forms[formName];
			
			var formLength = targetForm.length - 1;
			var frmIndex = 0;
			var postString = '';

			for(frmIndex = 0; frmIndex <= formLength; frmIndex++){
				
				targetElement = targetForm[frmIndex];
			
				if(targetElement.type == "radio"){
					if(targetElement.checked){
						postString = postString + targetElement.name + "=" + targetElement.value + "&";
					}
				}
				if(targetElement.type == "checkbox"){
					if(targetElement.checked){
						postString = postString + targetElement.name + "=" + targetElement.value + "&";
					}
				}
				if(targetElement.type == "text"){
					postString = postString + targetElement.name + "=" + targetElement.value + "&";
				}
				if(targetElement.type == "password" ){
					postString = postString + targetElement.name + "=" + targetElement.value + "&";
				}
				if(targetElement.type == "textarea"){
					postString = postString + targetElement.name + "=" + targetElement.value + "&";
				}	
				if(targetElement.type == "hidden"){
					postString = postString + targetElement.name + "=" + targetElement.value + "&";
				}	
				if(targetElement.type == "SELECT" || targetElement.type == 'select-one'){
					postString = postString + targetElement.name + "=" + targetElement.options[targetElement.selectedIndex].value + "&";
				}

			

			}
			
			return postString;

	}



	//returns true if findTarget is in findArray
	function isInArray(findTarget,findArray){

		var loopCounter = findArray.length - 1;
		
		for(var i = 0; i<=loopCounter; i++){
			
			if(findArray[i] == findTarget){
				return true;
			}

		}
		
	}

	function lightpage_getFileExtention(filename){

		var extentionSplit = filename.split(".");
		var extentionSplitCounter = extentionSplit.length;
		var pageExtention = extentionSplit[extentionSplitCounter-1];
		
		return pageExtention;
	}


	function hideSelect(hide){
		
		var cmd;
		if(hide == true) cmd = "hidden";
		else cmd = "visible";

		if (document.all) {
			for (formIdx=0; formIdx<document.forms.length; formIdx++) {
				var theForm = document.forms[formIdx];
					for (elementIdx=0; elementIdx<theForm.elements.length; elementIdx++) {

					if(theForm[elementIdx].type == "select-one") {
					theForm[elementIdx].style.visibility = cmd;
					}
				}
			}
		}
	}

	function getScrollWidth(){

	   var w = window.pageXOffset ||
			   document.body.scrollLeft ||
			   document.documentElement.scrollLeft;
			   
	   return w ? w : 0;
	} 

	function getScrollHeight(){

	   var h = window.pageYOffset ||
			   document.body.scrollTop ||
			   document.documentElement.scrollTop;
			   
	   return h ? h : 0;

	}

