
(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; 2010 Alabama Gulf Coast CVB</copyright>      <pubDate>Tue, 09 Feb 2010 10:17:29 GMT</pubDate>      <lastBuildDate>Tue, 09 Feb 2010 10:17:29 GMT</lastBuildDate>               <item>          <title>Mystical Order of Mirams Parade</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14649</link>             <description>             <![CDATA[           <p>Liz Cochran Miss Alabama America chosen as Grande Marshall for the Fourth Annual Mystical Order of Mirams Parade. Parade begins at 6:30 p.m. From Wintzell's ending at Rite Aid.  Watch and enjoy as revelers throw sweets, trinkets, beads and toys.  Parade route:here.</p>             ]]>             </description>             <pubDate>Fri, 12 Feb 2010 00:00:00 GMT</pubDate>             <dateDisplay>2/12/2010</dateDisplay>         </item>                   <item>          <title>Orange Beach Mardi Gras Parade</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14390</link>             <description>             <![CDATA[           <p>The City is very excited to have the Honor Flight South Alabama Veterans as the Grand Marshals this year. Laissez les bons temp roulet! (Let the good times roll!) Parade kicks off at 1:30 p.m. Get ready to shout, \"Hey, Mister! Throw me a Moon Pie!\" Parade route:here.. Future Mardi Gras dates: M ...</p>             ]]>             </description>             <pubDate>Tue, 16 Feb 2010 00:00:00 GMT</pubDate>             <dateDisplay>2/16/2010</dateDisplay>         </item>                   <item>          <title>Gulf Shores Mardi Gras Parade</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14391</link>             <description>             <![CDATA[           <p>Laissez les bons temp roulet! (Let the good times roll!) Parade kick off at 10:00 a.m. Get ready to shout, \"Hey, Mister! Throw me a Moon Pie!\" Future Mardi Gras Dates: March 8, 2011, February 21, 2012, February 12, 2013, March 4, 2014 and February 17, 2015.</p>             ]]>             </description>             <pubDate>Tue, 16 Feb 2010 00:00:00 GMT</pubDate>             <dateDisplay>2/16/2010</dateDisplay>         </item>                   <item>          <title>LuLu's 6th Anniversary Celebration and Boat Parade</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14716</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 6th Anniversary Celebration and Boat Parade. Doors open at 10am. Live music from New Orleans’ own Glen David Andrews Band between 1:00 to 5:00 pm and local favo ...</p>             ]]>             </description>             <pubDate>Tue, 16 Feb 2010 00:00:00 GMT</pubDate>             <dateDisplay>2/16/2010</dateDisplay>         </item>                   <item>          <title>17th Annual Seafood Festival and Car Show</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14648</link>             <description>             <![CDATA[           <p>The festival begins at 10am and concludes at 5pm.  The car show starts at 8am with judging at 11am and 2pm., and trophy presentation at 3pm.  Enjoy Arts & Crafts Vendors, Silent Auction, Balloon Jumps, Rock Wall Climb (for kids), Music from Brent Burns, Car Show, Great Seafood & Drinks, and much ...</p>             ]]>             </description>             <pubDate>Sat, 20 Feb 2010 00:00:00 GMT</pubDate>             <dateDisplay>2/20/2010</dateDisplay>         </item>                   <item>          <title>33rd Annual Gulf Shores Woman's Club Tour of Homes</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14719</link>             <description>             <![CDATA[           <p>9:30 a.m.-4:00 p.m. Five beautiful homes in the Gulf Shores area will be shown.  100% of the proceeds are distributed to area agencies and organizations and to scholarships for deserving students. </p>             ]]>             </description>             <pubDate>Sat, 20 Feb 2010 00:00:00 GMT</pubDate>             <dateDisplay>2/20/2010</dateDisplay>         </item>                   <item>          <title>The Wharf presents Rascal Flatts with special guest: Darius Rucker</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14663</link>             <description>             <![CDATA[           <p>Gates 7:00 p.m. Showtime 8:00 p.m. Get your tickets here.If any act embodies the place to which country music has evolved in the new century, it is Rascal Flatts. Since their inception a decade ago in 1999, the trio has helped change the face of popular music. Their trademark sound--Gary LeVox's ...</p>             ]]>             </description>             <pubDate>Sat, 06 Mar 2010 00:00:00 GMT</pubDate>             <dateDisplay>3/6/2010</dateDisplay>         </item>                   <item>          <title>2010 Orange Beach Festival of Art </title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=14532</link>             <description>             <![CDATA[           <p>Saturday and Sunday 10:00 a.m.-5:00 p.m. TThe Orange Beach Festival of Art is held each year on the second week-end of March.  The Festival of Art has been an anticipated annual event for nearly two decades. Juried artists from the region and around the nation come to display their work at the f ...</p>             ]]>             </description>             <pubDate>Sat, 13 Mar 2010 00:00:00 GMT</pubDate>             <dateDisplay>3/13/2010 - 3/14/2010</dateDisplay>         </item>                   <item>          <title>Pepsi Star Series and The Wharf presents: Corey Smith in Concert with Special Guest: Randy Rogers Band</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=15008</link>             <description>             <![CDATA[           <p>Gates open 7:00 p.m. Showtime: 8:00 p.m. For Corey Smith, one of the best things about making music has always been getting the chance to hang out and have a good time with his friends. And it’s still that way, nearly 10 years after his early days of playing the bars around Athens, Georgia.  The ...</p>             ]]>             </description>             <pubDate>Sat, 13 Mar 2010 00:00:00 GMT</pubDate>             <dateDisplay>3/13/2010</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";
		}
	}
})();