$(function() {

  /* ----------------------------------------------------------------
  	search
  ---------------------------------------------------------------- */
  var $searchInput = $('#header-search :text'),
      defaultValue = $searchInput.val();
  $searchInput
    .focus(function () {
      // remove text
      if (this.value === defaultValue) {
        $(this).val('');
      }
    })
    .blur(function() {
      // add text
      if ($.trim(this.value) == '') {
        $(this).val(defaultValue);
      }
    });


  /* ----------------------------------------------------------------
  	Homepage header carousel
  ---------------------------------------------------------------- */
  var $cycle = $('#header-carousel'),
      $navLinks = $('#header-carousel-navigation a');

  $cycle.cycle({
    before: function() {
      $navLinks.removeClass('selected');
      $('a[href=#'+ this.id +']', $('#header-carousel-navigation')).addClass('selected');
    }
  }).add($navLinks).mouseover(function() {
    $cycle.cycle('pause');
  }).mouseout(function() {
    $cycle.cycle('resume');
  });
  $navLinks.each(function(i) {
    $(this).hoverIntent({
      over: function() {
        $cycle.cycle(i);
      },
      out: function() {}
    });
  }).click(function() {
    return false;
  });


  /* ----------------------------------------------------------------
    Country selector
  ---------------------------------------------------------------- */
  /*$('#language').hide();
  $('#header-links > .link').click(function() {
    if($('#language').is(':visible')) {
      $('#language').hide().unbind('clickoutside');
    } else {
      $('#language').show().bind('clickoutside', function() {
        $('#language').hide().unbind('clickoutside');
      });
    }
    return false;
  });

  // clickoutside some element
  var elems = $([]);
  $.event.special.clickoutside = {
    setup: function(){
      elems = elems.add( this );
      if ( elems.length === 1 ) {
        $(document).bind( 'click', handle_event );
      }
    },
    teardown: function(){
      elems = elems.not( this );
      if ( elems.length === 0 ) {
        $(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 ) {
    $(elems).each(function(){
      var elem = $(this);
      if ( this !== event.target && !elem.has(event.target).length ) {
        elem.triggerHandler( 'clickoutside', [ event.target ] );
      }
    });
  };

  
  /* ----------------------------------------------------------------
    Product carousel
  ---------------------------------------------------------------- */
  $('.scrollable').scrollable({
    circular: true,
    speed: 500,
    easing: 'easeInCubic',
    mousewheel: true
  }).navigator();
  
  
  /* ----------------------------------------------------------------
    Read more
  ---------------------------------------------------------------- */
  $('.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() {
      $('a', this).html('read less<b></b>');
    },
    onClose: function() {
      $('a', this).html('read more<b></b>');
    }
  });
  
  
  /* ----------------------------------------------------------------
    File icons
  ---------------------------------------------------------------- */
  $.fn.addText = function(addText) {
    return this.each(function() {
      var $el = $(this),
          text = $el.text();
      $el.text(text + addText);
    });
  };
//  $('a[href$=.pdf]').addClass('file');
//  $('a[href$=.pdf]').addClass('pdf').addText(' (PDF)');
  
  
  /* ----------------------------------------------------------------
    tabs
  ---------------------------------------------------------------- */
  $.fn.tabs = function() {
    return this.each(function() {
      var $ul = $(this),
          containersSelector = [];
      $('a', $ul).each(function() {
        containersSelector.push($(this).attr('href'));
      });
      containersSelector = containersSelector.join(', ');
      $('a', $ul).click(function() { //[href^=#]
        $('a.selected', $ul).removeClass('selected');
        $(this).addClass('selected');
        $(containersSelector).hide().filter(this.hash).show();
        return false;
      }).filter(':first').click();
    });
  };
  // init
  $('#main .tabs, #sidebar .tabs').tabs();
  
  
  /* ----------------------------------------------------------------
    lightbox
  ---------------------------------------------------------------- */
  $('a.lightbox').fancybox({
    //overlayColor: '#fff',
    //overlayOpacity: '0.8',
    titlePosition: 'over',
    transitionIn: 'none',
    transitionOut: 'none'
  });
  
  
  /* ----------------------------------------------------------------
    print
  ---------------------------------------------------------------- */
  $('#main .product-detail .right-col').append('<p class="right"><a href="#" class="button-print">Print for later use<b></b></a></p>').find('.button-print').click(function() {
    window.print();
    return false;
  });
  
  
  /* ----------------------------------------------------------------
    loader
  ---------------------------------------------------------------- */
  var $loader = $('#loader'),
      loaderWidth = $loader.width(),
      loaderHeight = $loader.height(),
      $window = $(window),
      windowWidth = $window.width(),
      windowHeight = $window.height();
  $loader.offset({
    top: (windowHeight - loaderHeight) / 2,
    left: (windowWidth - loaderWidth) / 2
  });
  /*console.log($loader.offset());*/


  /* ----------------------------------------------------------------
    sub menu
  ---------------------------------------------------------------- */
  /*$('#sub-menu')
    .find('ul').hide()
    .end()
    .find('a.selected + ul').show()
    .end()
    .find('a:has(+ ul)').click(function() {
      var $ul = $(this).next(),
          $allUl = $('#sub-menu ul');
      if ($ul.is(':visible')) {
        $allUl.slideUp().prev().removeClass('open');
      } else {
        $allUl.slideUp().prev().removeClass('open');
        $ul.slideDown().prev().addClass('open');
      }
      return false;
    });*/

});
