$('html').addClass('js');

(function($) {
  // toggle the visibility of an element based on the checked attribute of another element
  $.fn.radioForms = function(options) {
    return this.each(function() {
      var opts = $.extend({}, $.fn.radioForms.defaults, options);
      var container = this;
      var showHide = function(showMethod) {
        $(opts.clickElement, container).each(function(index) {
          $(this).parents('div:first').removeClass('active');
          if (this.checked) {
            $(opts.changeElement + ':eq(' + index + ')', container)[showMethod]();
            $(this).parents('div:first').addClass('active');
          } else {
            $(opts.changeElement + ':eq(' + index + ')', container).hide();
          }
        });
      };
      $(opts.clickElement, container).click(function() {
        showHide('show');
      });
      showHide('show');
    });
  };
  $.fn.radioForms.defaults = {
    clickElement: ':radio',
    changeElement: 'form'
  };
})(jQuery);

/* product feature images */
$(document).ready(function() {
  var $productImages = $('#product-feature-images');
  if (!$productImages.length) { return; }

  $.getScript('/scripts/jquery.cycle.js', function() {
    setTimeout(function() {
      $productImages.cycle({
        speed: 400,
        timeout: 0,
        pager: '#product-feature-nav',
        pagerAnchorBuilder: function(index, el) {
          return $('#product-feature-nav').find('a')[index];
        }
      });
    }, 10);
  });

});

/* shadowbox video */
$(document).ready(function() {
  var $sbVideo = $('.video');
  if (!$sbVideo.length) { return; }

  var sboptions = {
    assetURL: '/',
    overlayBgImage: 'assets/images/overlay-85.png',
    loadingImage: 'assets/images/sb-loading.gif',
    flvPlayer: 'scripts/flvplayer.swf',
    keysClose:          ['c', 27] // c or esc
  };
  $sbVideo.attr({'rel': 'shadowbox;width=640;height=360'});

  $.getScript('/scripts/jquery.shadowbox.min.js', function() {
    setTimeout(function() {
      Shadowbox.init(sboptions);
    }, 10);
  });

});

// form handling: toggle visibility and validation
$(document).ready(function() {
  var $forms = $('.contact-forms').radioForms();
  $('.contact-forms').css('height', $('.contact-forms').height());
  var $contactForm = $forms.find('form');

  var submitOptions = {
    clearForm: true,
    beforeSubmit: function() {
      $('<div class="loading"><img src="/assets/images/loading.gif" alt="sending... "></div>').appendTo($forms);
      $('<div class="overlay"></div>').css({opacity: '.4'}).appendTo($forms);
    },
    success: function(responseText) {
      $('.form-select').fadeOut(600);
      $('.loading').hide().addClass('box noticebox');
      $contactForm.fadeOut(600, function() {
        var response = $('<div>' + responseText + '</div>').find('.content-inner p:first').html();
        $('.loading').fadeIn(2000).html(response);
      });
      $('div.overlay').fadeOut(600, function() {
        $(this).remove();
        $('.loading img').remove();
      });
    }
  };
  if (typeof $.fn.tinyvalidate !== 'undefined'){
    $contactForm.tinyvalidate({
      positionElement: ['inline','form'],
      primaryEvent: 'blur',
      secondaryEvent: 'submit',
      submitDetails: false,
      submitOverride: function() {
        $contactForm.ajaxSubmit(submitOptions);
      }
    });
    $('.validate form, form.validate').tinyvalidate({
      positionElement: ['inline','form'],
      primaryEvent: 'blur',
      secondaryEvent: 'submit',
      submitDetails: false
    });
  };

}); // document.ready

/* convert link to flv object */
$(document).ready(function() {
  if ($('a.media').length) {

    var mediaOptions = {
      flvPlayer:	 '/scripts/flvplayer.swf',
      flashvars:	{autostart: true},
      width: 640,
      height: 380,
      autoplay: 1
    };

    $.getScript('/scripts/jquery.media.js', function() {
      window.setTimeout(function() {
        $.fn.media.defaults.flvPlayer = '/scripts/flvplayer.swf';
        $('a.media').media(mediaOptions);
      }, 10);
    });
  }
});

$(document).ready(function() {
  // image zoom
  if (typeof $.fn.colorbox !== 'undefined') {
    $.fn.colorbox.next = $.fn.colorbox.prev = $.fn.colorbox.close;
    $("a[rel=lightbox]").colorbox({
      opacity: .5,
      current: ''
    });
    $(window).bind('load', function() {
      $('#cboxPrevious, #cboxNext').remove();
    });

  };
  // zoom icons
  $('a.zoom').append('<img class="icn-zoom" src="/assets/images/icons/plus.png">');

});

