/*--------------------------------------------------

changed from gridview.js in become.com

- use Yahoo! U.I.
- trigger: "li" -> "div.productscontents"
- base object's IDs: "r0" -> "itemid" (0:number)
- popup IDs: "exp_product_" -> "popup"

--------------------------------------------------*/

//window.onload = function(){initPopup();}

var flag = 0;
var t = new Array();

function check(obj){
	if (obj.checked) {flag=1;}
	else {flag=0;}
}

function initPopup() {
	div = document.getElementById("shopBlock");
	if(div){
		var items = YAHOO.util.Dom.getElementsByClassName('productscontents','div');
		for (var i=0; i<items.length; i++) {
			if (items[i].getAttribute("id") && items[i].getAttribute("id").substring(0, 6) == "itemid") { 
				addEvent(items[i], "mouseover", showPopup, false);
			}
		}
	}
}
	
function showPopup(event) {

	if (flag === 1) {
		return false;
	}

	var target = getEventTarget(event);
	var related = getEventRelatedTarget(event);
	var parent_el = target;
	while (parent_el.className != 'productscontents') {
		parent_el = parent_el.parentNode;
	}
			
	// mouseover is triggered by the div.productscontents
	// 	 - could be from a child (ignore)
	//   - could be from outside (execute)
	// mouseover is triggered by a child element 
	//   - could be from the div.productscontents (ignore)
	//	 - could be from another child of div.productscontents (ignore)
	//   - could be from outside (execute)
	if ((target == parent_el && !isChild(parent_el, related)) 
	|| (target != parent_el && parent_el != related && !isChild(parent_el, related))) {
		// green box mouseover
		highlightProduct(parent_el, "#66CC66", "#EEFFEE");
		YAHOO.util.Dom.setStyle(parent_el, 'z-index', 99);
		
		// popup on delayed mouseover 
		var index = parent_el.getAttribute("id").substring(6);
		clearTimeout(t[index - 1]);
		t[index - 1] = setTimeout("displayPopup(" + index + ")", 500);
		addEvent(parent_el, "mouseout", hidePopup, false);
	}
}
	
function hidePopup(event) {
	var target = getEventTarget(event);
	var related = getEventRelatedTarget(event);
	var parent_el = target;
	while (parent_el.className != 'productscontents') {
		parent_el = parent_el.parentNode;
	}
		
	// a. what you land on isn't the div.productscontents element
	// b. and what you land on isn't a child of the div.productscontents element
	if (related != parent_el && !isChild(parent_el, related)) {
		// remove green box on mouseout
		highlightProduct(parent_el, "#FFFFFF", "#FFFFFF");
		YAHOO.util.Dom.setStyle(parent_el, 'z-index', 9);
		
		// remove popup
		var index = parent_el.getAttribute("id").substring(6);
		clearTimeout(t[index - 1]);
		var div = document.getElementById("popup" + index);
		YAHOO.util.Dom.replaceClass(div, 'popupBlockShow', 'popupBlockHide');
		YAHOO.util.Dom.removeClass(div, 'popupLeft');
		YAHOO.util.Dom.removeClass(div, 'popupRight');
	}
}


	
function highlightProduct(p, trim, color) {
//	p.style.border = "1px solid " + trim;
	p.style.backgroundColor = color;
}
	
function displayPopup(index) {
	var sbox = document.getElementById("itemid" + index);
	var div = document.getElementById("popup" + index);

	var totalWidth  = YAHOO.util.Dom.getViewportWidth();
	var popupLeft  = YAHOO.util.Dom.getX(sbox);
	if (popupLeft > 500) {
		YAHOO.util.Dom.addClass(div, 'popupLeft');
	} else {
		YAHOO.util.Dom.addClass(div, 'popupRight');
	}
	
	YAHOO.util.Dom.replaceClass(div, 'popupBlockHide', 'popupBlockShow');
}







//[utilities]

// returns the event target
function getEventTarget(e) {
	if (window.event) {
		return window.event.srcElement;
	} else {
		return e.target;
	}
	return;
}

// returns the related target
// the element you came from in the case of a mouseover event
// the element you moved to in the case of a mouseout event
function getEventRelatedTarget(e) {
	if (window.event) {
		if (window.event.type == 'mouseover') {
			return window.event.fromElement;
		}
		if (window.event.type == 'mouseout') {
			return window.event.toElement;
		}
		return;
	} else {
		return e.relatedTarget;
	}
	return;
}

// checks to see if parent is child's parent
function isChild(parent, child) {
	var node = child; 
	if (node) {
		while (node != parent) {
			if (node.nodeName == 'BODY' || node.nodeName == 'HTML') {
				return false;
			}
			node = node.parentNode; 
		}
		return true;
	} 
	return false;
}

// adds an event listener to an object
function addEvent(obj, eType, func, capt) {
	/*W3 DOM */
	if (obj.addEventListener) {
		obj.addEventListener(eType, func, capt);
	} else if (obj.attachEvent) { /*Win IE*/
		obj.attachEvent("on" + eType, func);
	} else { /* Mac IE and older browsers */
		//eval("obj.on" + eType + "=func;");
		// remooved for now because mac IE has too many problems
	}
}