// Since jQuery is running in noConfilct mode, we need to specifically bind jQuery to $ insde our function so we can use $
jQuery(function($) {

	var popeye = {
		settings: {
			animation: {
				inDuration: 1000,
				outDuration: 750
			},
			elements: {
				popeye: '.popeye', // DON'T Change this.
				activationLink: '.minimodule .more-button,#feature .more-button, #feature .offerImageLink, .minimodule .offerImageLink, #feature .eventImageLink, .minimodule .offerImageLink',
				overlay: '#overlay',
				scrollPane: '.popeye .infoDescription .scroller:visible', // Make sure this returns only 1 element
				emailLink: '.popeye .emailLink',
				closeButton: '.popeye .closeButton',
				imageStack: '.imageStack',
				imageCapton: '.imageCaption',
				images: '.imageStack img',
				caption: '.caption',
				imageNav: '.imageNav',
				imageNavNext: '.imageNavNext',
				imageNavPrev: '.imageNavPrev',
				imageNavInfo: '.imageNavInfo',
				mediaNavVideo: '.videoLink',
				mediaNavImage: '.imageLink',
				videoWrapper: '.videoWrapper',
				video : '#vp'
			},
			deepLinkPrefix: '#show-',
			deepLinkQueryVar: 'dl',
			jScrollPane: {
				dragMinHeight: 10,
				dragMaxHeight: 10
			}
		}, // END settings
		
		
		/*
		 *	init():
		 *		Initializes the popeye object.
		 */
		init: function() {
			var that = this;
			
			// Grab each of the elements and move them to the end of the body
			$(that.settings.elements.popeye).each(function() {
				$(this).appendTo('body');
			});
			
			// Create needed event handlers.
			that.createHandlers();
			
			// Check to see if the URL has a deep link in it.
			that.deepLink();
			
		}, // END init()
		
		
		/*** FUNCTIONS IN ALPHABETICAL ORDER ***/
		
		
		/*
		 *	createHandlers()
		 *		Creates live event handlers for all elements that need them.
		 */
		createHandlers: function() {
			var that = this;
			
			$(that.settings.elements.popeye).scroll(function(event) { event.preventDefault(); return false; });
			
			$(that.settings.elements.activationLink).live('click', function(event) {
				that.show('.' + $(this).attr('rel'));
				$('#vp').show();
			});
			
			$(that.settings.elements.closeButton).live('click', function() {
				stopVideo();
				
				$(that.settings.elements.videoWrapper).hide();
				$('.popeye .imageColumn').fadeIn();
				that.hide();
			});
			
			$(that.settings.elements.emailLink).live('click', function(e){
				document.location = $(this).attr('rel');
				
				e.preventDefault();
				return false;
			});
			
			$(that.settings.elements.overlay).live('click', function(){
				stopVideo();
				
				$(that.settings.elements.videoWrapper).hide();
				$('.popeye .imageColumn').fadeIn();
				that.hide();
			});
			
			$(that.settings.elements.imageNavPrev).live('click', function() {
				that.slideshowChange('-');
			});
			
			$(that.settings.elements.imageNavNext).live('click', function() {
				that.slideshowChange('+');
			});
						
		}, // END createHandlers()
		
		
		/*
		 *	deepLink():
		 *		Checks the hash and the query string for deeplink info and opens popeye if found.
		 */
		deepLink: function() {
			var that = this;
			var deepLink = '';
			
			if (location.hash.indexOf(that.settings.deepLinkPrefix) > -1) {
				deepLink = '.' + location.hash.replace(that.settings.deepLinkPrefix, '');
				
			} else if(location.search.indexOf(that.settings.deepLinkPrefix.replace('#','')) > -1) {
				var queryArray = location.search.replace('?','').split('&');
				for (var i = 0; i < queryArray.length; i++) {
					var pair = queryArray[i].split('=');
					if (pair[0] == that.settings.deepLinkQueryVar) {
						deepLink = '.' + pair[1].replace(that.settings.deepLinkPrefix.replace('#',''), '');
						
						// We found what we're looking for, so stop looking.
						break;
					}
				}
			}
			
			if (deepLink != '') {
				that.show(deepLink);
			}
			
		}, // END deepLink()
		
		
		/*
		 *	hide():
		 *		Hides the popeye overlay and content.
		 */
		hide: function() {
			var that = this;
			
			$target = $('.popeye:visible');
			
			$target
				.fadeOut(that.settings.animation.outDuration);
			
			$(that.settings.elements.overlay).fadeOut(that.settings.animation.outDuration, function() {
				that.showing = '';
				location.hash = '';
				$('select').css({visibility: ''});
				$(that.settings.elements.scrollPane).jScrollPaneRemove();
			});
			
		}, // END hide()
		
		
		/*
		 *	show(target):
		 *		Shows the popeye overlay and content as defined by @target.
		 *		@target = The selector for the element that should be displayed.
		 */
		show: function(target) {
			var that = this;
			
			var $target = $(target);
			var setPosition = ($.browser.msie && $.browser.version < 7) ? 'absolute' : 'fixed' ;
			var setLeft = Math.floor(($(window).width() - $(target).width()) / 2) + 'px';
			var setTop = Math.floor(($(window).height() - $(target).height()) / 2) + 'px';
			
			that.showing = target;
			that.slideshowInit();
			
			location.hash = that.settings.deepLinkPrefix + target.replace('.', '');
			
			if (setPosition == 'absolute') {
				setTop = parseInt(setTop, 10) + $(document).scrollTop() + 'px';
			}
			
			// Remove the element if it exits so we start fresh each time
			if ($(that.settings.elements.overlay).length) {
				$(that.settings.elements.overlay).remove();
			}
			
			$('body')
				.append(
					$('<div>')
						.attr({
							id: that.stripPrefix(that.settings.elements.overlay)
						})
						.css({
							display: 'block',
							position: 'absolute',
							left: 0,
							top: 0,
							width: $(document).width(),
							height: $(document).height(),
							opacity: 0,
							background: '#000000'
						})
						.fadeTo(that.settings.animation.inDuration / 2, 0.5)
				);
			
			$('select').css({visibility: 'hidden'});
			
			$target
				.css({
					top: setTop,
					left: setLeft,
					position: setPosition
				})
				.fadeIn(that.settings.animation.inDuration, function(){
					try {
						$(that.settings.elements.scrollPane).jScrollPane(that.settings.jScrollPane);
					} catch(err) {}
				});
			
			

		}, // END show()
		
		
		/*
		 *	slideshowInit()
		 */
		slideshowInit: function() {
			var that = this;
			if ($(that.settings.elements.images, that.showing).length < 2) {
				// Hide the slideshow nav and we're done
				$(that.settings.elements.imageNav).hide();
				
			} else {
				// If slideshow nav is hidden, show it
				if($(that.settings.elements.imageNav).css('display') == 'none'){
					$(that.settings.elements.imageNav).show();
				}
				if($(that.settings.elements.imageNavPrev, that.showing).css('visibility') == 'hidden'){
					$(that.settings.elements.imageNavPrev, that.showing).css({ visibility: 'visible' });
				}
				if($(that.settings.elements.imageNavNext, that.showing).css('visibility') == 'hidden'){
					$(that.settings.elements.imageNavNext, that.showing).css({ visibility: 'visible' });
				}
				// Reset the slideshow to a known state.
				$(that.settings.elements.images, that.showing).each(function() { $(this).hide(); });
				$(that.settings.elements.images + ':first', that.showing).show();
				
				// Hide the previous button
				$(that.settings.elements.imageNavPrev, that.showing).css({visibility: 'hidden'});
				
				// Set the X of Y info.
				$(that.settings.elements.imageNavInfo, that.showing).text('1 of ' + $(that.settings.elements.images, that.showing).length);
			}
		},
		
		
		/*
		 *	slideshowNext()
		 */
		slideshowChange: function(direction) {
			var that = this;
			
			var index = $('img:visible', that.settings.elements.imageStack).index();
			var nextIndex = '';
			
			// Determine the nextIndex based on the current index and the direction
			if (direction.indexOf('-') > -1) {
				nextIndex = (index === 0) ? $(that.settings.elements.images, that.showing).length - 1 : index - 1 ;
			} else {
				nextIndex = (index === $(that.settings.elements.images, that.showing).length - 1) ? 0 : index + 1 ;
			}
			
			
			if (index > nextIndex) {
				$(that.settings.elements.images, that.showing).eq(nextIndex).show();
				$(that.settings.elements.images, that.showing).eq(index).fadeOut(that.settings.animation.inDuration);
					
			} else {
				$(that.settings.elements.images, that.showing).eq(nextIndex).fadeIn(that.settings.animation.inDuration, function() {
					$(that.settings.elements.images, that.showing).eq(index).hide();
				});
			}
			
			$(that.settings.elements.caption, that.showing).eq(index).hide();
			$(that.settings.elements.caption, that.showing).eq(nextIndex).fadeIn(that.settings.animation.inDuration);
			
			$(that.settings.elements.imageNavInfo, that.showing).text( $(that.settings.elements.caption + ':visible', that.showing).index() + ' of ' + $(that.settings.elements.images, that.showing).length);
			
			if (nextIndex > 0) {
				$(that.settings.elements.imageNavPrev, that.showing).css({ visibility: '' });
			} else {
				$(that.settings.elements.imageNavPrev, that.showing).css({ visibility: 'hidden' });
			}
			
			if (nextIndex === $(that.settings.elements.images, that.showing).length - 1) {
				$(that.settings.elements.imageNavNext, that.showing).css({ visibility: 'hidden' });
			} else {
				$(that.settings.elements.imageNavNext, that.showing).css({ visibility: '' });
			}
		},
		
		
		/*
		 *	slideshowPrev()
		 */
		slideshowPrev: function() {
			var that = this;
		},
		
		
		/*
		 *	stripPrefix():
		 *		Removes the leading . or # from a CSS class or ID.
		 */
		stripPrefix: function(selector) {
			return selector.replace('.', '').replace('#', '');
		} // END stripPrefix()

		
	}; // END popeye
		
	popeye.init();

});

jQuery(window).load(function(){
	vp = document.getElementById('vp');
}); // END $(window).load()


/*
 * BEGIN FUNCTIONS
 */

// External interface functions for flash video player.
function stopVideo(video) {
	try {
			var v=document.getElementById(video);
			v.videoStop();
	} catch(err) {}
}
