var ts_current = 0;
var ts_total = 0;

function ctrl() {

	if (
		$('input[name=nome_struttura_it]').val() == ""
		|| $('input[name=descrizione_struttura_it]').val() == ""
		|| $('input[name=nome_it]').val() == ""
		|| $('input[name=descrizione_it]').val() == ""
		|| $('input[name=nome_struttura_en]').val() == ""
		|| $('input[name=descrizione_struttura_en]').val() == ""
		|| $('input[name=nome_en]').val() == ""
		|| $('input[name=descrizione_en]').val() == ""
		|| $('input[name=cap]').val() == ""
		|| $('input[name=indirizzo]').val() == ""
		|| $('select[name=id_provincia]').val() == ""
		|| $('select[name=id_citta]').val() == ""
		) {
		alert("ATTENZIONE\n\nDevi inserire tutti i dati obbligatori (*) per poter proseguire");
		return false;
	}
	return true;
}

function gotoAnchor(elementAnchor) {
	var destination = 0;
	if (elementAnchor==undefined) {	} else {
		if ($('#'+elementAnchor).length>0) {destination = $("#"+elementAnchor).offset().top;}
	}
	$("html:not(:animated),body:not(:animated)").animate({ scrollTop:destination-150}, 500 );
} 


function ask(message) {
	if (message==undefined) 
		message = "sicuro?"; 

	var agree=confirm(message); 
	if (agree) return true; else return false;
}

function toggle (id) {
	if ($('#'+id).css('display') == "none") {
		$('#'+id).stop().slideDown(200);
	} else {
		$('#'+id).stop().slideUp(200);
	}
}

// ricerchiamo le città
function retrieve_cities() {
	var id_province = +$("select[name=id_provincia] option:selected").val();
	$.get(api_url+"retrieve_cities.php?id_province="+id_province,function(data) {
		$("select[name=id_citta]").html(data);
		codeAddress();
	});
}

// controlliamo 2 Date
function ctrl_date() {

	var t = "";
	t = $('#data_iniziale').val().split("/");
	var d1 = new Date(t[2],t[1],t[0]);
	t = $('#data_finale').val().split("/");
	var d2 = new Date(t[2],t[1],t[0]);
	
	if ((d2.getTime() - d1.getTime()) < 0 ) {
		alert("Errore: hai selezionato la data finale minore della data iniziale");
		return false;
	} else {
		return true;
	}
}

function DateDiff(date1, date2) {
    return date1.getTime() - date2.getTime();
}

function pad(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}

function loader() {
	$('#loader').remove();
	var s = "<div id='loader' class='round_br round_bl'>attendere ...</div>";
	$('body').append(s);
	$('#loader').fadeIn(200);
}

function create_url() {
	var tmp_url = "";
	var str = "";
	var _tmp = "";
	
	if (!ctrl_date($('#data_iniziale').val(), $('#data_finale').val())) { return false; }

	loader();
	
	
	if ($('#data_iniziale').val() != "") {
		str = jQuery.trim($('#data_iniziale').val());
		_tmp = str.split("/");
		tmp_url += "a-" + _tmp[2] + "-" + pad(_tmp[1],2) + "-" + _tmp[0] + "/";
	}
	if ($('#data_finale').val() != "") {
		str = jQuery.trim($('#data_finale').val());
		_tmp = str.split("/");
		tmp_url += "d-" + _tmp[2] + "-" + pad(_tmp[1],2) + "-" + _tmp[0] + "/";
	}

	if ($('#b_from').length > 0 && $('#b_from').val() != "") {
		_tmp = $('#b_from').val();
		tmp_url += "bfrom-" + _tmp + "/";
	}
	if ($('#b_to').length > 0 && $('#b_to').val() != "") {
		_tmp = $('#b_to').val();
		tmp_url += "bto-" + _tmp + "/";
	}
	if ($('#s_from').length > 0 && $('#s_from').val() != "") {
		_tmp = $('#s_from').val();
		tmp_url += "sfrom-" + _tmp + "/";
	}
	if ($('#s_to').length > 0 && $('#s_to').val() != "") {
		_tmp = $('#s_to').val();
		tmp_url += "sto-" + _tmp + "/";
	}
	if ($('#ba_from').length > 0 && $('#ba_from').val() != "") {
		_tmp = $('#ba_from').val();
		tmp_url += "bafrom-" + _tmp + "/";
	}
	if ($('#ba_to').length > 0 && $('#ba_to').val() != "") {
		_tmp = $('#ba_to').val();
		tmp_url += "bato-" + _tmp + "/";
	}
	if ($('#provincia').length > 0 && $('#provincia :selected').val() != "") {
		_tmp = $('#provincia :selected').val();
		tmp_url += "p-" + _tmp + "/";
	}

	if ($('#destination').val() != "") {
		_tmp = $('#destination').val();
		tmp_url += "k-" + _tmp + "/";
		//tmp_url += "k-" + encodeURIComponent(_tmp) + "/";
		
	}
	
	window.location.href = _url + tmp_url;
}

