var debug = '';

/**
 * Init function
 * Checks the content field for uls with the products class. For each of
 * these lists, addProductHoverEvents is called to attach hover events
 */
__onloadStack.push(function() {
	if ($('productimage')) {
		var uls = $('content').getElementsByTagName('ul');
		var ulsize = uls.length;
		var rgx = /products/;
		for (var i=0; i<ulsize; i++) {
			if (rgx.test(uls[i].className)) {
				addProductHoverEvents(uls[i]);
			}
		}
	}
});
/**
 * Init function
 * Adds hover events to the link in a product list. Hover event will only be
 * attached if the following conditions are met:
 *  1. the link has a rel attribute containing an image url
 *  2. the image url could be preloaded
 */
function addProductHoverEvents(ul) {
	var lis = ul.getElementsByTagName('li');
	var lissize = lis.length;

	for(var i=0; i<lissize; i++) {
		var as = lis[i].getElementsByTagName('a');
		
		if (as[0] && as[0].rel) {
			debug = debug + 'init: ' + as[0].rel + "\n";
			var img = new Image();
			//img.evNode = as[0];
			//img.onload = function() { this.evNode.onmouseover = swapImage; debug = debug + 'attached: ' + this.evNode.rel + "\n"; }
			img.src = as[0].rel;
			as[0].onmouseover = swapImage;
		}
	}
}

function swapImage(e) {
	//$('debug').firstChild.nodeValue = debug;
	if (!e) var e = window.event;
	var a = (e.srcElement) ? e.srcElement : e.target;
	while(a.nodeName.toLowerCase() != 'a' && a.parentNode) {
		a = a.parentNode;
	}
	
	if(a.rel) {
		var div = $('productimage');
		var imgs = div.getElementsByTagName('img');
		if (imgs.length == 1) {
			var productId = a.href.slice(a.href.lastIndexOf('=')+1);
			if (productId==0) {
				productId=a.href.slice(a.href.indexOf('product_id=')+11); // now with fotoid
				productId=productId.substring(0, productId.lastIndexOf('=')); 
				imgs[0].src = a.rel;
				//imgs[0].style.cursor = 'pointer';
				//imgs[0].onclick = function(){ openWindowSimpel('foto.asp?product_id='+productId,'',600,522); };
			}else{
				imgs[0].src = a.rel;
				imgs[0].style.cursor = 'pointer';
				imgs[0].onclick = function(){ openWindowSimpel('foto.asp?product_id='+productId,'',600,522); };
			}
		}
	}
}

/************************************************************
Openen van een simpele window;
Argumenten: URL, titel window, breedte en hoogte.
Eigenschappen window:
	toolbar=no;
	location=no;
	directories=no;
	status=no;
	menubar=no;
	scrollbars=yes;
	resizable=no;
	copyhistory=no;
************************************************************/
function openWindowSimpel(strURL,strWindowName,intWidth,intHeight){
	intLeftPosition = (screen.width) ? (screen.width-intWidth)/2 : 0;
	intTopPosition = (screen.height) ? (screen.height-intHeight)/2 : 0;
	window.open(strURL,strWindowName,"top="+intTopPosition+",left="+intLeftPosition+",toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=" + intWidth + ", height=" + intHeight);
}
