/*-------- SLIDE --------------*/

function slide_button_clicked () {
    clearTimeout(window.slide_to);
    var index = $(this).attr('id').split('_')[1];
    if (index == 'next') {
      next_slide();
    } else if (index == 'prev') {
      prev_slide();
    } else {
      index = parseInt(index);
      caro_scroll_to_index(index);
    }
  }
function next_slide()
{
  var next = window.active_slide + 1;
  if (next > window.n_slide) {
    next = 1;
  }
  caro_scroll_to_index(next);
}
function prev_slide()
{
  var next = window.active_slide - 1;
  if (next <= 0) {
    next = window.n_slide;
  }
  caro_scroll_to_index(next);
}
  function caro_scroll_to_index(index)
  {
    $('.home-slider-nav-button').unbind('click');
    var active_slide = window.active_slide;
    var target_slide = index;
    if (active_slide != target_slide) {     
        var active_button = '#slider-btn_' + active_slide;
        $(active_button).removeClass('slide_button_active');
        $('#home-slide_' + active_slide).fadeOut(400, function () {
          $('#home-slide_' + target_slide).fadeIn(400);
          window.active_slide = target_slide;
          var active_button = "#slider-btn_" + target_slide;
          $(active_button).addClass('slide_button_active');
          if (window.mouse_inside_slide == 0) {
            addTimeout();
          }
          $('.home-slider-nav-button').click(slide_button_clicked);
        });
    } else {
      $('.home-slider-nav-button').click(slide_button_clicked);
    }
  }
function addTimeout()
{
   clearTimeout(window.slide_to);
   window.slide_to = setTimeout('next_slide()', window.fade_delay);
}

function Carouselku()
{
  this.slide_delay = 1000;
  this.carousels = new Array();
  this.checkHashChange = function () {
    for (var i in this.carousels) {
      if (this.carousels[i].hash_change) {
        this.carousels[i].hash_change();
      }
    }
  };
  this.shiftFinish = function (param, caro, with_hook) {
    if (with_hook == undefined) {
      with_hook = true;
    }
    this.enableShiftControl(caro);
    if (with_hook && caro.finish_hook) {
      caro.finish_hook(caro);
    }
  }
  this.goToId = function (index, caro, caroku, with_hook, with_animation) {
    if (with_hook == undefined) {
      with_hook = true;
    }
    if (with_animation == undefined) {
      with_animation = true;
    }
    caroku.disableShiftControl(caro);
    if (!index) {
      index = 0;
    }
    if ((index < 0) || (index >= caro.count)) {
      index = 0;
    }
    if (index == caro.curpos) {
      caroku.shiftFinish('goto', caro, with_hook);
    } else {
      var shift;
      if (index > caro.curpos) {
        shift = '-=' + (caro.width * (index-caro.curpos))
      } else {
        shift = '+=' + (caro.width * (caro.curpos-index))
      }
      caro.curpos = index;
      caroku.ensureActive(caro);
      if (with_animation) {
        delay = caroku.slide_delay;
      } else {
        delay = 0;
      }
      $('#' + caro.slider_id).animate({
          left: shift
        }, delay, function () {
          caroku.shiftFinish('goto', caro, with_hook);
        }
      );
    }
  }
  this.select = function (ev) {
    var id;
    id = parseInt($(this).attr('id').split('_')[1]);
    var caro = ev.data.caro;
    var caroku = ev.data.caroku;
    caroku.goToId(id, caro, caroku);
  }
  this.shiftLeft = function (ev) {
    var caro = ev.data.caro;
    var caroku = ev.data.caroku;
    caroku.disableShiftControl(caro);
    if (caro.curpos > 0) {
      caro.curpos--;
      caroku.ensureActive(caro);
      $('#' + caro.slider_id).animate({
          left: '+=' + caro.width
        }, caroku.slide_delay, function () {
          caroku.shiftFinish('left', caro);
        }
      );
    } else {
      caroku.shiftFinish('left', caro);
    }
  }
  this.shiftRight = function (ev) {
    var caro = ev.data.caro;
    var caroku = ev.data.caroku;
    caroku.disableShiftControl(caro);
    if (caro.curpos < caro.count - 1) {
      caro.curpos++;
      caroku.ensureActive(caro);
      $('#' + caro.slider_id).animate({
          left: '-=' + caro.width
        }, caroku.slide_delay, function () {
          caroku.shiftFinish('right', caro);
        }
      );
    } else {
      caroku.shiftFinish('right', caro);
    }
  }
  this.enableShiftControl = function (caro) {
    $('.' + caro.shift_left_class).bind('click', {caro: caro, caroku: this}, this.shiftLeft);
    $('.' + caro.shift_right_class).bind('click', {caro: caro, caroku: this}, this.shiftRight);
    $('.' + caro.select_class).bind('click', {caro: caro, caroku: this}, this.select);
  }
  this.disableShiftControl = function (caro) {
    $('.' + caro.shift_left_class).unbind('click');
    $('.' + caro.shift_right_class).unbind('click');
    $('.' + caro.select_class).unbind('click');
  }
  this.addCarousel = function (caro) {
    this.carousels.push(caro);
  }
  this.ensureActive = function (caro) {
    if (caro.count > 0) {
      $('.' + caro.nav_class).addClass('active');
    } else {
      $('.' + caro.nav_class).removeClass('active');
    }
    if (caro.curpos == 0) {
      $('.' + caro.shift_left_class).removeClass('active');
    }
    if (caro.curpos == caro.count - 1) {
      $('.' + caro.shift_right_class).removeClass('active');
    }
    var cur_select_id = '#' + caro.select_class + '_' + caro.curpos;
    $(cur_select_id).removeClass('active');
  }
  this.run = function () {
    for (var i in this.carousels) {
      this.ensureActive(this.carousels[i]);
      this.enableShiftControl(this.carousels[i]);
    }
    this.checkHashChange();
  }
}

