$(document).ready(function() {

    $('.rounded').corners("5px");

    $("a[rel^='prettyPhoto']").prettyPhoto();
    $("a[rel^='prettyPopin']").prettyPopin(
    {
	modal: true,
	loader_path: 'img/prettyPopin/loader.gif',
	width: 700,
	height: false,
	followScroll: false,
	opacity: 0.5,
	animationSpeed: 'fast'
    });


});


function setSelectedColor(color_id,label)
{

    $('.gallery img').removeClass("highlight");
    $('img#color_'+color_id).addClass("highlight");
    $('#selectedColorText').text(label);
    $('#selectedColor').val(color_id);
}

function toggleCommentForm()
{
    $('#comment-form').fadeIn();
}
function sendSelection()
{
    var email = $('#email').val();
    var validEmail = /^([0-9a-zA-Z]+(?:[._][0-9a-zA-Z]+)*)@([0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*\.[0-9a-zA-Z]{2,3})$/;
    var rega = new RegExp(validEmail);

    if (!rega.test(email))
    {
	alert("L'indirizzo E-Mail inserito non risulta valido.");
	return;
    }
    else
    {
	$.post("ajaxHandler.php",{
	    action: 'sendMail',
	    emailValue : email,
	    noteValue : $('#note').val(),
	    data: $('#resume').html()
	},
	function(response){
	    alert(response.log);
	},"json");



	$('#overlay').fadeOut('fast',function(){
	    $(this).remove();
	});
	$('.prettyPopin').fadeOut('fast',function(){
	    $(this).remove();
	});
	return;
    }

}

function triggerNext()
{
    var currIndex =	$('#currIndexSelected').val();
    eval($('.galleria li:eq('+currIndex+')').next().find('a').attr("href"));

}

function triggerLast()
{
    var currIndex =	$('#currIndexSelected').val();
    eval($('.galleria li:eq('+currIndex+')').prev().find('a').attr("href"));

}

function setCurrentPicture(label,path,currIndex)
{
    $('#mainPic').fadeOut("fast", function(){

	$('#caption').hide();
	$('#mainPic').attr("src","phpThumb/phpThumb.php?src=../coloriPics/"+path+"&w=500&h=400&far=1&bg=FAFAFA").load(function() {


	    $('#currIndexSelected').val(currIndex);
	    $('#mainPic').fadeIn("fast");
	    $('#caption').text(label).show();
	});

    });




}

function isEmailValid(email){
    var e = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return e.test(email);
}

function sendForm(){


    $("#imessageOK, #imessageERROR").hide();
    $(".required").removeClass("required");
    if($("#from_name").val()==""){
	$("#from_name").addClass("required");
	$("#imessageERROR").fadeIn();
	window.scroll(0,0);
	return false;
    }
    if($("#from_email").val()==""){
	$("#from_email").addClass("required");
	$("#imessageERROR").fadeIn();
	window.scroll(0,0);
	return false;
    }
    if(!isEmailValid($("#from_email").val())){
	$("#from_email").addClass("required");
	$("#imessageERROR").fadeIn();
	window.scroll(0,0);
	return false;
    }
    if($("#text").val()==""){
	$("#text").addClass("required");
	$("#imessageERROR").fadeIn();
	window.scroll(0,0);
	return false;
    }
    $("#Invia").val("Attendi...");



    var AddDetails;
    AddDetails="new=1";
    AddDetails+="&from_name="+encodeURIComponent($("#from_name").val());
    AddDetails+="&from_email="+encodeURIComponent($("#from_email").val());
    AddDetails+="&text="+encodeURIComponent($("#text").val());

    $.post( 'http://www.bomboniererbario.com/commenti.php', AddDetails,
	function(response) {
	    if (response.success)
	    {
		$("#imessageOK").fadeIn();

	    }
	    else
	    {
		$("#imessageKO").fadeIn();
	    }
	    $("#comment-form ul").remove();
	}, "json");
}
