// JavaScript Document
function setHeight(){
	var current = $('#nav').height();
	var other = $('#yui-main').height();
	var newHeight = (current > other) ? current : other;
	$('#nav').css({height: newHeight+ 'px'});
	
}

var monthArray = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];

function parseDate(date){
	// date should be received in the form mm/dd/yyyy but we can check for others like mm-dd-yyyy or mm.dd.yyyy
	var d = date.split(/[- /.]/);
	if(d.length <= 0) {
		alert('we errored on the date parse');
	}
	var month = monthArray[d[0] - 1];
	var day = d[1];
	var z = '<div class="date">';
	z += '<span class="month">' + month + '</span>';
	z += '<span class="day">' + day + '</span>';
	z += '</div>';
	return z;
}

function parseTime(time){
	//time should be in 24-hour hh:mm time, but we'll check to see if they have entered a.m./p.m.
	
	if(time.match(/[a p].?m/)){ return time; }
	
	var t = time.split(/[- /.:]/);
	if(t.length <= 0) { alert('we errored on the time parse');}
	
	var hour = t[0];
	var minute = t[1];
	var txt;
	if(hour >=12) { txt = "p.m."; hour = hour - 12; } else { txt = "a.m."; }
	if(hour == 0) { hour = 12; }
	return hour + ":" + minute + txt;
}

function loadXML(){
	$.ajax({ type: "GET", 
		   	  url: "events.xml", 
			  dataType: "xml", 
			  error: function(){ displayError2('#events'); },
			  success: function(xml){
					//This simple XPath XML function looks through the returned XML data
					//All tags can now be accessed within the loop
					var _title = "";
					var _time = "";
					var _date = "";
					var _loc = "";
					var _description = "";
					var z = "<h2>Upcoming Events</h2><ul>";
					var ctr = 0;
					//This function will loop for each match on addresses/address
					if($("event", xml).size() < 1) {
						displayError2('#events');	
						return;
					}
					$("event", xml).each(function(){
						ctr++;
						_title = $("title", this).text();
						_time = $("time", this).text();
						_date = $("date", this).text();
						_loc = $("location", this).text();
						var date = parseDate(_date);
						var time = _time;
						_description = $("description", this).text();
						if(ctr <= 3){
						z +="<li><div class='event clearfix'>"+ date + '<div class="content"><p class="time">'+time + ' at ' + _loc +'</p>'+ '<h4>'+_title +'</h4><p class="description">'+ _description + "</p></div></div></li>";
						}
					});
					z += "</ul>";
					if(z == '<ul></ul>'){
						z = '<p class="notice">There are no upcoming events</p>';	
					}
					$('#events').html(z);
			  } });
}

function loadEvents(){
	$.ajax({ type: "GET", 
		   	  url: "events.xml", 
			  dataType: "xml", 
			  error: function(){ displayError('#list'); showOtherBlurb();},
			  success: function(xml){
					//This simple XPath XML function looks through the returned XML data
					//All tags can now be accessed within the loop
					var _title = "";
					var _time = "";
					var _date = "";
					var _loc = "";
					var _description = "";
					var z = "<ul>";
					var ctr = 0;
					if($("event", xml).size() < 1) {
						displayError('#list');
						showOtherBlurb();
						return;
					}
					//This function will loop for each match on addresses/address
					$("event", xml).each(function(){
						ctr++;
						_title = $("title", this).text();
						_time = $("time", this).text();
						_date = $("date", this).text();
						_loc = $("location", this).text();
						var date = parseDate(_date);
						var time = _time;
						_description = $("description", this).text();
						
						z +="<li><div class='event clearfix'>"+ date + '<div class="content"><p class="time">'+time + ' at ' + _loc +'</p>'+ '<h4>'+_title +'</h4><p class="description">'+ _description + "</p></div></div></li>";
						
					});
					z += "</ul>";
					if(z == '<ul></ul>'){
						z = '<p class="notice">There are no upcoming events</p>';	
					}
					$('#list').html(z);
					setHeight();
			  } });
}
function displayError2(node){
	$(node).html('<h2>Upcoming Events</h2><p class="para">Check back later this summer for a list of our upcoming events.</p>'); 
}
function displayError(node){
	$(node).html('<p class="para">Check back later this summer for a list of our upcoming events.</p>'); 
}
function showOtherBlurb(){
	$('.message').show();
}
