
	
	
	var delaytimer;
	var billboardtimer;
	var autoplay=true;
	var flashMovie;
	
	function billboard(encodedxml){
		var xml = decode(encodedxml);
		xml = cleanUpXmlString(xml);
		buildHTML(xml);
	}
	
	function decode(encoded) {
		return unescape(encoded.replace(/\+/g,  " "));
		
	}
	function cleanUpXmlString(jumbled)
	{
		var indexFound = jumbled.search(/<billboardxml/i);
		if(indexFound!=-1)
		{
			var end = jumbled.search(/<\/billboardxml>/i);
			if (end!=-1) {
				jumbled = jumbled.substr(indexFound, end+15-indexFound);
			}
			else {
				jumbled = 'fail';
			}
		}
		else {
			jumbled = 'fail'; 
		}
		
		return jumbled;
	}
	

	function buildHTML(xml){
		var html = '';
		parseXML(xml);
		
		var i = 0;
		var itemCount = xmlDoc.getElementsByTagName("item").length;
		
		html = '<div class="billboard news"><div class="billboard1 news"><div class="billboard2 news">'+writeBillboardTimer()+writePlaySwitch()+'<ul class="billboardbarkerList">';
		while(i<itemCount){
			html += writeBillBoardBarker(i, xmlDoc.getElementsByTagName("link")[i].firstChild.nodeValue, xmlDoc.getElementsByTagName("image")[i].firstChild.nodeValue, xmlDoc.getElementsByTagName("transitionTitle")[i].firstChild.nodeValue, xmlDoc.getElementsByTagName("transitionDescription")[i].firstChild.nodeValue);		
			i++;
		}
		html += '</ul>';
		html += '<div class="controls">'+writeSelectionControls(itemCount)+'</div>';
		html += '</div></div></div>';
		document.write(html);
		return html;
	}
	function writePlaySwitch()
	{
		var switchcode = '<div class="switch play"><div></div></div>';
		return switchcode;
		
	}
	
	function pressSwitch(){
		if(isAuto())
		{
			toManual();			
		}
		else {
			toAuto();
		}
	}
	function getActiveBillboardIndex(){
		var index = jQuery('.billboardbarkerList li.active dt').html();
		return index;
	}
	
	
	
	function toAuto(){
		jQuery(".billboard2 div.switch").removeClass("pause");
		jQuery(".billboard2 div.switch").addClass("play");
		jQuery("#billboardtimerholder").addClass("timer");
		var index =getActiveBillboardIndex();
		if(navigator.appName=="Netscape"){
			restartBillboardPlayer(index);
		}
		else{
			resetBillboardPlayer(index);
		}
		autoplay=true;
	}
	
	
	
	
	function toManual(){
		jQuery(".billboard2 div.switch").removeClass("play");
		jQuery(".billboard2 div.switch").addClass("pause");	
		clearTimeout(billboardtimer);
		jQuery("#billboardtimerholder").removeClass("timer");
		autoplay=false;
	}

	function writeBillboardTimer(){
		var timercode = '<div class="billboardtimer"><div id="billboardtimerholder" class="timer"></div></div>';

		timercode += '<script type="text/javascript">';
		timercode += 'window["timer"] = new Object();';
		timercode += 'var so = new SWFObject("/webapplications/betroot/includes/base/img/billboard/timer.swf", "timer", "15", "15", "8, 0,  0, 0", "#000000");';
		/*timercode += 'so.addParam("quality", "high");';
		timercode += 'so.addParam("wmode", "opaque");';
		timercode += 'so.addParam("salign", "t");';*/
		timercode += 'so.addParam("allowScriptAccess","always");';
		timercode += 'so.write("billboardtimerholder");';
		timercode += '</script>';

		return timercode;

	}

	function writeSelectionControls(numOfItems)
	{
		var c = 0;
		var markup = '<ul class="buttons">';
		while (c<numOfItems){
			c++;
			markup += '<li class="control'+c+' option">'+c+'</li>';
		}
		markup += '</ul>';
		return markup;
	}
	
	function parseXML(text)
	{
		if (navigator.appName=="Microsoft Internet Explorer") //Internet Explorer
		{
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async="false";
			xmlDoc.loadXML(text);
		}
		else
		{
			try //Firefox, Mozilla, Opera, etc.
		  	{
				parser=new DOMParser();
				xmlDoc=parser.parseFromString(text,"text/xml");
		  	}
		  	catch(e)
		  	{
				alert("Oh NO!");
				return;
		  	}
		}
	}
	
	function writeBillBoardBarker(index, link, image, title, description)
	{
		var barkerHTML = '<li class="bbb'+index+'">';
		barkerHTML += '<dl><dt>'+index+'</dt>';
		barkerHTML += '<dd><h3 class="title">'+title+'</h3>';
		barkerHTML += '<a class="link" href="'+link+'"><img class="billboardImage" src="'+image+'"/></a>';
		barkerHTML += '<a class="descriptionLink" href="'+link+'"><p class="description">'+description+'</p></a>';
		barkerHTML += '</dd></dl></li>';
		return barkerHTML;
		
	}
	
	function indexThru(current){
		
		var bbsize = xmlDoc.getElementsByTagName("item").length;
		
		deactivate(".bbb"+current);
		
		
		if (current<bbsize-1){
			current++;
		}
		else
		{
			current=0;
		}
		
		activate(".bbb"+current, current);

		resetBillboardPlayer(current);
	}	
	
	function deactivate(selector){
		jQuery(selector).removeClass("active");
		jQuery(".billboard2 .controls .selected").removeClass("selected");
	}
	function activate(selector, index){
		jQuery(selector).addClass("active");
		index++;
		jQuery(".billboard2 .control"+index).addClass("selected"); 
			
	}

	
	function goToIndex(slide){
		if(isAuto()){
			clearTimeout(billboardtimer);
		}
		deactivate(".billboardbarkerList li.active");
		var select = ".bbb"+slide;
		activate(select, slide
		);
		
		if(isAuto()){
			resetBillboardPlayer(slide);
		}
	}
	function isAuto(){
		return autoplay;
	}
	
	function resetBillboardPlayer(index){
		doReset();
		restartBillboardPlayer(index);
	}
	function restartBillboardPlayer(index){
		billboardtimer=setTimeout("indexThru("+index+")", 7600);
	}
	
	function addHover(selector){
		jQuery(selector).addClass("hover");
	}
	function removeHover(selector){
		jQuery(selector).removeClass("hover");
	}
	
	function doReset() {
	  flashMovie.reset();
	}
	
	function MM_findObj(n, d) { //v4.01
	  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	  if(!x && d.getElementById) x=d.getElementById(n); return x;
	}

