// Main constructor for the weather widget functions
function weatherWidget(){
	
//	jQuery.easing.def = "easeInOutBack";
	var easingMethod = 'easeInOutBack';	// default easing method for animation
	var weatherDisplay = 'current';		// default weather conditions to be displayed
	var widgetMode = 'daytime';			// default widget mode

	this.current = function(e){
		if(typeof(e.preventDefault)!='undefined'){e.preventDefault();}
		$("#wdata")
			.animate({left:"-300px"},800,"easeInOutBack", function(){$("#wlinks").show();});
	}

	this.outlook = function(e){
		if(typeof(e.preventDefault)!='undefined'){e.preventDefault();}
		$("#wdata")
			.animate({left:"-600px"},800,"easeInOutBack");
	}
	
	this.failed = function(){
		$("#wcurrent .wtemp").html("N/A");
		$("#wcurrent .wcity").html("Couldn't get in touch with nature...");
	}

	this.mode = function(e){
		if(typeof(e.preventDefault)!='undefined'){e.preventDefault();}
		var wmode = $(this).html();
		$("#weather_contents").attr("class",wmode);
	}
	
	this.getWeather = function(){
		// Set some global paths
		if (typeof(icon_url)=='undefined') { icon_url = 'http://media.fortmilltimes.com/images/accuweather/indigio/'; }
		if (typeof(icon_set_large)=='undefined') { icon_set_large = '80x80'; }
		if (typeof(icon_set_small)=='undefined') { icon_set_small = '45x45'; }
		if (typeof(icon_format)=='undefined') { icon_format = 'png'; }
	
		var w_image_url_large = icon_url+icon_set_large+"/"+icon_format+"/";
		var w_image_url_small = icon_url+icon_set_small+"/"+icon_format+"/";
		
		var conditions = {"01":"Sunny","02":"Mostly Sunny","03":"Partly Sunny","04":"Some Clouds","05":"Hazy Sunshine","06":"Mostly Cloudy","07":"Cloudy","08":"Dreary","11":"Fog","12":"Showers","13":"M'Cldy w/Shwrs","14":"P'Sunny w/Shwrs","15":"Thunderstorms","16":"M'Cldy w/Storms","17":"P'Sunny w/Strms","18":"Rain","19":"Flurries","20":"Flurries","21":"Flurries","22":"Snow","23":"M'Cldy w/Snow","24":"Ice","25":"Sleet","26":"Freezing Rain","29":"Wintery Mix","30":"Hot","31":"Cold","32":"Windy","33":"Clear","34":"Mostly Clear","35":"Partly Cloudy","36":"Some Clouds","37":"Hazy","38":"Mostly Cloudy","39":"P'Cldy w/Shwrs","40":"M'Cldy w/Shwrs","41":"P'Cldy w/Storms","42":"M'Cldy w/Storms","43":"Flurries","44":"Snow"}
		
		// Forecast
		$.ajax({
			type: "GET",
			url: "/static/accuweather/forecast.xml",
			dataType: "xml",
			complete: this.current,
			error: this.failed,
			success: function(xml) {
				// Get forcast data for next 3 days
				$(xml).find("day:not(:first)").each(
					function(){
						var day_number = $(this).attr('number');
						var day_name = $(this).find('daycode').text().substring(0,3);
						var weather_icon = $(this).find('daytime').find('weathericon').text();
						var high_temp = $(this).find('daytime').find('hightemperature').text();
						var low_temp = $(this).find('daytime').find('lowtemperature').text();
						var desc = conditions[weather_icon];
						var outlook_day = "#woutlook #wday"+day_number;
						
						$(outlook_day+" .wday").html(day_name);
						$(outlook_day+" .wicon").css({background:"url('"+w_image_url_small+weather_icon+"."+icon_format+"') 0 0 no-repeat"});
						$(outlook_day+" .whi").html("H: "+high_temp);
						$(outlook_day+" .wlo").html("H: "+low_temp);
						$(outlook_day+" .wdesc").html(desc);

					}
				);
			
			
				// Get today's hi/lo temps
				var temp_units =        $(xml).find('units').find('temp').text();
				var today_high_temp =   $(xml).find('day:first').find('daytime').find('hightemperature').text();
				var tonight_low_temp =  $(xml).find('day:first').find('nighttime').find('lowtemperature').text();
	
				$("#wcurrent .whi").html('High: '+today_high_temp+'&deg;');
				$("#wcurrent .wlo").html('Low: '+tonight_low_temp+'&deg;');
				
				// Now get current conditions
				$.ajax({
					type: "GET",
					url: "/static/accuweather/current.xml",
					dataType: "xml",
					error: this.failed,
					success: function(xml) {
						var loc = $(xml).find('local').find('city').text()+', '+$(xml).find('local').find('state').text();
						var daylight = $(xml).find('local').find('obsDaylight').text();
						var time = $(xml).find('local').find('time').text();
						var cur_temp = $(xml).find('currentconditions').find('temperature').text();
						var weather_icon = $(xml).find('currentconditions').find('weathericon').text();
						var weather_text = $(xml).find('currentconditions').find('weathertext').text();
					
						// Current Conditions
						$("#wcurrent .wcity").html(loc);
						$("#wcurrent .wicon").css({background:"url('"+w_image_url_large+weather_icon+"."+icon_format+"') 0 0 no-repeat"});
						$("#wcurrent .wtemp").html(cur_temp+'&deg;');
			
						// Now check for watches/warnings and set widget mode accordingly
						$.ajax({
							type: "GET",
							url: "/static/accuweather/watchwarn.xml",
							dataType: "xml",
							success: function(xml) {
								var watchwarn_active = $(xml).find('watchwarnareas').attr('isactive');
								if (watchwarn_active != '0') {
									$("#weather_contents").attr("class","severe");

								}else if(time < '19:30'){
									$("#weather_contents").attr("class","daytime");

								}else{
									//alert(time);
									$("#weather_contents").attr("class","nighttime");
								}
		
						  	}
						});
			
					}
				});
				
			}
		});
	}
	
	$(".wcurrent").click(this.current);
	$(".woutlook").click(this.outlook);
	$(".modelinks a").click(this.mode);
}
	
$(document)
	.ready(
		function(){
			var rhWeatherWidget = new weatherWidget();
			rhWeatherWidget.getWeather();
		}
	);