$(document).ready(function() {

    $("#dati_societari").click(function(){
        window.open('corporate.php','dati','height=520, width=380,toolbar=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no');         
    })
			
	$(function() {
		$("a[rel='_blank']").attr("target","_blank");				
	});
	
	$('a.scrollTop').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
		return false
	});
			
	$("a[rel='_back']").click(function(){
		history.back();
		return false
	});

    
    $("input, select, textarea").each(function () {
        $(this).attr("name", $(this).attr("id"));
    });


	
	$.urlParam = function(name){
		var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
		if(results) {
			return results[1] || 0;
		}
	};
	
	jQuery.fn.delay = function(time,func){
		this.each(function(){
			setTimeout(func,time);
		});	
	return this;
	};
	
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
    	return this.animate({opacity: 'toggle'}, speed, easing, callback);
	};
	
	// FUNZIONE CUSTOM FADEIN/FADEOUT PER RIMUOVERE ALPHA FILTER SU IE CHE SGRANA IL TESTO
	(function($) {
	$.fn.fadeInPlus = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.fadeOutPlus = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	})(jQuery);
	
	jQuery.preloadImages = function(){
  		for(var i = 0; i<arguments.length; i++){
    		$("<img>").attr("src", arguments[i]);
  		}
	}
	$.preloadImages("img/ajax-loader.gif");
	
	$("a.anchor-link").anchorAnimate();
	
	// FUNZIONE CUSTOM SELECT
	$(function() {
		$(".select-replacement-holder select").change(function(){
			$(this).parent().parent().find(".selected-replacement-value").html($(this).find("option:selected").text());
		});	
		$(".select-replacement-holder select").each(function(){
			$(this).parent().parent().find(".selected-replacement-value").html($(this).find("option:selected").text());
		});
	});
	
	// FUNZIONE MENU BOXES
	$(function() {
		function addBox(){
  			$(this).find('.menu-box').fadeInPlus('fast');
		}
		function removeBox(){
  			$(this).find('.menu-box').fadeOutPlus('fast');
		}
		var menuBox = {    
     		interval: 100,
     		sensitivity: 2,
			over: addBox,
			timeout: 100,
     		out: removeBox
     	};
		$('li.toggler').hoverIntent(menuBox);
		$('li.toggler').find('a:first').click(function(){
			return false
		});
		$('#menu a, a.current').hover(function(){
			$(this).attr('title','');
		});
	});
	
	// FUNZIONE MENU ORIZZONTALE
	$(function() {
		function addBar(){
  			$(this).find('.submenu').fadeInPlus('fast');
			$(this).find('a.submenu-toggler').addClass('current');
			$(this).find('a.submenu-toggler').attr('title','');
		}
		function removeBar(){
  			$(this).find('.submenu').fadeOutPlus('fast');
			$(this).find('a.submenu-toggler').removeClass('current');
		}
		var showSubmenu = {    
     		interval: 100,
     		sensitivity: 2,
			over: addBar,
			timeout: 100,
     		out: removeBar
     	};
		if($('#headbanner').length) {
			$('li.has-submenu').hoverIntent(showSubmenu);
		}
	});
	
	// FUNZIONE CONTEXT MENU
	$(function() {
		if($('#context-menu').length) {	
			$('#context-menu ul li').find('a').click(function(){
				$(this).parent().parent().find('a.current').removeClass('current');
				$(this).addClass('current');
			});
		}
	});
	
	// FUNZIONE JOB LIST
	$(function() {
		if($('ul.collapsing-list').length) {
			
			$('.list-details').hide();
			$('.details-toggler').show();
			
			$(this).find("li").each(function() {
				var initBorderColor = $(this).css("borderColor");							 
				$(this).find("a.details-toggler").hover(
					function () {
						$(this).parent().css("border-color","#155399");
					}, 
					function () {
						$(this).parent().css("border-color",initBorderColor);
					}
   				);
				$(this).find('a.details-toggler').click(function(){
					$(this).parent().find('.list-details').slideToggle();
					$(this).toggleClass('active');
				return false
				});				
			});
							
			$('a.super-list-details-toggler').click(function(){
				if($('.list-details').is(':hidden')) {								 
					$('.list-details').slideDown();
					$('a.details-toggler').addClass('active');
				} else {
					$('.list-details').slideUp();
					$('a.details-toggler').removeClass('active');
				}
			return false
			});
		}
	});
	
	// FUNZIONE ALLEGATI NEWS
	$(function() {
		if($('div.news-attachment').length) {			
			$('.news-attachment-content').hide();
			$('.details-toggler').show();
			
			$('div.news-attachment').each(function() {
				var initBorderColor = $(this).css("borderColor");							 
				$(this).find("a.details-toggler").hover(
					function () {
						$(this).parent().css("border-color","#155399");
					}, 
					function () {
						$(this).parent().css("border-color",initBorderColor);
					}
   				);
				$(this).find('a.details-toggler').click(function(){
					$(this).parent().find('.news-attachment-content').slideToggle();
					$(this).toggleClass('active');
					if($('.news-attachment-content').is(':visible') && $('div.news-gallery-wrapper').length){
						launchPhotogallery();
					}					
				return false
				});				
			});
			
			$('a.super-attachments-toggler').click(function(){
				if($('.news-attachment-content').is(':hidden')) {								 
					$('.news-attachment-content').slideDown();
					$('a.details-toggler').addClass('active');
					if($('.news-attachment-content').is(':visible') && $('div.news-gallery-wrapper').length){
						launchPhotogallery();
					}
				} else {
					$('.news-attachment-content').slideUp();
					$('a.details-toggler').removeClass('active');
				}
			return false
			});
		}
		
		function launchPhotogallery() {
			$('.news-attachment').each(function(){
				var div = $(this).find('div.news-gallery-wrapper');
				var ul = $(this).find('ul.news-gallery');		
				var lastLi = ul.find('li:last-child');
				var divWidth = div.width();
				
				div.css({overflow: 'hidden'});
				
				div.mousemove(function(e){
					var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth();
					var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
					div.scrollLeft(left);
				});
			});
		}
		
		var galleries = new Array();
		$('ul.news-gallery').each(function(){
			$(this).find('li:last-child').css('padding-right','0px');
			$(this).find('a').each(function(){
				if(!in_array(galleries, $(this).attr('class')) && $(this).attr('class')){
					galleries.push($(this).attr('class'));
				}
			});
		});
		for(i=0; i<galleries.length; i++){
			$('a.'+galleries[i]).fancybox({
				'overlayOpacity': 0.95,
				'overlayColor': '#cdcdcd'
			});
		}
		
	});	
			
});

function in_array(thaArray, element){
 var res=false;
  for(var e=0;e<thaArray.length;e++){
     if(thaArray[e] == element){
       res=true;
       break;
     }
   }
 return res;
}

jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}

function checkEmail(email){
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email)) {
        return false;
    }
    return true;
}

function changeLang(){
    var lang = $('#language option:selected').val();
    window.location.href = $('#hidden_page').val()+'?lang='+lang;
    return true;
}

/*$(document).ready(function(){
    showHideControl();
    $(window).resize(function(){
        showHideControl();
    });        
});

function showHideControl() {
    var h = $(window).height();
    var ch = $("#container").height();
    if (ch > h) {
        $("#text").append('<p><a href="#">Back to top</a></p>');
    }
}
*/

