  function updateActiveTabAndSubMenu(menu, submenu){
    $('.sub-menu').removeClass('active active-without-sub-menu');
    $('.menu li').removeClass('active active-without-sub-menu');
    if(submenu){
      $(menu).addClass('active');
      $(submenu).addClass('active');
    }
    else
      $(menu).addClass('active-without-sub-menu');
  }


  function mouseEnterInTab(menu, submenu){
    updateActiveTabAndSubMenu(menu, submenu);
  }


  function setCurrentTabAsActiveTab(){
      // We are not over a 'ul' tag so we assume that we are not over the menu nor the submenu
      var menu = $('.menu .current')
      var obj = $('.sub-menu-container .current');
      var submenu = (obj.length == 1)?obj[0]: null;
    
      updateActiveTabAndSubMenu(menu, submenu);
  }

  function mouseLeaveTab(event){
    if (event.relatedTarget.nodeName != 'UL' && $(event.relatedTarget).parents("ul").length == 0){
      setCurrentTabAsActiveTab();
    }
  }


  function loadTopMenu(){

      // Add events on all the tabs
      $('.menu li').each(function(index) {
        // Get the submenu (if we have it)
        var obj = $('#sub-'+ this.id);
        var submenu = (obj.length ==1)?obj[0]:null;

        $(this).mouseenter(function(){mouseEnterInTab(this, submenu)});
        $(this).mouseleave(function(event) {mouseLeaveTab(event, this, submenu)});
        if (submenu)
          $(submenu).mouseleave(function(event){mouseLeaveTab(event, this, submenu)});

        // All the part of the tab should be clickable
        $(this).click(function(){window.location.href = $('a', this).attr('href');});
      });
  }
  

  function onLoad(){
    loadTopMenu();
    setCurrentTabAsActiveTab();
    updateSiteVersion();
    initImageRotator();
    initGuidedTour();
    addEventsOnInterests();
    initFormValidator();
    $(document).pngFix();
    // Init the tooltip (use for lemonde.fr)
    $(".tooltip-to-add").tooltip({ position: "bottom middle", relative: true, offset: [0, -30]});
  }

  $(document).ready(function() {
    onLoad();
    })


function initImageRotator(){
    $(".images-rotator .tabs").tabs(".images-rotator .images > div", {
    //
	// enable "cross-fading" effect
	effect: 'fade',
	fadeOutSpeed: "slow",

	// start from the beginning after the last tab
	rotate: true

    // use the slideshow plugin. It accepts its own configuration
    }).slideshow({clickable: false,  autoplay: true});
}


function initGuidedTour(){
    $(".guided-tour .guided-tour-middle .more").toggle(false);
    $(".guided-tour .guided-tour-middle .more-text").bind({click: function() {
        $(this).parent().children('.more').toggle();
    }});

    $(".guided-tour .tabs").tabs(".items > div", {
            //
	// enable "cross-fading" effect
	effect: 'fade',
	// fadeOutSpeed: "slow",

	// start from the beginning after the last tab
	rotate: false

    // use the slideshow plugin. It accepts its own configuration
    }).slideshow({clickable: false});
}


function initFormValidator(){

    $.tools.validator.addEffect("wall", function(errors, event) {
    
      var conf = this.getConf();
      // "show" function
      $.each(errors, function(index, error) {
        // erroneous input
        var input = error.input;
    	input.addClass(conf.errorClass);
    	input.removeClass('isValidated');
      });
    
    }, function(inputs)  {
      // "hide" function
        var conf = this.getConf();
    	inputs.removeClass(conf.errorClass);
    	inputs.addClass('isValidated');
    
    });
}

function checkInputText(inputObj, defaultValue, color){
    color = color || '#999999';
    if (inputObj.value == "" ) {
	    inputObj.value = defaultValue;
	    inputObj.style.color = color;
        $(inputObj).removeClass('isValidated');
        $(inputObj).removeClass('invalid');
    }
    else
	    inputObj.value = inputObj.value;
}

function clearInputText(inputObj, defaultValue){
    if (inputObj.value == defaultValue ) {
	inputObj.value="";
	inputObj.style.color = 'black';
    }
}

function addEventsOnInterests(){
    // Add onchange event on all the interest checkboxes
    $(".field_interests input:checkbox").change(function() {
        showOrHideContactInformation();
    });
    // Set default display
    showOrHideContactInformation();
}

function showOrHideContactInformation(){
    // Show or hide the contact information according to the interest checkboxes status
    var fieldIds = ['cinfo_companyname', 'cinfo_fonction', 'cinfo_telephone', 'address', 'cinfo_postalcode', 'cinfo_city', 'cinfo_countrycode', 'cinfo_contactme'];
    if($(".field_interests input:checked[name!=field_personaluse]").length > 0)
        $.each(fieldIds, function(index, value){$('.field_'+value).show()});
    else
        $.each(fieldIds, function(index, value){$('.field_'+value).hide()});
}

function hideOrShowSiteVersionLanguages(){
    // Toggle the display and add the good css class according to the display
        $('.site-version-languages .languages').toggle();
        if ($('.site-version-languages .languages').css('display') == 'none'){
            $('.site-version-languages .current').removeClass('current-opened');
        }
        else
            $('.site-version-languages .current').addClass('current-opened');
}

function updateSiteVersion(){
    // Added event on the site version
    $('.site-version-languages a.current').click(
        function (event){
            hideOrShowSiteVersionLanguages();
            event.stopPropagation();
            $(document).click(
                function () {
                   hideOrShowSiteVersionLanguages();
                   $(document).unbind('click');
                }
            );
    });
}
