var spInterval = null;
var cats = new Array();
var spDisplayAds = new Array();
var spURL = "";
var spInfoDiv = null;
var spTextDiv = null;
var spImageDiv = null;
var spBannerWidth = 0;
var spLoadedImages = 0;
var spVisItems = 0;
var spInterval2 = null;
var spOr = 0;
var spFade = 100;
var spIntervalAr = new Array();
var spMoveInterval = 0;

var spAffiliate = "";
var spAffid = "";
var spAppend = "";
var spColor = "";
var spCurrentItem = 0;

function spWriteImageBanner(bWidth,visItems,itemCount,orientation,color, affiliate, affid, append, url,title){
	spURL = url;
	spBannerWidth = bWidth==0?(visItems * 100)-1:bWidth;
	spBannerWidth--;
	spOr = orientation;
	
	spAffiliate = affiliate;
	spAffid = affid;
	spAppend = append;
	spColor = color;
	
	if(cats.length>0){
		counter=0;
		for(var a=0;a<cats.length;a++){
			if(cats[a][3]!=null){
				spDisplayAds[counter] = a;
				counter++;
			} 
		}
		spGetRandom(itemCount);
		spVisItems = visItems;
		document.write("<div id='spTotalDiv' style='text-align:left;position:relative;height:" + (spBannerWidth+79) + "px;width:120px;overflow:hidden;background-color:#FFFFFF;'><div id='spTitleDiv' style='text-align:center;position:relative;top:0px;left:0px;width:120px;background-color:#000000;color:#ffffff;font-family:arial,helvetica;font-size:14px;'><b>" + title + "</b></div><div style='position:relative;height:" + spBannerWidth + "px;border:1px solid #" + color + ";'><div id='spScroller' style='display:none;text-align:left;position:absolute;top:0px;left:0px;height:" + spBannerWidth + "px;overflow:hidden;width:119px;'>");
		spLoadedImages = spDisplayAds.length>visItems+1?visItems+1:spDisplayAds.length;//spDisplayAds.length;
		for(var a=0;a<spLoadedImages;a++){
			spPrepareItem(spDisplayAds[a], a, affiliate, affid, append, color);
			
		}
		spCurrentItem= spLoadedImages-1;
		document.write("<\/div><\/div><div id='spBottomDiv' style='background-color:#" + color + ";text-align:left;position:relative;left:0px;width:120px;overflow:hidden;'><form method=\"get\" target=\"_blank\" action=\"http://www.speurders.nl/overzicht/\" style=\"margin:0px 0px 0px 0px;\"><input style=\"margin:0px 0px 0px 0px;\" type=\"hidden\" name=\"extern_call\" value=\"true\"><table border=0 cellspacing=0 cellpadding=0 width=119><tr><td bgcolor='#000000' style='height:19px;'><input type='text' style='margin:0px 0px 0px 0px;border:0px;border-left:1px solid #000000;font-family:arial,helvetica;font-size:11px;width:85px;height:19px;padding:0px 0px 0px 0px;background:#FFFFFF;' name='q'></td><td bgcolor='#000000'><input type='image' src='" + spURL + "/zoek.gif' style=\"background-color:#000000;Margin:0px 0px 0px 0px;padding:0px 0px 0px 0px;\" border=0></td></tr><tr><td bgcolor='#" + color +"' valign='bottom' colspan='2' valign='top'><a href='http://www.speurders.nl' target='_blank'><img id='spBannerLogo' src=\"" + url + "bannerlogo.png\" border=0></a></td></tr></table></form></div><div id='spLogoHolder' style='text-align:center;position:absolute;top:0px;left:0px;width:119px;overflow:hidden;height:" + (spBannerWidth+100) + "px;background-color:#" + color + ";'>&nbsp;</div><\/div>");
		
		var s = document.getElementById("spScroller");
		s.onmouseover = function() {spStopTimer()};
		s.onmouseout = function() {spStartTimer()};
		
		if (document.getElementById && document.all){
			var logo = document.getElementById("spBannerLogo");
			var $ALPHA_IMAGE_LOADER = "DXImageTransform.Microsoft.AlphaImageLoader";
			var $FILTER = "progid:" + $ALPHA_IMAGE_LOADER + "(src='%1',sizingMethod='scale')";
			var image = new Image();
			image.onload = function() {
				logo.width = image.width;
				logo.height = image.height;
				image = null;
			}; 
			image.src = logo.src;
			logo.runtimeStyle.filter = $FILTER.replace(/%1/, logo.src);
			logo.src = url + "blank.gif";
		}
		spStartTimer();
	}
}

function spStartTimer(){
	if(spDisplayAds.length!=0)
		spMoveInterval = setInterval(function(){spScrollMove(true);},5000);
}

function spStopTimer(){
	clearInterval(spMoveInterval);
}

