var vech = new Array(464,174,330);
//  vech[0] = 464 : main
//  vech[1] = 174 : main_noflash
//	vech[2] = 330 : main_flash_small

PEPS = {};
PEPS.rollover =
{
	init: function()
	{
		this.preload();
      
		$(".ro").hover(
			function () { $(this).attr( 'src', PEPS.rollover.newimage($(this).attr('src')) ); }, 
			function () { $(this).attr( 'src', PEPS.rollover.oldimage($(this).attr('src')) ); }
		);
	},
	preload: function()
	{
		$(window).bind('load', function() {
			$('.ro').each( function( key, elm ) { $('<img>').attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) ); });
	});
   },
   newimage: function( src )
   { 
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_o' + src.match(/(\.[a-z]+)$/)[0]; 
   },
   oldimage: function( src )
   { 
      return src.replace(/_o\./, '.'); 
   }
};

function pos_foo(pVal)
{
	var hw = $(window).height();
	var hc = $("#home_container").height()+pVal;
	var hf = $("#tgt_footer").height();

/*alert("win: "+hw + " # tgt:" + hc +  " # foo:" + hf + " # val:"+pVal + " #1:"+  hc);
$("#tgt_footer").css("margin-top",(hc)+"px");
return;*/

	if(hc >= hw-hf){
		$("#tgt_footer").css("margin-top",(hc)+"px");
//		alert("win: "+hw + " # tgt:" + hc +  " # foo:" + hf + " # val:"+pVal + " #1:"+  hc);
	}
	else{
		$("#tgt_footer").css("margin-top",(hw-hf)+"px");
//		alert("win: "+hw + " # tgt:" + hc +  " # foo:" + hf + " # val:"+pVal + " #2:"+  (hw-hf));
	}
}

function init_main(pPos)
{
	PEPS.rollover.init();
	$(window).resize(function(){pos_foo(vech[pPos]);});
	$("body").append("<div style='position:fixed; width:760px; height:410px; left:50%; top:50%; margin-left:-380px; margin-top:-205px; z-index:78;' id='tgt_info'></div>");
	$("#tgt_info").fadeOut(0);
	pos_foo(vech[pPos]);
}

function gallery(pVal)
{
	$.post('lib/vb/gallery.asp',{id:pVal},gallery_result,"json");
}

var galw = 0;
var vecimg = new Array();
function gallery_result(data)
{
	$("#gallery_list").html("");
	for (var k in data)
	{
		galw+=124;
		$("#gallery_list").append("<div id='im-"+k+"' class='tgtimggal'></div>");
		$("#gallery_list").width(galw);
		
		vecimg.push({ims:"/public/"+getSmall(data[k]["img"]),imb:"/public/"+data[k]["img"]});
	}
	
	if(vecimg.length<=6)
	{
		$("#fr_dx").hide();
		$("#fr_sx").hide();
	}
	
	load_img(0);
}

function getSmall(pVal)
{
	var ls = pVal.lastIndexOf("/");
	var sm = pVal.substring(0,ls+1) + "thumb_" + pVal.substring(ls+1,pVal.length)
	return sm
}

function load_img(ix)
{
	if(ix==0){obig(vecimg[ix].imb);}
	var img = new Image();
	$(img).css("cursor","pointer");
	$(img).load(function () {
		//$(this).hide();
		$("#im-"+ix).html(this);
		//$(this).fadeIn(300);
		if(ix<vecimg.length-1)
			load_img(ix+1);
			
	}).error(function () {
		// notify the user that the image could not be loaded
	}).attr("src",vecimg[ix].ims).click(function(e){obig(vecimg[ix].imb)});	//alert(vecimg[ix].imb); 
}
function obig(pVal)
{
	$("#img_big").attr("src",pVal)
}

