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 searchForm(affiliate,affid,append){
	start = 'http://www.speurders.nl/locator/locator.php?action=entrance';
	end = '&ret=/search.php?title_descr=' + document.speurders.title_descr.value;
	ret = start + '&affiliate=' + affiliate;
	if ( affid !=null){
		
		ret += '&af_id=' + affid;
	}
	ret +=  append + end;
	window.open(ret,'_NEW'); 
	return false;
}

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 spGetLink(id,affiliate,aff_id, append){
	var ret ='http://www.speurders.nl/locator/locator.php?action=entrance&speurders_id=';
	ret += id +'&affiliate=' + affiliate;
	if ( aff_id !=null){
		ret += '&af_id=' + aff_id;
	}
	ret +=  append + '"';
	return ret;
}


function spPrepareItem(itemId, itemCount, rowCount, affiliate, affid, append){
	var itemLink = spGetLink(cats[itemId][0],affiliate, affid, append);
	var itemTitle = unescape(escape(cats[itemId][1]));
	var itemText =(rowCount>1)? cats[itemId][2]:"";
	spWriteItem(itemTitle, itemText, itemLink, itemCount,affid);
}

function makeClick(aff_id)
{
	document.cT.src = "http://affiliates.kliks.nl/affsol/click.php?speurders&&"+aff_id+"&&";
}

function spWriteItem(itemTitle, itemText, itemLink, itemId,affid){
	cls = "";
	if(itemId > 0 && itemId % 2 == 1){
		cls=" class='bgcolor'";
	}
	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\" onClick=\"makeClick('"+affid+"')\">" + 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 spWriteBanner(width, itemCount, rowCount, title, color, url, affiliate, affid, append, altLogo, placeAd){
	document.write("<style type=\"text/css\">");
	document.write("#speurders a {text-decoration:none;color:#000000;display: block;}");
	document.write("#speurders a:hover {text-decoration:underline;color:#000000;}");
	document.write("#speurders a:visited{color:#000000;}");
	document.write("#speurders a:active{color:#000000;}");
	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.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>");
	document.write("<img name=\"cT\" src=\"about:blank\" style=\"display: none\">");
	document.write("<table id=\"speurders\" width=\"" + width + "\" class=\"table\" cellspacing=\"0\" cellpadding=\"0\">");
	document.write("<form name=\"speurders\" method=\"get\" target=\"_blank\" onSubmit=\"return searchForm('"+affiliate+"','"+affid+"','"+append+"');\">");
	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){
			addlink = 'http://www.speurders.nl/locator/locator.php?action=entrance';
			addlink+= '&affiliate=' + affiliate;
			if ( affid !=null){
				addlink += '&af_id=' + affid;
			}
			addlink +=  append + '&ret=/new_ad_form.php?';
			spWriteAdItem("Plaats advertentie", addlink, itemCount-1)
		}
	}
	//---- end writing items ---
	var ret ='http://www.speurders.nl/locator/locator.php?action=entrance';
	ret += '&affiliate=' + affiliate;
	if ( affid !=null){
		ret += '&af_id=' + affid;
	}
	ret +=  append;
	
	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('"+ret+"');\" 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('"+ret+"');\" align='center' valign='middle'><img src=\"" + url + "images/bannerlogo_" + color + ".gif\"></td></tr></form></table>");
	}
}