/**
 nl.startpagina.Adbest js API (c) 2008 by Startpagina BV ( webmaster dot startpagina nl )
 available under a revised BSD-like library
 
 @projectDescription AdBest ad-serving javascript library
 @author Sander Kieft ( sander at startpagina dot nl )
 @version 1.01
 @namespace nl.startpagina
 */

/**
 * @classDescription AdBest
 */
function AdBest(options)
{
	// defaults
	this.page = "";
	this.target = "";
	this.format = "234x60";
	this.zone = null;
	this.bannerDataUrl = "data.xml";
	this.servedCampaignId = "";
	
	if(typeof(options)=='object')
	{
		if (options.page != null) this.page = options.page;
		if (options.target != null)
		{
			if(typeof(options.target)=='string')
			{
				this.target = document.getElementById(options.target);
			}
			else
			{
				// presuming dom object
			}
		}
		if (options.format != null) this.format = options.format;
		if (options.zone != null) this.zone = options.zone;
	}
	
}

AdBest.prototype.handle = function(url)
{
	var url = 'http://manager.arnostat.nl/click.gif'+url;

	if(document.images)
	{
		var lc_img=new Image(1,1);
		lc_img.src=url;
		lc_img.onload=function ()
		{
			return true;
		}
	}
	else
	{
		document.write('<img src="'+url+'" width="1" height="1" alt="">');
	}
}
	

	
AdBest.prototype.visit = function(lc_s, lc_p, lc_pv, lc_c)
{
	var lc_url='?a=v&s='+lc_s+'&p='+lc_p+'&pv='+lc_pv+'&c='+lc_c+'&t='+(new Date()).getTime();

	var ref=document.referrer;
	ref=(ref.lastIndexOf('/')==ref.length-1)?ref.substring(ref.lastIndexOf('/'),0):ref;
	if(ref.length>0) 
		lc_url+='&r='+escape(ref);

	this.handle(lc_url);
}

AdBest.prototype.click = function(lc_s, lc_p, lc_pv, lc_c, lc_l)
{
	var lc_url='?a=c&s='+lc_s+'&p='+lc_p+'&pv='+lc_pv+'&c='+lc_c+'&l='+escape(lc_l)+'&t='+(new Date()).getTime();
	this.handle(lc_url);
}	


AdBest.prototype.inArray = function(a, i)
{
	for(var x=0;x<a.length;x++)	
		if(a[x]==i) return true;
	return false
}

AdBest.callDoOnClick = function(obj)
{
	return function(ev)
	{
		obj.handleClick(ev);
	};
};

AdBest.prototype.handleClick = function(ev)
{
	this.click('startpagina', this.page, this.selectedCampaignId, '', '');
}

AdBest.prototype.updatepage = function (str,xml)
{
	var self = this;
	this.target.innerHTML = "";
	
	//
	// Show pages
	//
	if(xml != null)
	{
		var o = "";
		var campaigns = xml.getElementsByTagName("campaign");
		var totalWeight = 0;
		
		var selectedCampaigns = new Array();
			
		for(x=0;x<campaigns.length;x++)
		{
			var campaign = campaigns[x];
			
			okFormat = false;
			if(campaign.getAttribute("format")==null) okFormat = true;
			if(campaign.getAttribute("format")==this.format) okFormat = true;
			
			okZone = false;
			//alert(campaign.getAttribute("zones"));
			if(this.zone == null) okZone = true;
			if(campaign.getAttribute("zones") == null)
			{
				okZone = true;
			}
			else
			{
				zones = campaign.getAttribute("zones");
				if(zones.replace(/^\s+|\s+$/,'') == "")
				{
					okZone = true;
				}
				else
				{
					zones = zones.split(',');
					if(zones!=null)
						if(this.inArray(zones,this.zone)) okZone = true;
				}
			}
			
			if(okFormat && okZone)
			{
				totalWeight += parseInt(campaign.getAttribute("weight"));
				selectedCampaigns.push(campaign);
			}
		}
		
		var chance_limit = 0;
		var randomly_selected_chance = Math.round(((totalWeight) * Math.random())+0.5);
	
		for(x=0;x<selectedCampaigns.length;x++)
		{
			var campaign = selectedCampaigns[x];
			chance_limit += parseInt(campaign.getAttribute("weight"));
	
			if (randomly_selected_chance <= chance_limit)
			{
				this.selectedCampaignId = campaign.getAttribute("id");
				if((campaign.getAttribute("type")=="intern") || (campaign.getAttribute("type")=="extern"))
				{
					i = document.createElement('img');
					i.src = campaign.getAttribute("bannerurl");
						
					a = document.createElement('a');
					a.target = '_blank';
					a.href = campaign.getAttribute("clickurl");
					a.onclick = AdBest.callDoOnClick(this);
					a.appendChild(i);
					
					this.target.appendChild(a);
				}
				else 
				if(campaign.textContent != undefined)
				{
					this.target.innerHTML  = campaign.textContent;
				}
				else
				{
					if(campaign.childNodes.length>0)
					{
						for(aa=0;aa<campaign.childNodes.length;aa++)
						{
							this.target.innerHTML += campaign.childNodes[aa].data;
						}
					}
				}  
				this.visit('startpagina',this.page,campaign.getAttribute("id"),'');
				break;
			}
		}
	}
}     

AdBest.prototype.display = function()
{
	var xmlHttpReq = false;
	var self = this;

	// Mozilla/Safari
	if (window.XMLHttpRequest)
	{
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject)
	{
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('GET', this.bannerDataUrl, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function()
	{
		if (self.xmlHttpReq.readyState == 4)
		{
			self.updatepage(self.xmlHttpReq.responseText, self.xmlHttpReq.responseXML);
		}
	}
	self.xmlHttpReq.send("");
}


