/* htdocs/dpsweb/quickLook.js
   Copyright (c) 2007-2008, Decorative Product Source, Inc.
   $Id: quickLook.js,v 1.12 2009/11/12 14:57:08 dferruggia Exp $ */
  
var boughtOnce,popTimerDone,withPopupShown,iebody,objref,productInfoObj,myQuickLookImage,showonscrollvar,mozfadevar,poptimervar,checkNSwapvar,magnifierLinkObj,xhtml;

//JQuery call to hide the fadeinbox on mouseout.
//Also, don't let the "add to cart" link in the popup be clickable until the timer is done.
jQuery(document).ready(function() { 
	withPopupShown = 0;
	popTimerDone = 1;
	boughtOnce = 0;
	jQuery("#fadeinbox").hover(function() { },function() { hideQuickLook();}); 
	jQuery(document).hover(function() { },function() { hideQuickLook();});
});

function addtocartclick() {
	if (! popTimerDone){ 
		return false;
	} 
	if (boughtOnce){
		return false;
	}
	boughtOnce = 1;
	return true;
}

function cancelpop() {
	if (! withPopupShown) {
			clearTimeout(poptimervar);
			popTimerDone = 1;
	}
}

function checkNSwap() {
	if (myQuickLookImage.complete) {		// if it is loaded to broswer cache
		locateElement("productImg250x250").src = myQuickLookImage.src; //assign it to the html element to display it
		locateElement("loadingImage").style.visibility = 'hidden';  // Now safe to hide the 'loading' mask to show image.
		clearInterval(checkNSwapvar);
	}
}

function displayQuickLook(productInfoObj,magnifierLinkObj,xhtml) {
	
	populateQuickLook(productInfoObj);
	
	
	var ie = document.all && !window.opera;
	iebody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
	objref = locateElement("fadeinbox");
	var scroll_top=(ie)? iebody.scrollTop : window.pageYOffset;

	if (xhtml == 'transitional'){
		objref.style.left=(getAbsolutePosition(magnifierLinkObj).x - 300 )+"px";
		objref.style.top=(getAbsolutePosition(magnifierLinkObj).y - 230 )+"px";	
		if ((getAbsolutePosition(magnifierLinkObj).x - 400 ) < 0){
			objref.style.left = "0px";
		}  
	} else {
		objref.style.left=(getAbsolutePosition(magnifierLinkObj).x - 260 )+"px";  
		objref.style.top=(getAbsolutePosition(magnifierLinkObj).y - 120 )+"px";	
		if ((getAbsolutePosition(magnifierLinkObj).x - 360 ) < 0){
			objref.style.left = "0px";
		} 
	}


	mozfadevar=setInterval("fadeEffect()", 30); //this will run until Opacity is 1, i.e. until fade in effect completed.
	objref.style.visibility="visible"; //put it at last line before other style scripting
	document.getElementById("addToCartLinkOnPopupCart").style.display = "inline";
	document.getElementById("addToCartLinkOnPopupCart").style.visibility = "visible";
	withPopupShown = 1;  //our own flag stored at window global context
	WCH.Apply("fadeinbox","body",true);
	
	
}	

function fadeEffect() {	
	var steps = 5; // Can be adjusted, this affects the speed of the fade in effect.
	var percent = 1 / steps;

	if (parseFloat(objref.style.MozOpacity)<1) {
		objref.style.MozOpacity=parseFloat(objref.style.MozOpacity)+ percent; 
		objref.style.KHTMLOpacity=parseFloat(objref.style.KHTMLOpacity)+ percent;
		objref.style.opacity=parseFloat(objref.style.opacity)+ percent;
		if (typeof(objref.filters) != 'unknown' && objref.filters) {
			objref.filters[0].opacity=parseFloat(objref.filters[0].opacity) + (percent * 100); //IE full opacity is 100 instead of 1
		}
	} else {
		clearInterval(mozfadevar);
	}
}

function findPos (obj){
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;	
		} while (obj = obj.offsetParent);
		return [curleft,curtop];
	}
}

function getAbsolutePosition(element) {
	var xy = { x: element.offsetLeft, y: element.offsetTop };
	if (element.offsetParent) {
		var tmp = getAbsolutePosition(element.offsetParent);
		xy.x += tmp.x;
		xy.y += tmp.y;
	} 
	return xy;
}

function hideQuickLook() {
	clearInterval(showonscrollvar);
	clearInterval(mozfadevar);
	clearInterval(checkNSwapvar);
	if ( objref){
		objref.style.MozOpacity = 0;
		objref.style.opacity = 0;
		objref.style.KHTMLOpacity = 0;
		if (typeof(objref.filters) != 'unknown' && objref.filters)	{
			objref.filters[0].opacity=0;
		}
		withPopupShown = 0;
		document.getElementById("addToCartLinkOnPopupCart").style.display = "none";
		objref.style.visibility="hidden";
	}
	WCH.Discard("fadeinbox");
}

function initQuickLook(tproductInfoObj,tmagnifierLinkObj,txhtml) {
	productInfoObj = tproductInfoObj;
	magnifierLinkObj = tmagnifierLinkObj;
	xhtml = txhtml;
	popTimerDone = 0;
	poptimervar = setTimeout("tDisplay()",300);
	setTimeout("tDone()",1500);
}

function locateElement(elementName) {
	if (document.getElementById) {
		return document.getElementById(elementName);
	} else {
		return document.all[elementName];
	}
}

