/* UNIFORM	*/
$(function(){
	initUniform();
});

/* ATTENZIONE: se si utilizza Uniform, ogni volta che si apporta una modifica alle caratteristiche di un elemento 
(per esempio, se si sostituiscono le entries di una select o se si cambia il valore selezionato via javascript), a valle della modifica bisogna richiamare il metodo di update di Uniform
tramite la seguente istruzione:
$.uniform.update();
*/
function initUniform() {
//	$("select, input:text, input:checkbox, input:radio, input:file").uniform({ 
	$("select, input:text, input:checkbox, input:radio").uniform({ 
		selectClass:   'selector',
		radioClass: 'radio',
		checkboxClass: 'checker',
//		fileClass: 'uploader',
//		filenameClass: 'filename',
//		fileBtnClass: 'action',
//		fileDefaultText: 'nessun file selezionato',
//		fileBtnText: 'Scegli',
		checkedClass: 'checked',
		focusClass: 'focus',
		disabledClass: 'disabled',
		activeClass: 'active',
		hoverClass: 'hover'
	});	
	$("input:text").focus(function(){
		$(this).parents("div.box_Input").addClass("focus");
	}).blur(function(){
		$(this).parents("div.box_Input").removeClass("focus");
	});

	$("input:text").each(function () {
		if (this.disabled) {
			$(this).parents("div.box_Input").addClass("disabled");
		}
	})
}

/* MENU	*/
function initMenu() {
	$('.js_SubMenu').live("mouseenter", function (event) {
		//$(this).find(".js_SubMenu_Container").fadeIn("fast").css({'z-index': 999});
		$(this).find(".js_SubMenu_Container").show().css({'z-index': 999});
/*		if (!$(this).hasClass("li_Selected")) {
			sIFR.getReplacementByFlashElement($(this).find(".box_Menu_First object")[0]).changeCSS( 
			[
				'.sIFR-root {color: #FFFFFF; font-size: 16px; font-weight: bold; white-space: nowrap; background-color: transparent;}'
				,'a {text-decoration: none;}'
				,'a:link {color: #FFFFFF;}'
				,'a:hover {color: #FFFFFF; cursor: pointer;}'
			]);
		}*/
	});
	$('.js_SubMenu').live("mouseleave", function (event) {
		//$(this).find(".js_SubMenu_Container").fadeOut("fast").css({'z-index': 1});
		$(this).find(".js_SubMenu_Container").hide().css({'z-index': 1});
/*		if (!$(this).hasClass("li_Selected")) {
			sIFR.getReplacementByFlashElement($(this).find(".box_Menu_First object")[0]).resetMovie();
			sIFR.getReplacementByFlashElement($(this).find(".box_Menu_First object")[0]).changeCSS( 
			[
				'.sIFR-root {color: #999999; font-size: 16px; font-weight: bold; white-space: nowrap; background-color: transparent;}'
				,'a {text-decoration: none;}'
				,'a:link {color: #999999;}'
				,'a:hover {color: #FFFFFF; cursor: pointer;}'
			]);
		}*/
	});
}
if ($.browser.msie && $.browser.version.substr(0,1)=='6') {
	$(function() {
		$('.list_Menu_Main li').hover(function(){
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		});
	});
}

/* OPEN a SLIDING BOX	*/
$('.js_Description_Opener').live("click", function () {
	$(this).parents(".js_Description").find(".js_Description_Container").slideToggle("slow");
	$(this).parents(".js_Description").find(".js_Description_Opener").toggleClass("Selected");
});

$(".js_Description_Opener_Multi").live("click", function (event) {
	var title_Opened = "Chiudi i dettagli";
	var text_Opened = "chiudi i dettagli";
	var title_Closed = "Scopri i dettagli";
	var text_Closed = "scopri i dettagli";
	
	// Variabili per il box Services
	if ($(this).parents(".box_Services").size() > 0) {
		title_Opened = "Nascondi gli altri operatori";
		text_Opened = "nascondi gli altri operatori";
		title_Closed = "Scopri gli altri operatori";
		text_Closed = "scopri gli altri operatori";
	}
	
	var visible = $(this).parents(".js_Description").find(".js_Description_Container").is(":visible");
	var myThis = this;
	if (visible) {
		// shown -> hidden
		$(this).parents(".js_Description").find(".js_Description_Container").slideUp("slow", function () {
			$(myThis).parents(".js_SuperParent").find(".js_Description").css({height: ''});
		});
		$(this).removeClass("Selected").attr("title", title_Opened).find("span:eq(0)").html(text_Closed);
	} else {
		// hidden -> shown
		var origLiH = $(this).parents(".js_Description").height();
		$(this).parents(".js_SuperParent").find(".js_Description_Container:visible").slideUp("slow");
		$(this).parents(".js_Description").find(".js_Description_Container").slideDown("slow", function () {
			var containerH = $(myThis).parents(".js_Description").find(".js_Description_Container").height();
			var liH = $(myThis).parents(".js_Description").height();
			//if (liH < containerH) {
			if (liH == origLiH) {
				// IE6: non viene aggiornata automaticamente la dimensione del container
				$(myThis).parents(".js_Description").css({height: (containerH + liH + 20) + 'px'}); // 20px padding top + bottom
				$(myThis).parents(".js_Description").prevAll(".js_Description").css({height: ''});
				$(myThis).parents(".js_Description").nextAll(".js_Description").css({height: ''});
			}
		});
	
		$(this).parents(".js_SuperParent").find(".js_Description_Container:visible").parents(".js_Description").find(".js_Description_Opener_Multi").removeClass("Selected").attr("title", title_Closed).find("span:eq(0)").html(text_Closed);
		$(this).addClass("Selected").attr("title", title_Opened).find("span:eq(0)").html(text_Opened);
	}
});

