/**
 * Class for asynchronous loading of ads using xml.
 *
 * @author	Alex Buchgeher <alex.buchgeher(at)gmail.com>
 * @version 0.2.1
 */
var Ads = Class.create();
Ads.prototype = {
	initialize: function(id, pid, imgW, imgH, secret, interval) {
		this.elemNode = $(id);
		this.pid = pid;
		this.imgW = imgW;
		this.imgH = imgH;
		this.secret = secret;
		this.interval = interval;
		this.ads = new Array();
		this.id = Math.random() * 9999;
		this.curAd = null;
		this.intervalId = null;
		this.render();
		Event.observe(window, "unload", this.unload.bind(this));
	},
	render: function() {
		var url = "index.php?eID=tx_ads_xml";
		var parameters = "pid=" +this.pid + "&imgW=" + this.imgW + "&imgH=" + this.imgH + "&secret="+this.secret + "&cHash=" + Math.random() * 9999;;
		var options = {
			asynchronous: true,
			method: "get",
			requestHeaders: ['Pragma', 'no-cache', 'Cache-Control', 'must-revalidate'],
			parameters: parameters,
			onComplete: this.processXML.bind(this)
		};
		new Ajax.Request(url, options);
	},
	processXML: function(xhr) {
		var xml = xhr.responseXML;
		var elems = xml.getElementsByTagName("image");
		// var wrapper = document.createElement("div");
		var maxHeight = 0;
		for(var i=0; i<elems.length; i++) {
			var div = document.createElement('div');
			var img = document.createElement('img');
			var a = document.createElement('a');
			var link = elems[i].getAttribute("link");
			var imgHeight = elems[i].getAttribute("imgHeight");
			if(isNaN(link)) {
				a.target = "_blank";
			}
			a.href = link;
			img.title = elems[i].getAttribute("company");
			img.src = elems[i].getAttribute("path");
			this.preloadImage(img.src);
			if(maxHeight < imgHeight) {
				maxHeight = imgHeight;
			}
			a.appendChild(img);
			div.appendChild(a);
			this.ads.push(div);
			// wrapper.appendChild(div);
		}
		if(maxHeight != 0) {
			this.elemNode.style.height = maxHeight + "px";
		}

		var noOfAds = this.ads.length;
		//this.idx = Math.floor(Math.random() * noOfAds);
		this.idx = 0;
		// wrapper.style.display = "none";
		// this.elemNode.appendChild(wrapper);
		// Effect.Appear(wrapper, {duration: 0.8});
		this.rotate();
	},
	rotate: function() {
		if(this.curAd != null) {
			try {
				Element.remove(this.curAd);
			} catch(e) { }
		}
		this.appear();
	},
	appear: function() {
		this.curAd = this.ads[this.idx];
		this.idx++;
		if(this.idx == this.ads.length) {
			this.idx = 0;
		}
		this.curAd.style.display = "block";
		this.elemNode.appendChild(this.curAd);
		if(this.intervalId == null) {
			this.intervalId = window.setInterval(this.remove.bind(this), this.interval);
		}		
	},
	remove: function() {
		try {
			Element.remove(this.curAd);
		} catch(e) {}
		this.appear();
	},
/*	
	rotate: function() {
		if(this.curAd != null) {
			try {
				Element.remove(this.curAd);
			} catch(e) { }
		}
		this.appear();
		if(this.intervalId == null) {
			this.intervalId = window.setInterval(this.remove.bind(this), this.interval);
		}
	},
	appear: function() {
		this.curAd = this.ads[this.idx];
		this.curAd.style.display = "none";
		this.elemNode.appendChild(this.curAd);
		Effect.Appear(this.curAd, {duration: 0.8});
		this.idx++;
		if(this.idx == this.ads.length) {
			this.idx = 0;
		}
	},
	remove: function() {
		Effect.Fade(this.curAd, {duration: 0.8, afterFinish: this.rotate.bind(this)});
	},
*/

	/**
	 * Preloads the given image.
	 * @param string	The filepath
	 * @return void
	 */
	preloadImage: function(a) {
		if(document.images) {
			var dummy = new Image();
			dummy.src = a;
		}
	},
	
	/**
	 * Memory Cleanup for IE
	 * Call this onbeforeunload-event
	 * @return void
	 */
	unload: function() {
		for(var i=0; i<this.ads.length; i++) {
			this.ads[i] = null;
		}
		window.clearInterval(this.intervalId);
		this.curAd = null;
	}
};