$(document).ready(function(){
	$("#weather").css("display","block");					   
						   
	var childData = function(selector, arg){
		return selector.find(arg).attr('data');
	}
	var sendAjax = function(where){
		var oldHeight = $('#condition').height();
		//$('#data, #error').stop().hide();
		$.ajax({
			type: "GET",
			data: "where="+where,
			url: "/static/javascript/weather.php",
			success: function(data){
				forecast = $(data).find('forecast_information');
				cCondition = $(data).find('current_conditions');
				var city = childData(forecast, 'city');
				if(city!=undefined){
					//date = childData(forecast, 'forecast_date');
					var condition = childData(cCondition, 'condition');
					//tempC = childData(cCondition, 'temp_c');
					//humidity = childData(cCondition, 'humidity');
					var icon = childData(cCondition, 'icon');
					icon = icon.split('/');
					icon = icon[icon.length-1].split('.gif');
					icon = icon[0];
					$('#icon').append('<img src="/static/images/' + icon + '.png" />')
					$('#condition').text(condition);
					//$('#city').text(city);
					//$('#date').text(date);
					//$('#tempC').text(tempC+' C');
					//$('#humidity').text(humidity);
					//$('#icon').attr({'src':'http://www.google.com'+icon})
					//$('#data').stop().show();
					/*var newHeight = $('#condition').height();
					$('#condition').fadeTo(500, 0, function(){
						if($('#condition').height() != oldHeight){
							$('#condition').height(oldHeight);
							$('#condition').animate({ 
								height: newHeight
							}, 500, function(){
								$('#condition').fadeTo(500, 1);
							});
						}
					});*/
				}
				else{
					//$('#error').stop().show();
					$('#icon').append('<img src="images/error.png" />')
					$('#condition').text('ERROR');
				}
			}
		});
	}
	//$('#submit').click(function(){
	//where = $('#place').val();
	//sendAjax(where);
	//});
	//var where = document.location.href
	//where = document.location.href.split('#')
	//where = where[1];
	sendAjax('London');
});

function loadPopup(id){
	
	$("#backgroundPopup").css({
		"opacity": "0.7"
	});
	$("#backgroundPopup").fadeIn("slow");
	
	$(id).css({"z-index": "20"});
	$(id).css({"top": "100px"});
	
}


function disablePopup(){
	$("#backgroundPopup").fadeOut("slow");
	$(".popupForecast").css({"z-index": "-20"});
	$(".popupForecast").css({"top": "-1000px"});
}

$(document).ready(function(){

	$("#showThames").click(function(){
		id = "#popupThames";
		loadPopup(id);
	});
	
	$("#showDover").click(function(){
		id = "#popupDover";
		loadPopup(id);
	});
				
	$(".popupClose").click(function(){
		disablePopup();
	});
	
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});