
(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>Sat, 07 Nov 2009 03:31:00 GMT</pubDate>      <lastBuildDate>Sat, 07 Nov 2009 03:31:00 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>The Blue Angels Homecoming Air Show</title>             <link>http://www.gulfshores.com/things-to-do/calendar-events/?coe_id=13904</link>             <description>             <![CDATA[           <p>The Blue Angels Homecoming Air Show is scheduled for Friday and Saturday, November 14 & 15, on board Naval Air Station Pensacola, Florida.  Admission is FREE and the gates open at 8 am each day. The show includes static displays of aircraft of all types, food, beverage and vendor booths, virtual ...</p>             ]]>             </description>             <pubDate>Fri, 13 Nov 2009 00:00:00 GMT</pubDate>             <dateDisplay>11/13/2009 - 11/14/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>5:30 p.m. The \"Nautical Night of Lights\" is a lighted boat parade. Boats of all sizes and shapes and decorated with holiday lights and decorations meander along the waters of Orange Beach to the delight of the spectators that line the banks of the parade route. The parade will initiate its cruis ...</p>             ]]>             </description>             <pubDate>Sat, 12 Dec 2009 00:00:00 GMT</pubDate>             <dateDisplay>12/12/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";
		}
	}
})();