
showCarousel = function(carousel) {
  jQuery("#caroussel-content li").css("visibility", "visible");
	jQuery('#pyxissien-caroussel li.pyxissien-carousel-item').css("visibility", "visible");
  jQuery('#medias li').css("visibility", "visible");
}

mountCarousel = function() {
  jQuery('#caroussel-content').jcarousel({
    buttonNextHTML: null, 
    buttonPrevHTML: null,
    scroll:1,
    auto:10,
    animation:"slow",
    wrap:"both",
    itemVisibleInCallback: itemVisibleIn,
    itemVisibleOutCallback: itemVisibleOut,
		itemLoadCallback: showCarousel
});

  jQuery("#caroussel .caroussel-prev").click(function(e){
    e.preventDefault();
    jQuery('#caroussel-content').data('jcarousel').prev();
  });
  jQuery("#caroussel .caroussel-next").click(function(e){
    e.preventDefault();
    jQuery('#caroussel-content').data('jcarousel').next();
  });
  jQuery("#caroussel-dots div.dot").click(function() {
    jQuery('#caroussel-content').data('jcarousel').scroll(jQuery(this).index()+1);
    return false;
  });    
  jQuery('#caroussel-content').hover(function() {
    jQuery('#caroussel-content').data('jcarousel').stopAuto();
  }, function() {
    jQuery('#caroussel-content').data('jcarousel').startAuto();
  });
};

mountEquipeCaroussel = function() {
  if(jQuery("#pyxissien-caroussel li").length > 1) {
    jQuery("#pyxissien-caroussel").jcarousel({
      buttonNextHTML: null, 
      buttonPrevHTML: null,
      wrap:"both",
      scroll:1,
      itemLoadCallback: showCarousel
    });
  
    jQuery("#pyxissiens-section .caroussel-prev.small").click(function(e){
      e.preventDefault();
      jQuery('#pyxissien-caroussel').data('jcarousel').prev();
    });
    jQuery("#pyxissiens-section .caroussel-next.small").click(function(e){
      e.preventDefault();
      jQuery('#pyxissien-caroussel').data('jcarousel').next();
    });
  }
  else {
    jQuery(".pyxissien-carousel-item").css("height", "auto");
    jQuery("#pyxissiens-section a.caroussel-prev").hide();
    jQuery("#pyxissiens-section a.caroussel-next").hide();
  }
};

mountMediaCaroussel = function() {
  jQuery("#medias").jcarousel({
    buttonNextHTML: null, 
    buttonPrevHTML: null,
    wrap:"both",
    scroll:1,
    itemVisibleInCallback: mediaVisibleIn,
    itemVisibleOutCallback: mediaVisibleOut,
    itemLoadCallback: showCarousel
  });

  jQuery("#medias-section .caroussel-prev.small").click(function(e){
    e.preventDefault();
    jQuery('#medias').data('jcarousel').prev();
  });
  jQuery("#medias-section .caroussel-next.small").click(function(e){
    e.preventDefault();
    jQuery('#medias').data('jcarousel').next();
  });
  jQuery("#medias-section div.dot").click(function() {
    jQuery('#medias').data('jcarousel').scroll(jQuery(this).index()+1);
    return false;
  });   
};

function itemVisibleIn(carousel, li, index, state) {
  jQuery("div.dot:eq(" + (parseInt(index)-1) + ")").addClass("on");
}

function itemVisibleOut(carousel, li, index, state) {
  jQuery("div.dot:eq(" + (parseInt(index)-1) + ")").removeClass("on");
}

function mediaVisibleIn(carousel, li, index, state) {
  jQuery("#medias-section div.dot:eq(" + (parseInt(index)-1) + ")").addClass("on");
}

function mediaVisibleOut(carousel, li, index, state) {
  jQuery("#medias-section div.dot:eq(" + (parseInt(index)-1) + ")").removeClass("on");
}

Cufon.replace("#header-supnav li a", {
  hover: {color: "#6dcd00" }
});
Cufon.replace("h1,h2,h3,h4,.tree-heading", { 
    hover: {color: "#d42000" }
});

$(window).load(function () {
  jQuery("#clients .client-logo").each(function() {
    jQuery(this).css("padding-top", (100 - jQuery(this).find("img").height()) / 2 + "px");
  });
});

