$(function() {
	$('#frm-quick-login #email').val('e-mail');
	$('#frm-quick-login #password').val('password');
	$('#frm-quick-login #email').focus(function(){
		$(this).val('');
		$(this).css({color: '#000'});
	});
	
	$('#frm-quick-login #email').blur(function(){
		if ($(this).val() == '') {
			$(this).val('e-mail');
			$(this).css({color: '#AEAEAE'});
		}
	});
	
	$('#frm-quick-login #password').focus(function(){
		$(this).val('');
		$(this).css({color: '#000'});
	});
	
	$('#frm-quick-login #password').blur(function(){
		if ($(this).val() == '') {
			$(this).val('password');
			$(this).css({color: '#AEAEAE'});
		}
	});
	
	//----------------------
		$('#frm-pet-quick-login #email').val('e-mail');
	$('#frm-pet-quick-login #password').val('password');
	$('#frm-pet-quick-login #email').focus(function(){
		$(this).val('');
		$(this).css({color: '#000'});
	});
	
	$('#frm-pet-quick-login #email').blur(function(){
		if ($(this).val() == '') {
			$(this).val('e-mail');
			$(this).css({color: '#AEAEAE'});
		}
	});
	
	$('#frm-pet-quick-login #password').focus(function(){
		$(this).val('');
		$(this).css({color: '#000'});
	});
	
	$('#frm-pet-quick-login #password').blur(function(){
		if ($(this).val() == '') {
			$(this).val('password');
			$(this).css({color: '#AEAEAE'});
		}
	});
	//===================
	
	$('#frm-quick-login').submit(function () { 
		$('#utente').mask();
		$.post($(this).attr('action'), $('#frm-quick-login').serializeArray(), function( data ){
			if (typeof(data.error) == 'undefined' && data.redirect) {
				deactive_tiptip($('#email'), "email");
				window.location.reload();
			} else {
				active_tiptip($('#email'), "email", data.error);
			}
			$('#utente').unmask();
		}, 'json');
		return false; 
	});
	
	$('#utente .accedi-btn .crea-btn').click(function() {
		$('#frm-quick-login').submit();
		return false;
	});
	
	$('#pet-log-buttons #pet-login_reg .pet-accedi-btn .crea-btn').click(function() {
		$('#frm-pet-quick-login').submit();
		return false;
	});
	
	$('#frm-pet-quick-login').submit(function () { 
		//$('#petition-login-form').mask();
		$.post($(this).attr('action'), $('#frm-pet-quick-login').serializeArray(), function( data ){
			if (typeof(data.error) == 'undefined' && data.redirect) {
				$('#frm-pet-quick-login #email').tipTip({defaultPosition: 'top', content: ''});
				window.location.reload();
			} else {
				$('#frm-pet-quick-login #email').tipTip({defaultPosition: 'top',keepAlive:true, edgeOffset: 10, content: data.error, delay:200});
			}
			//$('#petition-login-form').unmask();
		}, 'json');
		return false; 
	});
	
	/* Quick Search */
	$("#frmQuickSearch").submit(function () {
		deactive_tiptip();
		var action = $(this).attr('action');
		var search = strip_tags($('#quickSearchWord').val());
		if(search.length < 3){
			/*$('#quickSearchWord').tipTip({defaultPosition: 'top',keepAlive:true, edgeOffset: 10, delay:50, content:"Devi inserire almeno 3 caratteri per la ricerca"});*/
			active_tiptip($('#quickSearchWord'), "quickSearchWord", "Devi inserire almeno 3 caratteri per la ricerca");
			setTimeout('deactive_tiptip($("#quickSearchWord"), "quickSearchWord");', 4500);
			return false;
		}
		if (search != quickSearchFormDefText) {
			$(this).attr('action',action+'/'+search);
		} else {
			$('#quickSearchWord').focus();
			return false;
		}
		//return false; 
	});
	$('#quickSearchWord').focus(function(){
		$(this).val('');
		$(this).css({color: '#000'});
	});

	$('#quickSearchWord').blur(function(){
		if ($(this).val() == '') {
			$(this).css({color: '#333'});
			$(this).val(quickSearchFormDefText);
		}
	});
	
	$('#show-fb-reg-form').click(function(){
		$("#fb-reg-form").lightbox_me();
	});	

	
	
	// if ($('#word').length > 0){
		// var searchedWord = $('#word').val();
		// if(searchedWord !== ''){
			// $('#quickSearchWord').val(searchedWord);
		// }
	// }
	
	/* End Quick Search */
	$(window).load(function() {
		$('#sign-petition #email_user, #sign-petition #password_user, #sign-petition textarea').val('');
		$('#frmCreatePetition input[type!=button], #frmCreatePetition textarea').val('');
	})
	
	
	/**
	 * rigth panel
	 * pdf locked for unlogged
	 */
	$('#pdf-button').click(function(){
		if(notLogged == true){
			$('html, body').animate({scrollTop: $('#pet-login_reg').offset().top - 5}, 700);
			active_tiptip($('#pdf-button'), 'pdf-button', loginForPdfAccess);
			setTimeout(function() {deactive_tiptip($('#pdf-button'), 'pdf-button');}, 3000);

			return false;
		}
	});
			
		$("#contatti-button").toggle(function () {
			$("#cs-main").show("slide", {}, 300);
                        $("#contact-arrow").hide();
		},
		function () {
			$("#cs-main").hide("slide", {}, 300);
                        $("#contact-arrow").show();
		});		
			
        });
