// -------------------------------------------------------------------
// Advanced RSS Ticker (Ajax invocation) core file
// Author: Dynamic Drive (http://www.dynamicdrive.com)
// -------------------------------------------------------------------

var rss_page="/rss-actualites"

function createAjaxObj(){
	var h=false
	if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		h=new XMLHttpRequest()
		if (h.overrideMimeType)
			h.overrideMimeType('text/xml')
	}
	else if (window.ActiveXObject){ // if IE
		try {
			h=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
				h=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	return h
}

function rssticker_ajax(divId, divClass, delay, logicswitch){
	this.tickerid=divId
	this.delay=delay
	this.logicswitch=(typeof logicswitch!="undefined")? logicswitch : ""
	this.mouseoverBol=0 
	this.pointer=0
	this.opacitysetting=0.2
	this.title=[], this.link=[], this.description=[], this.pubdate=[]
	this.ajaxobj=createAjaxObj()
	document.write('<div id="'+divId+'" class="'+divClass+'" >...</div>')
	if (window.getComputedStyle)
		this.mozopacityisdefined=(window.getComputedStyle(document.getElementById(this.tickerid), "").getPropertyValue("-moz-opacity")==1)? 0 : 1
	
	if (this.ajaxobj){
		var i=this
		this.ajaxobj.onreadystatechange=function(){i.initialize()}
		this.ajaxobj.open('GET', rss_page, true)
		this.ajaxobj.send(null)
	}
}

rssticker_ajax.prototype.initialize=function()
{ 
	if (this.ajaxobj.readyState == 4){ //request completed
		if (this.ajaxobj.status==200){ //successful
			var xmldata
			if (window.XMLHttpRequest) {
				xmldata = this.ajaxobj.responseXML;
			} else if (window.ActiveXObject) {
				xmldata = new ActiveXObject("Microsoft.XMLDOM");
				xmldata.loadXML(this.ajaxobj.responseText);
			}
			if(xmldata.getElementsByTagName("item").length==0){
				document.getElementById(this.tickerid).innerHTML="<b>...</b>"
				return
			}
			var i=this
			this.feeditems=xmldata.getElementsByTagName("item")
			for (var i=0; i<this.feeditems.length; i++){
				this.title[i]=this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue
				this.link[i]=this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue
				this.description[i]=this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue
				this.pubdate[i]=this.feeditems[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue
			}
			document.getElementById(this.tickerid).onmouseover=function(){i.mouseoverBol=1}
			document.getElementById(this.tickerid).onmouseout=function(){i.mouseoverBol=0}
			this.rotatemsg()
		}
	}
}

rssticker_ajax.prototype.rotatemsg=function(){
	var i=this
	if (this.mouseoverBol==1) //if mouse is currently over ticker, do nothing (pause it)
		setTimeout(function(){i.rotatemsg()}, 100)
		else{ //else, construct item, show and rotate it!
		var tickerDiv=document.getElementById(this.tickerid)
		var linktitle='<div class="rsstitle"><a href="'+this.link[this.pointer]+'">'+this.title[this.pointer]+'</a></div>'
		var description='<div class="rssdescription">'+this.description[this.pointer]+'</div>'
		var feeddate='<div class="rssdate">'+this.pubdate[this.pointer]+'</div>'
		if (this.logicswitch.indexOf("description")==-1) description=""
		if (this.logicswitch.indexOf("date")==-1) feeddate=""
		var tickercontent=linktitle+feeddate+description //STRING FOR FEED CONTENTS 
		this.fadetransition("reset") //FADE EFFECT- RESET OPACITY
		tickerDiv.innerHTML=tickercontent
		this.fadetimer1=setInterval(function(){i.fadetransition('up', 'fadetimer1')}, 100) //FADE EFFECT- PLAY IT
		this.pointer=(this.pointer<this.feeditems.length-1)? this.pointer+1 : 0
		setTimeout(function(){i.rotatemsg()}, this.delay) //update container every second
	}
}

rssticker_ajax.prototype.fadetransition=function(fadetype, timerid){
	var tickerDiv=document.getElementById(this.tickerid)
	if (fadetype=="reset")
	this.opacitysetting=0.2
	if (tickerDiv.filters && tickerDiv.filters[0]){
	if (typeof tickerDiv.filters[0].opacity=="number") //IE6+
	tickerDiv.filters[0].opacity=this.opacitysetting*100
	else //IE 5.5
	tickerDiv.style.filter="alpha(opacity="+this.opacitysetting*100+")"
	}
	else if (typeof tickerDiv.style.MozOpacity!="undefined" && this.mozopacityisdefined){
	tickerDiv.style.MozOpacity=this.opacitysetting
	}
	if (fadetype=="up")
	this.opacitysetting+=0.2
	if (fadetype=="up" && this.opacitysetting>=1)
	clearInterval(this[timerid])
}
