

$(document).ready(function(){
	
	/***************************************************************/
	/*  There are several sections in the templates that will not  */
	/*  function when javascript is turned off; such as: Facebook, */
	/*  Twitter and Flickr feeds, the current weather status shown */
	/*  at the end of each page, and the random profile sections.  */
	/*  A message will be shown to remind the user to turn         */
	/*  javascript on to view these sections.                      */
	/***************************************************************/	
	
	$('div.javascriptMessage').css("display","none");	

	/***************************************************************/
	/*  This set_search() function is called to place the text     */
	/*  "Search" in the searchbox. The clear_search() function     */
	/*  clears the text once the searchbox has been given focus.   */
	/***************************************************************/
	
	set_search();
	
	/***************************************************************/
	/*  Call the following function to move the background logo    */
	/*  image to the correct position. It is called on page load   */
	/*  and when the window is resized.                            */  
	/***************************************************************/	
	
	positionTheBackgroundLogo();

	$(window).resize(function() {
		positionTheBackgroundLogo();
	});


	/***************************************************************/
	/*  This function pulls the weather information based on the   */
	/*  zipcode provided near the end of the url below.            */ 
	/***************************************************************/	
	
	$.getJSON("http://query.yahooapis.com/v1/public/yql?format=json&diagnostics=true&callback=?&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&q=select * from weather.forecast where location in ('71999') and u='f'", displayWeather);
	function displayWeather(data) {	
	  if(data != null) {
				$.each(data.query.results, function(i, result) {							
					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<23)
						windDirection = "N";
					else if (wind>24 && wind<68)
						windDirection = "NE";
					else if (wind>69 && wind<113)
						windDirection = "E";
					else if (wind>114 && wind<185)
						windDirection = "SE";
					else if (wind>186 && wind<203)
						windDirection = "S";
					else if (wind>204 && wind<248)
						windDirection = "SW";
					else if (wind>249 && wind<293)
						windDirection = "W";
					else if (wind>294 && wind<337)
						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
					};
					
					$("#weather").append("<p><strong>"+weather.temp+"&deg; "+weather.units.temp+" "+weather.currently+"</strong></p>");
				});
			} else {
				$("#weather").html("<p>Weather could not be displayed.</p>");
			}
	}

	
	/***************************************************************/
	/*  This function grabs the information stored in profile.xml  */
	/*  and randomly creates different profiles that are currently */
	/*  used on interior3.html and interior4.html.                 */
	/***************************************************************/
	
	// $('#profileMiddle').randomProfile({xmlPath: "xml/profile.xml", nodeName: "profile"});
	
	
	
	
	
});

/***************************************************************/
/*  This function finds the width of 1/2 the window size and   */
/*  subtracts 1/2 of the width of the content and then offsets */
/*  the logo 380px from that point. We are looking for the     */
/*  amount of room we need from the left edge of the window.   */
/***************************************************************/	

function positionTheBackgroundLogo()
{
	$("#bodyBkgd").css("background-position", function(index) {
	  var newPos = ((($("body").width()/2)-492.5)-380);
	  return newPos + "px top";
	});
}


function clear_search()
{
	if (document.getElementById("searchBox").value == "Search" ) 
	{
		document.getElementById("searchBox").value = "";
	}
}

function set_search() {
    try {
        if (document.getElementById("searchBox").value == "") {
            document.getElementById("searchBox").value = "Search";
        }
    }
    catch (e)
    { /* nothing */ }
}




//$.fn.randomProfile = function(options){
//	
//	var defaults = {
//		xmlPath: "",
//		nodeName: ""
//	};
//	
//	var options = $.extend(defaults, options);
//	
//	var nameArray = new Array();
//	var majorArray = new Array();
//	var descArray = new Array();
//	var photoArray = new Array();
//	
//	var rc = this;
//	
//	$.get(defaults.xmlPath, {}, function(xml){
//		$(defaults.nodeName,xml).each(function(i) {
//			nameArray.push($(this).find("name").text());
//			majorArray.push($(this).find("major").text());
//			descArray.push($(this).find("description").text());
//			photoArray.push($(this).find("photo").text());
//		});
//	
//		getRandom = function() {
//			var num = nameArray.length
//			var randNum = Math.floor(Math.random()*num);
//			
//			var name = "";
//			var major = "";
//			var desc = "";
//			var photo = "";
//			
//			for(x in nameArray){
//				if(x==randNum){
//					name = nameArray[x];
//					major = majorArray[x];
//					desc = descArray[x];
//					photo = photoArray[x];
//				}
//			};
//			
//			return "<img src='" + photo + "' alt='profiles' /><p>" + desc + "</p><span class='profileName'>" + name + "</span><span class='profileMajor'>" + major + "</span>";		
//		}
//		
//		rc.each(function(){
//			$(this).append(getRandom());
//		});
//	});
//};

