/*
 * Main Nav
 * ----------------------------------------------------------------------------------------
 */
var navCloseTimer, navOpenTimer, currOpenListItem;
var mainNav = {

	init : function() {
		// tools and action_bar dropdown event listeners
		$('a.has_dropdown').mouseenter(function() {
			$(this).parent().addClass('hover');
		});
		$('#action_bar>div, #tools>li').mouseleave(function() {
			$(this).removeClass('hover');
		});

		// action bar subnav flyout event listeners
		$('#action_bar li.flyout').mouseenter(function() {
			var li = this;
			// start the open timer if the flyout isn't already open
				if (li != currOpenListItem) {
					navOpenTimer = setTimeout(
							function() {
								$(li).find('.flyout_container').css('display',
										'block');
								currOpenListItem = li;
							}, 200);
				}
			});

		$('#action_bar li.flyout').mouseleave(function() {
			var li = this;

			// if we're leaving the li of the current open flyout start a close
				// timer
				if (currOpenListItem == li) {
					navCloseTimer = setTimeout(function() {
						$(li).find('.flyout_container').css('display', 'none');
						currOpenListItem = null;
					}, 200);
				}
				// otherwise cancel the open timer
				else {
					clearTimeout(navOpenTimer);
				}
			});

		$('#action_bar .flyout_container').mouseenter(function() {
			clearTimeout(navCloseTimer);
		});

		$('#action_bar .flyout_container').mouseleave(function() {
			$(this).css('display', 'none');
		});
	}
}

/*
 * Add to Cart Overlay
 * ---------------------------------------------------------------------------------------
 */
function showOverlay() {
	if ($('.add_overlay_wrapper').hasClass('active')) {
		$('.add_overlay_wrapper').removeClass('active');
	}

	else
		$('.add_overlay_wrapper').addClass('active');
}

/*
 * Tab Switch
 * ---------------------------------------------------------------------------------------
 */
function contentSwitch(link, section) {

	$(link).parent().parent().children().removeClass('active');
	$(link).parent().addClass('active');

	$(section).parent().children().removeClass('active');
	$(section).addClass('active');
}

/*
 * ----------------------------------------------------------------------------------------------
 * Class: Carousel containerID - string - The ID of the HTML element containing
 * the carousel items options - object - rotationSpeed - the speed that the
 * carousel autorotates at animationSpeed - the speed the carousel animates at
 * ----------------------------------------------------------------------------------------------
 */
function Carousel(containerID, options) {
	// store options
	this.options = options;

	// set variables
	this.containerID = containerID
	this.container = $('#' + this.containerID);
	this.rotationSpeed = (options.rotationSpeed) ? options.rotationSpeed : 5000;
	this.animationSpeed = (options.animationSpeed) ? options.animationSpeed
			: 500;
	this.currIndex = 0;
	this.maxIndex = 0;
	this.timer = null;
	this.animating = false;

	// Method: init
	this.init = function() {
		var classRef = this;

		// add event handlers to carousel nav items
		$('#' + this.containerID + ' .carousel_nav a').each(function(i) {
			$(this).bind("click", function(e) {
				classRef.change(i);
				clearInterval(classRef.timer);
				return false;
			});
		});

		this.maxIndex = $('#' + this.containerID + ' .carousel_item').length - 1; // find
		// number
		// of
		// carousel
		// items
		$('#' + this.containerID + ' .carousel_item:first').addClass('active')
				.css('display', 'block'); // show first carousel item
		$('#' + this.containerID + ' .carousel_nav').css('display', 'block'); // display
		// the
		// carousel
		// nav

		this.timer = setInterval(function() {
			classRef.change(false)
		}, this.rotationSpeed); // itialize the auto rotate timer
	}

	// Method: change
	this.change = function(newIndex) {
		if (!this.animating) {
			var classRef = this;
			this.animating = true;

			var newIndex = (newIndex === false) ? this.currIndex + 1 : newIndex; // determine
			// the
			// newIndex
			// if
			// auto
			// rotate
			// is
			// being
			// used
			newIndex = (newIndex > this.maxIndex) ? 0 : newIndex; // make sure
			// newIndex
			// doesn't
			// go past
			// the
			// maxIndex

			var currCarousel = $('#' + this.containerID + ' .carousel_item.active');
			var newCarousel = $('#' + this.containerID + ' .carousel_item')[newIndex];
			var currNav = $('#' + this.containerID + ' .carousel_nav li.active');
			var newNav = $('#' + this.containerID + ' .carousel_nav li')[newIndex];

			currCarousel.fadeOut(this.animationSpeed, function() {

				currNav.removeClass('active');
				$(newNav).addClass('active');

				$(newCarousel).fadeIn(this.animationSpeed, function() {
					currCarousel.removeClass('active');
					$(newCarousel).addClass('active');

					classRef.currIndex = newIndex;
					classRef.animating = false;
				});
			});
		}
	}

	this.init();
}

/*
 * Live Search
 * --------------------------------------------------------------------------
 */
