// elements can have the attribute "adjusty" and "adjustx" to fix posistioning

popupTemplate = "<div id=\"popup-template\" class=\"popup\">\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n<tr>\n<td class=\"puTopLeft\">&nbsp;</td>\n<td class=\"puTopCenter\">&nbsp;</td>\n<td class=\"puTopRight\">&nbsp;</td>\n</tr>\n<tr>\n<td class=\"puMiddleLeft\" valign=\"top\"></td>\n<td class=\"puContent\"><table width=\"100%\" class=\"puMiddleCenter\"></table></td>\n<td class=\"puMiddleRight\" valign=\"top\"></td>\n</tr>\n<tr>\n<td class=\"puBottomLeft\">&nbsp;</td><td class=\"puBottomCenter\">&nbsp;</td>\n<td class=\"puBottomRight\">&nbsp;</td>\n</tr>\n</table>\n<div id=\"puBeak\"></div>\n</div>";

configHoverIntent = {
	sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
	interval: 200, // number = milliseconds for onMouseOver polling interval
	over: createPopup, // function = onMouseOver callback (REQUIRED)
	timeout: 200, // number = milliseconds delay before onMouseOut
	out: removePopup // function = onMouseOut callback (REQUIRED)
};

widthAdjust = 87; // The width of the popup minus the with of the incoming content.
                  // ie. width of .puMiddleLeft and .puMiddleRight plus the padding of puMiddleRight
									
// ** Don't Edit Passed This Point *********** //
dontClose = 0;                // When value is 1 popup will not close, else 0.
zIndex = 620;                 // Placed new popups over exsiting popups.
disClass = "";                // Will be used to determine display class.

// onLoad
$(document).ready(
	function() {
		//$(".products #pageContent li").after("<br />");
		$("#productLists [class*='popStart-']").each(function(e){ // In effect, create an "auto" width block-level element.
			$(this).css("display","inline");
			$(this).css("width",$(this).width());
			$(this).css("display","block");
		});
		// Add li hover when ul class IsNot noHover
		$("#pageContent :not(.noHover) [class*='popStart-']").hoverIntent( configHoverIntent );
		$("#pageContent :not(.noHover) [class*='popStart-']").hover(function() { $(this).addClass("highlight"); }, function() { $(this).removeClass("highlight"); });
		
	}
);

function createPopup(e) {
	newPopID = $(this).attr("class").replace(/popStart-/, "").replace(/highlight/,"").replace(/ /,"");
	docWidth = $("body").width(); // Gets the width of the body element.
	docHeight = $("body").height();
	nLeft = 0;
	nTop = 0;
	if (newPopID != "") {
		// Retrieve innerContent
		getContent = $(".popup-"+newPopID);
		newWidth = getContent.width()+widthAdjust;
		if (newWidth < 300)
			newWidth = 300;
		newHeight = getContent.height();
		//end
		
		// determine display (class = [disLeft | disRight])
		popStart = $(".popStart-"+newPopID);
		adjustY = (isNaN(parseInt(popStart.attr("adjusty"))) ? 0 : parseInt(popStart.attr("adjusty")));
		adjustX = (isNaN(parseInt(popStart.attr("adjustx"))) ? 0 : parseInt(popStart.attr("adjustx")));
		
		//alert("Y: "+adjustY+"\nX: "+adjustX);
		
		offset = popStart.offset();
		
		negOffsetTop = docHeight-(offset.top+popStart.height());
		if (newHeight > negOffsetTop) {
			nTop = newHeight-negOffsetTop+42;
		}
		nTop = offset.top+popStart.height()/2-5-nTop;
		
		if (offset.left <= docWidth/2) {
			negOffsetLeft = docWidth-(offset.left+popStart.outerWidth());
			if (newWidth > negOffsetLeft) {
				nLeft = newWidth-negOffsetLeft;
			}
			nLeft = offset.left+popStart.outerWidth()-nLeft;
			disClass = "disRight";
		} else {
			if (offset.left < newWidth) {
				nLeft = newWidth-offset.left;
			}
			nLeft = offset.left-newWidth+nLeft;
			disClass = "disLeft";
		}
		// end
		
		if ( !$("#popup-template-"+newPopID).length > 0 ) {
			$("body").append(popupTemplate);
			newContainer = $("#popup-template");
			newContainer.attr("id", newContainer.attr("id")+"-"+newPopID);
			
			toContent = $("#popup-template-"+newPopID+" .puMiddleCenter");	
			toContent.html($(getContent).html());
			//toContent.html(getContent.html());
			
			//alert("nTop: "+nTop+"\nadjustY: "+adjustY+"\n"+eval(nTop+adjustY)+"px");
			//alert("nLeft: "+nLeft+"\nadjustX: "+adjustX+"\n"+eval(nLeft+adjustX)+"px");
			
			newContainer.css("top", eval(nTop+adjustY)+"px").css("left", eval(nLeft+adjustX)+"px").css("opacity", "0").css("display", "block").addClass(disClass);
			newContainer.width(newWidth);
			
			newContainer.bind("mouseenter", function(){
				dontClose = 1;
			}).bind("mouseleave", function(){
				dontClose = 0;
				removePopup($(this).attr("id"));
			});
			
		} else {
			newContainer = $("#popup-template-"+newPopID);
			newContainer.stop(true);
		}
		++zIndex;
		newContainer.css("z-index", zIndex).animate({opacity: "1"}, 150);
	}
}

function removePopup(comingID) {
	if (dontClose == 0) {
		oldPopID = (typeof comingID == "string" ? comingID.replace(/popup-template-/, "") : $(this).attr("class").replace(/popStart-/, "") );
		oldContainer = $("#popup-template-"+oldPopID);
		
		oldContainer.animate({opacity: "0"}, 500, function callback() {
			oldContainer.unbind("mouseenter").unbind("mouseleave");
			$(this).remove();
			dontClose = 0;
		});
	}
}