//Global Vars
var divUp = false;
var last_clicked = null;
var idle = false;
var current_timer = null;
////////////


function hideValue(this_input, this_value) {		
	if (this_input.value != '') {
		this_input.value = '';
	} else {
		this_input.value = this_value;
	}
}



function getPageSize() {
	        
	 var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;		
	} else if (document.scrollHeight > document.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
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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.clientWidth;
		windowHeight = document.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 = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	//alert (pageWidth + " " + pageHeight);
	return [pageWidth,pageHeight];
}




function getOverlay() {	
	/*
	var over = document.getElementById('overlay');
	var popup = document.getElementById('lightbox');
	
	var arrayPageSize = this.getPageSize();
	over.style.width = arrayPageSize[0] + 'px';
	over.style.height = arrayPageSize[1] + 'px';
	
	popup.style.display = 'block';
	*/
}




function showHide(id) {
	e = document.getElementById(id);

	if (e.style.display == 'none') {
		e.style.display = 'block';
	} else if (e.style.display == 'block') {
		e.style.display = 'none';
	}	
}

//////////////////////////////////////////////////////////////////////////////
//runBox
//
//
//Desc: A wrappers for TA_attachbox that packages up all the needed vars for
//calling run.php via attachBox. Also manages toggling of the box
//////////////////////////////////////////////////////////////////////////////
function runBox(elementName,classStyle,funcName,param1,param2,param3,param4){
	
	if(divUp && elementName==last_clicked){
		divUp=false;
		TA_hideDiv();
		current_timer = null;
		idle = false;
	}else{
		divUp=true;
		last_clicked=elementName;
		var vars = "function_name="+funcName+"&param[1]="+param1+"&param[2]="+param2+"&param[3]="+param3+"&param[4]="+param4;
		TA_attachBox(elementName,classStyle,vars,"../php/run.php");
		current_timer = setTimeout('checkTimer()',1800);
	}//end else
	
}//end function

function checkTimer(){
	console.log('checking timer');
	if(current_timer!=null){
		if(idle){
			TA_hideDiv();
			idle = false;
			divUp = false;
			current_timer = null;
		}else{
			idle = true;
			current_timer = setTimeout('checkTimer();',1800);
		}//end else
	}//end if
}//end function

//Adds a product to a book. Accepts a callback function
function addToBook(divName,book_id,product_id,callback){

	//We use updateDiv cause we need the callback
	var vars = "function_name=addToBook&param[1]="+book_id+"&param[2]="+product_id;
	updateContentArea(divName,vars,"../php/run.php",callback);	
	
}//end function

//Creates a new book and adds the item too it
function newBook(divName,product_id,callback){
	
	//get bookName
	var target = "bookName"+product_id;
	var bookName = document.getElementById(target).value;

	var vars = "function_name=createNewBook&param[1]="+product_id+"&param[2]="+bookName;
	updateContentArea(divName,vars,"../php/run.php",callback);	
	
}//end function

function updateDiv(target,fName,param1,param2,param3,param4){
	
	var timestamp = new Date().getTime();
	var vars = "function_name="+fName+"&param[1]="+param1+"&param[2]="+param2+"&param[3]="+param3+"&param[4]="+param4+"&randomized="+timestamp;
	var callback = null;
	
	
	try{
		updateContentArea(target,vars,"../php/run.php",callback);
	}catch(e){
		alert(e);
	}
}//end function

//Called by book builder editor
function saveBookItem(ID){
	
	//Grab field values
	var Room_Name = document.getElementById("Room_Name"+ID).value;
	var Misc_Info = document.getElementById("Misc_Info"+ID).value;
	
	updateDiv("bookItem"+ID,'saveCLBookItem',ID,Room_Name,Misc_Info);
	
}//end function

function saveBookName(Book_ID){
	//Grab field vals
	var Book_Name = document.getElementById("Book_Title").value;
	updateDiv("bookName",'saveBookName',Book_ID,Book_Name);
}
//////////////////////////////////////////////////////////////////////////////
//scaleImg
//
//
//Desc: enforces a maximum width/height on an image.
//var img = an object passed in usally as this onload
//var max_size = size in pixels the height and width are not allowed to exceed 
//////////////////////////////////////////////////////////////////////////////
function scaleImg(img,max_size){
	
	var height = img.height;
	var width = img.width;
	
	
	//Find the longest side and check to see if it exceeds that max
	if(height>=width){
		//perform stuff based on height
		
		//Is it too big?
		if(height>max_size){
			//yes, resize it with respect to the aspect ratio

			//find the ratio
			var ratio = width/height;
			
			//reset the height
			img.height=max_size;
			height=img.height;
			
			//reset the width
			img.width=(height*ratio);
			
		}//end if
		
	}else{
		//perform stuff based on width
		
		//Is it too big?
		if(width>max_size){
			//yes, resize it with respect to the aspect ratio
			
			//find the ratio
			var ratio = height/width;
			
			//reset width
			img.width=max_size;
			width=img.width;
			
			//reset height
			img.height = (width*ratio);
			
		}//end if
		
	}//end if

	
}//end function

//Alters the login form's action
function changeAction(path){
	
	var form = document.getElementById('loginForm');
	form.action=path;
	
}//end function



function TB_showBox(functionName,param1,param2,param3,param4){
					
	//Use TA_showBox cause the normal lightbox isn't centered
	var vars = "function_name="+functionName+"&param[1]="+param1+"&param[2]="+param2+"&param[3]="+param3+"&param[4]="+param4;

	try{
		TA_showBox("lightbox","600",vars,"../php/run.php",0,100);			
	}catch(e){
		alert("TA_showBox:"+e);
		return;
	}//end catch


	//Role out the overlay
	/*
	var overlay =document.getElementById("overlay");
	overlay.style.display="block";
	*/
		
}//end function


function TB_showBox2(functionName,param1,param2,param3,param4){
					
	//Use TA_showBox cause the normal lightbox isn't centered
	var vars = "function_name="+functionName+"&param[1]="+param1+"&param[2]="+param2+"&param[3]="+param3+"&param[4]="+param4;

	try{
		TA_showBox("lightbox","600",vars,"php/run.php",0,100);			
	}catch(e){
		alert("TA_showBox:"+e);
		return;
	}//end catch


	//Role out the overlay
	/*
	var overlay =document.getElementById("overlay");
	overlay.style.display="block";
	*/
		
}//end function

function TB_hideBox(){

	//Hide the overlay
//	document.getElementById('overlay').style.display="none";

	//Hide the floatDiv
	TA_hideDiv();	

}//end function

//This is used on the product profile pages.	
function changeBigPicture(section){

	var main = document.getElementById('BigPicture1');
	var crystals = document.getElementById('BigPicture2');
	var room = document.getElementById('BigPicture3');
	
	main.style.display = "none";
	crystals.style.display = "none";
	room.style.display = "none";
	
	if(section==1)
		main.style.display = "block";
	
	if(section==2)
		crystals.style.display = "block";

	if(section==3)
		room.style.display = "block";

}//end function
