/* menu
- - - - - - - - - - - - - - - - - - - - */
function Menu(){
$("#menu ul li a").hover(
  function () {
    $(this).next().show();
  }, 
  function () {
    $(this).next().hide();
  }
);

$("#menu ul li ul").hover(
  function () {
    $(this).show();
	$(this).prev().addClass('attivo');
  }, 
  function () {
    $(this).hide();
	$(this).prev().removeClass('attivo');
  }
);	
}

/* Scroller prodotti per N
- - - - - - - - - - - - - - - - - - - - */
function scrollablePerN (n) {
	var qi = $('#prodotto .scrollable .items').children('a').size();
	if (qi>0){
		var c = 0
		while(qi>0 && c<15){
				$('#prodotto .scrollable .items').children('a:lt(' + (n) + ')').wrapAll('<div class="item"></div>');
				qi = $('#prodotto .scrollable .items').children('a').size();
				c+=1;
			}
	}
}

/* Scroller
- - - - - - - - - - - - - - - - - - - - */
function Scrollable () {
	var schermo = $("#image_wrap");
	// IE SOOKA
	if (location.href.indexOf("&gal=")!=-1){
		var gal = location.href.split("&gal=")[1];
		//schermo.html('<img class="reflex iheight10 iopacity35 itiltnone" alt="" src="' + $(".bm_galleria_immagini a").eq(gal).attr("href") + '">');
	}
	scrollablePerN(6);
	$(".bm_galleria_immagini").scrollable({ vertical: true, mousewheel: false });
	schermo.load (function(){$(this).fadeTo("normal", 1);});
	$(".bm_galleria_immagini .items a").click(function() {
		if ($(this).hasClass("active")) { return false; }
		var url = $(this).attr("href");
		// loading
		$l = $('<div class="loading"></div>');
		$("#image_wrap").append($l);
		// ajax
		$.ajax({
			url:url,
			cache:true,
			success:function(e){
				changeImg();	
			},
			error:function(e){
				changeImg();
			}
		});
		function changeImg(){			
			$(".loading").remove();
			schermo.fadeOut(500,function(){
				schermo.html('<img alt="" src="' + url + '">');
				schermo.fadeIn(500);
			});
			
			$(".bm_galleria_immagini .items a").removeClass("active");
			$(this).addClass("active");
		}
		return false;
	});
}

/*--------------------------------------------------
input
--------------------------------------------------*/
function input(){
 $("input:text").focus(function(){
 if ($(this).val() == $(this).attr("title")){
 $(this).val("");
 }
}).blur(function(){
 if ($.trim($(this).val()) == ""){
 $(this).val($(this).attr("title"));
 }
 });
} 

/*--------------------------------------------------
alignColHeight
--------------------------------------------------*/
function alignColHeight() {
	/*if($("#box2").height() > $(".box").height()){
		$(".wrap_col_sx_center").height(($("#col_dx").height()-10));
	} else {
		$("#col_dx").height($("#col_sx").height());
	}*/
	var maxH = 0;
	$("#wrap_box .box_pre").each(function(){
		maxH = ($(this).height() > maxH) ? $(this).height() : maxH;
	});
	$("#wrap_box .box_pre").css({height:maxH+"px"});
}

/*--------------------------------------------------
Scroller
--------------------------------------------------*/
function ScrollerElenco() {
	if ($("#elenco .scrollable .items").size()>0){
		$("#elenco .scrollable").scrollable();
	}
	if ( $('#elenco .scrollable .items').children().size()<5){
		$('#elenco a.next.walk, #elenco a.prev.walk').addClass('disabled');
	}
}

function ScrollerRealizzazioni() {
	if ($("#elenco .scrollable_realizzazioni .items").size()>0){
		$("#elenco .scrollable_realizzazioni").scrollable({touch:(!$.browser.msie)});
	}
	if ( $('#elenco .scrollable_realizzazioni .items').children().size()<4){
		$('#elenco a.next.realizzazioni, #elenco a.prev.realizzazioni').addClass('disabled');
	}
}


$(document).ready(function(){
	$("ul.tabs").tabs("div.panes > div");
	//$("#elenco .scrollable").scrollable({mousewheel: true });

	/*if ($("#elenco .scrollable_realizzazioni .items").size()>0){
		$("#elenco .scrollable_realizzazioni").scrollable();
	}*/
	/*if ( $('#elenco .scrollable_realizzazioni .items').children().size()<3){
		$('#elenco a.next, #elenco a.prev').addClass('disabled');
	}*/
	if ( $('.bm_galleria_immagini .items').children().size()<2){
		$('.scrollable .bm_galleria_immagini .items').css('display','none');
	}
	$("#image_wrap img").addClass("reflex iheight10 iopacity35 itiltnone");
	$(".wrap_img img").tooltip({ relative: "false", offset: [-5, 0]});
	$("ul.tabs").tabs("div.panes > div");
	Scrollable ();
	alignColHeight();
	init();
	Menu();
	input();
	ScrollerElenco();
	ScrollerRealizzazioni();
});
