(function($) {
$.extend({
simpleWeather: function(options){
var options = $.extend({
zipcode: '85716',
location: '',
unit: 'i',
success: function(weather){},
error: function(message){}
}, options);

var weatherUrl = 'http://query.yahooapis.com/v1/public/yql?format=json&diagnostics=true&callback=?&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&q=';
if(options.location != '')
weatherUrl += 'select * from weather.forecast where location in (select id from weather.search where query="'+options.location+'") and u="'+options.unit+'"';
else if(options.zipcode != '')
weatherUrl += 'select * from weather.forecast where location in ("'+options.zipcode+'") and u="'+options.unit+'"';
else {
options.error("No location given.");
return false;
}

$.getJSON(
weatherUrl,
function(data) {
if(data != null && data.query.results != null) {
$.each(data.query.results, function(i, result) {
if (result.constructor.toString().indexOf("Array") != -1)
result = result[0];

currentDate = new Date();
sunRise = new Date(currentDate.toDateString() +' '+ result.astronomy.sunrise);
sunSet = new Date(currentDate.toDateString() +' '+ result.astronomy.sunset);
if (currentDate>sunRise && currentDate<sunSet)
timeOfDay = 'd';
else
timeOfDay = 'n';

wind = result.wind.direction;
if (wind>338)
windDirection = "N";
else if (wind>=0 && wind<24)
windDirection = "N";
else if (wind>=24 && wind<69)
windDirection = "NE";
else if (wind>=69 && wind<114)
windDirection = "E";
else if (wind>=114 && wind<186)
windDirection = "SE";
else if (wind>=186 && wind<204)
windDirection = "S";
else if (wind>=204 && wind<249)
windDirection = "SW";
else if (wind>=249 && wind<294)
windDirection = "W";
else if (wind>=294 && wind<338)
windDirection = "NW";

var weather = {
title: result.item.title,
temp: result.item.condition.temp,
units:{
temp: result.units.temperature,
distance: result.units.distance,
pressure: result.units.pressure,
speed: result.units.speed
},
currently: result.item.condition.text,
high: result.item.forecast[0].high,
low: result.item.forecast[0].low,
forecast: result.item.forecast[0].text,
wind:{
chill: result.wind.chill,
direction: windDirection,
speed: result.wind.speed
},
humidity: result.atmosphere.humidity,
pressure: result.atmosphere.pressure,
rising: result.atmosphere.rising,
visibility: result.atmosphere.visibility,
sunrise: result.astronomy.sunrise,
sunset: result.astronomy.sunset,
description: result.item.description,
thumbnail: "http://l.yimg.com/a/i/us/nws/weather/gr/"+result.item.condition.code+timeOfDay+"s.png",
image: "http://l.yimg.com/a/i/us/nws/weather/gr/"+result.item.condition.code+timeOfDay+".png",
tomorrow:{
high: result.item.forecast[1].high,
low: result.item.forecast[1].low,
forecast: result.item.forecast[1].text,
date: result.item.forecast[1].date,
day: result.item.forecast[1].day,
image: "http://l.yimg.com/a/i/us/nws/weather/gr/"+result.item.forecast[1].code+"d.png"
},
city: result.location.city,
country: result.location.country,
region: result.location.region,
updated: result.item.pubDate,
link: result.item.link
};

options.success(weather);
});
} else {
if (data.query.results == null)
options.error("Invalid location given.");
else
options.error("Weather could not be displayed. Try again.");
}
}
);
return this;
}
});
})(jQuery);