function spGetWidth(){
	if(window.innerWidth){
		cWidth = window.innerWidth - 20;
	} else {
		cWidth  = (document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.clientWidth;
	}
	/* firefox hack */
	cWidth = (document.body.scrollWidth < cWidth) ? document.body.scrollWidth : cWidth;
	return cWidth;
}
function spStopScroll(){
	clearInterval(spInterval);
}
function spScrollMove(amount){
	spStopScroll();
	var scrollDiv = document.getElementById("spScroller");
	var scrollAmount = 100;
	//scrollAmount = scrollAmount<60?60:scrollAmount;
	scrollAmount = amount?scrollAmount:-scrollAmount;
	//scrollDiv.scrollLeft +=scrollAmount;
	var sAmount = scrollAmount + scrollDiv.scrollTop;
	if(sAmount<0) sAmount=0;
	
	if(spCurrentItem < spDisplayAds.length-1){
		spCurrentItem++;
		spAddItem(spDisplayAds[spCurrentItem],spCurrentItem, spAffiliate, spAffid, spAppend, spColor);	
	}
	spInterval = setInterval(function() { spDoScroll(scrollDiv,sAmount,amount);},20);
}

function spDoScroll(spScrollDiv,amount,b){
		spScrollDiv.scrollTop += b?10:-10;
		if(!b){
			if(spScrollDiv.scrollTop <= amount){
				spStopScroll();
				spScrollDiv.scrollTop = amount;
			}
		} else {
			if(spScrollDiv.scrollTop >= amount || spScrollDiv.scrollTop == (spScrollDiv.scrollHeight-spBannerWidth)){
				spStopScroll();
				spScrollDiv.scrollTop = amount;
			}
		}
	
}

function spPrepareItem(itemId, itemCount, affiliate, affid, append,color){
    if (append.indexOf ('?')!=0) {
        append='?'+append.substring(1);
    }
	var itemLink = cats[itemId][5] + append;
	var itemTitle = escape(cats[itemId][1]);
	var itemText ="";//(rowCount>1)? cats[itemId][2]:"";
	var itemImage = cats[itemId][3];
	var itemPrice = spMakePrice(cats[itemId][4]);
	spWriteItem(itemTitle, itemText, itemLink, itemCount, itemImage,color, itemPrice);
}

function spAddItem(itemId, itemCount, affiliate, affid, append,color){
    if (append.indexOf ('?')!=0) {
        append='?'+append.substring(1);
    }
    
	var itemLink = cats[itemId][5] + append;
	var itemTitle = escape(cats[itemId][1]);
	var itemText ="";//(rowCount>1)? cats[itemId][2]:"";
	var itemImage = cats[itemId][3];
	var itemPrice = spMakePrice(cats[itemId][4]);
	//spWriteItem(itemTitle, itemText, itemLink, itemCount, itemImage,color, itemPrice);
	//alert(itemCount);
	var result = "<div style='text-align:left;position:absolute;border-bottom:1px solid #" + color +";width:119px;height:99px;overflow:hidden;top:" + (itemCount * 100) +"px;'><div style='position:absolute;top:0px;left:25px;width:70px;height:70px;overflow:hidden;'><a href=\"" + itemLink + "\" target=\"_blank\" onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\"><img style='position:absolute;top:-3px;left:-3px;' src='" + itemImage + "' border=0 width=75 height=75><\/a></div><div onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\" style=\"position:absolute;top:69px;left:2px;overflow:hidden;width:114px;white-space:nowrap;height:12px;text-align:center;\" id=\"itemDiv" + itemId +"\">";
	result +="<a onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\" href=\"" + itemLink + "\" target=\"_blank\" style='text-decoration:none;font-family:arial,helvetica;color:#000000;font-size:11px;'>" + unescape(itemTitle) + "<\/a><\/div><div style=\"position:absolute;top:84px;left:0px;overflow:hidden;width:119px;white-space:nowrap;height:15px;font-size:12px;font-weight:bold;text-align:center;background-color:#" + spCalculateColor(color) + ";\"><a onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\" href=\"" + itemLink + "\" target=\"_blank\" style='text-decoration:none;font-family:arial,helvetica;color:#000000;font-size:11px;'>Prijs: " + spMakePrice(itemPrice) + "</a><\/div><\/div>";
	
	//alert(result);
	var s = document.getElementById("spScroller");
	s.innerHTML = s.innerHTML + result;
	//alert(s.innerHTML);
}

function spMakePrice(priceStr){
	if(!isNaN(priceStr.substring(0,1))){
		return "&euro; " + priceStr;
	}
	return priceStr;
}

function spGetRandom(itemCount){
	var loopCount = spDisplayAds.length - itemCount;
	
	for(b=0;b<loopCount;b++){
		var r = Math.round(Math.random() * (spDisplayAds.length-1));	
		var tempAr = new Array();
		var found = false;
		for(var a=0;a<spDisplayAds.length;a++){
			if(r==a){
				found = true;
			}
			else{
				if(found){
					tempAr[a-1] = spDisplayAds[a];
				}
				else{
					tempAr[a] = spDisplayAds[a];
				}
			}
		}
		spDisplayAds = tempAr;
	}
}

function spWriteItem(itemTitle, itemText, itemLink, itemId, itemImage,color, itemPrice){
	cls = "";
	if(itemId > 0 && itemId % 2 == 1){
		cls=" class='bgColor'";
	} else {
	        cls=" class='white'";
	}
	if(itemText == ""){
		text = itemTitle;
	}
	else{
		text = itemTitle + "<br>" + itemText;
	}
	document.write("<div style='text-align:left;position:absolute;border-bottom:1px solid #" + color +";width:119px;height:99px;overflow:hidden;top:" + (itemId * 100) +"px;'><div style='position:absolute;top:0px;left:25px;width:70px;height:70px;overflow:hidden;'><a href=\"" + itemLink + "\" target=\"_blank\" onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\"><img style='position:absolute;top:-3px;left:-3px;' src='" + itemImage + "' border=0 width=75 height=75 onerror=\"this.src=\'http:\/\/www.speurders.nl\/img\/nophoto75x75.gif\';\" onload='spImageLoaded()'><\/a></div><div onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\" style=\"position:absolute;top:69px;left:2px;overflow:hidden;width:114px;white-space:nowrap;height:12px;text-align:center;\" id=\"itemDiv" + itemId +"\">");
	document.write("<a onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\" href=\"" + itemLink + "\" target=\"_blank\" style='text-decoration:none;font-family:arial,helvetica;color:#000000;font-size:11px;'>" + unescape(text) + "<\/a><\/div><div style=\"position:absolute;top:84px;left:0px;overflow:hidden;width:119px;white-space:nowrap;height:15px;font-size:12px;font-weight:bold;text-align:center;background-color:#" + spCalculateColor(color) + ";\"><a onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\" href=\"" + itemLink + "\" target=\"_blank\" style='text-decoration:none;font-family:arial,helvetica;color:#000000;font-size:11px;'>Prijs: " + spMakePrice(itemPrice) + "</a><\/div><\/div>");
}

function spImageLoaded(){
	spLoadedImages--;
	if(spLoadedImages==0){
		if(spOr==0){
			spInterval2 = setInterval(function() { spZoomLogo();},20);
		} else {
			document.getElementById("spScroller").style.display="inline";
			spInterval2 = setInterval(function() { spFadeOverlay();},20);
			var tHeight = document.getElementById("spTitleDiv").offsetHeight;
			var bHeight = document.getElementById("spBottomDiv").offsetHeight;
			if(tHeight==0) tHeight = 32;
			if(bHeight==0) bHeight = 49;
			document.getElementById("spTotalDiv").style.height = (spBannerWidth + tHeight + bHeight) + "px";
			
		}
	}
}

function spSetFade(op, obj) {
	var obj = obj.style;
	if (navigator.userAgent.indexOf("Firefox") != -1) {
		if (op >= 100) { op = 99.999; }
	}
	
	obj.filter = "alpha(opacity=" + op + ")";
	obj.KhtmlOpacity = (op / 100);
	obj.MozOpacity = (op / 100);
	obj.opacity = (op / 100);
}

function spFadeOverlay() {
	var logoHolder = document.getElementById("spLogoHolder");
	spFade -= 5;
	spSetFade(spFade,logoHolder);
	if(spFade==0){
		clearInterval(spInterval2);	
		logoHolder.style.display = "none";
	}
}

function spMove(divId){
	clearInterval(spIntervalAr[divId]);
	divObj = document.getElementById("itemDiv"+divId);
	if(divObj.scrollWidth - divObj.offsetWidth > 4){
		spIntervalAr[divId] = setInterval(function() { spScroll(divId);},20);
	}
}

function spMoveBack(divId){
	clearInterval(spIntervalAr[divId]);
	spIntervalAr[divId] = setInterval(function() { spScrollBack(divId);},20);
}

function spScrollBack(divId){
divObj = document.getElementById("itemDiv"+divId);
if(divObj.scrollLeft <= 0){
	clearInterval(spIntervalAr[divId]);
	spIntervalAr[divId] = null;
	divObj.scrollLeft = 0;
}
divObj.scrollLeft -=5;
}

function spScroll(divId){
divObj = document.getElementById("itemDiv"+divId);
if(divObj.scrollLeft >= divObj.scrollWidth - divObj.offsetWidth){
	clearInterval(spIntervalAr[divId]);
}
divObj.scrollLeft +=1;
}

function spReset(divId){
spMoveBack(divId);
}

function spCalculateColor(color){
	
	var r = color.substring(0,2);
	var g = color.substring(2,4);
	var b = color.substring(4,6);
	
	r = spHex2dec(r);
	g = spHex2dec(g);
	b = spHex2dec(b);
	
	r +=255;
	g +=255;
	b +=255;
	
	r = Math.round(r/2);
	g = Math.round(g/2);
	b = Math.round(b/2);
	
	r = spDec2hex(r);
	g = spDec2hex(g);
	b = spDec2hex(b);
	
	return '' + r + g + b;
	
}

function spDec2hex(val){
	return val.toString(16);
}

function spHex2dec(hex){
	return parseInt(hex,16);
}
