
(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; 2012 Alabama Gulf Coast CVB</copyright>      <pubDate>Sat, 04 Feb 2012 02:37:43 GMT</pubDate>      <lastBuildDate>Sat, 04 Feb 2012 02:37:43 GMT</lastBuildDate>               <item>          <title>18th Annual &quot;Super&quot; Chili Bowl Cook-off</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=17314</link>             <description>             <![CDATA[           <p>Proceeds going to the American Cancer Society Relay for Life.The event includes food Good Times and continuous, original music provided by our Flora~Bama® Players.  Souvenir T-shirts, Free Beer and other give-a-ways will be provided for all participants.</p>             ]]>             </description>             <pubDate>Saturday, February 04, 2012</pubDate>             <dateDisplay>2/4/2012</dateDisplay>         </item>                   <item>          <title>Mystical Order of Mirams Parade</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=17190</link>             <description>             <![CDATA[           <p> Watch and enjoy as revelers throw sweets, trinkets, beads and toys.  </p>             ]]>             </description>             <pubDate>Friday, February 17, 2012</pubDate>             <dateDisplay>2/17/2012</dateDisplay>         </item>                   <item>          <title>Mystics of Pleasure Mardi Gras Parade</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=17183</link>             <description>             <![CDATA[           <p>See the Mystics of Pleasure after the Orange Beach Parade!!!  Watch and enjoy as revellers throw sweets, trinkets, beads and toys. </p>             ]]>             </description>             <pubDate>Saturday, February 18, 2012</pubDate>             <dateDisplay>2/18/2012</dateDisplay>         </item>                   <item>          <title>Gulf Shores Mardi Gras Parade</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=17187</link>             <description>             <![CDATA[           <p>Laissez les bons temp roulet! (Let the good times roll!) Get ready to shout, \"Hey, Mister! Throw me a Moon Pie!\" Future Mardi Gras Dates: Future Mardi Gras Day Dates: February 12, 2013, March 4, 2014 and February 17, 2015.</p>             ]]>             </description>             <pubDate>Tuesday, February 21, 2012</pubDate>             <dateDisplay>2/21/2012</dateDisplay>         </item>                   <item>          <title>Orange Beach Mardi Gras Parade</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=17188</link>             <description>             <![CDATA[           <p>Laissez les bons temp roulet! (Let the good times roll!) Parade will end at Hwy. 161. Get ready to shout, \"Hey, Mister! Throw me a Moon Pie!\"  Future Mardi Gras Day Dates: February 12, 2013, March 4, 2014 and February 17, 2015.</p>             ]]>             </description>             <pubDate>Tuesday, February 21, 2012</pubDate>             <dateDisplay>2/21/2012</dateDisplay>         </item>                   <item>          <title>LuLu&#39;s 8th Anniversary Celebration and Boat Parade</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=17302</link>             <description>             <![CDATA[           <p>Laissez Bon Temps Roule. Or, as we say on the Intracoastal, \"Let the Good Times FLOW.\" Join us on FAT TUESDAY for the LuLu’s Mardi Gras 8th Anniversary Celebration and Boat Parade. Parade leaves the Wharf at 3:00 p.m. and ends at Lulus. Doors open at 10am.  Live music from Glen David Andrews and ...</p>             ]]>             </description>             <pubDate>Tuesday, February 21, 2012</pubDate>             <dateDisplay>2/21/2012</dateDisplay>         </item>                   <item>          <title>2012 Orange Beach Festival of Art </title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=16652</link>             <description>             <![CDATA[           <p>The Orange Beach Festival of Art is held each year on the second week-end of March. Come and See Visual Arts, Fine Art and Crafts,   Glass Artists, Potters, Painters, Sculptors, Watercolorist, Textile Artists, and Wood Artists. There will also be a Kid's Art Alley and Great Festival Food. </p>             ]]>             </description>             <pubDate>Saturday, March 10, 2012</pubDate>             <dateDisplay>3/10/2012 - 3/11/2012</dateDisplay>         </item>                   <item>          <title>Eric Church at The Wharf Amphitheater</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=17118</link>             <description>             <![CDATA[           <p>ERIC CHURCH ³BLOOD, SWEAT & BEERS TOUR² 2012  With special guests:Brantley Gilbert & Drake White </p>             ]]>             </description>             <pubDate>Saturday, March 10, 2012</pubDate>             <dateDisplay>3/10/2012</dateDisplay>         </item>                   <item>          <title>The Avett Brothers at The Wharf Amphitheater</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=17261</link>             <description>             <![CDATA[           <p> </p>             ]]>             </description>             <pubDate>Friday, March 16, 2012</pubDate>             <dateDisplay>3/16/2012</dateDisplay>         </item>                   <item>          <title>Experience Hendrix at The Wharf Amphitheater</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=17293</link>             <description>             <![CDATA[           <p>Featuring Billy Cox, David Hidalgo & Cesar Rosas Of Los Lobos, Jonny Lang, Robert Randolph, Kenny Wayne Shepherd, Dweezil Zappa, Robby Krieger, Eric  Johnson, Brad Whitford Of Aerosmith, Slide Brothers, Chris Layton Of Stevie Ray Vaughan & Double Trouble, and Mato Of Indigenous.   </p>             ]]>             </description>             <pubDate>Saturday, March 17, 2012</pubDate>             <dateDisplay>3/17/2012</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";
		}
	}
})();
