$(document).ready(	
function()
{
	/* Menu */
	$('#navigator li').hover(  
		function () { $('ul', this).slideDown(100);  if ($('ul', this).length>0) $(this).addClass("open") },   
		function () { $('ul', this).slideUp(100);	 $(this).removeClass("open") });
	
	/* Styled SELECT */
	$('select').not(".nostyle").selectbox();
	
	/* Styled CHECBOXES */
	$('.checkbox').customInput();

	/* Search form */
	$("#where").autocomplete(baseurl+"comuni");
	
	/* PopUp */
	$('#popup-mask').click(function () { closePopUp(); });
	$('#popup #chiudi').click(function () { closePopUp(); });
	
	$('INPUT.text, TEXTAREA.text').focus( function() { $("LABEL", $(this).parent()).fadeOut("fast"); });
	$('INPUT.text, TEXTAREA.text').blur( function() { if($(this).val()=="") { $("LABEL", $(this).parent()).fadeIn("fast");} });
	$('INPUT.text, TEXTAREA.text').each( function() { if($(this).val()!="") { $("LABEL", $(this).parent()).fadeOut("fast");} });
	
	/* Visore homepage */
	if ($("#visore").length > 0)
	{
		position = $("#img-slide1").position();
		$("#img-slide1").css("left", position.left+"px");
		$("#slide1 .slide-box").css("left", (position.left+630)+"px");
		posA1 = $("#slide1 A").position().left;
		$("#slide1 A").css("left", posA1+"px");
		
		position = $("#img-slide2").position();
		$("#img-slide2").css("left", "auto").css("right", $("#slide2").innerWidth()-$("#img-slide2").innerWidth()+"px");
		$("#slide2 .slide-box").css("right", ($("#slide2").innerWidth()-$("#img-slide2").innerWidth()+600)+"px");
		posA2 = $("#slide2").innerWidth()-$("#slide2 A").innerWidth()-40;
		$("#slide2 A").css("left", "auto").css("right", posA2+"px");
		
		$('#slide1').mouseenter( function () { 
	    	$(this).css("zIndex", 3).stop().animate({ width:'100%' }, 500, 'easeInCirc', function() { $(".slide-box", $(this)).fadeIn(500); } );
	    	$("A", $(this)).addClass("open").stop().animate({ left: posA1+280 }, 500, 'easeInCirc');
	 	}).mouseleave( function () { 
	 		$(".slide-box", $(this)).fadeOut(500);
	    	$(this).css("zIndex", 3).stop().animate({ width:'50%' }, 500, 'easeOutCirc', function() { $(this).css("zIndex", 1); } );
	    	$("A", $(this)).removeClass("open").stop().animate({ left: posA1 }, 500, 'easeOutCirc');
	 	});
		$('#slide2').mouseenter( function () { 
	    	$(this).css("zIndex", 3).stop().animate({ width:'100%' }, 500, 'easeInCirc', function() { $(".slide-box", $(this)).fadeIn(500); } );
	    	$("A", $(this)).addClass("open").stop().animate({ right: posA2+280 }, 500, 'easeInCirc');
	 	}).mouseleave( function () { 
	 		$(".slide-box", $(this)).fadeOut(500);
	    	$(this).css("zIndex", 3).stop().animate({ width:'50%' }, 500, 'easeOutCirc', function() { $(this).css("zIndex", 1); } );
	    	$("A", $(this)).removeClass("open").stop().animate({ right: posA2 }, 500, 'easeOutCirc');
	 	});
	}
	
	var newsPage = 0;
	/* Paginazione news homepage */
	if ($("#home-content #news").length > 0)
	{
		$("#news .rullo").css("height", $("#news UL").outerHeight()).css({overflow:"hidden", position:"relative"});
		$("#news UL").css("position", "absolute");
			
		$("A.prev").click( function() {
			if (!$(this).hasClass("disabled"))
			{
				$("A.next").removeClass("disabled");
				newsPage--;
				
				up = $("#news UL").outerHeight();
				$("#news UL LI").addClass(".remove")
				$.get(baseurl+"/newsticker/"+newsPage, function(data) {
					$("#news UL").prepend(data);
					up = 0-($("#news UL").outerHeight()-up);
					$("#news UL").css("top", up);
					$("#news UL").animate({ top: 0}, 700, 'easeOutQuad', function() {
						up = 0-up;
						$("#news .rullo").animate( { height : up}, "100", function() {
							$("#news UL LI:last").remove();
							$("#news UL LI:last").remove();
						});
					});
				});
				
				if (newsPage == 0) $("A.prev").addClass("disabled");
			}
		});
		$("A.next").click( function() {
			if (!$(this).hasClass("disabled"))
			{
				$("A.prev").removeClass("disabled");
				newsPage++;
				
				if (newsPage == newsPages-1) $("A.next").addClass("disabled");
				
				up = 0-$("#news UL").outerHeight();
				$("#news UL LI").addClass(".remove")
				$.get(baseurl+"/newsticker/"+newsPage, function(data) {
					$("#news UL").append(data);
					$("#news UL").animate({ top: up}, 700, 'easeOutQuad', function() {
						$("#news UL LI:first").remove();
						$("#news UL LI:first").remove();
						$("#news UL").css("top", 0);
						$("#news .rullo").animate( { height : $("#news UL").outerHeight()}, "200" );
					});
				});
			}
		});
	}

	/* Salary guide */
	$("#salaryguide-box #businessline").change( function()
	{
		$("#function-cont").hide();
		$("#position-cont").hide();
		$("#result-cont").hide();
		
		if ($("option:selected", this).val() != "")
		$.get('getSalaryGuideList/area/'+$(this).val(), function(data)
		{
			$("#function-cont").show();
			$("#function").html(data);
		});
	});
	$("#salaryguide-box #function").change( function()
	{
		$("#position-cont").hide();
		$("#result-cont").hide();

		if ($("option:selected", this).val() != "")
		$.get('getSalaryGuideList/posizione/'+$(this).val()+'/'+$("#salaryguide-box #businessline").val(), function(data)
		{
			$("#position-cont").show();
			$("#position").html(data);
		});
	});
	$("#salaryguide-box #position").change( function()
	{
		$("#result-cont").hide();
		
		if ($("option:selected", this).val() != "")
		$.get('getSalaryGuideList/result/'+$(this).val()+'/'+$("#salaryguide-box #function").val()+'/'+$("#salaryguide-box #businessline").val(), function(data)
		{
			data = data.split("|");
			$("#result-cont").show();
			$("#result-cont #inquadramento P:last").html(data[0]);
			$("#result-cont #grande SPAN").html(data[1]);
			$("#result-cont #media SPAN").html(data[2]);
			$("#result-cont #piccola SPAN").html(data[3]);
		});
	});
	
	$("div#content-settori DIV.slide H3").click( function () {
		
		if ($(this).parent().hasClass("itslide"))
		{
			$(this).css("opacity", 0);
			$(".slide DIV.text").not($(this).parent().find("DIV.text")).parent().find("H3").css("opacity", 1);
			$(this).parent().find("DIV.text").slideDown();
		}
		else
		{
			$(".slide DIV.text").not($(this).parent().find("DIV.text")).slideUp();
			$(this).parent().find("DIV.text").slideToggle();
			$(this).toggleClass("opened");
		}
	});
	

	$("div#content-settori DIV.slide A.chiudi").click( function (event) {
		event.preventDefault();
		$(this).parent().parent().find("H3").css("opacity", 1);
		$(this).parent().slideUp();
	});
});

