/**
 * This is part of a patch to address a jQueryUI bug.  The bug is responsible
 * for the inability to scroll a page when a modal dialog is active. If the content
 * of the dialog extends beyond the bottom of the viewport, the user is only able
 * to scroll with a mousewheel or up/down keyboard keys.
 *
 * @see http://bugs.jqueryui.com/ticket/4671
 * @see https://bugs.webkit.org/show_bug.cgi?id=19033
 * @see /views_ui.module
 * @see /js/jquery.ui.dialog.min.js
 *
 * This javascript patch overwrites the $.ui.dialog.overlay.events object to remove
 * the mousedown, mouseup and click events from the list of events that are bound
 * in $.ui.dialog.overlay.create
 *
 * The original code for this object:
 * $.ui.dialog.overlay.events: $.map('focus,mousedown,mouseup,keydown,keypress,click'.split(','),
 *  function(event) { return event + '.dialog-overlay'; }).join(' '),
 *
 */

(function ($, undefined) {
  if ($.ui && $.ui.dialog) {
    $.ui.dialog.overlay.events = $.map('focus,keydown,keypress'.split(','),
                                 function(event) { return event + '.dialog-overlay'; }).join(' ');
  }
}(jQuery));
;
/**
 * @author Antoine Lafontaine
 */

(function ($) {
  
  $(function() {
  	// Declare global variables
  	var totalItems = $('#feature .content .item').length;
  	var currentItem = null;
  	var inTransition = false;
  	var captionAnimationSpeed = 500;
  	var imageAnimationSpeed = 500;
  	var interval;
  	
  	$('#feature .caption').css('opacity', 0.0)
  	
  	$('#feature .nav .item').bind('click', function(/* Event */ e) {
  		clearInterval(interval);
  		var elm = $(e.target);
  		if (elm.is('h1') || elm.is('p')) {
  			elm = elm.parent();
  		}
  		showItem(elm.attr('id').replace(/item-/i, ''));
  	});
  	function showItem(id) {
  		if (!inTransition) {
  			inTransition = true;
  			if (currentItem) {
  				$('#item-' + currentItem).removeClass('current');
  				$('#item-' + id).addClass('current');
  				$('#caption-' + currentItem).animate({opacity: 0.0, height: '0'}, captionAnimationSpeed, function() {
  					$('#caption-' + id).animate({opacity: 0.7, height: '64px'}, captionAnimationSpeed);
  					$('#content-' + currentItem).animate({opacity: 0.0}, imageAnimationSpeed, function() {
  						$(this).css('display', 'none');
  					});
  					$('#content-' + id).css('opacity', 0.0).css('display', 'block').animate({opacity: 1.0}, imageAnimationSpeed, function() {
  						currentItem = id;
  						inTransition = false;
  					});
  				});
  			} else {
  				$('#item-' + id).addClass('current');
  				$('#caption-' + id).animate({opacity: 0.7, height: '64px'}, 250, function() {
  					currentItem = id;
  					inTransition = false;
  				});
  			}
  		}
  	}
  	showItem(1);
  	interval = setInterval(function() {
  		id = (currentItem + 1 <= totalItems) ? currentItem + 1 : 1;
  		showItem(id);
  	}, 5000)
  });
  
})(jQuery);;

