jQuery(document).ready(function(){
	
	/**
	 * 	home.php
	 */
	
	jQuery('div.hidden-content').hide();
	
	// constants
	var slideshow_manual	 = false;
	var slideshow_delay		 = 3000;
	var initial_delay		 	 = 3000;
	var slideshow_autoplay;
	
	// activate pause button
	jQuery('a.slideshow-pause').click(function(){
		slideshow_manual = true;		
		// kill autoplay
	  if (slideshow_autoplay) {
			clearTimeout(slideshow_autoplay);
		}
	});
	
	// activate play button
	jQuery('a.slideshow-play').click(function(){
		// restart autoplay, advance frame
		slideshow_manual = false;
		next_frame();		
	});
	
	// activate pager frame links
	jQuery('a.slideshow-pager').click(function(){
		// kill autoplay
		jQuery('a.slideshow-pause').click();
		load_frame(this);
	});

	// kill autoplay on mouseover/out
	jQuery('div#slideshow-container').hover(function(){
		// over, kill autoplay
	  if (slideshow_autoplay) {
			clearTimeout(slideshow_autoplay);
		}
	}, function(){
		// start over autoplay over again -- only if not in manual mode
		if (!slideshow_manual) {
			if (slideshow_autoplay) {
				clearTimeout(slideshow_autoplay);
			}
			slideshow_autoplay = setTimeout(next_frame, slideshow_delay);	
		}
	});
	
	// find the next frame in sequence (for autoplay)
	function next_frame() {
		// get <a> obj of currently active li
		var n = jQuery('div#slideshow-container li.active').next();
		if (jQuery(n).attr('class') == 'play') {
			// play button is next - we're at the end. next sibling of pause button is always 1st
			var a = jQuery('div#slideshow-container li.pause').next().children('a:first');			
		} else {
			// use next
			var a = jQuery(n).children('a:first');
		}
		load_frame(a);
	}
	
	// load a frame into the slideshow
	function load_frame(a) {
		// hide deal of day, if its there
		if (jQuery('div.deal-day-box')) {
			jQuery('div.deal-day-box').slideUp('normal', function(){
				jQuery(this).remove();
			});
		}
		// remove active class
		jQuery(a).parent('li').siblings('li.active').each(function(){
			jQuery(this).removeClass('active');
		});
		// add active class to this page
		jQuery(a).parent('li').addClass('active');
		// corresponding frame ID is packed into rel attr w/ comma-delim for deal of day
		var meta = jQuery(a).attr('rel').split(',');
		var html = jQuery('#' + meta[0]).html();
		jQuery('div.slideshow-page-container').fadeTo('normal', .1, function(){
			jQuery(this).html(html);
			
			// force lightbox to see the newly updated HTML,
			// so the thumbnail viewer works
			// if (myLightbox) myLightbox.updateImageList();
			
			jQuery(this).fadeTo('normal', 1, function(){
				// show deal of day?
				if (meta[1] == 'dod') {
					var dod = '<div style="display:none;" class="deal-day-box">Deal of the Day</div>';
					jQuery('div#slideshow-container div.content-box').prepend(dod);
					jQuery('div.deal-day-box').slideDown('normal');
				}
			});
		});
		// continue autoplay?
		if (!slideshow_manual) {
			if (slideshow_autoplay) {
				clearTimeout(slideshow_autoplay);
			}
			slideshow_autoplay = setTimeout(next_frame, slideshow_delay);		
		}
	}
	
	// init
	if (HOMEPAGE_SLIDESHOW_INITIAL_DELAY) {
		initial_delay = HOMEPAGE_SLIDESHOW_INITIAL_DELAY * 1000;
	}
	if (HOMEPAGE_SLIDESHOW_REGULAR_DELAY) {
		slideshow_delay = HOMEPAGE_SLIDESHOW_REGULAR_DELAY * 1000;
	}
	slideshow_autoplay = setTimeout(next_frame, initial_delay);

});