/*
DST starts on Sunday, March 11, 2007 at 2:00 AM local standard time
2nd Sunday in March at 2:00 AM

DST ends on Sunday, November 4, 2007 at 2:00 AM local daylight time
1st Sunday in November

Indianapolis - marion county
Standard time zone:			UTC/GMT -5 hours
Daylight saving time:		+1 hour	// -4 hours

Lafayette - tippecanoe county
Standard time zone:			UTC/GMT -5 hours
Daylight saving time:		+1 hour	// -4 hours

>>Evansville - vanderburgh county
Standard time zone:			UTC/GMT -6 hours
Daylight saving time:		+1 hour	// -5 hours

Fort Wayne - allen county
Standard time zone:			UTC/GMT -5 hours
Daylight saving time:		+1 hour	// -4 hours

Jeffersonville - clark county
Standard time zone:			UTC/GMT -5 hours
Daylight saving time:		+1 hour	// -4 hours

>>Merrillville - lake county
Standard time zone:			UTC/GMT -6 hours
Daylight saving time:		+1 hour	// -5 hours

Richmond - wayne county
Standard time zone:			UTC/GMT -5 hours
Daylight saving time:		+1 hour	// -4 hours

South Bend - st. joseph county
Standard time zone:			UTC/GMT -5 hours
Daylight saving time:		+1 hour	// -4 hours

Terre Haute - vigo county
Standard time zone:			UTC/GMT -5 hours
Daylight saving time:		+1 hour	// -4 hours

*/
function getZone() {
	el = document.getElementById("site-header-city");
	var city = el.options[el.selectedIndex].value;

	var today = new Date();

	var m = today.getUTCMinutes();
	var h = today.getUTCHours();

	switch(city) {
		case "Indianapolis":
		case "Lafayette":
		case "Fort Wayne":
		case "Jeffersonville":
		case "Richmond":
		case "South Bend":
		case "Terre Haute":
		var dstOffset = dst_isActive(today) ? -4 : -5;
		break;

		case "Evansville":
		case "Merrillville":
		var dstOffset = dst_isActive(today) ? -5 : -6;
		break;
	}
	//alert(dstOffset);
	h += dstOffset;
	today.setHours(h);
	var ampm = (h>=12) ? "p.m." : "a.m.";
	h -= (h>12) ? 12 : 0;
	if (h<0){
		//weird. not sure why numbers go negative, but they do.
		h = h+12;
		ampm = "p.m.";
		//stop negative numbers sometime soon, so this weird fix doesn't need to be here.
	}
	if (m<10){ m = "0" + m; }
	if (h<1 || isNaN(h)) { h = "12"; }
	document.getElementById('site-header-time').innerHTML = (h + ":" + m + " " + ampm + " ");
	t = setTimeout('getZone()',1000);
}

function dst_isActive(dateObj){
	var year = dateObj.getFullYear();

	var sun2mar = 15 - new Date(year, 2, 1).getDay(); // the day of the 2nd Sunday in March
	var spring = new Date("Mar " + sun2mar + ", " + year + " 2:00:00");

	var sun1nov = 8 - new Date(year, 10, 1).getDay(); // the day of the 1st Sunday in November
	var fall = new Date("Nov " + sun1nov + ", " + year + " 2:00:00");

	return ((dateObj > spring) && (dateObj < fall));
}

/*  NEW STUFF  */

function gTemp() {
    document.getElementById("site-header-temp").style.visibility='hidden';
    document.getElementById("site-header-temp").innerHTML='';
    document.getElementById("site-header-temp-at").style.visibility='hidden';
    var el = document.getElementById("site-header-city");
	var city = el.options[el.selectedIndex].value;
	var zip = "46225";	//default
	switch(city) {
		case "Indianapolis":
			zip = "46225";
		break;
		case "Lafayette":
			zip = "47901";
		break;
		case "Fort Wayne":
			zip = "46802";
		break;
		case "Jeffersonville":
			zip = "47129";
		break;
		case "Richmond":
			zip = "47374";
		break;
		case "South Bend":
			zip = "46601";
		break;
		case "Terre Haute":
			zip = "47802";
		break;
		case "Evansville":
			zip = "47708";
		break;
		case "Merrillville":
			zip = "46410";
		break;
	}
	
	var feed = new google.feeds.Feed("http://weather.yahooapis.com/forecastrss?p=" + zip + "&sid="+Math.random());
	feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
	feed.load(function(result) {
		if (!result.error && result.xmlDocument.getElementsByTagName("item").length > 0) {
		    var conditions = google.feeds.getElementsByTagNameNS(result.xmlDocument.getElementsByTagName("item")[0], "http://xml.weather.yahoo.com/ns/rss/1.0", "condition");
		    if (conditions.length > 0) {
		        document.getElementById("site-header-temp").innerHTML= conditions[0].getAttribute('temp') + "&deg;F";
                document.getElementById("site-header-temp-at").style.visibility='visible';
                document.getElementById("site-header-temp").style.visibility='visible';
		    }
		}
	});
	
	t = setTimeout('gTemp()',1000*60*5); /* every 5 minutes */
}

function fillFeed(listId, feedUrl) {
	var feed = new google.feeds.Feed(feedUrl);
	feed.load(function(result) {
		if (!result.error) {
			var container = document.getElementById(listId);
			while (container.hasChildNodes()) {
				container.innerHTML = '';
			}
			for (var i = 0; i < result.feed.entries.length && i < 3; i++) {
				var entry = result.feed.entries[i];
				var div = document.createElement("li");
				var link = document.createElement("a");
				link.appendChild(document.createTextNode(entry.title));
				link.setAttribute("href", entry.link);
				div.appendChild(link);
				container.appendChild(div);
			}
		}
	});
}