/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options.expires=-1;}var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000));}else{date=options.expires;}expires='; expires='+date.toUTCString();}var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break;}}}return cookieValue;}};



/* <![CDATA[ */
$(document).ready(function(){
	//$("#invia").click(function(){form_contattaci(); return false;});
	
	$("#richiediinfoadv").click(function(){
		if($(".form_div:first").length)
		{
			$(".form_div:first").toggle(); return false;
		}
		else
		{
			window.location = "http://www.shiatsuirte.it/contatti/"; return false;
		}
	});
	
	$("#richiediinfo").click(function(){
		window.location = "http://www.shiatsuirte.it/contatti/"; return false;
	});
	
	$(".form_info_span").each(function(){
		$(this).click(function(){
			$("#form_"+$(this).attr('id')).slideToggle();
			$("#"+$(this).attr('id')+"-nome").focus();
			return false;
		});
	});
	
	if($.cookie('regione') == null || $.cookie('regione').length < 1)
	{
		$.ajax({
			url: '/geot.php',
			success: function(data) {
				//$('#regione').html(data);
				$.cookie('regione', data, { expires: 1000, path: '/'});
			}
		});
	}
	
	$('#searchcitta').focus(function(){
			if($(this).val() == 'Cerca città') $(this).val(''); return false;
		});
	$('#searchcitta').blur(function(){ if($(this).val() == '') $(this).val('Cerca città'); $('#search img').hide(); return false; });
	
	
	var cache = {};
	$("#searchcitta").autocomplete({
		minLength: 2,
		source: function(request, response) {
			if ( request.term in cache ) {
				response( cache[ request.term ] );
				return;
			}

			$.ajax({
				url: "/pannello/admin/search/city/"+request.term,
				dataType: "json",
				beforeSend: function(){
					$('#search img').show();
					$('#searchcitta').css('top','0px');
				},
				success: function( data ) {
					cache[ request.term ] = data;
					response( data );
					$('.ui-autocomplete').css('width','200px');
					$('#search img').hide();
					$('#searchcitta').css('top','4px');
				}
			});
		},
		
		select: function(event, ui) {
			window.location = "http://www.shiatsuirte.it/corsi/"+ui.item.value;
			return false;
		}
	});
	
	
	$('#infostext').animate({height:'40px'},1000).css('color','#333');
	$('#infosemail').fadeIn(1000);
	$('#infosnome').fadeIn(2500);
	$('#infosinvia').fadeIn(5000);
	
	$('#infostext').focus(function(){
		if($(this).val() == 'Richiedi informazioni') $(this).val('');
		//$(this).animate({height:'40px'},1000).css('color','#333');
		//$('#infosemail').fadeIn(1000);
		//$('#infosnome').fadeIn(2500);
		//$('#infosinvia').fadeIn(5000);
		return false;
	});
	$('#infostext').blur(function(){
		if($(this).val() == '')
		{
			$(this).val('Richiedi informazioni');
		}
		return false;
	});
	
	$('#infosemail').focus(function(){
			$(this).css('color','#333');
			if($(this).val() == 'Inserisci la tua email') $(this).val('');
			return false;
	});
	$('#infosemail').blur(function(){ if($(this).val() == '') $(this).val('Inserisci la tua email'); return false; });

	$('#infosnome').focus(function(){
			$(this).css('color','#333');
			if($(this).val() == 'Inserisci il tuo nome') $(this).val('');
			return false;
	});
	$('#infosnome').blur(function(){ if($(this).val() == '') $(this).val('Inserisci il tuo nome'); return false; });
	
	
	
	
	$('h2').each(function(){
		var patt=/professiona/i;
		var str = $(this).text();
		if(patt.test(str))
		{
			$('#formativi').show(); return false;
		}
		else
		{
			$('#formativi').hide();
		}
	});
	
	$('<fb:like layout="standard" show_faces="true" width="290" action="like" colorscheme="light"></fb:like>').appendTo('#facebook_like');
});


function form_contattaci() {
	adv = encodeURIComponent($('#adv').val());
	advsrc = encodeURIComponent($('#advsrc').val());
	nome = encodeURIComponent($('#nome').val());
	cognome = encodeURIComponent($('#cognome').val());
	email = encodeURIComponent($('#email').val());
	citta = encodeURIComponent($('#citta').val());
	note = encodeURIComponentonent($('#note').val());
	telefono = encodeURIComponentonent($('#telefono').val());

	url = ''
	+'adv='+adv
	+'&advsrc='+advsrc
	+'&nome='+nome
	+'&cognome='+cognome
	+'&email='+email
	+'&citta='+citta
	+'&note='+note
	+'&telefono='+telefono;

	$.ajax({
		type: "POST",
		url: "/wp-content/themes/overeasy/ajax.php",
		data: "action=contact&"+url,
		beforeSend: function(){
			$('#error').css('display','block').empty().html('<img src="/wp-content/themes/overeasy/images/loading.gif" style="border:0px;" />');
			$('#invia').attr('disabled','true');
		},
		success: function(result){
			$('#invia').removeAttr('disabled');
			$('#error').css('display','block').empty().html(result);
		},
		error: function() {
			$('#invia').removeAttr('disabled');
			$('#error').css('display','block').empty().html('Connection Error');
			setTimeout("$('#error').css('display','none').empty();",5000);
		}
	});
return false;
}

