Effect.DefaultOptions.duration = 0.3;
NewsPromo = Class.create();
Object.extend(NewsPromo.prototype, {

	tickerTitle: "newPromo",
	feedURL: "newpromoxml.aspx",
	pauseLength: 5000,
	timer: 0,
	currentTitle: 0,
	items: [],
	initialize: function() {
	    
		new Ajax.Request(
			this.feedURL, {
				method: "get",
				onSuccess: function(response) {
					this.parseXML(response.responseXML);
					this.buildTicker();
				}.bind(this),
				onFailure: function() {
				},
				onException: function(req, err) {
				}
			}
		);
	},
	
	parseXML: function(xml) {	
		$A(xml.getElementsByTagName("promo")).each(function(item) {
			title = item.getAttribute("title");
			img = item.getAttribute("imgsrc");
			lien = item.getAttribute("lien");
			this.items.push({title: title, lien: lien, img: img});
		}.bind(this));
	},
	
	buildTicker: function() {
		if (this.items[this.currentTitle]) {
			$(this.tickerTitle).getElementsByTagName("a")[0].setAttribute("href",this.items[this.currentTitle]['lien']);
			$(this.tickerTitle).getElementsByTagName("a")[0].getElementsByTagName("img")[0].setAttribute("src",this.items[this.currentTitle]['img']);
			$(this.tickerTitle).getElementsByTagName("a")[0].getElementsByTagName("img")[0].setAttribute("alt",this.items[this.currentTitle]['title']);
			this.start();
		}
	},
	
	start: function() {
		this.interval = setInterval(this.showNext.bind(this), this.pauseLength);
	},
	
	stop: function() {
		clearInterval(this.interval)
	},
	
	showNext: function() {
	    this.currentTitle = Math.floor(Math.random()* this.items.length);
		this.switchData();
        
//		new Effect.Fade('newPromo' ,{
//		    duration:0.5,
//			afterFinish: function() {
//				this.switchData();
//				new Effect.BlindDown('newPromo', {duration:0.5}); }.bind(this)});

		
	},
	
    switchData: function() {
		if (this.items[this.currentTitle]) {
			$(this.tickerTitle).getElementsByTagName("a")[0].setAttribute("href",this.items[this.currentTitle]['lien']);
			$(this.tickerTitle).getElementsByTagName("img")[0].setAttribute("src",this.items[this.currentTitle]['img']);
			$(this.tickerTitle).getElementsByTagName("img")[0].setAttribute("alt",this.items[this.currentTitle]['img']);
		}
	}
});

Event.observe(window, 'load', function() {
	var promo = new NewsPromo();
});