var curleft=0;
var wbox=124+5;
function gal_np(va)
{
	if(va==1)
	{
		var a = $("#gallery_list div:last");
		var path = a.find("img").attr("src");
		var pid = a.attr("id");

		$("#gallery_list div:last").remove();
		$("#gallery_list").prepend(a);
		a.find("img").click(function(e){obig(path.replace("thumb_",""))});
		
		var mo=$("#gallery_list").offset();
		mo.top=0; mo.left=curleft-wbox;
		$("#gallery_list").css(mo);
		curleft=mo.left+wbox;
		var mo=$("#gallery_list").offset();
		mo.top=0; mo.left=curleft;
		$("#gallery_list").stop().animate(mo);
	}
	else if(va==2)
	{
		curleft-=wbox;
		var mo=$("#gallery_list").offset();
		mo.top=0; mo.left=curleft;
		$("#gallery_list").stop().animate(mo, function(){
			var a = $("#gallery_list div:first");
			var path = a.find("img").attr("src");
			var pid = a.attr("id");

			$("#gallery_list div:first").remove();
			$("#gallery_list").append(a);
			a.find("img").click(function(e){obig(path.replace("thumb_",""))});
			
			var mo=$("#gallery_list").offset();
			mo.top=0; mo.left=curleft+wbox;
			$("#gallery_list").css(mo);
			curleft=0;
		});
	}
}

function pan_info(pVal)
{
	$.post('comp/pan_info.asp',{id:pVal},function(data)
	{
		$("#tgt_info").html(data);
		$("#tgt_close").css("cursor","pointer");
		$("#tgt_close").click(function(){$("#tgt_info").fadeOut(function(){$("#tgt_info").html("");});});
		$("#tgt_info").fadeIn();
	});
	
}


// -------------------------- NEWSLETTER -------------------------- \\
function init_newsletter()
{
	$('#email_newsletter').keydown(function(event){if(event.keyCode=='13'){join_newsletter();}});
	$('#email_newsletter').focusin(function(){if($('#email_newsletter').val()=="la tua e-mail"){$('#email_newsletter').val("");}});
	$('#email_newsletter').focusout(function(){if($('#email_newsletter').val()==""){$('#email_newsletter').val("la tua e-mail");}});
	
}
function join_newsletter()
{
	if($('#email_newsletter').val()=="" || !validateEmail($('#email_newsletter').val()))
	{
		tinyAlert("Inserisci un indirizzo e-mail corretto!");
		$('#email_newsletter').focus();
	}
	else
	{
		if($("#email_check").attr("checked"))
			$.post("comp/joinnewsletter.asp",{email:$('#email_newsletter').val()},join_newsletter_result);
		else
			tinyAlert("Devi accettare le condizioni di utilizzo.");
	}
}
function join_newsletter_result(data)
{
	if(data==0)			//OK
		tinyAlert("Grazie per esserti registrato!");
	else if(data==1)	//Error generic
		tinyAlert("Si è verificato un errore");
	else if(data==2)	//Email presente
		tinyAlert("Questo indirizzo risulta già registrato");
		
	$('#email_newsletter').val("");
}
function tinyAlert(pMsg)
{
	$("#welc_txt").html(pMsg);
	$(this).stopTime();
	$(this).oneTime(6000, function(){
		$("#welc_txt").html("");
	});
}
function validateEmail(pEmail)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return reg.test(pEmail);
}
// -------------------------- NEWSLETTER -------------------------- \\

