// -------------------------------------------------------------------
// Sample message script to pickup sample flashs in background
// -------------------------------------------------------------------

//Relative URL syntax:
//var flashtxturl="http://localhost:8080/flsample.jsp?formcode=5150"
//

function createAjaxObj(){
	var httprequest=false
	if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		httprequest=new XMLHttpRequest()
	if (httprequest.overrideMimeType)
		httprequest.overrideMimeType('text/xml')
	}
	else if (window.ActiveXObject){ // if IE
		try {
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	return httprequest
}

// -------------------------------------------------------------------
// Main Ticker Object function
// flashticker_ajax(postcode, cachetime, divId, divClass, delay, optionallogicswitch)
// -------------------------------------------------------------------

function flashticker_ajax(postcode, cachetime, divId, divClass, delay, logicswitch){
	this.postcode=postcode //Postcode key indicating location to display if logged in.
	this.cachetime=cachetime //Time to cache feed, in minutes. 0=no cache.
	this.tickerid=divId //ID of ticker div to display information
	this.delay=delay //Delay between msg change, in miliseconds.
	this.logicswitch=(typeof logicswitch!="undefined")? logicswitch : ""
	this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
	this.pointer=0
	this.opacitysetting=0.2 //Opacity value when reset. Internal use.
	this.offer=[], this.vendor=[], this.contact=[] //Arrays to hold each component of Flastxt item
	this.ajaxobj=createAjaxObj()
	document.write('<div id="'+divId+'" class="'+divClass+'" >Initializing sample Flashtxt ticker...</div>')
	if (window.getComputedStyle) //detect if moz-opacity is defined in external CSS for specified class
	this.mozopacityisdefined=(window.getComputedStyle(document.getElementById(this.tickerid), "").getPropertyValue("-moz-opacity")==1)? 0 : 1
	this.getAjaxcontent()
}

// -------------------------------------------------------------------
// getAjaxcontent()- Makes asynchronous GET request with the supplied parameters
// -------------------------------------------------------------------

flashticker_ajax.prototype.getAjaxcontent=function(){
	if (this.ajaxobj){
		var instanceOfTicker=this
		var parameters="&id="+encodeURIComponent(this.postcode)+"&cachetime="+this.cachetime+"&bustcache="+new Date().getTime()
		this.ajaxobj.onreadystatechange=function(){instanceOfTicker.initialize()}
		this.ajaxobj.open('GET', flashtxturl+parameters, true)
		this.ajaxobj.send(null)
	}
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of Flashtxt samples content and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

flashticker_ajax.prototype.initialize=function(){ 
	if (this.ajaxobj.readyState == 4){ //if request of file completed
		if (this.ajaxobj.status==200){ //if request was successful
			var xmldata=this.ajaxobj.responseXML
			if(xmldata.getElementsByTagName("item").length==0){ //if no <item> elements found in returned content
				document.getElementById(this.tickerid).innerHTML="<b>Sorry</b> problem getting sample Flashtxt ticker feed!<br />"+this.ajaxobj.responseText
				return
			}
			var instanceOfTicker=this
			this.feeditems=xmldata.getElementsByTagName("item")
			//Cycle through XML object and store each piece of an item inside a corresponding array
			for (var i=0; i<this.feeditems.length; i++){
				this.offer[i]=this.feeditems[i].getElementsByTagName("offer")[0].firstChild.nodeValue
				this.vendor[i]=this.feeditems[i].getElementsByTagName("vendor")[0].firstChild.nodeValue
				this.contact[i]=this.feeditems[i].getElementsByTagName("contact")[0].firstChild.nodeValue
			}
			document.getElementById(this.tickerid).onmouseover=function(){instanceOfTicker.mouseoverBol=1}
			document.getElementById(this.tickerid).onmouseout=function(){instanceOfTicker.mouseoverBol=0}
			this.rotatemsg()
		}
	}
}

// -------------------------------------------------------------------
// rotatemsg()- Rotate through RSS messages and displays them
// -------------------------------------------------------------------

flashticker_ajax.prototype.rotatemsg=function(){
	var instanceOfTicker=this
	if (this.mouseoverBol==1) //if mouse is currently over ticker, do nothing (pause it)
		{setTimeout(function(){instanceOfTicker.rotatemsg()}, 100);}
	else{ //else, construct item, show and rotate it!
		var tickerDiv=document.getElementById(this.tickerid)
		var msgpointer=parseInt((this.pointer)/2);
		var offer='<div class="flashoffer">'+this.offer[msgpointer]+'</div>'
		var vendor='<div class="flashvendor">'+this.vendor[msgpointer]+'</div>'
		var contact='<div class="flashcontact">'+this.contact[msgpointer]+'</div>'
		if (this.logicswitch.indexOf("vendor")==-1) vendor=""
		if (this.logicswitch.indexOf("contact")==-1) contact=""
		var tickercontent=offer+vendor+contact //STRING FOR FEED CONTENTS 
		this.fadetransition("reset") //FADE EFFECT- RESET OPACITY
		if((this.pointer%2)==0){tickerDiv.innerHTML="<center><img src='images/logo.jpg' width=80 height=80>";}
		else{tickerDiv.innerHTML=tickercontent;}
		
		this.fadetimer1=setInterval(function(){instanceOfTicker.fadetransition('up', 'fadetimer1')}, 100) //FADE EFFECT- PLAY IT
		this.pointer=(msgpointer<this.feeditems.length-1)? this.pointer+1 : 1
		setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay) //update container every second
	}
}

// -------------------------------------------------------------------
// fadetransition()- cross browser fade method for IE5.5+ and Mozilla/Firefox
// -------------------------------------------------------------------

flashticker_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])
}
