
///////////////////////////////////////////////
//											 //
// Created by The Vandiver Group			 //
// Developed for Ulrich on Feb 12th 2010	 //
//											 //
///////////////////////////////////////////////


function createAjaxObject()
{
	var bHttpRequest = false
	
	// If we are using mozilla or safari
	if (window.XMLHttpRequest)
	{
	
		bHttpRequest = new XMLHttpRequest()
		
		if (bHttpRequest.overrideMimeType)
			bHttpRequest.overrideMimeType('text/xml')
	}
	
	else if (window.ActiveXObject)
	{
	
		try 
		{
			bHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		
		catch (e)
		{
		
			try
			{
				bHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			catch (e){}
		}
	}
	
	return bHttpRequest;
}

function ticker(sFile, sDivID, sDivClass, iDelay, sType, bFade)
{
	this.m_sFile = sFile;
	this.m_sDivID = sDivID;
	this.m_sDivCLass = sDivClass;
	this.m_iDelay = iDelay;
	this.m_sMessages = [];
	this.m_sType = sType;
	this.m_bMouseOver = false;
	this.m_iMsgIndex = 0;
	
	if(sType == "hor" || sType == "ver")
	{
		// Update using normal...?
	}
	else
	{
		// Update using ajax
		this.m_bFade = bFade;
		this.m_ajaxObj = createAjaxObject();

		document.write('<div id="' + sDivID + '" class="' + sDivClass + '">Initializing ticker...</div>')
		this.loadFile();
	}
}

ticker.prototype.loadFile = function()
{
	if(this.m_ajaxObj)
	{
		// Use a "burst-cache" method to make sure IE doesn't cache the results of the ajax script
		var url= this.m_sFile + "?bustcache=" + new Date().getTime();
		
		var self = this;
		
		// Once ready, initialize the ticker
		this.m_ajaxObj.onreadystatechange = function(){ self.init() };

		// Have ajax send a get request to the file
		this.m_ajaxObj.open('GET', url, true);
		this.m_ajaxObj.send(null);
	}
}

ticker.prototype.init = function()
{
	var self = this;

	// Has our file been properly received?
	if(this.m_ajaxObj.readyState == 4 && (this.m_ajaxObj.status == 200 || window.location.href.indexOf("http") == -1) )
	{
		// Get the div that holds the messages
		this.contentdiv = document.getElementById(this.m_sDivID);
		
		this.contentdiv.style.display = "none";
		this.contentdiv.innerHTML = this.m_ajaxObj.responseText;
		
		// Did we not find any <div> elements?
		if (this.contentdiv.getElementsByTagName("div").length == 0)
		{
			this.contentdiv.innerHTML="<b>Error</b> with file!";
			return;
		}

		document.getElementById(this.m_sDivID).onmouseover=function(){self.m_bMouseOver = true;}
		document.getElementById(this.m_sDivID).onmouseout=function(){self.m_bMouseOver = false;}
		
		if (window.attachEvent) //Clean up loose references in IE
			window.attachEvent("onunload", function(){self.contentdiv = self.m_ajaxObj = null})

		//  Store each message inside the array
		for (var i=0; i < this.contentdiv.getElementsByTagName("div").length; i++)
		{
			if (this.contentdiv.getElementsByTagName("div")[i].className == "message")
			{
				this.m_sMessages[this.m_sMessages.length] = this.contentdiv.getElementsByTagName("div")[i].innerHTML;
			}
		}

		this.contentdiv.innerHTML = "";
		this.contentdiv.style.display = "block";
		this.play();
	}
}

ticker.prototype.play = function()
{
	var self = this;

	if(this.m_bMouseOver == true)
	{
		setTimeout( function() { self.play() }, 100 );
	}
	else
	{
		this.contentdiv.innerHTML = this.m_sMessages[this.m_iMsgIndex];
		
		this.m_iMsgIndex++;
		
		if(this.m_iMsgIndex >= this.m_sMessages.length)
			this.m_iMsgIndex = 0;
			
		setTimeout( function(){ self.play() }, this.m_iDelay );
	}
}
