if (typeof Array.prototype['max'] == 'undefined') {
  Array.prototype.max = function() {
    return Math.max.apply({},this);
  }
}

$(document).ready(function() {
 
    //carousel home page
	//get the max height of carousel-items
    heights = new Array();
    $('.news-carousel li').each(function(){
    	heights.push($(this).height());
    });
    //set max height as the height of all the carousel items*/
    	
    $('.news-carousel li').height(heights.max());
    
    	$('.news-carousel').jCarouselLite({
    			visible: 3,
    			scroll: 3,
    			speed: 1000,
    			start: 0,
    			auto: 3000,
    			vertical:true,
    			circular: true
    }); 
    
    $('#nav-services li.first').hover(function() {
        $(this).css('cursor', 'pointer').addClass('hover');
    }, function() {
        $(this).css('cursor', 'normal').removeClass('hover');
    })
    
    $("ul.linkboxes li").hover(function() {
        $(this).css({'cursor':'pointer'}).addClass("hovering");
    }, function() {
        $(this).removeClass("hovering");
    }).click(function(evt) {
        evt.preventDefault();
        $(this).find("a:first").click(function(event) { preventDefault(); });
        var rel = ""; 
        rel = $(this).find("a:first").attr("rel");
        if (rel == "external") {
            window.open($(this).find("a:first").attr("href"));
        } else {
            document.location = $(this).find("a:first").attr("href");
        }
    });
    
    // gestione dei tabs
    $('.tabs-nav li a').live('click', function(e) {
        e.preventDefault();
        
        //risolve bug idiota di IE7 (aggiunge tutto l'url agli attributi href di elementi creati dinamicamente)
        var tab_to_show = '#' + $(this).attr('href').split('#')[1];
        
        $(tab_to_show).show().siblings('div.tab').hide();
        $(tab_to_show).find('h3:first').hide();
        $(this).parent('li').addClass('selected').siblings('li').removeClass('selected');
    });

    // creazione dei tabs
    $('div.tabs').each(function() {
        $(this).children('div.tab').hide();
        $(this).children('div.tab:first').show().find('h3:first').hide();
        var tab_nav = "<ul class='tabs-nav'>";
        $(this).children('div.tab').each(function() {
            tab_nav += "<li><a href='#" + $(this).attr('id') + "'>" + $(this).find('h3:first').text() + "</a></li>";
        });
        tab_nav += "</ul>";
        $(this).children('.tab:first').before(tab_nav);
        $(this).children('.tabs-nav li:first').addClass('first').addClass('selected');
        $(this).children('.tabs-nav li:last').addClass('last');
    });
    
    // gestione accordion
    $('ul.accordion li div.accordion-description').hide();
    $('ul.accordion li .accordion-key').hover(function() {
        $(this).css({'cursor': 'pointer', 'color': '#007EC3'});
    },function(){
        $(this).css({'color': '#0647A5'});      
    }).click(function(){
        $(this).toggleClass('open');
        $(this).siblings('.accordion-description').slideToggle('fast');
    });
    
    // form contatti
    $('li.hide-show label.input').hide();
    $('li.hide-show input').hide();
    $('li.hide-show label.select').show();
    $('li.hide-show select').show();
    $('li.hide-show select').change(function() {
        $(this).siblings('input').val($(this).val());
    });
    
    //gestione external
    $('a[rel=external]').click(function(evt) {
        evt.preventDefault();
        if ($(this).parents('ul').hasClass('linkboxes') || $(this).parents('ul').hasClass('icon-list')) {
        } else {
            window.open($(this).attr("href"));
        };
    });

    // gestione classe external
    $('a.external').click(function(evt) {
        window.open($(this).attr("href"), '', 'width=800,resizable=0,scrollbars=yes');
        return false;
    });
    
    // stampa la pagina
    $('a.print').click(function(evt) {
        evt.preventDefault();
        window.print();
    }); 
        
    // apre l'accordion dell'anno più recente nella sezione news e appuntamenti
    $('ul.accordion.anno li:first .accordion-key').click();
    
    // hover descrizione newsletter
    // $("ul.lista-newsletter li div").hide();
    //   $("ul.lista-newsletter li").hover(function() {
    //    $(this).children('div').show();
    //}, function() {
   //     $(this).children('div').hide();
    //    });
        
        $(".lista-newsletter div").tooltip();
});

function getCookie(name, defaultValue) {
    var value = $.cookie(name);
    return (value == null) ? defaultValue : value;
}

function setCookie(name, value, cookieSettings) {
    if (cookieSettings == null) {
        cookieSettings = getDefaultCookieSettings();
    }
    $.cookie(name, value, cookieSettings);
}

function getDefaultCookieSettings() {
    return {
        expires: 365,
        path: '/'
    };
}

function showHomePageAdIfRequired() {
    // NB: month is zero-based, ex: 0 - january
    var startDate = new Date(2011, 4, 12, 11, 0, 0, 0);
    var endDate = new Date(2011, 4, 20, 18, 0, 0, 0);
		//var startDate = new Date(2010, 10, 18, 16, 0, 0, 0);
    //var endDate = new Date(2010, 11, 2, 8, 0, 0, 0);
    var nowDate = new Date();

    var timeBeforeStartInMs = startDate.getTime() - nowDate.getTime();
    var timeLeftInMs = endDate.getTime() - nowDate.getTime();
    if (timeBeforeStartInMs >= 0 || timeLeftInMs <= 0) {
        return;
    }
    
    //$('#homepage-ad-launcher-link').click();
    
    if (parseInt(getCookie('hpas', 0)) == 0) {
        setCookie(
            'hpas',
            1,
            {
                expires: 0,
                path: '/'
            }
        );
        $('#homepage-ad-launcher-link').click();

        setTimeout(function() {

           tb_remove(); 

        }, 12000);
    }
}

