
Effect.DefaultOptions.duration = 0.3;
NewsTicker = Class.create();
//alert("0");
Object.extend(NewsTicker.prototype, {
	
	tickerDiv: "tickNoticias", //"ticker", 
	tickerLocation: "billboard", 
	tickerTitle: "news-link",
	tickerLink: "/portal/templates/template0005/eVcceeXSWbYffcfbVUaadZTXbQVRQRca/imageneshomeTxtNoticias_es_ES.gif",
	feedURL: "../../../../portal/rss/noticias2/noticiasRSS.jsp?site=Termaria&filtro=noticia&vgnextchannel=d26acf1afccb8110VgnVCM1000000b1510acRCRD&base=blank&lang="+lang,//base=http://www.termaria.es si es para rss real
	pauseLength: 3500,
	timer: 0,
	currentTitle: 0,
	items: null,
	initialize: function() {
		//alert("1");
		this.items = [];
		
		new Ajax.Request(
			this.feedURL, {
				method: "get",
				onSuccess: function(response) {
					
					
					this.parseXML(response.responseXML);
					//alert("2b");
					this.buildTicker();
				}.bind(this),
				onFailure: function() {
					//alert("excep");
					console.log("Noticias");
					
				},
				onException: function(req, err) {
					//alert(""+err);
					//throw(err);
				}
			}
		);
	},
	
	buildTicker: function() {
		// replace the placeholder content with the first news title
		if (this.items[this.currentTitle]) {
			//alert('uno:'+$((this.tickerTitle).innerHTML));
			//alert('dos:'+this.items[this.currentTitle]['title']);
			$(this.tickerTitle).innerHTML = this.items[this.currentTitle]['title'];
				$(this.tickerTitle).href=		this.items[this.currentTitle]['link'];
			this.start();// start the timer if we have valid headlines
		}
		
	},
	
	parseXML: function(xml) {
		try{
		// build the array of news titles
		//alert("pa1"+xml.getElementsByTagName("item").iner);
		title2 = 'uno';
			//alert("pa4"+title);
			var link2 = 'dos';
			this.items.push({title: title2, link: link2});
		$A(xml.getElementsByTagName("item")).each(function(item) {
			//alert("pa3"+item.getElementsByTagName("title")[0]);
			title = item.getElementsByTagName("title")[0].childNodes[0].nodeValue;
			//alert("pa4"+title);
			var link = item.getElementsByTagName("link")[0].childNodes[0].nodeValue; //NewsTicker.tickerLink;
			this.items.push({title: title, link: link});
		}.bind(this));
		}catch(err){
			//alert(err);
			alert('error en parseXML:'+err);
		}
	},
	
	start: function() {
		//alert("4");
		this.interval = setInterval(this.showNext.bind(this), this.pauseLength);
	},
	
	stop: function() {
		clearInterval(this.interval);
	},
	
	showNext: function() {
		//alert("6");
		//determine next headline
		if ( this.currentTitle < this.items.length-1 ) {
			this.currentTitle = this.currentTitle+1;
		} else {
			this.currentTitle = 0;
		}
		
		new Effect.Fade('news-link', {
			afterFinish: function() {
				this.switchData();
				new Effect.Appear('news-link'); }.bind(this)});

	},
	
    switchData: function() {
		//alert("7");
		$(this.tickerTitle).setAttribute("href", this.tickerLink);
		if (this.items[this.currentTitle]) {
			//alert('uno:'+$(this.tickerTitle).innerHTML);
			//alert('dos:'+this.items[this.currentTitle]['title']);
			$(this.tickerTitle).innerHTML = this.items[this.currentTitle]['title'];
			$(this.tickerTitle).href=		this.items[this.currentTitle]['link'];
		}
	}
});

Event.observe(window, 'load', function() {
	var ticker = new NewsTicker();
});

