// global request and XML document objects
var req;

//MAIN TICKER FUNCTION
//SET PREFERENCE VARIABLES
var myTimer;
var theCharacterTimeout = 2;
var theStoryTimeout = 3000;
// Ticker startup function
function startTicker() {
	//clear the ticker timer
	window.clearTimeout(myTimer);
	// Define run time values
	//Get the Items array
	items = req.responseXML.getElementsByTagName("item");
	theItemCount = items.length;
	theCurrentStory = -1;
	theCurrentLength = 0;
	// Locate base objects
	if (document.getElementById) {
			//Write the anchor text for the ticker
			document.getElementById("details").innerHTML = '<span id="logo" style="height:125px; padding-left:40px;"><\/span><h3><a id="tickerAnchor" href="#" onclick="clicked(this.href);return true;"><\/a><\/h3><p><div id="tickerStory" style="height:70px;"><\/div><div id="button" class="buttonOff" onclick="nextVacancy();" onmouseover="this.className=\'buttonOn\'" onmouseout="this.className=\'buttonOff\'">&nbsp;</div><\/p>';
			theAnchorLogo = document.getElementById("logo");
			theAnchorObject = document.getElementById("tickerAnchor");
			theStoryObject = document.getElementById("tickerStory");			
			runTheTicker();
		} else {
			return true;
	}
}

// Ticker main run loop function
function runTheTicker() {
	var myTimeout;
	// Go for the next story data block
	if(theCurrentLength == 0) {
		theCurrentStory++;
		theCurrentStory = theCurrentStory % theItemCount;
		
		theStoryHeader = getElementTextNS("", "title", items[theCurrentStory], 0);
		theStorySummary = getElementTextNS("", "description", items[theCurrentStory], 0);
		theTargetLink = getElementTextNS("", "link", items[theCurrentStory], 0);
		theLogo = getElementTextNS("", "logo", items[theCurrentStory], 0);

		theAnchorLogo.innerHTML = '<img src="' + theLogo + '" height="125">';
		theAnchorObject.innerHTML = theStoryHeader;
		theAnchorObject.href = theTargetLink;

	}
	// Stuff the current ticker text into the anchor
	theStoryObject.innerHTML = theStorySummary.substring(0,theCurrentLength);
	// Modify the length for the substring and define the timer
	if(theCurrentLength < theStorySummary.length) {
		theCurrentLength++;
		myTimeout = theCharacterTimeout;
	} else {
		theCurrentLength = 0;
		myTimeout = theStoryTimeout;
	}
	// Call up the next cycle of the ticker
	myTimer = window.setTimeout("runTheTicker()", myTimeout);
}

// retrieve XML document (reusable generic function);
function loadXMLDoc(url) {
	// Display loading flag
	startLoadFlag();
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	}
	else {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	}
}

// handle onreadystatechange event of req object
function processReqChange() {
	// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			stopLoadFlag();
			startTicker();
		} else {
			// Stop displaying loading flag
			stopLoadFlag();
			document.getElementById('details').innerHTML = '<p>Unable load feed.<\/p>';
		}
	}
}

// DISPLAY LOADING ANIMATION
function startLoadFlag() {
	document.getElementById('details').innerHTML = '<p id="LoadMsg"><img src="g/loadani.gif" alt="" width="50" height="50" /><\/p>';
}

// HIDE LOADING ANIMATION
function stopLoadFlag() {
	document.getElementById('details').innerHTML = "";
}


//get the content to show
function show() {
	loadXMLDoc('http://www.tombok.com/rss/features.xml');
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
	var result = "";
	result = parentElem.getElementsByTagName(local)[index];
	if (result) {
		// get text, accounting for possible
		// whitespace (carriage return) text nodes
		if (result.childNodes.length > 1) {
			return result.childNodes[1].nodeValue;
		} else {
			return result.firstChild.nodeValue;
		}
	} else {
		return "n/a";
	}
}

//Set up the scroll area and scrollbar then run the show function
function loadTicker() {
	if (!window.widget) {
		show();
	} else {
		show();
	}
}


