/*
 ======================================================================
 ======================================================================
 Advanced Ajax RSS Displayer
 Author: Wiliam Cross for SEO-Shop (http://www.seo-shop.com)
 Created: February 11, 2010
 Credits: George at JavaScriptKit.com for base objects
 ======================================================================
 ======================================================================
*/

function createAjaxObj(){
		var httprequest=false
		if (window.XMLHttpRequest){
				httprequest=new XMLHttpRequest()
				if (httprequest.overrideMimeType)
						httprequest.overrideMimeType('text/xml')
		}
		else if (window.ActiveXObject){ 
				try {
						httprequest=new ActiveXObject("Msxml2.XMLHTTP");
				} 
				catch (e){
					try{
							httprequest=new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e){}
				}
		}
		return httprequest
}

function feedticker(feedurl, fetch, delay, optionalswitch, lendesc){
		this.feedurl=feedurl
		this.tickerid='feedticker'
		this.delay=delay
		this.logicswitch=(typeof optionalswitch!="undefined")? optionalswitch : -1
		this.lendesc=lendesc
		this.fetch=fetch
		this.mouseoverBol=0
		this.pointer=0
		this.ajaxobj=createAjaxObj()
		document.write('<div id="feedticker" class="feedticker">...</div>')
		this.getAjaxcontent()
}

feedticker.prototype.getAjaxcontent=function(){
		if (this.ajaxobj){
			var instanceOfTicker=this
			var myurl=this.feedurl
			this.ajaxobj.onreadystatechange=function(){instanceOfTicker.initialize()}
			if(this.fetch == 'remote'){
			  this.ajaxobj.open('GET', myurl+"feed.php", true)
			}
			else {
			  this.ajaxobj.open('GET', this.feedurl, true)
			}
			this.ajaxobj.send(null)
		}
}

feedticker.prototype.initialize=function(){ 
		if (this.ajaxobj.readyState == 4){
					if (this.ajaxobj.status==200){
							var xmldata=this.ajaxobj.responseXML
							if (xmldata.getElementsByTagName("item").length==0){ 
									document.getElementById(this.tickerid).innerHTML="<br />"
									return
							}
							var instanceOfTicker=this
							this.feeditems=xmldata.getElementsByTagName("item")
							for (var i=0; i<this.feeditems.length; i++){
									this.feeditems[i].setAttribute("ctitle", this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue)
									this.feeditems[i].setAttribute("clink", this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue)
									this.feeditems[i].setAttribute("cdescription", this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue)
							}
							document.getElementById(this.tickerid).onmouseover=function(){instanceOfTicker.mouseoverBol=1}
							document.getElementById(this.tickerid).onmouseout=function(){instanceOfTicker.mouseoverBol=0}
							this.rotatemsg()
					}
		}
}

feedticker.prototype.rotatemsg=function(){
		var instanceOfTicker=this;
		if (this.mouseoverBol==1) {
			setTimeout(function(){instanceOfTicker.rotatemsg()}, 100);
		} 
		else{
			var tickerDiv=document.getElementById(this.tickerid);
			var tickercontent='<a href="'+this.feeditems[this.pointer].getAttribute("clink")+'" target="_blank">'+this.feeditems[this.pointer].getAttribute("ctitle")+'</a>';
			if (this.logicswitch=="showdescription"){
        if(this.lendesc != ''){
					var mydesc = this.feeditems[this.pointer].getAttribute("cdescription");
				  if(mydesc.length > this.lendesc){  
    					var s = mydesc.substr(0, this.lendesc);
    					var words = s.split(' '); 
    					words[words.length-1] = '';
    					mydesc = words.join(' ') + '&hellip;'
  				}
  			}
  			else {
					var mydesc=this.feeditems[this.pointer].getAttribute("cdescription");
				}
				tickercontent+="<br />"+mydesc;
				tickerDiv.innerHTML=tickercontent;
				this.pointer=(this.pointer<this.feeditems.length-1)? this.pointer+1 : 0;
				setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay) ;
			}
		}
}
