// application.js

// namespacing custom functions
EB = {

  // modals: hide function
  hideModal: function(){
    $('.overlay').fadeOut(600, function(){
      $(this).remove();
    })
    $('.modal').fadeOut(400);
  },

  // carousel: callbacks
  mycarouselInitCallback: function(carousel) {
    jQuery('.slide-controls a').bind('click', function() {
      carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr("data-position")));
      return false;
    });
    jQuery('#carousel-next').bind('click', function() {
      carousel.next();
      return false;
    });
    jQuery('#carousel-prev').bind('click', function() {
      carousel.prev();
      return false;
    });
  },

  // tooltip setup
  tooltip: function() {
    var tooltip_id = $(this).attr("data-tooltip");
    $('#' + tooltip_id).fadeToggle(300);
    return false;
  },

  // functions for controlling the careers image slider and container
  scrollChange: function(e, ui){
    var maxScroll = $(".staff-image .image-wrapper").width() - $(".staff-image .image-container").width();
    $(".staff-image .image-wrapper").animate({
      style: 'margin-left: -' + ui.value * (maxScroll / 100) + 'px' }, 1000
    );
  },
  scrollSlide: function(e, ui){
    var maxScroll = $(".staff-image .image-wrapper").width() - $(".staff-image .image-container").width();
    $(".staff-image .image-wrapper").attr({
      style: 'margin-left: -' + ui.value * (maxScroll / 100) + 'px' }
    );
  },

  // randomly display a different piece of trivia in the footer
  randomTrivia: function(){
    var length = $(".trivia ul li").length;
    var ran = Math.floor(Math.random()*length) + 1;
    $(".trivia ul li:nth-child(" + ran + ")").show();
  }

}

// modal: center function
jQuery.fn.centerModal = function () {
  var top = (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop();
  top = top > 0 ? top : 30;
  this.css("top", top + "px");
  this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
  return this;
}

// document load javascript
$(function(){

  // carousel: homepage > product slideshow (top)
  $('.product-promos ul').jcarousel({
    wrap: 'last',
    scroll: 1,
    visible: 1,
    auto: 20,
    animation: 1200,
    initCallback: EB.mycarouselInitCallback,
    itemVisibleInCallback: function(carousel, item, index, action) {
      var itemClass = $(item).attr("class").split(' ')[0];
      $('.slide-controls li').removeClass('active');
      $('.slide-controls .' + itemClass).addClass('active');
    },
    buttonNextHTML: null,
    buttonPrevHTML: null
  });

  // carousel: homepage > featured press (bottom)
  $('.home .press-slideshow ul').jcarousel({
    wrap: 'circular',
    scroll: 3,
    auto: 0,
    animation: 1200,
    visible: 3
  });

//carousel: internal
  $('.ems-accessories ul, .smart-accessories ul').jcarousel({
	    wrap: 'circular',
	    scroll: 0,
	    auto: 0,
	    animation: 0,
	    visible: 3
	  });
  
  
  // modal: open if link rel == modal #id
  $(".modal-button" ).click(function(e) {
    if ($(this).data('disabled')) {
      return false;
    }
    var modalID = $(this).data('modal');
    $('#' + modalID).fadeIn(300);
    $('.modal').centerModal();
    $("body").append('<div class="overlay"></div>');
    $('.overlay').hide().css({height: $(document).height() + "px"}).fadeIn(300);
    return false;
  });

  // modal: call hide function
  $(".overlay").live("click", function() {
    EB.hideModal();
  });
  $(".modal .close").click(function(){
    EB.hideModal();
    return false;
  });

  // webportal login: toggle modal box
  $('.login-button').click(function(){
    $('.login-modal').fadeToggle("slow");
    $("body").append('<div class="login-overlay"></div>');
    $('.overlay').css({height: $(document).height() + "px"});
    return false;
  });

  // homepage: animated sprite hover on steps area
  $('.steps').hover(
    function(){
      $(this).find('.slide-2').clearQueue().delay(200).fadeIn();
      $(this).find('.slide-3').clearQueue().delay(400).fadeIn();
      $(this).find('.slide-4').clearQueue().delay(600).fadeIn();
    },
    function(){
      $(this).find('.slide-2').clearQueue().delay(600).fadeOut();
      $(this).find('.slide-3').clearQueue().delay(400).fadeOut();
      $(this).find('.slide-4').clearQueue().delay(200).fadeOut();
    }
  );

  // init accordion effects
  $('.accordion h2').click(function(){
    $(this).toggleClass('active').siblings('.accordion-body').slideToggle('fast');
  });

  // tooltip function for comparison tables
  $(".tooltip-button" ).hover(EB.tooltip, EB.tooltip);
  $(".tooltip-button" ).click(function(){
    return false;
  });

  // Where to buy form area, hide and show location notice
  $('.where-buy-intro-form input').hover(
    function () {
      $('.where-buy-intro-form .notice').fadeIn('200');
    },
    function () {
      $('.where-buy-intro-form .notice').fadeOut('100');
    }
  );

  // init slider for careers page
  $(".staff-image .slider-wrapper").slider({
    animate: true,
    //change: EB.scrollChange,
    slide: EB.scrollSlide
  });

  // toggle tutorial videos via select tag
  $('#tutorials-videos-selector').change(function() {
    $('.tutorial-video-list ol').hide();
    var panel = $(this).val();
    $('#' + panel).slideDown();
  });

  // init the random trivia function
  EB.randomTrivia();

 // toggle accessories via select tag
  $('#accessories-selector').change(function() {
    $('.accessory-panel').hide();
    var panel = $(this).val();
    $('#' + panel).slideDown();
    $('html, body').animate({ scrollTop: '1050px' }, 'slow');
  });

 // toggle accessories via links
  $('.accessories-banner a').click(function() {
    $('.accessory-panel').hide();
    var accID = $(this).attr('href');
    $(accID).show();
    $('html, body').animate({ scrollTop: '1050px' }, 'slow');
    return false;
  });
 $('#partner-seller-modal button.redirect').click(function(){
    window.location = $(this).data('url');
    return false;
  });

  $('#partner-seller-modal button.cancel').click(function(){
    $('#partner-seller-modal a.close').click();
    return false;
  });

});

