/*init Functions*/
function init(){			
	//-----> Accordion MENU SX
	$("#menu_sx ul.sub-l1").each(function(){
		//add opensub class if href != #
		if( $(this).parent().find("a:first").attr("href") == "#" ){
			$(this).parent().find("a:first").addClass("opensub-l1");
		}else{			
			//if header is not active -> hide menu
			if($(this).parent().find("a:first").hasClass('on')==false){
				$(this).hide();					
			}else{					
				 $(this).parent().find("a:first").addClass('fix-on');
			}
		}
	});
	$('#menu_sx ul.sub-main').accordion({
		active: '.on',
		header: '.opensub-l1',
		navigation: false,
		event: 'click',
		autoHeight: false,
		collapsible: true
	});
	$("#menu_sx ul.sub-l2").each(function(){
		//add opensub class if href != #
		if( $(this).parent().find("a:first").attr("href") == "#" ){
			$(this).parent().find("a:first").addClass("opensub-l2");
		}else{
			//if header is not active -> hide menu
			if($(this).parent().find("a:first").hasClass('on')==false){
				$(this).hide();
			}else{
				 $(this).parent().find("a:first").addClass('fix-ui-state-active');
			}
		}
	});
	$('#menu_sx ul.sub-l1').accordion({
		active: '.on',
		header: '.opensub-l2',
		navigation: false,
		event: 'click',
		autoHeight: false,
		collapsible: true
	});
	$("#menu_sx ul.sub-l3").each(function(){
		//add opensub class if href != #
		if( $(this).parent().find("a:first").attr("href") == "#" ){
			$(this).parent().find("a:first").addClass("opensub-l3");
		}else{
			//if header is not active -> hide menu
			if($(this).parent().find("a:first").hasClass('on')==false){
				$(this).hide();
			}else{
				 $(this).parent().find("a:first").addClass('fix-ui-state-active');
			}
		}
	});
	$('#menu_sx ul.sub-l2').accordion({
		active: '.on',
		header: '.opensub-l3',
		navigation: false,
		event: 'click',
		autoHeight: false,
		collapsible: true
	});
	$("#menu_sx ul.sub-l1").each(function(){
		if(!$(this).find("li").size()){
			$(this).remove()
		}		
	})

	//-----> Shadowbox
	Shadowbox.init({
			overlayOpacity: 0.8,
			skipSetup:true
	});
	Shadowbox.setup("a#disclaimer",{width: 850, height:600});
	Shadowbox.setup("a[rel=shadowbox]",{gallery:"gallery"});
	
	//-----> fitted
	$(".lista-corsi > li:not(.not-ext), .extended-anchor").fitted({
		className:{
			'hover' : 'hovering'
		}
	});
	//-----> input and .button focus and hover
	addHoverFocusInputClass();
}
/**
 * addHoverFocusInputClass
 */
function addHoverFocusInputClass(){
	$('input[type=submit],input[type=reset],input[type=button],button,a.button,input.button').hover(function(){
		$(this).addClass('hover')
	},function(){
		$(this).removeClass('hover')
	});
	$('input[type=submit],input[type=reset],input[type=button],input[type=text],input[type=password],textarea,select').focus(function(){
		$(this).addClass('focus')
	});
	$('input[type=submit],input[type=reset],input[type=button],input[type=text],input[type=password],textarea,select').blur(function(){
		$(this).removeClass('focus')
	});
}
/**
 * changeTypeOfUser
 */
function isCompany(p){
	$(p).show();
	$(p+" input").removeAttr("disabled");
}
function isNotCompany(p){
	$(p).hide();
	$(p+" input").val("");
	$(p+" input").attr("disabled","disabled");
}
function changeTypeOfUser(choice,p,inline){
	$(choice).radiobutton({
        liInline:inline,
		clickAll:false
	});
	isNotCompany(p); /* default */
	if($(choice+" li:first input[type='radio']").attr("checked")){
		isNotCompany(p);
	}
	if($(choice+" li:last input[type='radio']").attr("checked")){
		isCompany(p);
	}
	$(choice+" li:first span:first").click(function(){
		isNotCompany(p);
	});
	$(choice+" li:last span:first").click(function(){
		isCompany(p);
	});
	$(choice).parents("form").bind('reset',function(){
		isNotCompany(p); /* default */
	})
}

/**
 * validateAndCaptcha
 */
function validateAndCaptcha(form,errorContainer,captchaContainer,label,title,msg){
	var firstNumber = Math.ceil(Math.random()*10);
	var secondNumber = Math.ceil(Math.random()*10);
	var theAnswer = firstNumber + secondNumber;
	$(captchaContainer).html("<label for='captcha'>"+label+"<span>*</span>: "+firstNumber+" + "+secondNumber+" =</label> <input type='text' name='captcha' id='captcha' maxlength='2' class='captcha' title='"+title+"' />");
	var validator=installFormValidate(form,errorContainer);
	if (validator){
		$(form+" input[type=reset]").bind("click", function(e){
			if($.fn.resetForm){               
				validator.resetForm();
				resetStylesAndValuesFields(form);
				return false;
			}else{
				resetStylesAndValuesFields(form);
				return true;
			}
			return true;
		});
		$(captchaContainer+ " input[name=captcha]").rules("add", {
			required:true,
			digits:true,
			range:[theAnswer,theAnswer],
			messages:{
				range: msg
			}
		} );

	}
	
	
}

/**
 * privacyOverlay
 */
function privacyOverlay(el){
	$(el+' a').click(function(){
         Shadowbox.open({
            content:    $(this).attr('href'),
            player:     "iframe",
            title:      $(this).attr('title'),
            height:     600,
            width:      850
        });
        return false;
    });
	$(el).next('ul').radiobutton({
		clickAll: false,
		liInline: true
	});
}

/**
 * selectCascade
 */
function selectCascade(url, loading, select, result, param, onSuccess){    
	$(loading).hide();
	$(select).change(function(){
		opt=$(this).find(':selected').val();

		$.ajax({
			beforeSend: function(){
				$(result).html("");
				$(loading).show();
			},
			complete: function(){
                $(loading).hide();
			},
			type: "GET",
			url: url,
			cache: false,
			data: param+"="+opt,
			success: function(html){
				$(result).html(html);				
                if(onSuccess){
                    onSuccess();
                }
			},
			error: function(){
				$(result).html('Error, please try again later..');
			}
		});
	});
}
