$(document).ready(function(){

  if (typeof(icon_url_page)=='undefined') { icon_url_page = '/static/images/mi/third_party/accuweather/'; }
  if (typeof(icon_set_page)=='undefined') { icon_set_page = 'default_lg'; }
  if (typeof(subdir)=='undefined') { subdir = ''; }

  var w_image_url = icon_url_page+icon_set_page+"/";

  // Forecast
  $.ajax({
	type: "GET",
	url: "/static/accuweather/" + subdir + "forecast.xml",
	dataType: "xml",
	success: function(xml) {

	// Today & Tonight
	var temp_units =        $(xml).find('units').find('temp').text();
	var today_image =       $(xml).find('day:first').find('daytime').find('weathericon').text();
	var today_high_temp =   $(xml).find('day:first').find('daytime').find('hightemperature').text();
	var today_realfeel =    $(xml).find('day:first').find('daytime').find('realfeelhigh').text();
	var today_text =        $(xml).find('day:first').find('daytime').find('txtlong').text();
	var tonight_image =     $(xml).find('day:first').find('nighttime').find('weathericon').text();
	var tonight_low_temp =  $(xml).find('day:first').find('nighttime').find('lowtemperature').text();
	var tonight_realfeel =  $(xml).find('day:first').find('nighttime').find('realfeellow').text();
	var tonight_text =      $(xml).find('day:first').find('nighttime').find('txtlong').text();
	var sunrise =           $(xml).find('day:first').find('sunrise').text();
	var sunset =            $(xml).find('day:first').find('sunset').text();

	// Extended Forecast
	var dayName = new Array();
	var dayImage = new Array();
	var dayHigh = new Array();
	var dayLow = new Array();
	$(xml).find('day').each(function (i) {
		dayName[i+1] =    $(this).find('daycode').text();
		dayImage[i+1] =   $(this).find('daytime').find('weathericon').text();
		dayHigh[i+1] =    $(this).find('daytime').find('hightemperature').text();
		dayLow[i+1] =     $(this).find('daytime').find('lowtemperature').text();
	});

	// Today & Tonight
	$("#currentWeather #todayImage").html('<img src="'+w_image_url+today_image+'.GIF">');
	$("#currentWeather #todayTemp").html('High: '+today_high_temp+'&deg;'+temp_units+'<br />')
	                               .append('RealFeel: '+today_realfeel+'&deg;'+temp_units);
	$("#currentWeather #todayText").html(today_text);
	$("#currentWeather #tonightImage").html('<img src="'+w_image_url+tonight_image+'.GIF">');
	$("#currentWeather #tonightTemp").html('Low: '+tonight_low_temp+'&deg;'+temp_units+'<br />')
	                                 .append('RealFeel: '+tonight_realfeel+'&deg;'+temp_units);
	$("#currentWeather #tonightText").html(tonight_text);
	$("#currentWeather #sunrise").html('Sunrise: '+sunrise);
	$("#currentWeather #sunset").html('Sunset: '+sunset);

	// Extended Forecast
	$("#extendedForecast #dayName1").html(dayName[1]);
	$("#extendedForecast #dayImage1").html('<img src="'+w_image_url+dayImage[1]+'.GIF">');
	$("#extendedForecast #dayHigh1").html(dayHigh[1]+'&deg;'+temp_units);
	$("#extendedForecast #dayLow1").html(dayLow[1]+'&deg;'+temp_units);

	$("#extendedForecast #dayName2").html(dayName[2]);
	$("#extendedForecast #dayImage2").html('<img src="'+w_image_url+dayImage[2]+'.GIF">');
	$("#extendedForecast #dayHigh2").html(dayHigh[2]+'&deg;'+temp_units);
	$("#extendedForecast #dayLow2").html(dayLow[2]+'&deg;'+temp_units);

	$("#extendedForecast #dayName3").html(dayName[3]);
	$("#extendedForecast #dayImage3").html('<img src="'+w_image_url+dayImage[3]+'.GIF">');
	$("#extendedForecast #dayHigh3").html(dayHigh[3]+'&deg;'+temp_units);
	$("#extendedForecast #dayLow3").html(dayLow[3]+'&deg;'+temp_units);

	$("#extendedForecast #dayName4").html(dayName[4]);
	$("#extendedForecast #dayImage4").html('<img src="'+w_image_url+dayImage[4]+'.GIF">');
	$("#extendedForecast #dayHigh4").html(dayHigh[4]+'&deg;'+temp_units);
	$("#extendedForecast #dayLow4").html(dayLow[4]+'&deg;'+temp_units);

	$("#extendedForecast #dayName5").html(dayName[5]);
	$("#extendedForecast #dayImage5").html('<img src="'+w_image_url+dayImage[5]+'.GIF">');
	$("#extendedForecast #dayHigh5").html(dayHigh[5]+'&deg;'+temp_units);
	$("#extendedForecast #dayLow5").html(dayLow[5]+'&deg;'+temp_units);


	// Replace zipcode in links with proper one
	var zipcode = $(xml).find('zipcode').text();

	var href = $("#extendedForecast #inDepthInfo").attr('href');
	$("#extendedForecast #inDepthInfo").attr('href',href.replace(/zipcode=\d*/, "zipcode="+zipcode));

	var href = $("#extendedForecast #fifteenDay").attr('href');
	$("#extendedForecast #fifteenDay").attr('href',href.replace(/zipcode=\d*/, "zipcode="+zipcode));

	var href = $("#extendedForecast #precipitation").attr('href');
	$("#extendedForecast #precipitation").attr('href',href.replace(/zipcode=\d*/, "zipcode="+zipcode));

	var href = $("#extendedForecast #hourByHour").attr('href');
	$("#extendedForecast #hourByHour").attr('href',href.replace(/zipcode=\d*/, "zipcode="+zipcode));

	var href = $("#extendedForecast #monthly").attr('href');
	$("#extendedForecast #monthly").attr('href',href.replace(/zipcode=\d*/, "zipcode="+zipcode));

    }
  });

});

