dojo.event.connect(window, "onload", "showWeather");

function showWeather(){
	var wInfoBox=document.getElementById("wInfoBox") || false;
	try{
	  dojo.io.bind({
		url: "http://www.mcdonaldcountychamber.org/getweather.asp",
		error: function(type, evaldObj){
		  if(wInfoBox){
			wInfoBox.innerHTML='<br /><h1>Weather</h1>\nWeather currently unavailable'
		  }
		},
		load: function(type, data, evt){
		  var location={};
		  var units={};
		  var wind={};
		  var atmosphere={};
		  var astronomy={};
		  var conditions={};
		  var forecast1={};
		  var forecast2={};
		  var link;

		  //Get location data
		  location.data=getContents('<yweather:location','/>',data,0);
		  location.city=getContents('city="','"',location.data.contents,0).contents;
		  location.state=getContents('region="','"',location.data.contents,0).contents;

		  //Get units data
		  units.data=getContents('<yweather:units','/>',data,0);
		  units.temp=getContents('temperature="','"',units.data.contents,0).contents;
		  units.distance=getContents('distance="','"',units.data.contents,0).contents;
		  units.pressure=getContents('pressure="','"',units.data.contents,0).contents;
		  units.speed=getContents('speed="','"',units.data.contents,0).contents;

		  //Get wind data
		  wind.data=getContents('<yweather:wind','/>',data,0);
		  wind.chill=getContents('chill="','"',wind.data.contents,0).contents;
		  wind.direction=getContents('direction="','"',wind.data.contents,0).contents;
		  wind.speed=getContents('speed="','"',wind.data.contents,0).contents;

		  //Get atmosphere data
		  atmosphere.data=getContents('<yweather:atmosphere','/>',data,0);
		  atmosphere.humidity=getContents('humidity="','"',atmosphere.data.contents,0).contents;
		  atmosphere.visibility=getContents('visibility="','"',atmosphere.data.contents,0).contents;
		  atmosphere.pressure=getContents('pressure="','"',atmosphere.data.contents,0).contents;
		  atmosphere.rising=getContents('rising="','"',atmosphere.data.contents,0).contents;

		  //Get astronomy data
		  astronomy.data=getContents('<yweather:astronomy','/>',data,0);
		  astronomy.sunrise=getContents('sunrise="','"',astronomy.data.contents,0).contents;
		  astronomy.sunset=getContents('sunset="','"',astronomy.data.contents,0).contents;

		  //Get current conditions
		  conditions.data=getContents('<yweather:condition','/>',data,0);
		  conditions.text=getContents('text="','"',conditions.data.contents,0).contents;
		  conditions.code=getContents('code="','"',conditions.data.contents,0).contents;
		  conditions.temp=getContents('temp="','"',conditions.data.contents,0).contents;

		  //Get link
		  link=getContents('<link>','</link>',data,0).contents;

		  //Get tomorrow's forecast
		  forecast1.data=getContents('<yweather:forecast','/>',data,0);
		  forecast1.day=getContents('day="','"',forecast1.data.contents,0).contents;
		  forecast1.low=getContents('low="','"',forecast1.data.contents,0).contents;
		  forecast1.high=getContents('high="','"',forecast1.data.contents,0).contents;
		  forecast1.text=getContents('text="','"',forecast1.data.contents,0).contents;
		  forecast1.code=getContents('code="','"',forecast1.data.contents,0).contents;

		  //Get the next day's forecast
		  forecast2.data=getContents('<yweather:forecast','/>',data,forecast1.data.endPos);
		  forecast2.day=getContents('day="','"',forecast2.data.contents,0).contents;
		  forecast2.low=getContents('low="','"',forecast2.data.contents,0).contents;
		  forecast2.high=getContents('high="','"',forecast2.data.contents,0).contents;
		  forecast2.text=getContents('text="','"',forecast2.data.contents,0).contents;
		  forecast2.code=getContents('code="','"',forecast2.data.contents,0).contents;

		  var wInfo='<br /><h1>Weather</h1>\n';
		  wInfo+='<img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/'+conditions.code+'.gif" style="float:left;margin-right:5px;" />'
		  wInfo+='<span style="font-size:13pt;">'+conditions.text+'</span><br />';
		  wInfo+='<span style="font-size:13pt;">'+conditions.temp+'&#0176;F</span><br />';
		  wInfo+='<br /><span style="clear:both" class="small">Weather provided by <a href="'+link+'">Yahoo!</a></span>';

		  wInfoBox.innerHTML=wInfo;

		}
	  });
	}catch(e){
	  //alert(e);
	}
}

function getContents(start, end, searchString, searchStart){
	searchStart = searchStart || 0;
	var objReturn={};

	var startPos=searchString.substring(searchStart).indexOf(start)+start.length+searchStart;
	var endPos=searchString.substring(startPos).indexOf(end)+startPos;

	objReturn.contents=searchString.substring(startPos,endPos);
	objReturn.startPos=startPos-start.length;
	objReturn.endPos=endPos+end.length;

	return objReturn;
}