function populateQuickLook(productInfoObj) {
	var isStandard = document.getElementById ? 1 : 0;
	if (isStandard)	{
		myQuickLookImage = new Image();
		myQuickLookImage.src = productInfoObj.productImg250x250; // At this point, image is being fetched by browser.
		myQuickLookPreview = new Image();
		myQuickLookPreview.src = (productInfoObj.productImg125x125) ? productInfoObj.productImg125x125 : productInfoObj.productImg100x100; //This image should already be cached by the browser.
		if (myQuickLookPreview.complete) { //if it is loaded to browser cache
			locateElement("productImg250x250").src = myQuickLookPreview.src; //assign it to the html element to display it
			locateElement("loadingImage").style.visibility = 'hidden';  //safe now to hide the 'loading' mask to show image.
		}
		checkNSwapvar=setInterval("checkNSwap();", 100);
		document.getElementById("addToCartLinkOnPopup").getElementsByTagName("a")[0].href =  productInfoObj.productLinkOnPopup; 
		document.getElementById("largeProductImage").getElementsByTagName("a")[0].href =  productInfoObj.productLinkOnPopup;

		var addToCart = document.getElementById("addToCartLinkOnPopupCart");
		if (productInfoObj.addToCartLinkOnPopup) {
			addToCart.style.visibility = 'visible'; //necessary as the style can be over-ridden by other product popup
			addToCart.href =  productInfoObj.addToCartLinkOnPopup;
			addToCart.onclick =  function(){return(eval(productInfoObj.addToCartLinkOnPopupOnClick))};
			if (document.getElementById("buyAtImage"))
			{
				document.getElementById("buyAtImage").src = productInfoObj.buyAtImage;
				document.getElementById("buyAtImage").onclick =  function(){return(eval(productInfoObj.buyAtOnClick))};
			}
		} else {	
			addToCart.style.display = 'none';
			addToCart.style.visibility = 'hidden';
		}

		if (productInfoObj.colorchipOnPopup) {
			document.getElementById("colorchip").style.visibility = "visible"; //necessary as the style can be over-ridden by other product popup
			document.getElementById("lgColorchipOnPopup").src =  productInfoObj.colorchipOnPopup;
		} else {
			document.getElementById("colorchip").style.visibility = "hidden";
		}

		if (document.getElementById("rolloverPopupTable").height) {
			document.getElementById("rolloverPopupTable").height.value = productInfoObj.productImg250Height + 10;
		} else {
			document.getElementById("rolloverPopupTable").height = productInfoObj.productImg250Height + 10;
		}
		document.getElementById("loadingImage").style.height = productInfoObj.productImg250Height + 2 + "px";
		document.getElementById("productImg250x250").style.height = productInfoObj.productImg250Height + "px";
		
		for (var prop in productInfoObj) {  //populate td innerHTML
			if (prop == 'productImg250x250' || prop == 'productLinkOnPopup' || prop == 'colorchipOnPopup' || prop == 'addToCartLinkOnPopup' || prop =='addToCartLinkOnPopupOnClick')
			continue;
			try{document.getElementById(prop).innerHTML = productInfoObj[prop];} catch(e){};
		}
	} else { //This actually should be safe to be removed now
		myQuickLookImage = new Image();
		myQuickLookImage.src = productInfoObj.productImg250x250; //upon this point, image is being fetched by browser.
		checkNSwapvar=setInterval("checkNSwap();", 500);
		document.all["addToCartLinkOnPopup"].all[0].href =  productInfoObj.productLinkOnPopup; //first element is <a> in interest
		document.all["largeProductImage"].all[3].href =  productInfoObj.productLinkOnPopup; //4th element is <a> in interest
		
		if (productInfoObj.addToCartLinkOnPopup) {
			document.all["addToCartLinkOnPopup"].all[2].style.visibility = 'visible'; //3rd element is <a> in interest
			document.all["addToCartLinkOnPopup"].all[2].href =  productInfoObj.addToCartLinkOnPopup;
			document.all["addToCartLinkOnPopup"].all[2].onclick =  function(){return(eval(productInfoObj.addToCartLinkOnPopupOnClick))};
		} else {	
			document.all["addToCartLinkOnPopup"].all[2].style.visibility = 'hidden';
		}
		
		if (productInfoObj.colorchipOnPopup) {	
			document.all["colorchip"].style.visibility = "visible";
			document.all["lgColorchipOnPopup"].src =  productInfoObj.colorchipOnPopup;
		} else {
			document.all["colorchip"].style.visibility = "hidden";
		}
		
		for (var prop in productInfoObj) {
			if (prop == 'productImg250x250' || prop == 'productLinkOnPopup' || prop == 'colorchipOnPopup' || prop == 'addToCartLinkOnPopup' || prop =='addToCartLinkOnPopupOnClick')
			continue;
			try{document.all[prop].innerHTML = productInfoObj[prop];} catch(e){};
		}
	}
}

function tDisplay() {
	var fadeinbox = locateElement("fadeinbox");
	fadeinbox.style.MozOpacity = 0;
	fadeinbox.style.opacity = 0;
	fadeinbox.style.KHTMLOpacity = 0;
	fadeinbox.style.filter = "alpha(opacity:0) progid:DXImageTransform.Microsoft.Shadow(color=gray,direction=135)";
	locateElement("loadingImage").style.visibility = 'visible';
	displayQuickLook(productInfoObj,magnifierLinkObj,xhtml);
	boughtOnce = 0;
	locateElement("addToCartLinkOnPopupCart").onclick = addtocartclick;
}

function tDone(){
	popTimerDone = 1;
}