var liveSearch = {
	searchTimer : null,
	minCharacters : 3,
	searchDelay : 300,
	searchURL : "_search_dummy.html",

	init : function() {
		$('#input_searchbar').keyup(function() {
			if (this.value.length >= liveSearch.minCharacters) {
				var q = this.value;
				clearTimeout(liveSearch.searchTimer);
				liveSearch.searchTimer = setTimeout(function() {
					liveSearch.doSearch(q)
				}, liveSearch.searchDelay);
			} else {
				$('#livesearch').css('display', 'none');
			}
		});
		$('#input_searchbar').focus(function() {
			if (this.value.length >= 3) {
				liveSearch.doSearch(this.value);
			}
		});
		$('#input_searchbar').blur(function() {
			$('#livesearch').css('display', 'none');
		});
	},

	doSearch : function(q) {
		$.ajax( {
			type : "POST",
			url : liveSearch.searchURL,
			data : "q=" + q,
			dataType : "html",
			success : function(msg) {
				$('#livesearch').html(msg);
				$('#livesearch').css('display', 'block');
				Cufon.replace('h3, .archer_medium', {
					fontFamily : 'archer medium'
				});
			}
		});
	}
}

/*
 * Preview Stacks
 * --------------------------------------------------------------------------
 */
var previewStacks = {
	stacks : null,
	imgWrappers : null,
	interval : null,
	currStack : null,
	currIndex : null,
	currMaxIndex : null,
	currMouseX : null,
	timer : null,
	mouseOffset : 5,
	previewDelay : 150,

	init : function() {
		// find elements
	previewStacks.stacks = $('.preview_stack');
	previewStacks.imageWrappers = previewStacks.stacks.find('.image_wrapper');

	// add mouse enter event listener
	previewStacks.imageWrappers.mouseenter(function() {
		clearTimeout(previewStacks.timer);

		// set global variables for current stack
			previewStacks.currStack = $(this).parent().find('img');
			previewStacks.currIndex = 0;
			previewStacks.currMaxIndex = previewStacks.currStack.length - 1;

			// set a delay so we don't get crazy flickering when rolling over
			// different stacks quickly
			previewStacks.timer = setTimeout(function() {
				$(document).bind('mousemove', previewStacks.checkPreview)
			}, previewStacks.previewDelay);
		});

	// add mouse leave listener
	previewStacks.imageWrappers.mouseleave(function() {
		clearTimeout(previewStacks.timer);
		$(document).unbind('mousemove');
		previewStacks.currMouseX = null;

		// hide current, show first image in stack
			$(previewStacks.currStack.parent()).find('.active').removeClass(
					'active');
			$(previewStacks.currStack[0]).addClass('active');
		});
},

checkPreview : function(e) {
	// capture the initial mouse postion
	if (previewStacks.currMouseX == null) {
		previewStacks.currMouseX = e.pageX;
	}

	// detect mouse moving right
	if (e.pageX > (previewStacks.currMouseX + previewStacks.mouseOffset)) {
		nextIndex = ((previewStacks.currIndex + 1) > previewStacks.currMaxIndex) ? 0
				: previewStacks.currIndex + 1;
		previewStacks.doPreview(nextIndex);
		previewStacks.currMouseX = e.pageX;
	}
	// detect mouse moving left
	else if (e.pageX < (previewStacks.currMouseX - previewStacks.mouseOffset)) {
		nextIndex = ((previewStacks.currIndex - 1) < 0) ? previewStacks.currMaxIndex
				: previewStacks.currIndex - 1;
		previewStacks.doPreview(nextIndex);
		previewStacks.currMouseX = e.pageX;
	}
},

doPreview : function(nextIndex) {
	$(previewStacks.currStack.parent()).find('.active').removeClass('active');
	$(previewStacks.currStack[nextIndex]).addClass('active');
	previewStacks.currIndex = nextIndex;
}
}

/* PNG */

jQuery.fn.supersleight = function(settings) {
	settings = jQuery.extend( {
		imgs : true,
		backgrounds : true,
		shim : 'x.gif',
		apply_positioning : true
	}, settings);

	return this
			.each(function() {
				if (jQuery.browser.msie
						&& parseInt(jQuery.browser.version, 10) < 7
						&& parseInt(jQuery.browser.version, 10) > 4) {
					jQuery(this)
							.find('*')
							.andSelf()
							.each(function(i, obj) {
								var self = jQuery(obj);
								// background pngs
									if (settings.backgrounds
											&& self.css('background-image')
													.match(/\.png/i) !== null) {
										var bg = self.css('background-image');
										var src = bg
												.substring(5, bg.length - 2);
										var mode = (self
												.css('background-repeat') == 'no-repeat' ? 'crop'
												: 'scale');
										var styles = {
											'filter' : "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"
													+ src
													+ "', sizingMethod='"
													+ mode + "')",
											'background-image' : 'url(' + settings.shim + ')'
										};
										self.css(styles);
									}
									;
									// image elements
									if (settings.imgs
											&& self.is('img[src$=png]')) {
										var styles = {
											'width' : self.width() + 'px',
											'height' : self.height() + 'px',
											'filter' : "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"
													+ self.attr('src')
													+ "', sizingMethod='scale')"
										};
										self.css(styles).attr('src',
												settings.shim);
									}
									;
									// apply position to 'active' elements
									if (settings.apply_positioning
											&& self.is('a, input')
											&& (self.css('position') === '' || self
													.css('position') == 'static')) {
										self.css('position', 'relative');
									}
									;
								});
				}
				;
			});
};

