/**
 * Wp-Adserve dynamic update script, 0.4.
 * Copyright 2008, Ashley Kyd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
*/ 

// The ID prefix used by all of our ads.
adPrefix = 'iriAd';

// The frequency of the rotations (seconds)
adRotationFrequency = 20;

// The maximum number of rotations (the more impressions, the less likely anyone
// is paying attention.
adRotationLimit = 100;

adRotationFrequency = adRotationFrequency*1000;
// The banner class.
function banner(){
	this._bannum = '';
	this._numrows = '';
	this._number = '';
	this._href = '';
	this._src = '';
	this._alt = '';
}
banner.prototype._bannum;
banner.prototype._numrows;
banner.prototype._number;
banner.prototype._href;
banner.prototype._src;
banner.prototype._alt;

var i = 1;
var element;
var xmlDoc;


function switchAd(){
	// Create a new banner class and populate it with values from the XML.
	ad = new banner();
	try{
		ad.bannum = xmlDoc.getElementsByTagName("bannum")[0].childNodes[0].nodeValue;
		ad.numrows  = xmlDoc.getElementsByTagName("numrows")[0].childNodes[0].nodeValue;
		ad.number = xmlDoc.getElementsByTagName("number")[0].childNodes[0].nodeValue;
		ad.href = xmlDoc.getElementsByTagName("href")[0].childNodes[0].nodeValue;
		ad.src = xmlDoc.getElementsByTagName("src")[0].childNodes[0].nodeValue;
		ad.alt = xmlDoc.getElementsByTagName("alt")[0].childNodes[0].nodeValue;
		
		// If this is a valid ad, let's go.
		if(ad.number){
			element.href = ad.href;
			image = element.childNodes[0];
			image.src = ad.src;
			image.alt = ad.src;
		}
	} catch(e){
		//invalid xml.
		adRotationLimit++;
	}
	
	adRotationLimit--;
	
	// We want to repeat ad infinitum, or until adRotationLimit.
	// Set to a negative value and this will never be triggered.
	if(adRotationLimit!=0){
		newTimer = setTimeout("rotateAds()", adRotationFrequency);
	}
}

// Function to find and replace the existing ads.
function rotateAds(){

	elementName = adPrefix+i;
	element = document.getElementById(elementName);
	if(element){

		// Work out the zone this ad belongs to (class names are usually
		// "advert-zone_name", so we can chop off the first 7 characters.
		zone = element.className.substring(7);
	
		// Create an empty XML object
		try { // The Internet Explorer way
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.onreadystatechange = function () {
				if (xmlDoc[index].readyState == 4) switchAd;
			};

		} catch(e) {
			try { // The more standard way.
				xmlDoc = document.implementation.createDocument("","",null);
				xmlDoc.onload = switchAd;
			} catch(e) {
				//Fail silently.
			}
		}
		
		// We've found an ad element to update. Pull a new ad from the server.
		xmlDoc.load('?zone='+zone);
	}else{
		newTimer = setTimeout("rotateAds()", adRotationFrequency);
		i=0;
	}
	i++;
}

// This will set the ball rolling.
rotateAds();