function form_corso(slug) {
	adv = encodeURIComponent($('#'+slug+'-adv').val());
	advsrc = encodeURIComponent($('#'+slug+'-advsrc').val());
	slug = encodeURIComponent($('#'+slug+'-slug').val());
	nome = encodeURIComponent($('#'+slug+'-nome').val());
	cognome = encodeURIComponent($('#'+slug+'-cognome').val());
	email = encodeURIComponent($('#'+slug+'-email').val());
	citta = encodeURIComponent($('#'+slug+'-citta').val());
	note = encodeURIComponent($('#'+slug+'-note').val());
	telefono = encodeURIComponent($('#'+slug+'-telefono').val());

	url = ''
	+'adv='+adv
	+'&advsrc='+advsrc
	+'&slug='+slug
	+'&nome='+nome
	+'&cognome='+cognome
	+'&email='+email
	+'&citta='+citta
	+'&note='+note
	+'&telefono='+telefono;

	$.ajax({
		type: "POST",
		url: "/wp-content/themes/overeasy/ajax.php",
		data: "action=formcorsi&"+url,
		beforeSend: function(){
			$('#'+slug+'-error').css('display','block').empty().html('<img src="/wp-content/themes/overeasy/images/loading.gif" style="border:0px;" />');
			$('#'+slug+'-invia').attr('disabled','true');
		},
		success: function(result){
			//$('#'+slug+'-invia').removeAttr('disabled');
			$('#'+slug+'-error').css('display','block').empty().html(result);
		},
		error: function() {
			//$('#'+slug+'-invia').removeAttr('disabled');
			$('#'+slug+'-error').css('display','block').empty().html('Connection Error');
		}
	});
return false;
}

function form_infos()
{
	email = $('#infosemail').val();
	reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email) == false) {
		$('#infos_error').empty().html('<span class="errore">Inserisci un indirizzo di posta elettronica al quale potremmo risponderti.</span>');
		return false;
	}
	
	note = $('#infostext').val();
	if(note == 'Richiedi informazioni' || note.length < 5 ){
		$('#infos_error').empty().html('<span class="errore">Indicaci ciò di cui hai bisogno.</span>');
		return false;
	}
	
	nome = $('#infosnome').val();
	email = $('#infosemail').val();
	url = $('#infosurl').val();
	note = $('#infostext').val();
	telefono = $('#infostelefono').val();
	
	$.ajax({
		type: "POST",
		url: "/wp-content/themes/overeasy/ajax.php",
		data: 	"action=sidebarcorsi"+"&"+
				"nome="+nome+"&"+
				"email="+email+"&"+
				"url="+url+"&"+
				"note="+note+"&"+
				"telefono="+telefono,
		beforeSend: function(){
			$('#infosinvia').attr('disabled','true');
			$('#contattaci_error').empty().html('<img src="/wp-content/themes/overeasy/images/loading.gif" style="position:relative; top:4px; width:16px; height:16px;" />');
		},
		success: function(result){
			if(result)
			{
				$('#infoscorsi').empty().html('<h4>La richiesta è stata correttamente inoltrata, La ringraziamo. Le risponderemo nel più breve tempo possibile.</h4>');
				$('#infoscorsi').css('backgroundColor', 'white').css('padding','5px');
				return false;
			}
			else
			{
				$('#infos_error').empty().html('<span class="errore">Errore: ' + result + '.</span>');
				$('#infosinvia').removeAttr('disabled');
				return false;
			}
		},
		error: function() {
			$('#infos_error').empty().html('Connection error');
			$('#infosinvia').removeAttr('disabled');
		}
	});
	return false;
}

/*
function varDump(variable, maxDeep)
{
    var deep = 0;
    var maxDeep = maxDeep || 0;

    function fetch(object, parent)
    {
        var buffer = '';
        deep++;

        for (var i in object) {
            if (parent) {
                objectPath = parent + '.' + i;
            } else {
                objectPath = i;
            }

            buffer += objectPath + ' (' + typeof object[i] + ')';

            if (typeof object[i] == 'object') {
                buffer += "\n";
                if (deep < maxDeep) {
                    buffer += fetch(object[i], objectPath);
                }
            } else if (typeof object[i] == 'function') {
                buffer += "\n";
            } else if (typeof object[i] == 'string') {
                buffer += ': "' + object[i] + "\"\n";
            } else {
                buffer += ': ' + object[i] + "\n";
            }
        }

        deep--;
        return buffer;
    }

    if (typeof variable == 'object') {
        return fetch(variable);
    }

    return '(' + typeof variable + '): ' + variable + "\n";
}
*/

/* ]]> */
