/* Author: Anna Do <me at annahdo.com> */

$(function(){

$('nav .menu').hover(function () {
  var nav = $(this);
  nav.addClass('hover');
  nav.find('.submenu').slideDown('fast', function () {
    if (!nav.hasClass('hover')) {
      $(this).hide();
    }
  });
}, function () {
  var el = $(this);
  el.removeClass('hover');
  el.find('.submenu').hide();
});

// Constants
var SELECTED_CLS = 'selected',
   setRadioOption = function (target, classPrefix) {
      var $target = $(target);
      $form.find(classPrefix + ' li.selected').removeClass(SELECTED_CLS);
      $target = $target.parents('li').addClass(SELECTED_CLS);
      $form.find(classPrefix + '-selected').html($target.find('label').attr('title')).removeClass(classPrefix + '-not-selected');
      $form.find(classPrefix + ' .error').hide();
   };

/** Items Page */
// initialize image thumbnails
$('.alr-thumbnail').colorbox({ transition: 'fade', speed: 500, photo: true, current: '{current} of {total}', onComplete: function() {
  var content = $(this).attr('title');
  $('#cboxLoadedContent').append('<p>' + content + '</p>');
  $('#cboxTitle').hide();
  $.colorbox.resize();
} });

$('.alr-video').colorbox({ transition: 'fade', speed: 500, current: 'video {current} of {total}' }); 

$('.alr-popup').colorbox( { inline: true, href: function() {
  var href = $(this).attr('href');
  return href;
}});

// initialize shopping form    
var $form = $('#add-cart-form');
if ($form.length) {
   $form
      .delegate('.size input', 'change', function (e) { setRadioOption(e.target, '.size'); })
      .delegate('.color input', 'change', function (e) { setRadioOption(e.target, '.color'); });
   var size = $form.find('input:radio[name=os0]:checked');
   if (size.length) setRadioOption(size, '.size');
   var color = $form.find('input:radio[name=os1]:checked');
   if (color.length) {
      setRadioOption(color, '.color');
   }
}
$form.submit(function (e) {
  if (!$form.find('input:radio[name=os0]:checked').length) {
     $form.find('.sizing .error').show();
     e.preventDefault();
  } else if (!$form.find('input:radio[name=os1]:checked').length) {
     $form.find('.color .error').show();
     e.preventDefault();
  }
});
// initialize sizing and color info
$('#item .size-chart').colorbox({ transition: 'elastic', inline:true, href: '#sizing-info', width: '600px' });
$('#item .color-help').colorbox({ transition: 'elastic', inline:true, href: '#color-info', width: '400px' });

/*
$('#item .swatches a').colorbox({
   title: function () {
      return $(this).attr('title');
   },
   html: function () {
      var block, bg = $(this).css('background');
      block = $('<div style="width:100px; height:100px;">');
      block.css('background', bg);
      return block;
   }
})
*/


    
});