function ts_anim() {
	$('#ts_'+ts_current).fadeOut(200,function(){
		ts_current++;	
		if (ts_current > ts_total) ts_current = 1;
		if ($('#ts_'+ts_current).length > 0) {
			$('#ts_'+ts_current).fadeIn(200);
		}
	});	
}

function best(id) {
	var a = prompt ("Indica un voto da 1 a 5 per la strutture. 0 per 'no best'","0");
	if (a >= 0 && a <= 5)
		window.top.location = admin_url + "best/"+id+"/"+a;
}

$(document).ready(function(){

	$(".focus").focus(function () { 
		$(this).attr("old",$(this).val());
		$(this).val('');
	}).blur(function(){
		if ($(this).val() == "") $(this).val($(this).attr('old'));
	});

	
	if ($('#f_faq').length > 0) {
		$('#f_faq').submit(function(){
			
			var fields = $(this).serialize();
			loader();
			
			$('#invia').slideUp(200, function() {
				$.ajax({
					type: "POST",
					url: "/send-faq", // /?timestamp=" + new Date().getTime(),
					data: fields,
					success: function(msg){
						$('#loader').hide();
						if (msg.substr(0,6) == "ERROR:") {
							$('#invia').slideDown(200, function(){
								alert(msg);	
							});
						} else {
							alert(msg);	
						}
					}
				});			
			});
		});
	}
	
	if ($('#f_contatta').length > 0) {
		$('#f_contatta').submit(function(){
			
			var fields = $(this).serialize();
			loader();
			
			$('#invia').slideUp(200, function() {
				$.ajax({
					type: "POST",
					url: "/send-mail", // /?timestamp=" + new Date().getTime(),
					data: fields,
					success: function(msg){
						$('#loader').hide();
						if (msg.substr(0,6) == "ERROR:") {
							$('#invia').slideDown(200, function(){
								alert(msg);	
							});
						} else {
							alert(msg);	
						}
					}
				});			
			});
		});
	}

	if ($('#f_contatti').length > 0) {
		$('#f_contatti').submit(function(){
			
			var fields = $(this).serialize();
			loader();
			
			$('#invia').slideUp(200, function() {
				$.ajax({
					type: "POST",
					url: "/send-contatti", // /?timestamp=" + new Date().getTime(),
					data: fields,
					success: function(msg){
						$('#loader').hide();
						if (msg.substr(0,6) == "ERROR:") {
							$('#invia').slideDown(200, function(){
								alert(msg);	
							});
						} else {
							alert(msg);	
						}
					}
				});			
			});
		});
	}

	if ($('#f_signup').length > 0) {
		$('#f_signup').submit(function(){
			
			var fields = $(this).serialize();
			loader();
			
			$('#invia').fadeOut(200, function() {
				$.ajax({
					type: "POST",
					url: "/send-registrati",
					data: fields,
					success: function(msg){
						$('#loader').hide();
						if (msg.substr(0,6) == "ERROR:") {
							$('#invia').fadeIn(200, function(){
								alert(msg);	
							});
						} else {
							$('#registrazione').slideUp(200,function() {
								$('#signup-grazie').slideDown(200);
							});
						}
					}
				});			
			});
		});
	}


	if ($('#reset').length > 0) {
		$('#reset').click(function(){
			$('#data_iniziale').val('');
			$('#data_finale').val('');
			$('#destination').val('');
			$('#b_from').val('');
			$('#b_to').val('');
			$('#s_from').val('');
			$('#s_to').val('');
			$('#ba_from').val('');
			$('#ba_to').val('');
			$('#provincia').val('');
			
		});
	}
	
	if ($('.top').length > 0) {
		ts_current = 1;
		ts_total = $('.top').length;
		setInterval(ts_anim, 5000);

	}
	
	
	$('#destination').keypress(function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		 if(code == 13) { 
			create_url();
		 }
	})

	$('#ricerca-button').click(function() {
		create_url();
	});
	
	if ($("#slider").length > 0) {
		$("#slider").easySlider({
			auto: true, 
			continuous: true,
			speed:3000,
			pause: 2000,
			controlsShow:		true
		});	
	}
	
	if ($('#menu_mini_tab').length) {
		$('#menu_mini_tab a').click(function(){
			var tab = $(this).attr('tab');
			$('#menu_mini_tab a').removeClass('current');
			$(this).addClass('current');
			$('.layout_tab').fadeOut(500,function(){
				setTimeout ( function() {
					$('#layout_tab_'+tab).fadeIn(500);
				},500);
			});
			
		});
	}
	
	if ($('#data_iniziale').length > 0) $('#data_iniziale').simpleDatepicker();
	if ($('#data_finale').length > 0) $('#data_finale').simpleDatepicker();
	
    if ($('.gallery').length > 0) $('.gallery a:has(img)').lightbox();
	
	if ($('.editor').length > 0) {
		$('.editor').wysiwyg({
			rmUnusedControls: true,
			controls: {
				createLink: { visible : true },
				html: { visible : true },
				bold: { visible : true },
				italic: { visible : true },
				insertOrderedList: { visible : true },
				removeFormat: { visible : true }
			}
		});	
	}
	
});
