
(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>Thu, 02 Jul 2009 19:34:29 GMT</pubDate>      <lastBuildDate>Thu, 02 Jul 2009 19:34:29 GMT</lastBuildDate>               <item>          <title>The Red Snapper World Championship presents The Saltwater Series </title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=13933</link>             <description>             <![CDATA[           <p>With a lucrative payout schedule of more than $200,000 in cash and prizes, competition will be fierce as anglers battle from March 15th to August 15th for eight offshore and two inshore fish species.  Once red snapper season opens on June 1st, fisherman will have a 60-day window to land the top ...</p>             ]]>             </description>             <pubDate>Sat, 14 Mar 2009 00:00:00 GMT</pubDate>             <dateDisplay>3/14/2009 - 8/15/2009</dateDisplay>         </item>                   <item>          <title>SummerTide Theatre presents Godspell</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=13617</link>             <description>             <![CDATA[           <p>Based on the Gospel according to St. Matthew, and featuring a sparkling score by Stephen Schwartz, \"Godspell\" boasts a string of well-loved songs, led by the international hit, \"Day By Day.\" As the cast performs \"Prepare Ye The Way Of The Lord\", \"Learn Your Lessons Well\", \"All For The Best\", \"Al ...</p>             ]]>             </description>             <pubDate>Fri, 05 Jun 2009 00:00:00 GMT</pubDate>             <dateDisplay>6/5/2009 - 7/3/2009</dateDisplay>         </item>                   <item>          <title>Pepsi Star Series and The Wharf presents: Montgomery Gentry With Special Guest: Little Big Town</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=13909</link>             <description>             <![CDATA[           <p>TICKETS: here. Gates open 6:00 p.m. Showtime 7:00 p.m. Since their debut in 1999, Eddie Montgomery and Troy Gentry have been a cornerstone of the most important movement in country music since the Outlaws. Just as Waylon, Willie and the rest kicked open the genre's doors in the 1970s, Montgomery ...</p>             ]]>             </description>             <pubDate>Sat, 04 Jul 2009 00:00:00 GMT</pubDate>             <dateDisplay>7/4/2009</dateDisplay>         </item>                   <item>          <title>Blue Angels Air Show</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=13903</link>             <description>             <![CDATA[           <p>Blues scheduled to fly at 2:00 p.m. A Blue Angels flight demonstration exhibits choreographed refinements of skills possessed by all naval aviators. It includes the graceful aerobatic maneuvers of the four-plane Diamond Formation, in concert with the fast-paced, high-performance maneuvers of its ...</p>             ]]>             </description>             <pubDate>Sat, 11 Jul 2009 00:00:00 GMT</pubDate>             <dateDisplay>7/11/2009</dateDisplay>         </item>                   <item>          <title>Pepsi Star Series and The Wharf presents: Bob Dylan, Willie Nelson  &amp; John Mellencamp</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14075</link>             <description>             <![CDATA[           <p>Get tickets here.  Gates open 5:00 p.m. Showtime 6:00 p.m.   Bob Dylan along with fellow troubadour Willie Nelson, this summer The Bob Dylan Show will also feature John Mellencamp,  marking just the second time in the past 24 years that these three performers have shared the concert stage.  </p>             ]]>             </description>             <pubDate>Fri, 31 Jul 2009 00:00:00 GMT</pubDate>             <dateDisplay>7/31/2009</dateDisplay>         </item>                   <item>          <title>Pepsi Star Series and The Wharf presents: Kenny Chesney With Special Guests: Miranda Lambert &amp; Lady Antebellum</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=13773</link>             <description>             <![CDATA[           <p>7:30 Showtime, 6:30 Gates Get your tickets here. American country music artist. Since 1993, Chesney has recorded thirteen albums, eleven of which have been certified gold or higher by the RIAA. He has also produced more than thirty Top Ten singles on the U.S. Billboard Hot Country Songs charts, ...</p>             ]]>             </description>             <pubDate>Thu, 06 Aug 2009 00:00:00 GMT</pubDate>             <dateDisplay>8/6/2009</dateDisplay>         </item>                   <item>          <title>Pepsi Star Series and The Wharf presents: Counting Crowes with Michael Franti and Spearhead and Augustana </title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14078</link>             <description>             <![CDATA[           <p>8:00 Showtime, 7:00 Gates. Counting Crows are a rock band originating from Berkeley, California. The group gained popularity in 1994 following the release of its debut album August and Everything After, which featured the hit single \"Mr. Jones\". The band's influences include Van Morrison, R.E.M. ...</p>             ]]>             </description>             <pubDate>Fri, 07 Aug 2009 00:00:00 GMT</pubDate>             <dateDisplay>8/7/2009</dateDisplay>         </item>                   <item>          <title>7th Annual Thunder On The Gulf Cancelled for 2009</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=13277</link>             <description>             <![CDATA[           <p>The Gulf Coast Powerboat Association (GCPA) announces that the 7th Annual Thunder on the Gulf has been cancelled due to the current economic environment. See website for more information.</p>             ]]>             </description>             <pubDate>Thu, 13 Aug 2009 00:00:00 GMT</pubDate>             <dateDisplay>8/13/2009 - 8/16/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";
		}
	}
})();