jQuery(document).ready(function() {

  /* ----------------------------------------------------------------
  	search
  ---------------------------------------------------------------- */
  var $searchInput = jQuery('#header-search :text'),
      defaultValue = $searchInput.val();
  $searchInput
    .focus(function () {
      // remove text
      if (this.value === defaultValue) {
        jQuery(this).val('');
      }
    })
    .blur(function() {
      // add text
      if (jQuery.trim(this.value) == '') {
        jQuery(this).val(defaultValue);
      }
    });


  /* ----------------------------------------------------------------
  	Homepage header carousel
  ---------------------------------------------------------------- */
  var $cycle = jQuery('#header-carousel'),
      $navLinks = jQuery('#header-carousel-navigation a');

  $cycle.cycle({
    before: function() {
      $navLinks.removeClass('selected');
      jQuery('a[href=#'+ this.id +']', jQuery('#header-carousel-navigation')).addClass('selected');
    }
  }).add($navLinks).mouseover(function() {
    $cycle.cycle('pause');
  }).mouseout(function() {
    $cycle.cycle('resume');
  });
  $navLinks.each(function(i) {
    jQuery(this).hoverIntent({
      over: function() {
        $cycle.cycle(i);
      },
      out: function() {}
    });
  }).click(function() {
    return false;
  });


  /* ----------------------------------------------------------------
    Country selector
  ---------------------------------------------------------------- */
  /*jQuery('#language').hide();
  jQuery('#header-links > .link').click(function() {
    if(jQuery('#language').is(':visible')) {
      jQuery('#language').hide().unbind('clickoutside');
    } else {
      jQuery('#language').show().bind('clickoutside', function() {
        jQuery('#language').hide().unbind('clickoutside');
      });
    }
    return false;
  });

  // clickoutside some element
  var elems = jQuery([]);
  jQuery.event.special.clickoutside = {
    setup: function(){
      elems = elems.add( this );
      if ( elems.length === 1 ) {
        jQuery(document).bind( 'click', handle_event );
      }
    },
    teardown: function(){
      elems = elems.not( this );
      if ( elems.length === 0 ) {
        jQuery(document).unbind( 'click', handle_event );
      }
    },
    add: function( handleObj ) {
      var old_handler = handleObj.handler;
      handleObj.handler = function( event, elem ) {
        event.target = elem;
        old_handler.apply( this, arguments );
      };
    }
  };*/

  function handle_event( event ) {
    jQuery(elems).each(function(){
      var elem = jQuery(this);
      if ( this !== event.target && !elem.has(event.target).length ) {
        elem.triggerHandler( 'clickoutside', [ event.target ] );
      }
    });
  };

  
  /* ----------------------------------------------------------------
    Product carousel
  ---------------------------------------------------------------- */
  jQuery('.scrollable').scrollable({
    circular: true,
    speed: 500,
    easing: 'easeInCubic',
    mousewheel: true
  }).navigator();
  
  
  /* ----------------------------------------------------------------
    Read more
  ---------------------------------------------------------------- */
  jQuery('.read-more').collapser({
    duration: 250,
    easing: 'swing',
    triggerPosition: 'next',
    trigger: '<p class="right"><a href="#" class="button1">read more<b></b></a></p>',
    onOpen: function() {
      jQuery('a', this).html('read less<b></b>');
    },
    onClose: function() {
      jQuery('a', this).html('read more<b></b>');
    }
  });
  
  
  /* ----------------------------------------------------------------
    File icons
  ---------------------------------------------------------------- */
  jQuery.fn.addText = function(addText) {
    return this.each(function() {
      var $el = jQuery(this),
          text = $el.text();
      $el.text(text + addText);
    });
  };
  jQuery('a[href$=.pdf]').addClass('file');
  jQuery('a[href$=.pdf]').addClass('pdf').addText(' (PDF)');
  
  
  /* ----------------------------------------------------------------
    tabs
  ---------------------------------------------------------------- */
  jQuery.fn.tabs = function() {
    return this.each(function() {
      var $ul = jQuery(this),
          containersSelector = [];
      jQuery('a', $ul).each(function() {
        containersSelector.push(jQuery(this).attr('href'));
      });
      containersSelector = containersSelector.join(', ');
      jQuery('a', $ul).click(function() { //[href^=#]
        jQuery('a.selected', $ul).removeClass('selected');
        jQuery(this).addClass('selected');
        jQuery(containersSelector).hide().filter(this.hash).show();
        return false;
      }).filter(':first').click();
    });
  };
  // init
 jQuery('#main .client-tabs, #sidebar .client-tabs').tabs();
  
  
  /* ----------------------------------------------------------------
    lightbox
  ---------------------------------------------------------------- */
  jQuery('a.lightbox').fancybox({
    //overlayColor: '#fff',
    //overlayOpacity: '0.8',
    titlePosition: 'over',
    transitionIn: 'none',
    transitionOut: 'none'
  });
  
  
  /* ----------------------------------------------------------------
    loader
  ---------------------------------------------------------------- */
  var $loader = jQuery('#loader'),
      loaderWidth = $loader.width(),
      loaderHeight = $loader.height(),
      $window = jQuery(window),
      windowWidth = $window.width(),
      windowHeight = $window.height();
  $loader.offset({
    top: (windowHeight - loaderHeight) / 2,
    left: (windowWidth - loaderWidth) / 2
  });
  /*console.log($loader.offset());*/


  /* ----------------------------------------------------------------
    sub menu
  ---------------------------------------------------------------- */
  jQuery('#sub-menu')
    .find('ul').hide()
    .end()
    .find('a:has(+ ul)').click(function() {
      var $link = jQuery(this), 
          $ul = $link.next(),
          $allUl = jQuery('#sub-menu ul');
      if ($link.attr('rel') !== 'redirect') {
        if ($ul.is(':visible')) {
          $allUl.slideUp().prev().removeClass('open');
        } else {
          $allUl.slideUp().prev().removeClass('open');
          $ul.slideDown().prev().addClass('open');
        }
        return false;
      }
    });
    setTimeout(function() {
      jQuery('#sub-menu a.open + ul').slideDown();
    }, 1250);


  /* ----------------------------------------------------------------
    count height
  ---------------------------------------------------------------- */
  jQuery.fn.countHeight = function(el, iterator) {
    return this.each(function() {
      var $parent = jQuery(this),
          height,
          maxHeight = 0,
          heights = [];
          
      $parent.find(el).each(function(i) {
        height = jQuery(this).height();
        maxHeight = Math.max(height, maxHeight);
        if (i % iterator == iterator - 1) {
          heights.push(maxHeight);
          maxHeight = 0;
        }
      }).each(function(i) {
        var cycle = Math.floor(i / iterator);
        jQuery(this).height(heights[cycle]);
      });
    });
  };

  jQuery('ul.products').countHeight('> li', 2);
  jQuery('ul.ps-brands').countHeight('> li', 3);


});
