function getElementLeft(p_elm) {
	var x = 0;
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} 
	else {
		elm = document.getElementById(p_elm);
	}
	while (elm != null) {
	  x+= elm.offsetLeft;
	  elm = elm.offsetParent;
	}
	return parseInt(x);
}
function getElementTop(p_elm) {
	var y = 0;
	var elm;
	if(typeof(p_elm) == "object"){
	  elm = p_elm;
	} 
	else {
	  elm = document.getElementById(p_elm);
	}
	while (elm != null) {
	  y+= elm.offsetTop;
	  elm = elm.offsetParent;
	}
	return parseInt(y);
} 
/////////////
function ToggleFloatingLayer(DivID, iState, object) {
	
	//NEW STYLE
	if (document.getElementById("mini_cart") && page_loaded) {	
		var cur_status = document.getElementById(DivID).style.display;
		if (iState == 1 && (cur_status == "none" || cur_status == "")) {
			
			//CENTER DIV
			centerDiv('FloatingLayer', 40, 500);
			
			//LAUNCH CART
			sendRequest("action=display","mini_cart");
			
			//trigger google visit:
			registerPageVisitGoogle();
			
		} else {
			if (iState == 0) {
				if (document.getElementById('overlay').style.display != 'none') {
					hideOverlay();
					hideSelects('visible');
				}
			}
		}
		
		//HIDE OR SHOW CART
		if(document.layers)	   //NN4+
		{
		   document.layers[DivID].display = iState ? "block" : "none";
		}
		else if(document.getElementById)	  //gecko(NN6) + IE 5+
		{
			var obj = document.getElementById(DivID);
			obj.style.display = iState ? "block" : "none";
		}
		else if(document.all)	// IE 4
		{
			document.all[DivID].style.dispaly = iState ? "block" : "none";
		}
	}	
}
/** method to register special url as a page visit */
function registerPageVisitGoogle() {
	if (trackGoogleURL != '') {
		//pageTracker._trackPageview(trackGoogleURL);
	}
}
/*-----------------------------------------------------------------------------------------------*/

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
addOnloadEvent(getBrowserInfo);
function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 								= "an unknown operating system";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
function hideSelects(visibility){
	selects = document.getElementsByTagName('select');
	for(i = 0; i < selects.length; i++) {
		selects[i].style.visibility = visibility;
	}
}
//lightbox prevention onuser click everywhere on a page

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

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
//
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;
}

function showOverlay() {
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
}
function hideOverlay() {
	// get objects
	//alert("hiding ... ");
	if (document.getElementById('overlay') && document.getElementById('overlay').style.display != 'none') {
		var objOverlay = document.getElementById('overlay');
		// hide lightbox and overlay
		objOverlay.style.display = 'none';
	}
}

function initOverlay() {
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {
										//close_adding_layer();
										//ToggleFloatingLayer('FloatingLayer', 0, '');
										//close_div();
										//close_reg();
										//reset_account_layer();
										//if (objOverlay.style.display != 'none') objOverlay.style.display = 'none';
										return false;
										}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '5000';
 	objOverlay.style.width = '100%';
	objOverlay.style.backgroundColor = '#ffffff';
	objBody.insertBefore(objOverlay, objBody.firstChild);
}

addOnloadEvent(initOverlay);
var adding_id;
//move layer on resize:

window.onresize = function() {
	//check if they open:
	if (document.getElementById('FloatingLayer')) {
		if (document.getElementById('FloatingLayer').style.display == "block") {
			var div_width = document.getElementById("mini_cart").style.width;
			div_width = div_width.substring(0, div_width.length-2);
			var y = getElementTop(document.getElementById('cart_total'));
			var x = getElementLeft(document.getElementById("table_preview"));
			//alert("x="+x);
			y = 3 + y +document.getElementById("table_preview").clientHeight;//object.clientHeight;
			x = x - div_width + document.getElementById("table_preview").clientWidth;
			document.getElementById('FloatingLayer').style.top = y+'px';
			document.getElementById('FloatingLayer').style.left = x+'px';
			document.getElementById('FloatingLayer').style.width = div_width+'px';		
		}
	}
	if (document.getElementById('adding_layer')) {
		if (document.getElementById('adding_layer').style.display == "block") {
			//alert(document.getElementById("current_add_id").value);
			if (adding_id != "") {
				var obj = document.getElementById("add_"+adding_id);
				////////
				if (obj) {
					var spec_objs = obj.getElementsByTagName("img");
					var img_obj = spec_objs[0];
					if (document.getElementById("productsContent_"+adding_id)) {
						img_obj = document.getElementById("productsContent_"+adding_id);
					}
					var coor = findPos(img_obj);
					//set coordinat for adding layer
					if (document.getElementById("adding_layer")) {
						var obj_display = document.getElementById("adding_layer");
						if (document.getElementById("productsContent_"+adding_id)) {
							coor[0] = coor[0] + (parseInt(document.getElementById("productsContent_"+adding_id).offsetWidth) - 275) - 2;
							coor[1] = coor[1] + (parseInt(document.getElementById("productsContent_"+adding_id).offsetHeight) - 100) - 2;
						}
						else {
							//we are on a product page based on add to cart image button
							coor[0] = coor[0] + (parseInt(img_obj.width) - 275);
							coor[1] = coor[1] - 1;
							//alert(img_obj.width+" - "+img_obj.height);
						}
						showOverlay();
						obj_display.style.left = coor[0]+"px";
						obj_display.style.top = coor[1]+"px";
					}
				}
				///////
			}
		}
	}
}