function get_subcat_curpos()
{
  /*
  var curpos = parseInt(window.location.hash.split('#')[1]);
  if (isNaN(curpos)) {
    curpos = 0;
  } else {
    curpos = curpos-1;
  }
  window.subcat_curpos = curpos;
  */
  window.subcat_curpos = 0;
}

function promo_clicked()
{
    var index;
    index = $(this).attr('id').split('_')[1];
    promo_fade_to(index);
}

function promo_fade_to(target_index)
{
  if (window.active_promo != target_index) {
      $('.promotion-item-content').unbind('click');
      $('#promotion-item-content_' + window.active_promo).removeClass('promo-active');
      $('#promotion-item-content_' + target_index).addClass('promo-active');
      $('#promotion-' + window.active_promo).fadeOut(400, function () {
        //$('#promotion-' + target_index).fadeIn(400);
        $('#promotion-' + target_index).css('visibility','visible').hide().fadeIn(400);
        window.active_promo = target_index;
        $('.promotion-item-content').bind('click', promo_clicked);
      });
  } else {
  }
}

$(document).ready(function() {
    window.alert = $.ccDialogBox;
    /*Handles front-page fading on hover*/
	$("img.hover_fade").hover(
	function() {
	$(this).stop().animate({"opacity": "0"},  "fast");
	},
	function() {
	$(this).stop().animate({"opacity": "1"},  "fast");
	});	
    
    /*Handles front-page fading on hover*/
	$("img.hover_fade_rev").hover(
	function() {
	$(this).stop().animate({"opacity": "1"},  "fast");
	},
	function() {
	$(this).stop().animate({"opacity": "0"},  "fast");
	});	
    
    /*Handles register page hover */
    $("input.hover_fade").hover(
	function() {
	$(this).stop().animate({"opacity": "0"},  100);
	},
	function() {
	$(this).stop().animate({"opacity": "1"},  100);
	});	
	$("input.hover_fade").click(
	function() {
	$(this).stop().animate({"opacity": "1"},  100);
	});

	/*Handles colorbox */
    if ('colorbox' in $) {
        $(".aibi-video").colorbox({iframe:true, innerWidth:480, innerHeight:390});
        $(".aibi-page").colorbox({iframe:true, innerWidth:550, innerHeight:470});
        $(".aibi-tvc-video").colorbox({iframe:true, innerWidth:480, innerHeight:390});
    }
    /*Handles Superfish */
    $('ul.sf-menu').superfish();
    
  /*
   * Handles fading banner
   */
  window.active_slide = 1;
  //window.n_slide = 2; // moved to html page
  window.fade_delay = 3500;
  window.mouse_inside_slide = 0;
  
  $('.home-slider-nav-button').click(slide_button_clicked);
  
  $('.stop-fading').hover(
  function () {clearTimeout(window.slide_to); window.mouse_inside_slide = 1;}, 
  function () {addTimeout(); window.mouse_inside_slide = 0;}
  );
     
  window.slide_to = setTimeout('next_slide();', window.fade_delay);

  slide_delay = 1000;

  /*
   * Handle search box
   */
  $('.box').bind('focusin', function () {
      if ($(this).val() == window.search_text) {
        $(this).val('');
        $(this).removeClass('blur');
      }
  });
  $('.box').bind('focusout', function () {
      if ($(this).val() == '') {
        $(this).val(window.search_text);
        $(this).addClass('blur');
      }
  });
  if (($('.box').val() == '') || ($('.box').val() == window.search_text)) {
    $('.box').val(window.search_text);
    $('.box').addClass('blur');
  }

  /*
   * Handles fading promotion
   */
  window.active_promo = 0;
  $('.promotion-item-content').bind('click', promo_clicked);

  /*
   * Handle browse by brands
   */
  $('#browse_brands_select').bind('change', function () {
      var brand_id = $(this).attr('value');
      if (brand_id == 'all') {
        brand_id = '';
      }
      window.location = '?brand=' + brand_id;
  });

  /*
   * Handle locate
   */
  $('.locate-country-item').bind('click', function() {
      var country = $(this).attr('id').split('_')[1];
	  $('.locate-expandable').removeClass('invisible');
      $('.locate-expandable-item').addClass('display_none');
      $('#locate-expandable-' + country).removeClass('display_none');
      $('.locate-country-item').removeClass('promo-active');
      $('#locate-country-item_' + country).addClass('promo-active');
  });

  /*
   * button hover
   */
  $('.active-inactive-btn').bind('mouseover', function() {
      $(this).find('.btn-inactive').addClass('invisible');
      $(this).find('.btn-active').removeClass('invisible');
  });
  $('.active-inactive-btn').bind('mouseout', function() {
      $(this).find('.btn-inactive').removeClass('invisible');
      $(this).find('.btn-active').addClass('invisible');
  });

  get_subcat_curpos();

  /*
   * carouselku
   */
  carouselku = new Carouselku();
  carouselku.addCarousel({
    name: 'subcat',
    count: window.subcat_count,
    curpos: window.subcat_curpos,
    width: 784,
    nav_class: 'subcat-nav',
    shift_left_class: 'subcat-nav-left',
    shift_right_class: 'subcat-nav-right',
    select_class: 'subcat-nav-select',
    slider_id: 'subcat-slider',
    finish_hook: function (caro) {
      if ('onhashchange' in window) {
        window.location.hash = '#' + (caro.curpos+1);
      }
    },
    hash_change: function () {
      var id;
      var window_hash = window.location.hash;
      id = parseInt(window_hash.split('#')[1])-1;
      $('.change_hash').each(function () {
        $(this).attr('href', $(this).attr('href').split('#')[0] + window_hash);
      });
      carouselku.goToId(id, this, carouselku, false, false);
    }
  });
  carouselku.addCarousel({
    name: 'other-prods',
    count: window.other_prods_count,
    curpos: 0,
    width: 784,
    nav_class: 'other-prods-nav',
    shift_left_class: 'other-prods-nav-left',
    shift_right_class: 'other-prods-nav-right',
    select_class: 'other-prods-nav-select',
    slider_id: 'other-prods-slider'
  });
  carouselku.addCarousel({
    name: 'promotions',
    count: window.promotions_count,
    curpos: 0,
    width: 772,
    nav_class: 'promotions-nav',
    shift_left_class: 'promotions-nav-left',
    shift_right_class: 'promotions-nav-right',
    select_class: 'promotions-nav-select',
    slider_id: 'promotions-slider'
  });
  $(window).bind('hashchange', function () {
    carouselku.checkHashChange();
  });
  carouselku.run();

  $('form.krco-product-details-cart').cc_form_validate();
  $('#krco-product-image-frame').ccSlider({
      $nav: $('div.krco-product-image-thumbnail'),
      autoSlide: false
  });
  $('div.krco-product-image-display a').colorbox();
  $('div.aibi-product-specifications').ccGrouper('H3', {stripTitle: true, defaultTitle: 'Product Information'});
  $('div.aibi-product-specifications').ccCreateNav('aibi-product-spec-nav');
  $('div.aibi-product-specifications').ccSlider({$nav: $('.aibi-product-spec-nav'), autoSlide: false, transitionLength: 100});
  $('div.krco-product-details-description').ccGrouper('H3', {stripTitle: true, defaultTitle: 'Product Information'});
  $('div.krco-product-details-description').ccCreateNav('aibi-product-spec-nav');
  $('div.krco-product-details-description').ccSlider({$nav: $('.aibi-product-spec-nav'), autoSlide: false, transitionLength: 100});
});

