var phpAds_random;
var cur_ad_obj;
var adcounter = 0;
var showadcounter = 0;
var adverts = new Array();
var is_dom_compatible = (document.addEventListener || document.attachEvent) ? true : false;
var google_ad_client, google_alternate_ad_url, google_ad_width, google_ad_height, google_ad_format, google_color_border, google_color_bg, google_color_linkgoogle_color_url, google_color_text;
var is_complete = new Array();


/**
* Cross-browser helper function to add an event listener
*
* @param	object		The object to which we want to add an event listener
* @param	string		The event we want to listen for (eg. click, load, etc.)
* @param	function	The event handler function
* @param	bool		If true, the event should be announced in capture phase
*
* @return	bool		True if event could be attached, False if not
*/
function add_event(obj, eventType, func, useCaption)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(eventType, func, useCaption);
		return true;
	}
	else if (obj.attachEvent)
	{
		var retVal = obj.attachEvent("on"+eventType, func);
		return retVal;7
	}
	else
	{
		return false;
	}
}

/**
* Helper function to load external javascript
*
* @param	object		The object where teh <script> element should be inserted
* @param	string		The URL of the JavaScript file
* @param	integer		The counter for the advert
*/
function insert_javascript(obj, src, cnt)
{
	var script = document.createElement('script');
	script.setAttribute('src', src);
	script.setAttribute('type', 'text/javascript');
	obj.appendChild(script);


	if (cnt != -1)
	{
		if ((/msie/i).test(navigator.userAgent) && !(/AppleWebKit\/([^ ]*)/).test(navigator.userAgent) && !(/opera/i).test(navigator.userAgent))
		{
			// If this is IE, watch the last script's ready state.
			script.onreadystatechange = function ()
			{
				if (this.readyState === 'loaded')
				{
					init_adverts(showadcounter + 1);
				}
			};
		}
		else
		{
			script = document.createElement('script');
			script.type = 'text/javascript';
			scriptcode = document.createTextNode('init_adverts(' + (cnt + 1) + ');');
			script.appendChild(scriptcode);
			document.body.appendChild(script);
		}
	
	
	}

	return false;
}

/**
* Build advert code
*
* @param	integer		The OpenAds ZoneID for the advert
*/
function construct_adcode(zoneid)
{
	if (!is_dom_compatible)
	{
		document.write('<script type="text/javascript" src="http://info.adalizer.com/adx.js"></script>');
		if (!document.phpAds_used)
		{
			document.phpAds_used = ',';
		}
		phpAds_random = new String (Math.random()); phpAds_random = phpAds_random.substring(2, 11);
   
		document.write ('<' + 'script type="text/javascript" src="');
		document.write ("http://info.adalizer.com/adjs.php?n=" + phpAds_random);
		document.write ('&amp;what=zone:' + zoneid + '&amp;target=_blank');
		document.write ('&amp;exclude=' + document.phpAds_used);
		if (document.referrer)
		{
			document.write ('&amp;referer=' + escape(document.referrer));
			document.write ('"><' + '/script>');
		}
	}
	else
	{
		adcounter++;
		document.write('<div id="advert' + adcounter + '"></div>');
		adverts.push(zoneid);
	}
}

/**
* Initialize adverts
*
*/
function init_adverts()
{
	if (adverts.length == 0)
	{
		return;
	}
	
	document.write = function(code)
	{
		cur_ad_obj.innerHTML += code;
		
		var scriptregex = /<script\s*type="text\/javascript"\s*src="(.+)">/gi;
		
		while (match = scriptregex.exec(code))
		{
			if (match[1] == 'http://pagead2.googlesyndication.com/pagead/show_ads.js')
			{
				google_ad_client = code.match(/google_ad_client = "(.*)";/);
				google_ad_client = google_ad_client[1];
				
				google_alternate_ad_url = code.match(/google_alternate_ad_url = "(.*)";/);
				google_alternate_ad_url = google_alternate_ad_url[1];
				
				google_ad_width = code.match(/google_ad_width\s*=\s*(.*);/);
				google_ad_width = google_ad_width[1];

				google_ad_height = code.match(/google_ad_height\s*=\s*(.*);/);
				google_ad_height = google_ad_height[1];

				google_ad_format = code.match(/google_ad_format\s*=\s*"(.*)";/);
				google_ad_format = google_ad_format[1];

				google_ad_channel = code.match(/google_ad_channel\s*=\s*"(.*)";/);
				google_ad_channel = google_ad_channel[1];

				google_color_border = code.match(/google_color_border\s*=\s*"(.*)";/);
				google_color_border = google_color_border[1];

				google_color_bg = code.match(/google_color_bg\s*=\s*"(.*)";/);
				google_color_bg = google_color_bg[1];

				google_color_link = code.match(/google_color_link\s*=\s*"(.*)";/);
				google_color_link = google_ad_format[1];

				google_color_url = code.match(/google_color_url\s*=\s*"(.*)";/);
				google_color_url = google_color_url[1];

				google_color_text = code.match(/google_color_text\s*=\s*"(.*)";/);
				google_color_text = google_color_text[1];
			}
			insert_javascript(cur_ad_obj, match[1], -1);
		}
	}

	cur_ad_obj = document.getElementById('advert' + (showadcounter + 1));
	
	insert_javascript(cur_ad_obj, 'http://info.adalizer.com/adx.js');
	if (!document.phpAds_used)
	{
		document.phpAds_used = ',';
	}
		
	var zoneid = adverts[showadcounter];
	phpAds_random = new String (Math.random());
	phpAds_random = phpAds_random.substring(2, 11);
	adverts.splice(0, 1);

	insert_javascript(cur_ad_obj, 'http://info.adalizer.com/adjs.php?n=' + phpAds_random + '&what=zone:' + zoneid + '&target=_blank&exclude=' + document.phpAds_used + ((document.referrer) ? '&referer=' + escape(document.referrer) : ''), showadcounter);
}

if (is_dom_compatible)
{
	add_event(window, 'load', init_adverts, false);
}