var active_tiptip = function(elem, elId, text) {
	
	var org_title = text;
	var org_elem = elem;
	var timeout = 0;
	//var elId = elem.attr('id');
	
	if($(".tiptip_holder." + elId).length <= 0){
		var tiptip_holder = $('<div class="tiptip_holder ' + elId + '" style="max-width:200px;"></div>');
		var tiptip_content = $('<div class="tiptip_content ' + elId + '"></div>');
		var tiptip_arrow = $('<div class="tiptip_arrow ' + elId + '"></div>');
		$("body").append(tiptip_holder.html(tiptip_content).prepend(tiptip_arrow.html('<div class="tiptip_arrow_inner"></div>')));
	} else {
		var tiptip_holder = $(".tiptip_holder." + elId);
		var tiptip_content = $(".tiptip_content." + elId);
		var tiptip_arrow = $(".tiptip_arrow." + elId);
	}
	tiptip_content.html(org_title);
	tiptip_holder.hide()/*.removeAttr("class")*/.css("margin","0");
	tiptip_arrow.removeAttr("style");
	
	var top = parseInt(org_elem.offset()['top']);
	var left = parseInt(org_elem.offset()['left']);
	var org_width = parseInt(org_elem.outerWidth());
	var org_height = parseInt(org_elem.outerHeight());
	var tip_w = tiptip_holder.outerWidth();
	var tip_h = tiptip_holder.outerHeight();
	var w_compare = Math.round((org_width - tip_w) / 2);
	var h_compare = Math.round((org_height - tip_h) / 2);
	var marg_left = Math.round(left + w_compare);
	var marg_top = Math.round(top + org_height);
	var t_class = "";
	var arrow_top = "";
	var arrow_left = Math.round(tip_w - 12) / 2;
	
	t_class = "_top";
	
	var right_compare = (w_compare + left) < parseInt($(window).scrollLeft());
	var left_compare = (tip_w + left) > parseInt($(window).width());
	
	if((right_compare && w_compare < 0) || (t_class == "_right" && !left_compare) || (t_class == "_left" && left < (tip_w + 5))){
		t_class = "_right";
		arrow_top = Math.round(tip_h - 8) / 2;
		arrow_left = -12;
		marg_left = Math.round(left + org_width);
		marg_top = Math.round(top + h_compare);
	} else if((left_compare && w_compare < 0) || (t_class == "_left" && !right_compare)){
		t_class = "_left";
		arrow_top = Math.round(tip_h - 8) / 2;
		arrow_left =  Math.round(tip_w);
		marg_left = Math.round(left - (tip_w + 5));
		marg_top = Math.round(top + h_compare);
	}

	var top_compare = (top + org_height + tip_h + 8) > parseInt($(window).height() + $(window).scrollTop());
	var bottom_compare = ((top + org_height) - (tip_h + 8)) < 0;
	
	if(top_compare || (t_class == "_bottom" && top_compare) || (t_class == "_top" && !bottom_compare)){
		if(t_class == "_top" || t_class == "_bottom"){
			t_class = "_top";
		} else {
			t_class = t_class+"_top";
		}
		arrow_top = tip_h;
		marg_top = Math.round(top - (tip_h + 5));
	} else if(bottom_compare | (t_class == "_top" && bottom_compare) || (t_class == "_bottom" && !top_compare)){
		if(t_class == "_top" || t_class == "_bottom"){
			t_class = "_bottom";
		} else {
			t_class = t_class+"_bottom";
		}
		arrow_top = -12;
		marg_top = Math.round(top + org_height);
	}

	if(t_class == "_right_top" || t_class == "_left_top"){
		marg_top = marg_top + 5;
	} else if(t_class == "_right_bottom" || t_class == "_left_bottom"){
		marg_top = marg_top - 5;
	}
	if(t_class == "_left_top" || t_class == "_left_bottom"){
		marg_left = marg_left + 5;
	}
	tiptip_arrow.css({"margin-left": arrow_left+"px", "margin-top": arrow_top+"px"});
	tiptip_holder.css({"margin-left": marg_left+"px", "margin-top": marg_top+"px"}).addClass("tip"+t_class);
	
	//if (timeout){ clearTimeout(timeout); }
	//timeout = setTimeout(function(){ tiptip_holder.stop(true,true).fadeIn(); }, 200);
	tiptip_holder.stop(true,true).fadeIn();
};

var deactive_tiptip = function(elem, elId) {
	//var elId = elem.attr('id');
	if ($(".tiptip_holder." + elId).length >= 1) {
		var tiptip_holder = $(".tiptip_holder." + elId);
		//if (timeout){ clearTimeout(timeout); }
		tiptip_holder.hide();
	}
};

function isValidZipXhr(zip, callback){
    var result = false;
    $.ajax({
        url: '/petition/zipcode',
        type: 'POST',
        timeout: 3000,
        dataType: 'json',
        async: false,
        cache: false,
        data: {
            'zip': zip
        },
        success: function(data){
            result = callback(data);
        },
        error: function(){
            alert('Error!');
        }
    });
    return result;
}

function zipCallBack(data){
    if ((typeof data.zip === 'number')) {
        return true;
    }
    else {
        return false;
    }
}

function emailValidation(email){
    var result = false;
    $.ajax({
        url: '/petition/emailuser',
        type: 'POST',
        timeout: 3000,
        dataType: 'json',
        async: false,
        cache: false,
        data: {
            'email': email
        },
        success: function(data){
            if (data.status == null) 
                result = true;
        },
        error: function(){
            alert('Error!');
        }
    });
    return result;
}


