
(function() {

	// global flag
	var isIE = false;
	var DETAILS = "agsCOE_details";
	var monthAbbArray = new Array("Jan.", "Feb.", "Mar.", "Apr.", "May", "June", "July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec.");

	var _load = function() {
	if(document.getElementById) {
			if ( document.getElementById('template') ) {
				DETAILS = 'details';
			}
			var strXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>    <rss version=\"2.0\">     <channel>      <title>Alabama's Gulf Coast: Calendar of Events</title>      <link>http://www.gulfshores.com/things-to-do/calendar-events/</link>      <description>Events in Alabama's Gulf Coast</description>      <language>en-us</language>      <ttl>60</ttl>      <copyright>&#169; 2009 Alabama Gulf Coast CVB</copyright>      <pubDate>Fri, 20 Nov 2009 17:21:52 GMT</pubDate>      <lastBuildDate>Fri, 20 Nov 2009 17:21:52 GMT</lastBuildDate>               <item>          <title>25th Annual Frank Brown Songwriters Festival</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=13296</link>             <description>             <![CDATA[           <p>The festival provides an atmosphere to promote all writers, whether they had written hit songs which made people open their eyes and view the world in a different way or they are aspiring writers and musicians who knew they could do the same but needed a forum in order to get their music out to  ...</p>             ]]>             </description>             <pubDate>Thu, 12 Nov 2009 00:00:00 GMT</pubDate>             <dateDisplay>11/12/2009 - 11/22/2009</dateDisplay>         </item>                   <item>          <title>&quot;Nautical Night of Lights&quot; Lighted Boat Parade</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14528</link>             <description>             <![CDATA[           <p>The holiday season is right around the corner and the Alabama Gulf Coast Area Chamber of Commerce will be celebrating by hosting the 24th Annual “Nautical Night of Lights” lighted boat parade.  Santa Claus will arrive from the North Pole to start the parade.  This year’s parade will be held on F ...</p>             ]]>             </description>             <pubDate>Fri, 11 Dec 2009 00:00:00 GMT</pubDate>             <dateDisplay>12/11/2009</dateDisplay>         </item>                   <item>          <title>17th Annual Symphony by the Bay</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14601</link>             <description>             <![CDATA[           <p>5:30 p.m. Surround yourself with family and friends at United Way of Baldwin County's  Symphony by the Bay, featuring the holiday sounds of the Louisiana Philharmonic Orchestra and a delightful buffet. </p>             ]]>             </description>             <pubDate>Sun, 13 Dec 2009 00:00:00 GMT</pubDate>             <dateDisplay>12/13/2009</dateDisplay>         </item>                   <item>          <title>The Wharf’s 4th Annual New Year’s Eve &quot;Block Party Blowout&quot;</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14527</link>             <description>             <![CDATA[           <p>8:00 pm.  Festive decorations, laser effects, and holiday lighting. Festivities will include jugglers, fortune teller, caricature artist, and other street entertainers. Party horns, hats, and tiaras; concessions and a cash bar; and projection screens highlighting other New Year’s Eve events arou ...</p>             ]]>             </description>             <pubDate>Thu, 31 Dec 2009 00:00:00 GMT</pubDate>             <dateDisplay>12/31/2009</dateDisplay>         </item>               </channel> </rss> ";
			loadXMLDoc(strXML);
			
		}
	};
	//don't overwrite exsisting onload handlers
	if (window.addEventListener) {
		window.addEventListener('load', _load, false);
	}
	else if (window.attachEvent) {
		window.attachEvent('onload', _load );
	}
	// global request and XML document objects
	var doc;

	//MAIN TICKER FUNCTION
	//SET PREFERENCE VARIABLES
	var myTimer;
	var theCharacterTimeout = 15;
	var theStoryTimeout = 2500;
	// Ticker startup function
	function startTicker() {
		//clear the ticker timer
		window.clearTimeout(myTimer);
		// Define run time values
		//Get the Items array
		items = doc.getElementsByTagName("item");
		theItemCount = items.length;
		theCurrentStory = -1;
		theCurrentLength = 0;
		// Locate base objects
		if (document.getElementById) {
				//Write the anchor text for the ticker
				/*
				*/
				runTheTicker();
			} else {
				return true;
		}
	}

	// Ticker main run loop function
	function runTheTicker() {
		var myTimeout;
		
		var objDetails = document.getElementById(DETAILS)
		// Go for the next story data block
		if(theCurrentLength == 0 && theItemCount != 0) {
			theCurrentStory++;

			theCurrentStory = theCurrentStory % theItemCount;
			
			if(theCurrentStory == 0)
			{
			    for(i = document.getElementsByName("tickerAnchor").length - 1; document.getElementsByName("tickerAnchor").length > 0; i = document.getElementsByName("tickerAnchor").length - 1)
			    {
			        document.getElementsByName("tickerAnchor")[i].parentNode.parentNode.removeChild(document.getElementsByName("tickerAnchor")[i].parentNode);
			    }
			        
			    objDetails.scrollTop = 0;
			}
			
			objDetails.innerHTML += '<span name="itemSpanWrapper"><a class="tickerAnchor" name="tickerAnchor" href="#"><\/a><br><span id="tickerStory" name="tickerStory" ><\/span><br><br><\/span>';
			theAnchorObject = document.getElementsByName("tickerAnchor")[theCurrentStory];
			theStoryObject = document.getElementsByName("tickerStory")[theCurrentStory];
			
			theStoryHeader = getElementTextNS("", "title", items[theCurrentStory], 0);
			theStorySummary = getElementTextNS("", "dateDisplay", items[theCurrentStory], 0);
			theTargetLink = getElementTextNS("", "link", items[theCurrentStory], 0);

			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;
			
		}
		if(objDetails.scrollHeight > objDetails.clientHeight)
		{
			objDetails.scrollTop = objDetails.scrollHeight - objDetails.clientHeight;
		}

		// Call up the next cycle of the ticker
		myTimer = window.setTimeout(runTheTicker, myTimeout);
	}

	function loadXMLDoc(strXML) {
		if (window.ActiveXObject) {
			doc=new ActiveXObject("Microsoft.XMLDOM")
			doc.loadXML(strXML);   
		}
		else if (document.implementation && document.implementation.createDocument) {
			var objDOMParser = new DOMParser();
			doc = objDOMParser.parseFromString(strXML, "text/xml");
		}
		startTicker() ;
	}
	// DISPLAY LOADING ANIMATION
	function startLoadFlag() {
		document.getElementById(DETAILS).innerHTML = '<p id="LoadMsg"><\/p>';
	}

	// HIDE LOADING ANIMATION
	function stopLoadFlag() {
		document.getElementById(DETAILS).innerHTML = "";
	}

	// retrieve text of an XML document element, including
	// elements using namespaces
	function getElementTextNS(prefix, local, parentElem, index) {
		var result = "";
		if (prefix && isIE) {
			// IE/Windows way of handling namespaces
			result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
		} else {
			// the namespace versions of this method
			// (getElementsByTagNameNS()) operate
			// differently in Safari and Mozilla, but both
			// return value with just local name, provided
			// there aren't conflicts with non-namespace element names
			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";
		}
	}
})();