$(document).ready(function() {
    $('.pngfix').pngFix();
    
    mainNav();
    
    //diaporama on home page
    if($('#homeBanner').length) 
        $('#homeBanner').s3Slider({
          timeOut: 5000
        });
        
    //news slider on home page
    if($('#homeSlider').length) 
        $('#homeSlider').s3Slider({
          timeOut: 3000
        });
        
    if($('#topBanner').length) 
        $('#topBanner').s3Slider({
          timeOut: 7000
        });  
   
     //news slider on home page
    if($('#colBanner').length) 
        $('#colBanner').s3Slider({
          timeOut: 3000
        });   
     
      
            
    toggleMapLegend();
    
     //rounded corners:
    //rounded corners:
    var toRound = $('.programs-list-page .list-filter');
    if(toRound.length)
        toRound.corner("8px"); 
    if($('.contact-form .round').length)
        $('.contact-form .round').corner("keep");
        
        
        
    
    //switching images:
    imgHover();
    
    //simulateur
    $('.two-columns .simulateur .left-side').css({"height":$('.two-columns #main-col').height()});
    
    tabsHover();
    
    //toggle actus:
    toggleActu();
    
     //adjust width:
    adjustOverlay();
});



function updateprog(url,urlgif,node) {
ajaxload("#prog_result",urlgif);
$("#prog_result").load(url,"node="+node);
}

function ajaxload(div,image){
$(div).html('<p><img src='+image+' /></p>');
}
	
function mainNav() {
    $('.main-nav ul > li').mouseover(function() {
            $(this).addClass('over');
        })
        .mouseleave(function() {
            $(this).removeClass('over');      
    });
}


function toggleMapLegend() {
    $('a.open-legend').click(function() {
        $(this).hide();
        $('#map-legend').slideDown('fast');
        return false;
    });
    
    $('#map-legend img.close').click(function(){
        $('#map-legend').slideUp('fast');
        $('a.open-legend').show();
    });
}


function imgHover() {
    var largeImg = $('.photos-container .large-photo img');
    $('.photos-container .thumbnails a')
        .mouseover(function(){
        $(this).closest('.photos-container').find('.large-photo img').attr('src', $(this).attr('href'));
        })
        .click(function() {
            return false;
    });
}

function tabsHover() {
    if($('.program-tabs').length) {
        var initActive = $('.program-page .program-tabs .tabs-nav li a.active');
        var initActiveHref = initActive.attr('href');
        $('.tab-body').hide().filter(initActiveHref.substr(initActiveHref.indexOf('#'))).show();
        var clicked = false;
        $('.program-page .program-tabs .tabs-nav li a').hover(
            function() {
                $('.program-page .program-tabs .tabs-nav li a.active').removeClass('active');
                $(this).addClass('active');    
            },
            function() {
                if(!clicked) {
                    $(this).removeClass('active');
                    initActive.addClass('active');
                }
                else
                    clicked = false;
            }
        ).click(function(){
            initActive = $(this);
            clicked = true;
            var activeDivHref = $(this).attr('href');
            $('.tab-body').hide().filter(activeDivHref.substr(activeDivHref.indexOf('#'))).show();
            return false;
        });
    }
}

function toggleActu() {
    $('.actu-list li').click(function() {
        $(this).toggleClass('expanded');
    })
    .children().click(function(event){
        event.stopPropagation(); 
        return true;
    });
    
    /*$('.actu-toolbar span a.plus').click(function(){
        $('.actu-list li').addClass('expanded');  
        return false; 
    });
    
    $('.actu-toolbar span a.minus').click(function(){
        $('.actu-list li').removeClass('expanded');   
        return false; 
    });*/
}

function  adjustOverlay() {
    var toAdjust = $('.geo-search .search-result .result-image span');
    toAdjust.width(toAdjust.prev('img').width());
}