var first=1;
var random = 1;

$(document).ready(function(){
    $('#blockNewsletter input.cms_submit').val('');
    $('#nav li.parent,#nav li.menuparent').mouseenter(function(){
        $('#nav li.parent ul,#nav li.menuparent ul').not($(this).parent('ul')).hide();
        $(this).append('<div class="arrondi"></div>');
        $(this).children('ul').stop(true,true).fadeIn('slow');
        $(this).children('.arrondi').fadeIn('slow');
    });
    $('#nav li.parent,#nav li.menuparent').mouseleave(function(){ 
        $('#nav li.parent ul,#nav li.menuparent ul').not($(this).parent('ul')).hide();
        $(this).children('.arrondi').remove();
    });

    $('#nav ul.lvl2').parent('li.parent').children('ul.lvl2').removeClass('lvl2');

    $("#nav li.parent ul a[href="+window.location+"],#nav li.menuparent ul a[href="+window.location+"]").addClass('active');

    $('#nav li.parent ul a,#nav li.menuparent ul a').click(function(){
        $('#nav li.parent ul a,#nav li.menuparent ul a').removeClass('active');
        $(this).addClass('active');
        $("#nav li.parent ul a[href="+$(this).attr('href')+"],#nav li.menuparent ul a[href="+$(this).attr('href')+"]").addClass('active');
    });

    $('#nav li,#nav li a').corner(" tl 35px");
    $('.block,.block h4,.shadowblock h4').corner("top 8px");

    $('#galery').adGallery();

    $('#map-links a[href]').each(function()
       {
          $(this).qtip({
             content: '<img src="'+$(this).attr('href')+'" alt="" />', // Use the tooltip attribute of the element for the content
             style: {
                  width: 'auto',
                  padding: 0,
                  background: '#A2D959',
                  color: 'black',
                  textAlign: 'center',
                  border: {
                     width: 7,
                     radius: 5,
                     color: '#A2D959'
                  },
                  tip: 'bottomRight',
                  name: 'light' // Inherit the rest of the attributes from the preset dark style
               },
             position: {
                  corner: {
                     tooltip: 'bottomRight', // Use the corner...
                     target: 'topLeft' // ...and opposite corner
                  }
               }
          });
       });
       $('#header').prepend('<div style="z-index:1; position:absolute; left:0; bottom:0; background: url(/images/courbe_vert.png) no-repeat; width:900px; height:277px;"></div>');
       if($('#header-image img').length==0){
            while(random==first){
                random=Math.floor(Math.random()*5) + parseFloat(1);
            }
            $('#header-image').append('<img class="displayed" src="/uploads/images/header_home_'+random+'.jpg" alt="LEB" />');

             var newrandom=Math.floor(Math.random()*5) + parseFloat(1);
             while(random==newrandom){
                 newrandom=Math.floor(Math.random()*5) + parseFloat(1);
             }
             random=newrandom;
             $('#header-image').append('<img class="newBg" src="uploads/images/header_home_'+random+'.jpg" alt="" style="display:none;" />');
            window.setInterval("sliderHeader()",3500);
       }
});

function sliderHeader(){
    $('#header-image img.displayed').fadeOut('slow',function(){ $(this).remove();});
    $('#header-image img.newBg').fadeIn('slow',function(){ $(this).removeClass('newBg');$(this).addClass('displayed');});
    
    var newrandom=Math.floor(Math.random()*5) + parseFloat(1);
    while(random==newrandom){
        newrandom=Math.floor(Math.random()*5) + parseFloat(1);
    }
    random=newrandom;
    $('#header-image').append('<img class="newBg" src="uploads/images/header_home_'+random+'.jpg" alt="" style="display:none;" />');
}