jQuery(document).ready(function(){

	

	var currentIndex = 0;
	activate(".bbb"+currentIndex, currentIndex);
	flashMovie = MM_findObj('timer');
	billboardtimer=setTimeout("indexThru("+currentIndex+")", 7600);
	//resetBillboardPlayer(currentIndex);
	
	

	jQuery("ul.buttons li.control1").click(function () { 
     		 goToIndex(0);
    	});
    	jQuery("ul.buttons li.control2").click(function () { 
		 goToIndex(1);
	});
	jQuery("ul.buttons li.control3").click(function () { 
		 goToIndex(2);
	});
 	jQuery("ul.buttons li.control4").click(function () { 
     		 goToIndex(3);
    	});
 
    	jQuery("ul.buttons li.option").mouseover(function () {
    		addHover(this);
    	});
	jQuery("ul.buttons li.option").mouseout(function () {
		removeHover(this);
    	});
 
 
 	jQuery(".billboard2 div.switch").click(function () {
		pressSwitch();
	});
    	jQuery(".billboard2 div.switch div").mouseover(function () {
    		addHover(this);
    	});
  	jQuery(".billboard2 div.switch div").mouseout(function () {
  		removeHover(this);
    	}); 
    	
    	jQuery(".billboardImage").mouseover(function () {
    		addHover(".billboardbarkerList a");
    	});
    	jQuery(".billboardbarkerList a.descriptionLink").mouseover(function () {
	    	addHover(".billboardbarkerList a");
    	});
	jQuery(".billboardImage").mouseout(function () {
		removeHover(".billboardbarkerList a");
	});
	jQuery(".billboardbarkerList a.descriptionLink").mouseout(function () {
		removeHover(".billboardbarkerList a");
	});

	
});
