$(document).ready(function () {

    $('a').filter(function () {
        return this.hostname && this.hostname !== location.hostname;
    })
    .click(function () {
        window.open(this.href);
        return false;
    });         

    if ($('#side-menu-content').length != 0) {
        $('#page-text').addClass("page-text-splitted");
    }
    
    if ($('#lang-menu').length == 0){
      $('#menu-top li:last').addClass("last");
    }
  
  if ($("#index-content").length != 0){
    $("body").addClass("index-body");
  }
    // Home slider
  
    if ($('#slides').length != 0) {
        $('.anythingSlider').anythingSlider({
            easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
            autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
            delay: 4000,                    // How long between slide transitions in AutoPlay mode
            startStopped: false,            // If autoPlay is on, this can force it to start stopped
            animationTime: 1500,             // How long the slide transition takes
            hashTags: true,                 // Should links change the hashtag in the URL?
            buildNavigation: false,          // If true, builds and list of anchor links to link to each slide
            pauseOnHover: false,             // If true, and autoPlay is enabled, the show will pause on hover
            startText: "",             // Start text
            stopText: ""               // Stop text
        });
        resizeSlider();
        $(window).resize(function(){
            resizeSlider();
        });
      
      $('#slider').click(function(){
        if ($('.arrows a:hover').length == 0 && $('#start-stop:hover').length == 0 ){
          var currentPage = $('.anythingSlider').data('AnythingSlider').currentPage;
          var currentLi = $('#slides li').get(currentPage);
          var currentLink = $(currentLi).children('a');
          window.location.href = $(currentLink).attr('href');
        }
      })
    }
  
    // Home slider
        
    // Form elements styling

    if ($('#contour').length != 0 && $('#two-columns-form').length == 0){
        var width = 233;
    }else{
        var width = 131;
    }
          
    if ($('input[type=file]').length != 0){
      if ($('#menu-top-content #lang-menu li span').html() == 'fr'){
          $('input[type=file]').filestyle({
              image: "/images/common/bt-file-input.png",
              imageheight: 26,
              imagewidth: 57,
              width: width
          });
      }else{
          $('input[type=file]').filestyle({
              image: "/images/common/bt-file-input-en.png",
              imageheight: 26,
              imagewidth: 57,
              width: width
          });
      }
    }
    
    if ($('select').length != 0){    
      $('select').sb({
          fixedWidth: false
      });
    }    
        
    $('.radiobuttonlist br, .checkboxlist br').remove();
  
    $('input[type=radio], input[type=checkbox]').click(function(){
      var currentSelected = $(this).parent('.radiobuttonlist, .checkboxlist').children('label[class="activeInput"]');
      if (currentSelected != null){
        currentSelected.removeClass('activeInput');
      }
      $('label[for='+$(this).attr('id')+']').addClass('activeInput');
      
    });
  
    // Form elements styling

    // Redirect Links

    $("#redirect-content .redirect-personal, #redirect-content .redirect-business").hover(
          function () {
              var elem = $(this);
              elem.addClass(elem.attr("id") + "-hover");
              $("#redirect-logo").addClass(elem.attr("id") + "-logo");
          },
          function () {
              var elem = $(this);
              elem.removeClass(elem.attr("id") + "-hover");
              $("#redirect-logo").removeClass(elem.attr("id") + "-logo");
          }
        );

    // Redirect Links

    // SubMenu
    var activeMenu = null;
    var animationDone = true;

    $('#header a').click(function (event) {
        var $elem = $(this);
        if ($('.sub' + $elem.attr('class')).length) {
            event.preventDefault();
            if (animationDone == true) {
                animationDone = false;
                if (activeMenu != null) {
                    $('.' + activeMenu).animate({ backgroundPosition: '50% 82px' });
                    $('.sub' + activeMenu).slideToggle(function () {
                        if ('.sub' + $elem.attr('class') != '.sub' + activeMenu) {
                            $elem.css({ "background-position" : '50% 82px' });
                            $elem.animate({ backgroundPosition: '50% 69px' });
                            $('.sub' + $elem.attr('class')).slideToggle(function () {
                                animationDone = true;
                            });
                            activeMenu = $elem.attr('class');
                        } else {
                            animationDone = true;
                            activeMenu = null;
                        }
                    });
                } else {
                    $elem.css({ "background-position" : '50% 82px' });
                    $elem.animate({ backgroundPosition: '50% 69px' });
                    $('.sub' + $elem.attr('class')).slideToggle(function () {
                        animationDone = true;
                    });
                    activeMenu = $elem.attr('class');
                }
            }
        }

    });

    // SubMenu

    $(".product[data-id=1568]").hide();
  
    // Quicksand
    if (jQuery.browser.version != '8.0' && jQuery.browser.version != '7.0' && jQuery.browser.version != '6.0'){
      if ($('#productType').length != 0){
          var activeCat = $('.subNav[data-value="'+$('#productType').html()+'"]');
          $('.subNav[data-value="'+$('#productType').html()+'"]').addClass('active');
      }else{
          var activeCat = $('.subNav[data-value="all"]');
          $('.subNav[data-value="all"]').addClass('active');
      }
      
      if ($('.products-list-text').length != 0){
  
          if ($('.products-list-source').length != 0){
              var products = $('.products-list-source'); 
          }else{
              var products = $('.products-list'); 
          }
          
          var data = products.clone();
      
          var animationQuicksandDone = true;             
          $('#load-cats li .subNav').click(function(e){
                            
              e.preventDefault();
              var filter = $(this).attr('data-value');
  
              if($(this).attr('data-value') == 'all'){
                var filteredData = data.find('.product:not([data-id=1568])');
              }else{
                  var filteredData = data.find('[data-id='+filter+']');
              }
            
              if (animationQuicksandDone == true && $(this).attr('data-value') != activeCat.attr('data-value')){                
                  if ($('.products-list-source').length != 0){
                    
                    $('.products-list .product').css({'opacity' : 1});
                    $('.products-list .product').animate({ 'opacity': 0, 'scale': 0}, 1000, 'easeInOutQuint');
                    setTimeout(function(){
                      $('.products-list').remove();   
                      $('.products-list-source').addClass('products-list');
                      $('.products-list').removeClass('products-list-source');
                      products = $('.products-list');
                    }, 1000);                             
                  }
  
                  if (activeCat != null){
                      activeCat.removeClass('active');
                  }
                  animationQuicksandDone = false;
                  products.quicksand( filteredData,
                  {
                  adjustHeight: 'auto',
                  useScaling: true,
                  duration: 1000,
                  easing: 'easeInOutQuint'
                  }, function(){
                      $('.products-list').css({"height":"auto"});
                      $('.products-list').show();
                      $('.products-list .product').show();
                      $('.products-list .product').animate({ 'opacity': 1.0, 'scale': 1.0 }, 1000, 'easeInOutQuint');
                      animationQuicksandDone = true;
                  });
                  $(this).addClass('active');
                  activeCat = $(this);
              }           
          });
      }
    }else{
    }

    // Quicksand

    // Ads Slider
    if ($('#publicities-list').length != 0){
      var carousel = $('#publicities-list').jcarousel({
          scroll: 1,
          easing: 'easeInOutSine',
          animation: 500,
          wrap: "circular",
          vertical: true,
          buttonNextHTML: null,
          buttonPrevHTML: null,
          auto: 3
      });
    }
  
    // Ads Slider
  
    // Contact lightbox
    if ($('.page-content-contact-us .button').length != 0){
      $('.page-content-contact-us .button').fancybox({
          'padding': 0,
          'width': 442,
          'height': 480,
          'transitionIn': 'none',
          'transitionOut': 'none',
          'overlayColor': '#000000',
          'overlayOpacity': 0.7,
          'type': 'iframe'
      });
    }    
    
    // Contact lightbox
  
    // Vertical align bottom box
                
      $('#ads-box-wrapper .ad .description').each(function(index){
        var lineHeight = parseFloat($(this).css('line-height').substring(0,$(this).css('line-height').indexOf("px")));
        var height = $(this).height();
        var parentHeight = $(this).parent('.ad').height();
        $(this).css({'margin-top':((parentHeight-height)/2)-5+'px'})
      });
      
    // Vertical align bottom box
      
    // Footer Menu

    $("#footer-menu li:last").addClass("last");

    // Footer Menu
        
    //stick the footer at the bottom of the page if we're on an iPad/iPhone due to viewport/page bugs in mobile webkit
    if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
    {
       $("#ads-box").css("position", "relative");
       $("#ads-box").css("marginBottom", "-5px");
       $("#filler-ads").css("height", "108px");
    };
    if ($("input[type=submit]").length != 0){
      $.each($("input[type=submit]"), function(){
        $(this).val("Soumettre");
      });
    };
});

function resizeSlider(){
    if($(window).width() < 1920){
                if ($(window).width() <= 960){
                    var difference = 960/2;
                    $('.anythingSlider ul li').css({"right": difference});
                    $('.arrows, #start-stop').css({"left": difference-47});
                }else{
                    var difference = (1920 - $(window).width())/2;
                    var differenceFleche = 913 - (1920 - $(window).width())/2;
                    $('.anythingSlider ul li').css({"right": difference});
                    $('.arrows, #start-stop').css({"left": differenceFleche});
                }
            }else{
                $('.anythingSlider ul li').css({"right": 0});
                $('.arrows, #start-stop').css({"left": 913});
            }
}
