// ------------------------
// defaults
// ------------------------
var defaults = {  
	yahoo_rss: "http://weather.yahooapis.com/forecastrss?p=",
	temp_multiplier: 2.125, //2.125 is correct
	zip_code: "",
	conditions: "",
	temperature: "",
	pressure: "",
	windspeed: 0,
	windspeed_index: 0,
	windspeed_array: [0,8,7,8,5,12,11,12,5,0,0],
	humidity: ""
};


// ------------------------
// Document.ready
// ------------------------
$(document).ready(function () {
	
	$(".auto-weather .draggable").draggable({ stack: { group: '.auto-weather .draggable', min: 1 } });
	
	//the right way
	getCurrentWindspeed();	
	setInterval( "updateWindspeed()", 30000 ); //20000 milliseconds = 20 seconds
	
	//the faking it for video way
	//setPageLayout();
	//getCannedWindspeed();
	//setInterval( "getCannedWindspeed()", 1000 ); //20000 milliseconds = 20 seconds

});

// ---------------------------------------
// get windspeed from the array I just made
// ---------------------------------------
function getCannedWindspeed() {
	
	console.log("starting getCannedWindspeed---- ");
	console.log("defaults.windspeed", defaults.windspeed);
	defaults.windspeed = defaults.windspeed_array[defaults.windspeed_index];
	console.log("new defaults.windspeed", defaults.windspeed);
	
	updatePageLayout(defaults.windspeed);
	//$("#windspeed").append(defaults.windspeed + " ");
	
	defaults.windspeed_index = defaults.windspeed_index + 1;
	
}

// ---------------------------------------
// sets the left value based on windspeed
// ---------------------------------------
function getCurrentWindspeed() {
	
	console.log("starting getCurrentWindspeed---- ");
	
	$.ajax({
		type: "GET",
		url: "/scripts/xml.php",
		data: "uri=http://weather.yahooapis.com/forecastrss?p=02906",
		dataType: "xml", 
		success: function(xml){
			
			var rss = $.xml2json(xml);
			
				defaults.windspeed = rss.channel.wind.speed;
				console.log("defaults.windspeed", defaults.windspeed);
				
				//nowcalculate all those divs
				setPageLayout();
			
			}
			
			
	});
	
}

// ---------------------------------------
// sets the left value based on windspeed
// ---------------------------------------
function updateWindspeed() {
	
	console.log("starting updateWindspeed---- ");
	
	$.ajax({
		type: "GET",
		url: "/scripts/xml.php",
		data: "uri=http://weather.yahooapis.com/forecastrss?p=02906",
		dataType: "xml", 
		success: function(xml){
			
			var rss = $.xml2json(xml);
			
				var newwindspeed = rss.channel.wind.speed;
				
				console.log("old windspeed: ", defaults.windspeed);
				console.log("new windspeed: ", newwindspeed);
				
				if (newwindspeed != defaults.windspeed) {
					
					console.log("updating! I hope.");
					updatePageLayout(newwindspeed);
					defaults.windspeed = newwindspeed;
					
				} else {
					console.log("Nothing to see here.");
				}
				
			}
			
			
	});
	
}

// ---------------------------------------
// sets the left value based on windspeed
// ---------------------------------------
function setPageLayout() {
	
	//cycle through all the .auto-weather divs
   $(".auto-weather").not("noplace").each(function(containerIndex){
	
		//inside each .auto-weather div, cycle through all the child divs
		$(this).children("div").each(function(divIndex) {
			
			var horizontalSpacing = divIndex * defaults.windspeed;
			var verticalSpacing = 0;
			
			$(this).css("z-index", divIndex);
			$(this).css("left", horizontalSpacing);
			$(this).css("top", verticalSpacing);			
			
		});
		  
	});	
   
}

// ---------------------------------------
// sets the left value based on windspeed
// ---------------------------------------
function updatePageLayout(newwindspeed) {
	
	//cycle through all the .auto-weather divs
    $(".auto-weather").not("noplace").each(function(containerIndex){
	
		//inside each .auto-weather div, cycle through all the child divs
		$(this).children("div").each(function(divIndex) {
			
			var horizontalSpacing = divIndex * newwindspeed;
						
			$(this).animate({ 
			        left: horizontalSpacing,
					html: newwindspeed + " mph"
			      }, 1750, "swing" );
			
			
		});
		  
	});	
	
}