jQuery("document").ready(function() {
	
  if(jQuery("#interests").length > 0) {
    var interestsNum = jQuery("#interests li").length;
    jQuery("#interests li").each(function() {
      var percent = ((((interestsNum - jQuery(this).index()) / interestsNum) + .6) * 100);
      jQuery(this).css("font-size", percent + "%");
			jQuery(this).css("line-height", (percent / 10) + "pt");
    });
  }
  
  jQuery(".calendar-list").each(function() {
    jQuery(this).find("tr:last").css("border-bottom", "none");
  });

  jQuery("#caroussel-content li").each(function() {
    if (jQuery(this).find(".dotted-top").width() > jQuery(this).find(".dotted-bottom").width()) {
      jQuery(this).find(".dotted-bottom").removeClass("dotted-bottom");
    }
    else {
      jQuery(this).find(".dotted-top").removeClass("dotted-top");
    }
  });
  
  jQuery("#lightbox-container-image-box").live("mouseleave", function() {
    jQuery("#lightbox-nav-btnNext").stop().fadeTo("slow", 0);
    jQuery("#lightbox-nav-btnPrev").stop().fadeTo("slow", 0);
  });
  jQuery("#lightbox-container-image-box").live("mouseenter", function() {
    jQuery("#lightbox-nav-btnNext").stop().fadeTo("slow", .9);
    jQuery("#lightbox-nav-btnPrev").stop().fadeTo("slow", .9);
  });
  
  jQuery("#caroussel-dots").each(function() {
    jQuery(this).width(jQuery(this).find(".dot").length * 19);
  });
  
  jQuery(".center-me").bind("load", function() { 
    var parentBlock = jQuery(this).parents(".block:first");
    jQuery(this).css("margin-left", (parentBlock.width() / 2) - (jQuery(this).width() / 2) + "px")
                .css("margin-top", (parentBlock.height() / 2) - (jQuery(this).height() / 2) + "px");
  });
  
  jQuery("#gallery a, .open-in-lightbox").lightBox({fixedNavigation:true});
  
  jQuery(".result-item").live("mouseenter", function() {
    jQuery(this).find("img").each(function() { jQuery(this).addClass("on"); });
  });
  jQuery(".result-item").live("mouseleave", function() {
    jQuery(this).find("img").each(function() { jQuery(this).removeClass("on"); });
  });
  
  
  jQuery("li.pyxissien a").live("mouseenter", function() {
    if (jQuery(this).find("img.pyxissien-hover").length > 0) {
      jQuery(this).find("img.pyxissien-normal").hide();
      jQuery(this).find("img.pyxissien-hover").show();
    }
  });
  
  jQuery("li.pyxissien a").live("mouseleave", function() {
    if (jQuery(this).find("img.pyxissien-hover").length > 0) {
      jQuery(this).find("img.pyxissien-hover").hide();
      jQuery(this).find("img.pyxissien-normal").show();
    }
  });
  
  jQuery(".view-all").click(function() {
    jQuery(this).prev().show("slow");
    jQuery(this).next().show();
    jQuery(this).hide();
    return false;
  });
  
  jQuery(".view-less").click(function() {
    jQuery(this).prev().prev().hide("slow");
    jQuery(this).prev().show();
    jQuery(this).hide();
    return false;
  });
  
  jQuery("div.tag-hider a.on").each(function() {
    jQuery(this).parents("div.tag-hider").next(".view-all").click();
  });
  
  if (jQuery('#caroussel-content').length > 0) {
    mountCarousel();
  }
  
  if (jQuery("#pyxissien-caroussel").length > 0) {
    mountEquipeCaroussel();
  }
  
  if(jQuery("#medias").length > 0) {
    mountMediaCaroussel();
  }
  
  jQuery("#header-search").focus(function() {
    if (jQuery(this).val() == jQuery(this).attr("default")) {
      jQuery(this).val("");
    }
  });
  
  $('<div class="blockquote-open"></div>').insertBefore('blockquote');
  
  jQuery(".tree-heading").click(function() {
    jQuery(this).toggleClass("open").toggleClass("closed").next("div").slideToggle();
  });

  jQuery("div#pagination a.ajaxible").live("click", function() {
    url = $(this).attr('href');
    jQuery.ajax({
      url: url += '&ajax=true',
      type:"GET",
      dataType:'html',
      success: function(response){
        document.getElementById("ajax_content").innerHTML = response;
        return false;
      }      
    });
    
    return false;    
  });
  
  
  jQuery("a.button").click(function() {
    $(this).toggleClass("on");    
    var tag =  $(this).attr('rel');
    if ($(this).hasClass('ajaxible'))
    {
      if ($(this).hasClass('clear_tags'))
      {
        $('.item_tag').removeClass('on');
        url = $(this).attr('href');
        // cant remove this class
        $(this).addClass('on');
      }
      else
      {
        if ($(this).hasClass('on'))
        {
          $('.clear_tags').removeClass('on'); // one tag has been selected
        
          if ($(this).hasClass('equipe_tag'))
            url = heap.urlmap.equipe + "?add_tag=" + tag;
          else
          {
            url = $(this).attr('href');
            // remove others who had the on tag
            $('.item_tag.on').not($(this)).removeClass('on');
          }
        }
        else
        {
          if ($(this).hasClass('equipe_tag'))
            url = heap.urlmap.equipe + "?remove_tag=" + tag;
          else
            url = $(this).attr('href');
          
          // clicked on remove, check if more tags are on
          if (!$('.item_tag').hasClass('on'))
          {
            url = $('.clear_tags').attr('href');
            $('.clear_tags').addClass('on');
          }
        }
      }
          
      jQuery.ajax({
        url: url += '&ajax=true',
        type:"GET",
        dataType:'html',
        success: function(response){
          document.getElementById("ajax_content").innerHTML = response;
          return false;
        }      
      });
      
      return false;
    }
  });
  
  jQuery("#search-form").submit(function() {
    if (jQuery("#header-search").val() == jQuery("#header-search").attr("default")) {
      jQuery("#header-search").val("");
    }
    
    $(this).attr('action', $(this).attr('action') + '/' + jQuery('#header-search').val());
  });
  
  jQuery("#search-button").click( function(e) {
    e.preventDefault();
    jQuery("#search-form").submit();
  });
  
  if (jQuery("#formation_filters").length > 0) {
    jQuery("#formation_filters .effacer a").bind("click", function(e){
      e.preventDefault();
      jQuery("#formation_filters form label.on").each(function(){
        $(this).removeClass("on");
        $.cookie($(this).closest("form").attr("id"), null);
        $(this).children("input").prop('checked', false);
      });
      jQuery.ajax({
        url: window.location,
        type: "GET",
        data: {
          "ajax": true
        },
        dataType: 'html',
        success: function(response){
          document.getElementById("formations-by-month").innerHTML = response;
          Cufon.refresh();
          
          jQuery("#formations-by-month .tree-heading").click(function(){
            jQuery(this).toggleClass("open").toggleClass("closed").next("div").slideToggle();
          });
          
          return false;
        }
      });
    });
    
    var ajaxCall;
    
    jQuery("#formation_filters input[type='radio']").change(function(){
      $.cookie($(this).closest("form").attr("id"), $(this).val());
      
      formationFilterChanged();
    });
    
    jQuery("#formation_filters form").each(function(){
      var cookie_value = $.cookie($(this).attr("id"));
      if (cookie_value) {
        jQuery("#formation_filters input[id='" + cookie_value + "']").prop('checked', true);
        jQuery("#formation_filters input[id='" + cookie_value + "']").closest("label").addClass("on");
      }
    });
    
    if (jQuery("#formation_filters input:checked")) {
      formationFilterChanged();
    }
    
    var formation_filter_height = jQuery("#formation_filters").height() - 2;
    
    if (jQuery("#formation_filters form label.on").length == 0) {
      jQuery("#formation_filters").animate({
        "height": 27
      }, 500, function(){
        $(this).addClass("closed");
        jQuery("#formation_filters.closed thead th a").bind("click", openFormationFilters);
      });
    }
    else {
      jQuery("#formation_filters.open thead th a").bind("click", closeFormationFilters);
    }
  }
  
  function formationFilterChanged()
  {
    var data = {"ajax":true};
    
    if ($("#partner_filter").html() && $("#partner_filter").html() != "")
    {
      data["partenaire"] = $("#partner_filter").html();
    }
    
    jQuery("#formation_filters form input[type='radio']:checked").each(function() {
      $(this).closest("label").addClass("on");
      data[$(this).closest("form").attr("id")] = $(this).val();
    });
    jQuery("#formation_filters form label.on input:not(:checked)").each(function() {
      $(this).closest("label").removeClass("on");
    });
    
    if (ajaxCall)
      ajaxCall.abort();
    
    ajaxCall = jQuery.ajax({
      url: window.location,
      type:"GET",
      data:data,
      dataType:'html',
      success: function(response){
        document.getElementById("formations-by-month").innerHTML = response;
        Cufon.refresh();
        
        jQuery("#formations-by-month .tree-heading").click(function() {
          jQuery(this).toggleClass("open").toggleClass("closed").next("div").slideToggle();
        });
        return false;
      }      
    });
  }
  
  function openFormationFilters(e) {
    e.preventDefault();
    $("#formation_filters").animate({
      "height": formation_filter_height
    }, 500, function() {
      jQuery("#formation_filters.closed thead th a").unbind("click");
      $(this).removeClass("closed").addClass("open");
      jQuery("#formation_filters.open thead th a").bind("click", closeFormationFilters);
    });
  }
  
  function closeFormationFilters(e) {
    e.preventDefault();
    $("#formation_filters").animate({
      "height": 27
    }, 500, function() {
      jQuery("#formation_filters.open thead th a").unbind("click");
      $(this).removeClass("open").addClass("closed");
      jQuery("#formation_filters.closed thead th a").bind("click", openFormationFilters);
    });
  }
  
  jQuery.preloadCssImages();
});

