﻿
$(document).ready(function() {

  var $sizeGuide;
  var $deliveryInfo;

  $('div#stockInner div#step1 div#thumbnails a:not(.current)').live(
    'click',
    function(e) {
      e.preventDefault();
      $('div#top').load(this.href + '  div#image, div#stock',
        function onCallbackComplete() {
          initLightbox();
          initCloudZoom();
          initScrollViews();
          addthis.toolbox($('div.addthis_toolbox').get(0));
        }
      );
    }
  );

  function initLightbox() {
    $('a[rel^=prettyPhoto[image]]').prettyPhoto();
    if ($.browser.msie && parseInt($.browser.version) < 9) {
      $('a.video').click(function(e) {
        e.preventDefault();
        var $target = $($(this).data('inline-target'));
        $target.dialog({ modal: true, width: 670, resizable: false, dialogClass: 'video' });
      });
    } else {
      $('a[rel^=prettyPhoto[video]]').prettyPhoto({ default_width: 640, default_height: 360 });
    }
  }
  initLightbox();

  function initCloudZoom() {
    if (!isiPhone()) {
      $('.cloud-zoom, .cloud-zoom-gallery').one("mouseenter", function() {
        $('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
      });
    }
  }

  function isiPhone() {
    return (
              (navigator.platform.indexOf("iPhone") != -1) ||
              (navigator.platform.indexOf("iPod") != -1) ||
              (navigator.platform.indexOf("iPad") != -1)
          );
  }

  function initScrollViews() {
    var $views = $('div#alternativeViews');
    var $ctnr = $views.find('.scroll-ctnr');
    if ($ctnr.innerHeight() == parseInt($ctnr.css('max-height'))) {
      $ctnr.css('overflow-y', 'hidden');
      $views.find('.scroll-btn').css('display', 'block').eq(1).addClass('enabled');
    }
  }
  initScrollViews();

  $('a.scroll-up').live('click', function(e) {
    e.preventDefault();
    $('.scroll-btn').addClass('enabled');
    var $ctnr = $(this).next();
    $ctnr.scrollTop(Math.max(0, $ctnr.scrollTop() - $ctnr.innerHeight()));
    if ($ctnr.scrollTop() == 0) {
      $(this).removeClass('enabled');
    }
    return false;
  });

  $('a.scroll-down').live('click', function(e) {
    e.preventDefault();
    $('.scroll-btn').addClass('enabled');
    var $ctnr = $(this).prev();
    var max = $ctnr[0].scrollHeight - $ctnr.innerHeight();
    $ctnr.scrollTop(Math.min(max, $ctnr.scrollTop() + $ctnr.innerHeight()));
    if ($ctnr.scrollTop() == max) {
      $(this).removeClass('enabled');
    }
    return false;
  });

  $('a#sizeGuide').live('click', function(e) {
    e.preventDefault();
    showSizingInfo($(this))
  });

  function showSizingInfo(a) {
    if (!$sizeGuide) {
      $sizeGuide = $('<div title="Size Guide"></div>').load('../SizingInformation').dialog({ autoOpen: false, show: 'blind', hide: 'slide', modal: true, width: 490, height: 500 });
    }
    $sizeGuide.dialog('open');
  }

  $('a#deliveryInformation').live('click', function(e) {
    e.preventDefault();
    showDeliveryInfo($(this));
  });

  $('#detailsTechnology').removeClass('active');

  $('#details').delegate('.tabs a:not(.active)', 'click', function(e) {
    e.preventDefault();
    var $this = $(this);
    $('#details .tabs .tab').removeClass('active');
    $('#details .panels .panel').removeClass('active');
    $this.addClass('active');
    $($this.attr('href')).addClass('active');
    return false;
  });


  $('#detailsTechnology a.active').each(function(e) {
    var $this = $(this);
    $this.attr('title', '');
  });

  $('#detailsTechnology a.active')
    .mouseleave(function(e) {
      e.preventDefault();
      $('#ratingDescription').hide().find('#ratingDescriptionContent').html('');
      return false;
    })
    .mouseenter(function(e) {
      e.preventDefault();
      var $this = $(this);
      var $ratingDesc = $('#ratingDescription');
      $('#ratingDescriptionContent').html('<h5>' + $this.data('title') + '</h5><p>' + $this.data('description') + '</p>');
      var pos = $this.position();
      $('#ratingDescriptionArrow').css('left', pos.left + 10);
      $ratingDesc.css('top', pos.top - $ratingDesc.height() - 10);
      $('#ratingDescription').show();
      return false;
    });

  $("#GiftCardAmount").keydown(function(e) {
    if ((!e.shiftKey && !e.ctrlKey && !e.altKey) && ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105))) { }
    else if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 39) {
      e.preventDefault();
      return false;
    }
  });


  function showDeliveryInfo(a) {
    if (!$deliveryInfo) {
      $deliveryInfo = $('<div title="Delivery Information"></div>').load('../DeliveryInformation').dialog({ autoOpen: false, show: 'blind', hide: 'slide', modal: true, width: 620, height: 500 });
    }
    $deliveryInfo.dialog('open');
  }

  $(".tabs a").click(function() { _gaq.push(['_trackEvent', 'ProductDetail', 'TabsChange', $(this).attr('href')]); });
});