/* PopUp */
function loadPopUp(url, data)
{
	$('#popup #chiudi').hide().click(function () { closePopUp(); });

	//Get the screen height and width  
	var maskHeight = $(document).height();  
	var maskWidth = $(window).width();  

	//Set height and width to mask to fill up the whole screen  
	$('#popup-mask').css({'width':maskWidth,'height':maskHeight});  
	  
	//transition effect   
	$('#popup-mask').fadeTo("fast",0.7);    

	//Get the window height and width  
	var winH = $(window).height();
	var winW = $(window).width();  
	        
	//Set the popup window to center
	$('#popup').width(90);
	$('#popup').height(90);
	$('#popup').css('top',  winH/2-$('#popup').height()/2);
	$('#popup').css('left', winW/2-$('#popup').width()/2);
	
	loadingPopUp();
	$("#popup").show();
	if (url == '')
	{
		$('#contentAjax .content').html(data);
		posPopUp();
	}
	else $('#contentAjax .content').load(url, function() { posPopUp(); });

}

function posPopUp()
{
	var winH = $(window).height();
	var winW = $(window).width();
	newwidth = $('#contentAjax .content').width()>300?$('#contentAjax .content').width()+30:300;
	newheight = $('#contentAjax .content').height()+30;

	$('#popup').animate( {width: newwidth, height: newheight, left: (winW/2)-(newwidth/2), top: (winH/2)-(newheight/2) }, 600, "easeInOutCirc", function() { $('#popup #chiudi').show() });
}
function loadingPopUp()
{
	$('#popup #contentAjax').css("height", "100%");
	$('#popup #contentAjax .content').html("<div style='text-align:center; padding-top:20%'><img src='"+baseurl+"lib/img/site/loader.gif'><"+"/div>");
}
function closePopUp()
{
	$('#popup #contentAjax .content').html("");
	$('#popup-mask').fadeOut("fast");
	$('#popup').hide();
	$('#popup #chiudi').hide()
}