var errgb="#ffe4cf";
function sendmsg()
{
	$(".jq-msg").css("background","#f3fbfe");
	var err=0;
	if($("#nome").val()=="")
	{
		$("#nome").css("background",errgb);
		err++;
	}
	if($("#cognome").val()=="")
	{
		$("#cognome").css("background",errgb);
		err++;
	}
	if($("#email").val()==""||!validateEmail($("#email").val()))
	{
		$("#email").css("background",errgb);
		err++;
	}
	if($("#messaggio").val()=="")
	{
		$("#messaggio").css("background",errgb);
		err++;
	}
	
	if(err==0)
	{
		if($("#act_msg").attr("checked"))
		{
			$.post("comp/contact.asp",{nome:$('#nome').val(),
									   cognome:$('#cognome').val(),
									   email:$('#email').val(),
									   telefono:$('#telefono').val(),
									   messaggio:$('#messaggio').val()},function(data){
											if(data==0)
											{
												$("#welc_txt").html("Messaggio inviato correttamente!");
												$('#nome').val("");
												$('#cognome').val("");
												$('#email').val("");
												$('#telefono').val("");
												$('#messaggio').val("");
												$("#act_msg").attr("checked",false);
											}
											else
											{
												$("#welc_txt").html("Si è verificato un errore durante l'invio del messaggio!");
											}
									   });
		}
		else
		{
			$("#welc_txt").html("Devi accettare le condizioni di utilizzo!");
		}
	}
	else
	{
		$("#welc_txt").html("Compila tutti campi evidenziati!");
	}
}


function catering()
{
	$(".jq-cate").css("background","#f3fbfe");
	var err=0;
	if($("#nome").val()=="")
	{
		$("#nome").css("background",errgb);
		err++;
	}
	if($("#cognome").val()=="")
	{
		$("#cognome").css("background",errgb);
		err++;
	}
	if($("#telefono").val()=="")
	{
		$("#telefono").css("background",errgb);
		err++;
	}
	if($("#prov").val()=="")
	{
		$("#prov").css("background",errgb);
		err++;
	}
	if($("#citta").val()=="")
	{
		$("#citta").css("background",errgb);
		err++;
	}
	if($("#email").val()==""||!validateEmail($("#email").val()))
	{
		$("#email").css("background",errgb);
		err++;
	}
	if($("#evento").val()=="")
	{
		$("#evento").css("background",errgb);
		err++;
	}
	if($("#persone").val()=="")
	{
		$("#persone").css("background",errgb);
		err++;
	}
/*	if($("input[name='catering']:checked").val()=="undefined"||$("input[name='catering']:checked").val()==undefined)
	{

	}*/

	if(err==0)
	{
		if($("#accet").attr("checked"))
		{
			$.post("comp/catering.asp",{
				nome:$("#nome").val(),
				cognome:$("#cognome").val(),
				telefono:$("#telefono").val(),
				provincia:$("#prov").val(),
				citta:$("#citta").val(),
				email:$("#email").val(),
				evento:$("#evento").val(),
				persone:$("#persone").val(),
				catering:$("input[name='catering']:checked").val(),
				richieste:$("#richieste").val()},function(data){
				if(data==0)
				{
					$("#welc_txt").html("Richiesta inviata correttamente!");
					$("#nome").val("");
					$("#cognome").val("");
					$("#telefono").val("");
					$("#prov").val("");
					$("#citta").val("");
					$("#email").val("");
					$("#evento").val("");
					$("#persone").val("");
					$("#catering").attr("checked",false);
					$("#richieste").val("");
					$("#accet").attr("checked",false);
				}
				else
				{
					$("#welc_txt").html("Si è verificato un errore durante l'invio del messaggio!");
				}
		   });
		}
		else
		{
			$("#welc_txt").html("Devi accettare le condizioni di utilizzo!");
		}
	}
	else
	{
		$("#welc_txt").html("Compila tutti campi evidenziati!");
	}
}

function nphoto(pval)
{
	var imlast = "";
	var imlnex = "";
	for(var t=0; vecimg.length; t++)
	{
		if(vecimg[t].imb==$("#img_big").attr("src"))
		{
			//alert(glen(t-1) + " - " + glen(t+1))
			imlast=vecimg[glen(t-1)].imb
			imlnex=vecimg[glen(t+1)].imb;
			break;
		}
	}
	if(pval==1)
		obig(imlast);
	else if(pval==2)
		obig(imlnex);
}
function glen(val)
{
	if(val<0)
		return vecimg.length-1;
	else if(val>=vecimg.length)
		return 0;
	else
		return val;
}