/* RANDOM PAGE COLOR	*/
$(document).ready(function() {
	var colorBody = Math.floor(Math.random()*11) + 1; // numero casuale da 1 a 11 (compresi)
	$("body").addClass("color_" + (colorBody < 10 ? ("0" + colorBody) : colorBody));
});

/* REMOVE VALUE FROM js_FocusInput INPUT	*/
$(document).ready(function() {
	$("input.js_FocusInput").each(function() { 
		$(this).attr("mask", $(this).val()); 
	});
	$("input.js_FocusInput").focus(function() { 
		if ($(this).val() == $(this).attr("mask")) {
			$(this).val("");
		}
	});
	$("input.js_FocusInput").blur(function() { 
		if ($(this).val() == "") {
			$(this).val($(this).attr("mask"));
		}
	});
});

/* OPEN POPUP	*/
function openPopup(url, name, w, h, s)
{
  // Fudge factors for window decoration space.
  // In my tests these work well on all platforms & browsers.
  w += 0;
  h += 0;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  // IE5 and other old browsers might allow a window that is
  // partially offscreen or wider than the screen. Fix that.
  // (Newer browsers fix this for us, but let's be thorough.)
  if (wleft < 0) {
    w = screen.width;
    wleft = 0;
  }
  if (wtop < 0) {
    h = screen.height;
    wtop = 0;
  }
 	s = "no";
 
  var fin;
  fin = window.open(url,name,'width='+w+',height='+h+','+'left='+wleft+',top='+wtop+','+'location=no,menubar=no,'+'status=no,toolbar=no,scrollbars='+s+',resizable=no');
	if((fin != undefined) || (fin != null)) {
		fin.focus();
}

}

/* REMOVE VALUE FROM js_FocusPassword INPUT	*/
$(document).ready(function() {
	$(".js_Input_Password input").focus(function() { 
		$(this).parents(".js_Input_Password").find("label").removeClass("lbl_Visible");
	});
	$(".js_Input_Password input").blur(function() { 
		if ($(this).val() == "") {
			$(this).parents(".js_Input_Password").find("label").addClass("lbl_Visible");
		}
	});
	$(".js_Input_Password label").click(function() { 
		$(this).removeClass("lbl_Visible");
		$(".js_Input_Password input").focus();
	});
});

/* BANNER SLIDESHOW */
function slideSwitch() {
    var $active = $('#js_Slideshow div.active');

    if ( $active.length == 0 ) $active = $('#js_Slideshow div:last');  
    var $next =  $active.next().length ? $active.next()
        : $('#js_Slideshow div:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() { //durata transizione
            $active.removeClass('active last-active');
        });
}
$(function() {
    setInterval( "slideSwitch()", 8000 ); //durata visualizzazione
});    

/* JS TRACCIA CLICK BANNER */
function trackclick(link, category, action, label) {
	try {
			var i=0;
			var pageTracker = _gat._getTracker("UA-2764227-4"); 
			pageTracker._trackEvent(category, action, label);
			setTimeout('i++', 100)
			
	}
	catch(err) {}
}

/* TABS	*/
var currentToken = 0;
var rotatingTime = 8000;
var dltab={
	configholder: {},
	resetsettings:function($,config) {
		config.$dltab.unbind();
	},
	init:function($,config) {
		config.$dt=config.$dltab.find('dt'); //Find Belt DIV that contains all the panels
		config.$dd=config.$dltab.find('dd'); //Find Panel DIVs that each contain a slide
		config.selectedclass=config.selectedclass||'el_Selected';
		config.$dt.each(function(index){
			var $currentdt=$(this);
			$currentdt.click(function(){
				currentToken++;
				setInterval( "rotateBanner("+currentToken+")", rotatingTime );
				$(this)
					.siblings().removeClass(config.selectedclass).end()
					.next('dd').andSelf().addClass(config.selectedclass);
			});
		});
	},
	setup:function(config){
		jQuery(document).ready(function($){
			config.$dltab=$('#'+config.dlid);
			dltab.init($, config);
		}) ;//end document.ready
		jQuery(window).bind('unload', function(){ //clean up on page unload
			dltab.resetsettings($, config);
			jQuery.each(config, function(ai, oi){
				oi=null;
			});
			config=null;
		});
	}
}

/* ROTATE BANNER	*/
$(function() {
    setInterval( "rotateBanner("+currentToken+")", rotatingTime ); //durata visualizzazione
});
function rotateBanner(token){
	if (token != currentToken) return; // se l'eguaglianza non corrisponde allora esci
	var $dt_Active = $('.js_Rotating_Banner dt.el_Selected');
	var $dt_Next = ($('.js_Rotating_Banner dt.el_Selected').nextAll('dt').length > 0) ? $('.js_Rotating_Banner dt.el_Selected').nextAll('dt:eq(0)') : $('.js_Rotating_Banner dt:first');
	var $dd_Active = $('.js_Rotating_Banner dd.el_Selected');
	var $dd_Next = ($('.js_Rotating_Banner dd.el_Selected').nextAll('dd').length > 0) ? $('.js_Rotating_Banner dd.el_Selected').nextAll('dd:eq(0)') : $('.js_Rotating_Banner dd:first');

	$dt_Active.fadeOut(function(){
		$dt_Active.removeClass('el_Selected');
		$dt_Next.addClass('el_Selected');
		$dd_Active.removeClass('el_Selected');
		$dd_Next.addClass('el_Selected');
	});
};
