var spIntervalAr = new Array();
var cats = new Array();
var spDisplayAds = new Array();

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 spPrepareItem(itemId, itemCount, rowCount, affiliate, affid, append){
    if (append.indexOf ('?')!=0) {
        append='?'+append.substring(1);
    }

	var itemLink = cats[itemId][5] + append;
	var itemTitle = unescape(escape(cats[itemId][1]));
	var itemText =(rowCount>1)? cats[itemId][2]:"";
	spWriteItem(itemTitle, itemText, itemLink, itemCount);
}

function spWriteItem(itemTitle, itemText, itemLink, itemId){
	cls = "";
	if(itemId > 0 && itemId % 2 == 1){
		cls=" class='bgColor'";
	} else {
        cls=" class='white'";
    }
	if(itemText == ""){
		text = "<b>" + itemTitle + "</b>"
	}
	else{
		text = "<b>" + itemTitle + "</b><br>" + itemText;
	}
	document.write("<tr><td" + cls + " onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\"><div onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\" class=\"itemDiv\" id=\"itemDiv" + itemId +"\">");
	document.write("<a onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\" href=\"" + itemLink + "\" target=\"_blank\">" + text + "<\/a><\/div><\/td><\/tr>");
}

function spWriteAdItem(itemTitle, itemLink, itemId){
	cls=" class='bgFullcolor'";
	text = "<b>" + itemTitle + "</b>"
	document.write("<tr><td" + cls + " onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\"><div onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\" class=\"itemDiv\" id=\"itemDiv" + itemId +"\">");
	document.write("<a onmouseout=\"spReset(" + itemId + ");\" onmouseover=\"spMove(" + itemId + ");\" href=\"" + itemLink + "\" target=\"_blank\">" + text + "<\/a><\/div><\/td><\/tr>");
}

function spDec2hex(val){
	return val.toString(16);
}

function spHex2dec(hex){
	return parseInt(hex,16);
}
		
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 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 spWriteBannerTxtColor(width, itemCount, rowCount, title, textcolor,color, url, affiliate, affid, append, altLogo, placeAd){
    oldBanner=0;
    if (altLogo=='new') {
        oldBanner=1;
    }

	document.write("<style type=\"text/css\">");
	document.write("#speurders a {text-decoration:none;color:#"+textcolor+";display: block;}");
	document.write("#speurders a:hover {text-decoration:underline;color:#"+textcolor+";}");
	document.write("#speurders a:visited{color:#"+textcolor+";}");
	document.write("#speurders a:active{color:#"+textcolor+";}");
	document.write("#speurders td{border-left:1px solid;border-right: 1px solid;border-color:#" + color + ";padding: 2px 2px 2px 2px;font-family:Arial, Helvetica, sans-serif;font-size:11px;}");
	document.write("#speurders td.bgColor{background-color:#" + spCalculateColor(color) + ";}");
	document.write("#speurders td.white{background-color:#FFFFFF;}");
	document.write("#speurders td.bgFullColor{background-color:#" + color + ";}");
	document.write("#speurders td.kop{background-color:#000000;font-family:Arial, Helvetica, sans-serif;color:#FFFFFF;font-size:13px;text-align:center;border:0px;font-weight:bold;}");
	document.write("#speurders a.kop{background-color:#000000;font-family:Arial, Helvetica, sans-serif;color:#FFFFFF;font-size:13px;text-align:center;border:0px;}");
	document.write("#speurders a.kop:hover{color:#FFFFFF;}");
	document.write("#speurders a.kop:visited{color:#FFFFFF;}");
	document.write("#speurders a.kop:active{color:#FFFFFF;}");
	document.write("#speurders .itemDiv{position:relative;overflow:hidden;width:" + (width-6) + "px;white-space:nowrap;height:13px;}");
	document.write("#speurders td.search{border:0px;padding:0px 0px 0px 0px;height:19px;background-color:#000000;}");
	document.write("#speurders td.logo{border:0px;height:35px;padding:0px 0px 0px 0px;cursor:hand;background-color:#" + color + ";}");
	document.write("#speurders .searchField{width:" + (width-33) + "px;height:17px;border:0px solid;border-left:1px solid;}");
	document.write("</style>");
    if (oldBanner==0) {
        document.write("<table id=\"speurders\" width=\"" + width + "\" class=\"table\" cellspacing=\"0\" cellpadding=\"4\">");
    }
	document.write("<form method=\"get\" target=\"_blank\" action=\"http://www.speurders.nl/overzicht/\">");
	document.write("<input type=\"hidden\" name=\"extern_call\" value=\"true\">");
	document.write("<tr><td class=\"kop\">" + title + "</td></tr>");	
	
	if(cats.length>0){
	//generate array for randomfunction
		for(var a=0;a<cats.length;a++){
			spDisplayAds[a] = a;
		}
		spGetRandom(itemCount-placeAd);
	//---- write Items ---
		
		for(var a=0;a<spDisplayAds.length;a++){
			spPrepareItem(spDisplayAds[a], a, rowCount, affiliate, affid, append);
		}
		if(placeAd==1){
			spWriteAdItem("Plaats advertentie", "http://www.speurders.nl/new_ad_form.php", itemCount-1)
		}
	}
	//---- end writing items ---
    if (oldBanner!=0) {
	    document.write("<tr><td class=\"search\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td class=\"search\"><input type=\"text\" name=\"q\" class=\"searchField\"></td><td class=\"search\"><input type=\"image\" src=\"" + url + "images/zoek.gif\"></td></tr></table></td></tr></form>");
    } else {
        document.write("<tr><td class=\"search\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td class=\"search\"><input type=\"text\" name=\"title_descr\" class=\"searchField\"></td><td class=\"search\"><input type=\"image\" src=\"" + url + "images/zoek.gif\"></td></tr></table></td>");
        if(altLogo.length>0) {
            document.write("</tr><tr><td class=\"logo\" onclick=\"javascript:window.open('http://www.speurders.nl');\" align='center' valign='middle'><img src=\"" + url + "images/" + altLogo + "\"></td></tr></form></table>");
        } else {
            document.write("</tr><tr><td class=\"logo\" onclick=\"javascript:window.open('http://www.speurders.nl');\" align='center' valign='middle'><img src=\"" + url + "images/bannerlogo_" + color + ".gif\"></td></tr></form></table>");
        }
    }
}

function spWriteBanner(width, itemCount, rowCount, title, color, url, affiliate, affid, append, altLogo, placeAd){
	spWriteBannerTxtColor(width, itemCount, rowCount, title, "000000",color, url, affiliate, affid, append, altLogo, placeAd);
}
