// JavaScript Document

var is_open = false;

$(document).ready(
	function()
	{
		form_init();
		flashembed('flashcont',{src: "images/alchemia web_outfinal2.swf", wmode: 'opaque'});
    }
);

function form_init() {
    $('ul.buttcont li a').click(
        function() {
            var hrefstr = $(this).attr('href');
            if (!is_open) {
                is_open = true;
                showFormDesc(hrefstr);
	            slideBottom();
	            toggleForm();
		  	} else {
		  	    $("#data").animate(
			    	{height:'toggle'},
			    	1000 * 0.5,
			    	null,
					function() {
					    showFormDesc(hrefstr);
					    slideBottom();
					    toggleForm();
					}
			  	);
			}
			
			$('#data input[name=mtype]').val(hrefstr);
			
            return false;
        }
    );
    
    $("#data").toggle();
    
    $('#data a.send').click(
        function() {
            check_form();

            return false;
        }
    );
    
    $("a.privacy").fancybox({
		'scrolling'		: 'yes',
		'titleShow'		: false,
		'autoDimensions': false,
		'width'         : 500,
		'height'        : 500,
		'onClosed'		: function() {
		}
	});
	
	$("div.details ul li a").fancybox({
		'speedIn'		:	500,
		'speedOut'		:	500,
		'titlePosition' :	'over',
		'overlayColor'  :   '#000',
		'padding'       :   0,
		'transitionIn'	:	'elastic',
		'transitionOut' :   'fade',
		'easingIn'		:	jQuery.easing.def = 'easeOutQuart',
		'easingOut'     :   jQuery.easing.def = 'easeOutQuart'
	});
	
	//$("#discounttip").hide();
}

function check_form() {
	var msg = '';
	if ($("#data input[name='uname']").val() == '') {
	    msg += '- il campo Nome deve essere compilato \n';
	}
	if ($("#data input[name='usurname']").val() == '') {
	    msg += '- il campo Cognome deve essere compilato \n';
	}
	if ($("#data input[name='uemail']").val() == '') {
	    msg += '- il campo Email deve essere compilato \n';
	}
	if ($("#data input[name='uphone']").val() != '' && !exp_phone.test($("#data input[name='uphone']").val())) {
	    msg += '- il campo Telefono può contenere solo numeri e deve essere almeno di 6 cifre\n';
	}
	if ($("#data input[name='privacy']:checked").length == 0) {
	    msg += '- è necessario acconsentire alla normativa sulla Privacy \n';
	}
	
	if(msg == '') {
	    var inputs = $("#data select, #data input[type=radio]:checked, #data input[type=text], #data textarea, #data input[type=hidden]");

		var qstring = ''
		for (i=0; i<inputs.length; i++) {
		    //debstr += '[' + inputs[i].name + ' - ' + inputs[i].value + ']' +'\n';
		    qstring += '&' + inputs[i].name + '=' + inputs[i].value;
		}

		//alert(debstr);
		$('#data a.send').hide();

		var phpstring = 'action=sendemail' + qstring;
	    $.ajax({
	        type: "POST",
	        url: "aj_dialer.php",
	        dataType: 'json',
	        data: phpstring,
	        //Evento di riuscita
	        success: function(ret) {
				if (ret.msg_type == 'ok') {
				    alert('Invio avvenuto con successo');
				    $("#data")[0].reset();
				} else if (ret.msg_type == 'erremail') {
				    alert('Il campo Email non è corretto');
				} else if (ret.msg_type == 'errsend') {
				    alert('Si è verificato un errore durante l\'invio del messaggio, si prega di riprovare');
				}
				
				$('#data a.send').show();
	        }
	    });
	} else {
	    alert(msg);
	}
}

function toggleForm() {
	$("#data").animate(
		{height:'toggle'},
		1000 * 1,
		null,
		function() {}
	);
}
function showFormDesc(hrefstr) {
	if (hrefstr == '#buy') {
	    $('#data p.buy-txt').show();
	    $('#data p.newsl-txt').hide();
	} else if (hrefstr == '#newsl') {
		$('#data p.buy-txt').hide();
	    $('#data p.newsl-txt').show();
	}
}
function slideBottom() {
	var obj = $('#bottom')[0];
    var targetOffset = $(obj).offset().top;
	$('html,body').animate(
		{scrollTop: targetOffset},
		1000,
		null //jQuery.easing.def = 'easeInOutQuart'
	);
}
