var getMeteo = function(params, city, animate) {
	if(city)
		par = { city: params };
	else
		par = { coords: params };
	$.getJSON(
	    $('#root-url').html()+'meteo/', 
	    par, 
	    function(data) {
	    	var sessionWeather = data;
	    	if(data.status == 'ko')
	    	{
	    		message = meteo_noinfo;
	    		$('#meteo-resp p').prepend(message);
	    		$('#meteo-resp').animate({ top: 0 }, 350);
	    	}
	    	else
	    	{
	    		if(data.temperature) 
	    		{
	    			var date = new Date();
	    			date.setTime(date.getTime() + (60 * 60 * 1000));
	    			
	    			newSrc = $('#meteo #icon').attr('src')+data.icon;
	    			$('#meteo #icon').attr('src', newSrc);
	    			$('#meteo #place').html(data.city);
	    			$('#meteo #temperature').text(data.temperature[0]);
	    			$('#meteo #condition').text(data.condition);
	    			$('#meteo').animate({'opacity':1},300, function() {
	    				$('#meteo p:not(.note)').removeClass('no-display');
	    			});
	    			$.get(
	    				$('#root-url').html()+'video/', 
	    				{ 
	    					meteo: function() {
	    						array = data.icon.split('.');
	    						return array[0];
	    					} 
	    				}, 	
	    				function(data) {
	    	    			$('#meteo').animate({ top: 0 });
	    					$('#header #video-wrap').html(data);
	    				}
	    			);
	    		}
	    		else
	    		{
	    			message = meteo_noinfo;
	    			$('#meteo-resp p').prepend(message);
	    			$('#meteo-resp').animate({ top: 0 }, 350);
			    }
	    	}
	    	if(animate)
	    	{
	    		$('#meteo').animate({ top: 0 }, 350);
	    	}
//	    	$('#meteo').css({'opacity':1});
	});
}


$(document).ready(function() {
	
	$('.change-city').click(function(e) {
		e.preventDefault();
		$("#change-city-name").prev().removeClass('no-display').val(meteo_inseriscicitta).css({ 'opacity': 1 });
		$("#change-city-name").val('');
		$('#meteo-resp').animate({ top: -55 }, 350);
		$('#meteo').animate({ top: -55 }, 350, function() {
			$('#change-city-form').val('').animate({ top: 0 }, 350);
		});
	});
	
	$('#change-city-form label').each(function() {
		label = $(this);
		label.addClass('absolute');
		
		$(this).parent().mouseover(function() {
			$(this).children().first().animate({ 'opacity': 0.5 }, 200);
		});
		
		$(this).parent().mouseleave(function() {
			$(this).children().first().animate({ 'opacity': 1 }, 200);
		});
	});
	
	$("#change-city-name").focus(function() {
		$(this).prev().addClass('no-display'); 
	});
	
	$("#change-city-name").blur(function() {
		if($(this).val() == '')
	    	$(this).prev().removeClass('no-display'); 
	});
	    
	$("#change-city-name").autocomplete({
	    source: $('#root-url').html()+"cities/",
	    minLength: 3,
	    search: function() {
	    	$("#change-city-name").addClass('loader');
	    },
	    select: function(event, ui) {
	    	$('#meteo').animate({'opacity':0},100);
	    	city = ui.item.city;
	    	trackEvent('Meteo','Cambio città');
	    	$('#change-city-form').animate({ top: -35 }, 350, function() {
	    		$("#change-city-name").removeClass('loader').val(meteo_inseriscicitta);
	    		getMeteo(city, true, true);
	    	});
	    }
	});
	
	//console.log(sessionWeather);
	if(!sessionWeather)
	{
		if(google.loader.ClientLocation)
		{
			lat = google.loader.ClientLocation.latitude;
			lon = google.loader.ClientLocation.longitude;
			city = google.loader.ClientLocation.address.city;
			country = google.loader.ClientLocation.address.country;
			//getMeteo(lat+','+lon, false, false);
			if(city)
				getMeteo(city, true, false);
			else
				getMeteo(lat+','+lon, false, false);
		}
		else
		{
	    	message = meteo_noinfo;
	    	$('#meteo-resp p').prepend(message);
	    	$('#meteo-resp').animate({ top: 0 }, 350);
		}
	}
	else
	{
		data = jQuery.parseJSON(sessionWeather);
		if(data.status == 'ko')
		{
			message = meteo_noinfo;
	    	$('#meteo-resp p').prepend(message);
	    	$('#meteo-resp').animate({ top: 0 }, 350);
		}
		else
		{
			newSrc = $('#meteo #icon').attr('src')+data.icon;
			$('#meteo #icon').attr('src', newSrc);
			$('#meteo #place').html(data.city);
			$('#meteo #temperature').text(data.temperature[0]);
			$('#meteo #condition').text(data.condition);
	    	$.get(
	    	    $('#root-url').html()+'video/', 
	    	    { 
	    	    	meteo: function() {
	    	    		array = data.icon.split('.');
	    	    		return array[0];
	    	    	} 
	    	    }, 	
	    	    function(data) {
	    	    	$('#meteo').animate({ top: 0 });
	    	    	$('#header #video-wrap').html(data);
	    		}
	    	);
	    }
	}
	
	$('.search-box li:not(.label) label').each(function() {
		$(this).remove();
	});
	
	$('.menu-targets a').click(function(e) {
		e.preventDefault();
		$(this).parent().submit();
	});
	
});

//http://www.ipinfodb.com/ip_location_api.php
//newtargetweb
//twylb6eek
//afad611164ea68e5286c5b4dcafc9aa8473236ebcfd5b96ca0ac45654ab5af62