$(function() {
$.simpleWeather({
location: 'Phoenix, Arizona',
unit: 'f',
success: function(weather) {
$("#weather").append('<div style="width: 90px; height: 80px;  padding: 2px;  background-image: url('+weather.image+'); background-position: left; background-repeat: no-repeat;"><div style="font-size:140%;">'+weather.city+'</div><div style="font-size:80%;">'+weather.currently+'</div><div style="font-size:160%;">'+weather.temp+'&deg; '+weather.units.temp+'</div></div>');
},
error: function(error) {
$("#weather").html('<p>'+error+'</p>');
}
});

$.simpleWeather({
zipcode: '85712',
unit: 'g',
success: function(weather) {
$("#weather2").append('<div style="width: 90px; height: 80px;  padding: 2px;  background-image: url('+weather.image+'); background-position: left; background-repeat: no-repeat;"><div style="font-size:140%;">'+weather.city+'</div><div style="font-size:80%;">'+weather.currently+'</div><div style="font-size:160%;">'+weather.temp+'&deg; '+weather.units.temp+'</div></div>');
},
error: function(error) {
$("#weather2").html('<p>'+error+'</p>');
}
});

$.simpleWeather({
zipcode: '86001',
unit: 'h',
success: function(weather) {
$("#weather3").append('<div style="width: 90px; height: 80px; padding: 2px;  background-image: url('+weather.image+'); background-position: left; background-repeat: no-repeat;"><div style="font-size:140%;">'+weather.city+'</div><div style="font-size:80%;">'+weather.currently+'</div><div style="font-size:160%;">'+weather.temp+'&deg; '+weather.units.temp+'</div></div>');
},
error: function(error) {
$("#weather3").html('<p>'+error+'</p>');
}
});

$.simpleWeather({
zipcode: '85012',
unit: 'i',
success: function(weather) {
$("#weather4").append('<div style="width: 110px; height: 100px;  padding: 10px;  background-image: url('+weather.image+'); background-position: left-bottom; background-repeat: no-repeat; "><div style="font-size:120%;">'+weather.city+'</div><div style="font-size:80%;">'+weather.currently+'</div><div style="font-size:160%;">'+weather.temp+'&deg; '+weather.units.temp+'</div></div>');
},
error: function(error) {
$("#weather4").html('<p>'+error+'</p>');
}
});

$.simpleWeather({
zipcode: '85635',
unit: 'j',
success: function(weather) {
$("#weather5").append('<div style="width: 110px; height: 100px;  padding: 10px;  background-image: url('+weather.image+'); background-position: left-bottom; background-repeat: no-repeat; "><div style="font-size:120%;">'+weather.city+'</div><div style="font-size:80%;">'+weather.currently+'</div><div style="font-size:160%;">'+weather.temp+'&deg; '+weather.units.temp+'</div></div>');
},
error: function(error) {
$("#weather5").html('<p>'+error+'</p>');
}
});

$.simpleWeather({
zipcode: '86336',
unit: 'k',
success: function(weather) {
$("#weather6").append('<div style="width: 110px; height: 100px; padding: 10px;  background-image: url('+weather.image+'); background-position: left-bottom; background-repeat: no-repeat; "><div style="font-size:120%;">'+weather.city+'</div><div style="font-size:80%;">'+weather.currently+'</div><div style="font-size:160%;">'+weather.temp+'&deg; '+weather.units.temp+'</div></div>');
},
error: function(error) {
$("#weather6").html('<p>'+error+'</p>');
}
});
});

$.simpleWeather({
zipcode: '99801',
unit: 'm',
success: function(weather) {
$("#weather8").append('<div style="width: 90px; height: 80px;  padding: 2px;  background-image: url('+weather.image+'); background-position: left; background-repeat: no-repeat;"><div style="font-size:140%;">'+weather.city+'</div><div style="font-size:80%;">'+weather.currently+'</div><div style="font-size:160%;">'+weather.temp+'&deg; '+weather.units.temp+'</div></div>');
},
error: function(error) {
$("#weather8").html('<p>'+error+'</p>');
}
});



//$.simpleWeather({
//zipcode: '85012',
//unit: 'l',
//success: function(weather) {
//$("#weather7").append('<strong>'+weather.city+', '+weather.region+' Weather </strong><br />');
//$("#weather7").append('<img style="float:left;" width="100px" src="'+weather.thumbnail+'">');
//$("#weather7").append('<strong>Current: '+weather.temp+'&deg; '+weather.units.temp+'</strong> &nbsp;&nbsp; <span>'+weather.currently+'</span><br />');
//$("#weather7").append('<b>High</b>: '+weather.high+'&deg; '+weather.units.temp+'  &nbsp; <b>Low</b>: '+weather.low+'&deg; '+weather.units.temp+'<br />');
//$("#weather7").append('<b>Wind</b>: '+weather.wind.direction+' '+weather.wind.speed+' '+weather.units.speed+' <b>Chill</b>: '+weather.wind.chill+'<br />');
//$("#weather7").append('<small>Sunrise: '+weather.sunrise+' - Sunset: '+weather.sunset+'</small><br />');
//$("#weather7").append('<b>'+weather.tomorrow.day+'</b> - '+weather.tomorrow.forecast+' <b>High</b>: '+weather.tomorrow.high+' <b>Low</b>: '+weather.tomorrow.low+'');
//},
//error: function(error) {
//$("#weather7").html('<p>'+error+'</p>');
//}
//});