/* Ricerca */
function cercaAnnunci()
{
	var what = $('#what').val();
	var settore = $('#settore option:selected').val();
	var where = $('#where').val();
	var km = $('#km').val();
	var durata = $('#durata').val();
	
	if(what+where+settore=="")
	{
		alert('Per eseguire una ricerca è necessario selezionare almeno un criterio di ricerca.');
		return false;
	}
	
	search(what, settore, where, km, durata);
	return false;
}

function search(what, settore, where, km, durata)
{
	if (sendsearch == true)
	{
		search = "";
		if (what!="") search += "&what="+what;
		if (settore!="") search += "&settore="+settore;
		if (where!="") search += "&where="+where;
		if (km!="0") search += "&km="+km;
		if (durata!="0") search += "&durata="+durata;
		search = search.substring(1);
		document.location = searchloc+search;
	}
	else
	{
		$('#search-res').hide();
		$('#search-loader').toggle();
		
		$.post(baseurl+"result",
   			{ what: what, settore: settore, where: where, km: km, durata: durata},
			function(data)
			{
				$('#search-res').html(data);
				$('#search-res').show();
				$('#search-loader').toggle();
				last_candidati_page = 1;
			}
 		);		
	}
	return false;
}

var last_candidati_page = 1;
function showCandidatiPage(page, last_page, max_page) { 
	$('.pages').hide();
	$('#page-res-'+page).show();
	$('.selected').removeClass('selected');
	$('.pager-'+page).addClass('selected');
	
	var code = "";
	var to_sel = page;
	var limit = 0;
	var old_last_candidati_page = last_candidati_page;
	last_candidati_page = to_sel;
	
	if(((page % max_page) == 0) || page == 1 || (((last_candidati_page) % max_page) == 0)) {
		if(page <= old_last_candidati_page) {
			page = page-max_page;
			limit = page+max_page;
		} else limit = page+max_page;
		
		
		if(page>1) code+= '<span><a href="javascript:;" class="pager-item pager-1" onclick="showCandidatiPage(1,'+last_page+','+max_page+')">1<'+'/a><'+'/span> <span>...<'+'/span> ';
		else limit = max_page;
		
		for(var i=page; i<=limit; i++)
		{
			if(i<=last_page && i>0 ) {
				if(i==to_sel) code+= '<span><a href="javascript:;" class="pager-item pager-'+i+' selected" onclick="showCandidatiPage('+i+','+last_page+','+max_page+')">'+i+'<'+'/a><'+'/span> ';
				else code+= '<span><a href="javascript:;" class="pager-item pager-'+i+'" onclick="showCandidatiPage('+i+','+last_page+','+max_page+')">'+i+'<'+'/a><'+'/span> ';
			}
		}
		if (limit < last_page) code+= '<span>...</span> ';
		$('.pgs').html(code);
	}
	if(last_candidati_page==1) $('.pred-page').attr('src',baseurl+'lib/img/site/pager_sx_off.gif');
	else $('.pred-page').attr('src',baseurl+'lib/img/site/pager_sx_on.gif');
	if(last_candidati_page==last_page) $('.succ-page').attr('src',baseurl+'lib/img/site/pager_dx_off.gif');
	else $('.succ-page').attr('src',baseurl+'lib/img/site/pager_dx_on.gif');
	
	return false;
}

function showPredCandidatiPage(last_page,max_page)
{
	if(last_candidati_page>1) {
		var page = last_candidati_page-1;
		showCandidatiPage(page,last_page,max_page);
	}
}

function showSuccCandidatiPage(last_page,max_page)
{
	if(last_candidati_page<last_page) {
		var page = last_candidati_page+1;
		showCandidatiPage(page,last_page,max_page);
	}
}

function closeTipCandidati() {
	$('.tip-search').hide();
}

function executeTipCandidati(mode, value)
{
	var what = $('#what').val();
	var settore = $('#settore option:selected').val();
	var where = $('#where').val();
	var km = $('#km').val();
	var durata = $('#durata').val();
	
	if(mode=='KM')
	{ 
		km = value; 
		$('#km').val(value); 
	}
	if(mode=='LOC')
	{ 
		km = 0;
		where = value;
		$('#km').val(km);
		$('#where').val(where); 
	}

	search(what, settore, where, km, durata);
}

function filterResults(filter)
{
	var what = $('#what').val();
	var settore = $('#settore option:selected').val();
	var where = $('#where').val();
	var km = $('#km').val();
	var durata = $('#durata').val();
	search(what, settore, where, km,  durata);
}

function setSearchMask(what, settore, where, km, durata)
{
	$('#what').val(what);
	$('#where').val(where);
	$("#settore option").each( function() { if ($(this).val() == settore) $(this).attr("selected", true); } )
	$("#km").val(km);
	$('#durata').val(durata);
	$('.cerca-annunci LI').removeClass("selected");
	$('#durata'+durata).addClass("selected");
}

