	var delaytimer;
	var billboardtimer;
	var flashMovie;
	
	function billboard_v_2(size, encodedxml){
		var xml = decode(encodedxml);
		xml = cleanUpXmlString(xml);
		if(size<=5)
		{
			build5HTML(xml);
		}
		else
		{
			build7HTML(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 build5HTML(xml){
		var html = '';
		parseXML(xml);
		var items = new Array();
		
		var i = 0;
		var itemCount = xmlDoc.getElementsByTagName("item").length;
		html = '<div id="billboardContainer">';
		html += '<div class="billboard"><div class="billboard5_1"><div class="billboard5_2"><ul class="billboardbarkerList">';
		//html += '<div class="frame top-piece"></div><div class="frame left-piece"></div><div class="frame right-piece"></div><div class="frame bottom-piece"></div>';
		while(i<itemCount){
			items[i] = addBillBoardItem(xmlDoc.getElementsByTagName("link")[i].firstChild.nodeValue, xmlDoc.getElementsByTagName("image")[i].firstChild.nodeValue, xmlDoc.getElementsByTagName("transitionTitle")[i].firstChild.nodeValue, xmlDoc.getElementsByTagName("transitionDescription")[i].firstChild.nodeValue, xmlDoc.getElementsByTagName("theme")[i].firstChild.nodeValue.toLowerCase(), xmlDoc.getElementsByTagName("category")[i].firstChild.nodeValue);
			html += writeBillBoardBarker(i, items[i]);		
			i++;
		}
		html += '</ul>';
		html += '<div class="controls">'+writeSelectionControls(items)+'</div>';
		html += '</div></div></div></div>';
		html += '<div class="bottomHashing" style="height:15px;"></div>';
		document.write(html);
		return html;
	}
	function build7HTML(xml){
		var html = '';
		parseXML(xml);
		var items = new Array();

		var i = 0;
		var itemCount = xmlDoc.getElementsByTagName("item").length;

		html = '<div id="billboardContainer">';
		html += '<div class="billboard"><div class="billboard7_1"><div class="billboard7_2"><ul class="billboardbarkerList">';
		//html += '<div class="frame top-piece"></div><div class="frame left-piece"></div><div class="frame right-piece"></div><div class="frame bottom-piece"></div>';
		while(i<itemCount){
			items[i] = addBillBoardItem(xmlDoc.getElementsByTagName("link")[i].firstChild.nodeValue, xmlDoc.getElementsByTagName("image")[i].firstChild.nodeValue, xmlDoc.getElementsByTagName("transitionTitle")[i].firstChild.nodeValue, xmlDoc.getElementsByTagName("transitionDescription")[i].firstChild.nodeValue, xmlDoc.getElementsByTagName("theme")[i].firstChild.nodeValue.toLowerCase(), xmlDoc.getElementsByTagName("category")[i].firstChild.nodeValue);
			html += writeBillBoardBarker(i, items[i]);		
			i++;
		}
		html += '</ul>';
		html += '<div class="controls">'+writeSelectionControls(items)+'</div>';
		html += '</div></div></div></div>';
		html += '<div class="bottomHashing" style="height:15px;"></div>';
		document.write(html);
		return html;
	}
	function addBillBoardItem(link, image, title, description, theme, category)
	{
		return {'link': link, 'image': image, 'title': title, 'description': description, 'theme': theme, 'category': category};
	}


	function getActiveBillboardIndex(){
		var index = jQuery('.billboardbarkerList li.active dt').html();
		return index;
	}
	
	
	function writeSelectionControls(items)
	{
		var size = items.length;
		//alert("The Size of the Array: " + size);
		var i = 0;
		var markup = '<div class="holdingArea"></div><ul class="buttons">';
		while (i<size)
		{
			i++;
			markup += '<li class="control'+i+' option"><div class="window '+items[i-1].theme+'"><div class="preview '+items[i-1].theme+'"><img src="'+items[i-1].image+'"/></div></div></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)
		  	{
				return;
		  	}
		}
	}
	function writeBillBoardBarker(index, item)
	{
		var barkerHTML = '<li class="bbb'+index+'">';
		barkerHTML += '<dl><dt>'+index+'</dt>';
		barkerHTML += '<dd>';
		barkerHTML += '<a class="link" href="'+item.link+'"><img class="billboardImage" src="'+item.image+'"/></a>';
		barkerHTML += '<div class="textArea"><div class="translucent"></div><a class="descriptionLink '+item.theme+'" href="'+item.link+'"><span class="category">'+item.category+'</span><span class="delimiter">//</span><h3 class="title">'+item.title+'</h3><p class="description">'+item.description+'</p></a></div>';
		barkerHTML += '</dd></dl></li>';
		return barkerHTML;
			
	}
	
	function indexThru(current){
		
		var bbsize = xmlDoc.getElementsByTagName("item").length;
		var oldcurrent = current;
		deactivate(".bbb"+current);
		
		
		if (current<bbsize-1){
			current++;
		}
		else
		{
			current=0;
		}
		activate(".bbb"+current, current);

		resetBillboardPlayer(current);
	}	
	
	function deactivate(selector){
		stopTransition('.billboardbarkerList li.active img');
		jQuery(selector).removeClass("active");
		jQuery(".billboard .controls .selected").removeClass("selected");
	}
	function activate(selector, index){
		jQuery(selector).addClass("active");
		var height = jQuery(".billboard li.active a.descriptionLink").height()+10;		
		/*jQuery(".billboard li.active div.holdingArea").height(height);*/
		jQuery(".billboard5_2 .controls .holdingArea").height(305-height);
		smoothTransition(selector);
		index++;
		jQuery(".billboard .control"+index).addClass("selected"); 
			
	}

	function smoothTransition(to)
	{
		jQuery('.billboardbarkerList li.active img').animate({opacity: 1}, 500);
	}
	function stopTransition(selector)
	{
		jQuery(selector).css('opacity', '0');
		jQuery(selector).stop();

	}

	
	function goToIndex(slide){
		var select = ".bbb"+slide;
		deactivate(".billboardbarkerList li.active");
		activate(select, slide);	
	}

	function resetBillboardPlayer(index){
		restartBillboardPlayer(index);
	}
	function restartBillboardPlayer(index){
		//alert(index);
		billboardtimer=setTimeout("indexThru("+index+")", 7000);
	}
	
	function addHover(selector){
		jQuery(selector).addClass("hover");
	}
	function removeHover(selector){
		jQuery(selector).removeClass("hover");
	}



jQuery(document).ready(function(){


	if(document.getElementById("billboardContainer")!=null)	{
		var currentIndex = 0;
		activate(".bbb"+currentIndex, currentIndex);
		restartBillboardPlayer(currentIndex);
	}
	if(navigator.appName!="Netscape")
	{
		jQuery('.billboardbarkerList li img').css('opacity', '0');
	}
	
	jQuery("ul.buttons li.control1").mouseover(function () { 
		clearTimeout(billboardtimer);
		if(!jQuery(this).hasClass("selected"))
		{
     			goToIndex(0);
     		}
    	});
    	jQuery("ul.buttons li.control2").mouseover(function () { 
    		clearTimeout(billboardtimer);
    		if(!jQuery(this).hasClass("selected"))
		{
		 	goToIndex(1);
		}
	});
	jQuery("ul.buttons li.control3").mouseover(function () { 
		clearTimeout(billboardtimer);
		if(!jQuery(this).hasClass("selected"))
		{
			goToIndex(2);
		}
	});
 	jQuery("ul.buttons li.control4").mouseover(function () { 
 		clearTimeout(billboardtimer);
 		if(!jQuery(this).hasClass("selected"))
		{
     			goToIndex(3);
     		}
    	});
	jQuery("ul.buttons li.control5").mouseover(function () { 
		clearTimeout(billboardtimer);
	 	if(!jQuery(this).hasClass("selected"))
		{
			goToIndex(4);
		}
	});
	jQuery("ul.buttons li.control6").mouseover(function () { 
		clearTimeout(billboardtimer);
		if(!jQuery(this).hasClass("selected"))
		{
			goToIndex(5);
		}
	});
	jQuery("ul.buttons li.control7").mouseover(function () { 
		clearTimeout(billboardtimer);
		if(!jQuery(this).hasClass("selected"))
		{
			goToIndex(6);
		}
	});
	jQuery("ul.buttons li.control1").mouseout(function () { 
		restartBillboardPlayer(0);
	});
	jQuery("ul.buttons li.control2").mouseout(function () { 
		restartBillboardPlayer(1);

	});
	jQuery("ul.buttons li.control3").mouseout(function () { 
		restartBillboardPlayer(2);

	});
	jQuery("ul.buttons li.control4").mouseout(function () { 
		restartBillboardPlayer(3);
	});
	jQuery("ul.buttons li.control5").mouseout(function () { 
		restartBillboardPlayer(4);
	});
	jQuery("ul.buttons li.control6").mouseout(function () { 
		restartBillboardPlayer(5);
	});
	jQuery("ul.buttons li.control7").mouseout(function () { 
		restartBillboardPlayer(6);
	});
 	
 	
    	jQuery("ul.buttons li.option").mouseover(function () {
    		addHover(this);
    	});
	jQuery("ul.buttons li.option").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");
	});

	
});
