
// Create a closure to maintain scope of the $ function and remain compatible with other frameworks
(function($) {

  jQuery.browser.version = jQuery.browser.msie && parseInt(jQuery.browser.version) >= 6 && window["XMLHttpRequest"] ? "7.0" : jQuery.browser.version;

  // equivalent to $(document).ready
  $(function() {
    
    $('html').removeClass('no-js');
    var $slider = $('.slider'),
        $btns = '<div class="btns clearfix">';
    
    $slider.find('li:eq(0) a strong:eq(0)').after('<strong id="sandbox"/>');
    var b = $('<b />').append('azertyuiopmlkjhgfdsqwxcvbn').appendTo('#sandbox'),
        charWidth = Math.round(b.width()/26);
    $('#sandbox').remove();
        
    $slider.find('li a strong[id!="sandbox"]').each(function(){

      var text = $(this).text(),
          len = text.length,
          width = 520,
          charsPerRow = 25,//Math.floor(width/charWidth),
          breakingIndexes = [],
          gRowStart = 0,
          gRowEnd = charsPerRow;

      while( gRowEnd < len ) {
          var rowEnd = text.substring(gRowStart, gRowEnd).lastIndexOf(' ');
          breakingIndexes.push(gRowStart + rowEnd);
          gRowStart = gRowStart + rowEnd + 1; // next start is the next char
          gRowEnd = gRowStart + charsPerRow;
      }

      var start = 0, newText = '';
      for(var i=0; i < breakingIndexes.length; i++){
          newText += '<span>'+text.substring(start, breakingIndexes[i]) + '</span>';
          start = breakingIndexes[i];
      }
      
      newText += '<span>'+text.substring(start, len) + '</span>';
      $(this).html(newText);
      
    });
    
    var sliderWidth  = ( $('body').hasClass('page') ? 940 : 620 ),
        sliderHeight = ( $('body').hasClass('page') ? 355 : 420 );
        
    $slider.anythingSlider({
      width : sliderWidth,
      height : sliderHeight,
      buildArrows : false,
      animationTime : 347,
      delay : 5000    
    });
    
    $('.mini, .page .w300, .medias .presentation').equalHeights();
    /*, .single .w460*/
    $('.single .w230').equalHeights();
    $('.contacts .clist').each(function(){
      $(this).find('.w300').equalHeights();
    });
    
    $('header nav .cat-item-6 > a').click(function(e){
      e.preventDefault();
      $(this).parent().toggleClass('sub-on');
      $(this).next().toggle();
    });
    
    if ( $('.news div.on').length > 0 ) {
      var posTop = $('.news div.on').offset().top;
      $(window).scrollTop(posTop-30);
      
      setTimeout(function(){
        $('.news div.on').css('border-color', '#fff');
      },1000);
    }
    
    if( $.browser.msie && ( $.browser.version <= '7.0' ) ){
      
      var rootElement = ("onorientationchange" in window) ? $(document) : $(window);
      $('.inner-nav').width(rootElement.width());
  
      if( $.browser.msie && ( $.browser.version <= '6.0' ) ) {
        if( $('article.full').length > 0 ) {
          if ( $('article.full').height() < 316 ) {
            $('article.full').height(316);
          }
        }
        
        if( $('.presentation .tools').length > 0 ) {
          $('.presentation .tools li:first-child').addClass('first');
        }
      }

    }

  });
  
  $(window).resize(function(){
    if( $.browser.msie && ( $.browser.version <= '7.0' ) ){
      var rootElement = ("onorientationchange" in window) ? $(document) : $(window);
      $('.inner-nav').width(rootElement.width());
    }
  });
     
})(jQuery);


