/* This javascript file handles the image hover popups on the product thumbnail list pages */

if (!(typeof(addEvent)=='function')) {
  function addEvent(name,obj,f) {
    if (window.attachEvent) {
      obj.attachEvent("on"+name,f);
    } else if (window.addEventListener) {
      obj.addEventListener(name,f,false);
    }
  }
}

addEvent('load',window,function() {
	var productThumbs = document.getElementsByTagName("IMG");
	
	function showPopup(e) {
		var target=null;
    if (window.event) {
      target=window.event.srcElement;
      window.event.cancelBubble=true;
    } else {
      target=e.target;
      if (e.preventDefault) e.preventDefault();
      if (e.stopPropagation) e.stopPropagation();
    }
		
		var fullsize = target.parentNode.nextSibling;
		while(fullsize.tagName != "DIV")
		{
			fullsize = fullsize.nextSibling;
		}
		
		var fullsizechildren = fullsize.childNodes;
		for(var i=0; i<fullsizechildren.length; i++)
		{
			if(fullsizechildren[i].className == "productfull")
				fullsize = fullsizechildren[i];
		}
		
		//Determine where to show the popup. x axis
		if(target.title % 5 == 4 || target.title % 5 == 0)
		{
			var fullsizewidth = fullsize.getAttribute("width");
			fullsize.style.left = "-" + fullsizewidth + "px"; 
		}
		else
		{
			fullsize.style.left = "140px";
		}
		
		//Determine where to show the popup. y axis
		var tempheight = Math.ceil(target.title/5);
		tempheight = tempheight * 120;
		fullsize.style.top = "-" + tempheight + "px"; 
				
		//Show the popup	
		fullsize.style.display = "block";
	}//showPopup
	
	function hidePopup(e) {
		var target=null;
    if (window.event) {
      target=window.event.srcElement;
      window.event.cancelBubble=true;
    } else {
      target=e.target;
      if (e.preventDefault) e.preventDefault();
      if (e.stopPropagation) e.stopPropagation();
    }
		
		var fullsize = target.parentNode.nextSibling;
		while(fullsize.tagName != "DIV")
			fullsize = fullsize.nextSibling;
			
		var fullsizechildren = fullsize.childNodes;
		for(var i=0; i<fullsizechildren.length; i++)
		{
			if(fullsizechildren[i].className == "productfull")
				fullsize = fullsizechildren[i];
		}	
		
		//Hide the popup
		fullsize.style.display = "none";
	}//hidePopup
	
	for(var i=0; i<productThumbs.length; i++)
	{
		if(productThumbs[i].className == "productthumb")
		{
			addEvent('mouseover',productThumbs[i],showPopup);
			addEvent('mouseout',productThumbs[i],hidePopup);
		}
	}
	
});