/* Global */
	var isEvents = false;   // if its the events page...
/* /Global */

/* OnLoad */
	$(function() {
		
		/* $.brower calls are deprecated... we can only wait... and pray... */
		
			if( $.browser.mozilla )
				$('#top-navigation li a').attr('style','padding:6px 10px;');
			if( $.browser.msie )
				$('#lasso.directions-and-hours .directions-gadget').hide();
			
		/* featured-brew image wrangler */
			$('#lasso.featured-brew .beer img').parent("p").attr('class', 'thumbnail');
			
		/* featured-beer footer wrangler */
			$('#yellow-band #featured-beer img').parent("p").hide();
			var imgSrc = $('#yellow-band #featured-beer img').attr('src'); 
			$('#featured-beer .featured-beer-thumb').css('background','transparent url("' + imgSrc + '") center no-repeat');
			
	});	/* /OnLoad */	


/* to set the top nav */
	function setThatNav(pageTitle) {
		
		var current = pageTitle.toLowerCase();
		
		current = current.replace(/ /g,'-');
		
		addClassToLasso(current);
		
		if( current != 'home' ) {
			$('#top-navigation li a').each(function() {
				var sniffer = $(this).attr('href');			/* test's the href. sloppy. */
				if( sniffer.indexOf(current) >= 0 ) {
					$(this).attr('class','current-page');
					matched = true;
				}
			});
		} else $('#top-navigation li a:first').attr('class','current-page'); 	/* sets the first link. the home link. */
		
		if( current == 'events' ) 
			isEvents = true;
					
	}	/* /setThatNav */
	
/* to parse the feed */

	var feedURL = "http://www.google.com/calendar/feeds/info%40austinsbeverage.com/public/full";	// snag it
	var feedPARAMS = "?futureevents=true&orderby=starttime&sortorder=ascend";		// mod it
	var feedADDRESS= feedURL + feedPARAMS;
	
	$.get('/wp-content/themes/austins/includes/calendar.feed.php', { "url" : feedADDRESS }, function( feedContent ) {
		
	// raw arrays
		var rawTitles = $(feedContent).find('entry title');		
		var rawDescrips = $(feedContent).find('entry content');
		var rawTimes = $(feedContent).find('entry gd\\:when');
		
	// clean arrays
		var titles = new Array(rawTitles.length);
		var descrips = new Array(rawDescrips.length);
		var startTimes = new Array(rawTimes.length);
		var endTimes = new Array(rawTimes.length);
					
	// build clean arrays
		$.each( rawTitles, function(i) {	
			if( $(this).text() == '' ) titles[i] = "[empty]";			
			else titles[i] = $(this).text();
		});		// for titles
		
		$.each( rawDescrips, function(i) {					
			if( $(this).text() == '' ) descrips[i] = "[empty]";
			else descrips[i] = $(this).text();
		});		// for descriptions
		
		$.each( rawTimes, function(i) { 
				
			// the Alpha
				if( $(this).attr('startTime').indexOf('T') >= 0 ) 
					var tempAlpha = $(this).attr('startTime').match(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/);
				else
					var tempAlpha = $(this).attr('startTime').match(/(\d{4})-(\d{2})-(\d{2})/);
					
			// the Omega
				if( $(this).attr('endTime').indexOf('T') >= 0 ) 
					var tempOmega = $(this).attr('endTime').match(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/);
				else
					var tempOmega = $(this).attr('endTime').match(/(\d{4})-(\d{2})-(\d{2})/);
			
			// format them bad boys
				startTimes[i] = formatTimes(tempAlpha);
				if(isEvents)		// we only need the endTimes if we're on the Events page. Conservation is a big, and hot topic.
					endTimes[i] = formatTimes(tempOmega);					
								
		});		// for times/days			
		
	// to format + add HTML to times
		function formatTimes(alphaomega) {
			
			if( alphaomega.length > 4 ) {
				if( alphaomega[4] >= 12 ) {		// and if its in the evening...
					alphaomega[6] = 'PM';			// it overrides temp[6] (formerly miliseconds. go figure)
					alphaomega[4] = ( alphaomega[4] > 12 ) ? ( alphaomega[4]-12 ) : alphaomega[4];  // subtracts 12 for correct hour read and overwrites temp[4]
				} else {
					alphaomega[6] = 'AM';			// it overrides temp[6] regardless
				}
				return "<span class='event-date'>" + alphaomega[2] + "/" + alphaomega[3] + "/" + alphaomega[1] + "</span> @ <span class='event-time'>" + alphaomega[4] + ":" + alphaomega[5] + ' ' + alphaomega[6] + '</span>';
			} else  {
				return "<span class='event-date'>" + alphaomega[2] + "/" + alphaomega[3] + "/" + alphaomega[1] + "</span> - <span class='event-time'>All Day!</span>";
			}
			
		}
			
	// to echo HTML to Footer
		for( i=0;i<3;i++ ) {
			if( startTimes[i] == null && titles[i] == null )
				$('#footer-events-list').append("<li>&nbsp;</li>");
			else
				$('#footer-events-list').append("<li>" + startTimes[i].replace(/\s@.*/,'') + " - <span class='event-title'>" + titles[i] + "</span></li>");
		}

	// to echo HTML to Events page
		if(isEvents) {
			for( i=0;i<titles.length;i++ ) {
				
				if( titles[i] != "[empty]" )
					$('#full-events-list').append("<li class='event-title'>" + titles[i] + "</li>");
				else
					$('#full-events-list').append("<li class='event-title'>(Untitled Event)</li>");
					
				if( descrips[i] != "[empty]" )
					$('#full-events-list').append("<li class='event-description'>" + descrips[i] + "</li>");
				
				$('#full-events-list').append("<li class='event-start-time'> <strong>From:</strong> " + startTimes[i] + "</li><li class='event-end-time'> <strong>Until:</strong> " + endTimes[i] + "</li>");
			}
		}
	
	}, "xml"); /* /$.get */
	
/* to lasso the content... correctly. via: setThatNav(val) */
	function addClassToLasso(className) {
		$('#lasso').attr('class',className);
	}	/* /addClassToLasso */
	
/* Google Analytics */
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	try {
	var pageTracker = _gat._getTracker("UA-10955422-1");
	pageTracker._trackPageview();
	} catch(err) {}		/* /Google Analytics */
