(function ($) {
	$(function () {
		var $primaryContent = $('#primaryContent');
		// handle tabs
		var $tabbedContent = $('div.tabbed-content', $primaryContent);
		// only run if there's actually tabbed content to play with
		if ($tabbedContent.length) {
			var $tabbedContentTab		= $('> ul.tabs li', $tabbedContent);
			var $tabbedContentTabLink	= $('> a', $tabbedContentTab);
			var $tabbedContentTabContent= $('> div.tab', $tabbedContent);
			// do our initial hiding and attach event handlers
			var tabSwitchInit = function () {
				$tabbedContentTabLink.bind('click', switchTabs);
				$tabbedContentTabContent.not('.current').hide();
			};
			// switch which tab is visible
			var switchTabs =  function (e) {
				var $trigger = $(e.target);
				var $target = $('' + $trigger.attr('href'));
				if (!$target.is(':visible')) {
					$tabbedContentTabLink.filter('.current').removeClass('current');
					$trigger.addClass('current');
					
					$tabbedContentTabContent.not($target).removeClass('current').hide();
					$target.show();
				}
				e.preventDefault();
			};
			// set it off
			tabSwitchInit();
		}

		// handle finish switching
		var $orderForm		= $('div.ordering-info', $primaryContent);
		var $finishSwitcher	= $('a.finishSwitcher', $orderForm);
		var $finishSubmit	= $('input#Submit', $orderForm);
		var $finishNote		= $('p.note', $orderForm);
		if ($finishSwitcher.filter('.current').length == 0) {
			$finishSwitcher.eq(0).addClass('current');
		}
		if ($finishSwitcher.length) {
			var $listPriceHolder= $('.price .list-price', $orderForm);
			var $salePriceHolder= $('.price .sale-price', $orderForm);
			// set up our list, hide elements, and attach appropriate handlers
			var switchFinishInit	= function () {
				// keep track of the original price
				// Create list from options
				var selectedFinish	= null;
				$finishSwitcher.each(function (i) {
					var $me		= $(this);
					var $opts	= ($me.attr('data-opts') != '') ? eval("(" + $me.attr('data-opts') + ")") : false;
					// remove the title atribute, give each option a unique ID, and create our new list
					$me
						.attr({'id'	: 'finishOption' + i})
						.bind('click', switchFinish)
						.data('productOptions', $opts);
					
				});
			};
			// swap out the image and update the select
			var switchFinish = function (e) {
				$finishSwitcher.removeClass('current');
				var $finishImage	= $('img#mainProductFinish', $primaryContent);
				var $trigger		= $(e.currentTarget).addClass('current');
				var $productOptions	= $trigger.data('productOptions');
				// we only want to run the swapper if it's a new finish
				//if ($finishImage.attr('src') != $productOptions.imagePath) {
				//just in case we have blank images, src will be same, but alt should always be unique as it is the finish
				if ($finishImage.attr('alt') != $productOptions.imageAlt) {
					/*
					var $availableOptions	= $('option', $finishSwitcher);
					var $selectedOption		= $availableOptions.filter('' + $productOptions.target);
					*/
					// switch the image with the new finish
					$finishImage.fadeOut(500, function () {
						$(this)
							.attr('src', $productOptions.imagePath)
							.attr('alt', $productOptions.imageAlt)
							.fadeIn(250);
					});
					
					if ($productOptions.inventory != ''){
						$('#Submit').show();
						$('#QuantityField input').show();
						if ($productOptions.inventory < 21 && $productOptions.inventory > 0){
							$('#InventoryMsg').text('Hurry, only ' + $productOptions.inventory + ' left in stock');
							$('#InventoryMsg').show();							
						}
						else if ($productOptions.inventory == 0){
							$('#InventoryMsg').text('Out of stock');
							$('#InventoryMsg').show();
							$('#Submit').hide();
							$('#QuantityField input').hide();
						}
						else if ($productOptions.inventory > 20){
							$('#InventoryMsg').hide();
						}
					} else {
						$('#InventoryMsg').hide();
						$('#Submit').show();
					}
					
					// update the prices
					if ($productOptions.salePrice == '') {
						$listPriceHolder.removeClass('on-sale').find('.you-pay').text($productOptions.listPrice);
						$salePriceHolder.removeClass('on-sale').find('.you-pay').text('');
					} else {
						$listPriceHolder.addClass('on-sale').find('.you-pay').text($productOptions.listPrice);
						$salePriceHolder.addClass('on-sale').find('.you-pay').text($productOptions.salePrice);
					}
					$('#model-sku').text($productOptions.sku);
					$('#SKUId').val($productOptions.skuID);
					$('#ShippingOptions').slideUp();
				}

				e.preventDefault();
			};
			// go go go!
			switchFinishInit();
		}
		
		// launch blackout
		var $additionalImages = $('div.additional-images', $primaryContent);
		var $additionalImagesList = $('ul', $additionalImages);
		if ($additionalImagesList.length) {
			var $additionalImagesTrigger = $('<a class="trigger" href="javascript:void();">+ Other Views</a>')
				.bind('click', function (e) {
					$($('> li > a', $additionalImagesList)[0]).click();
					e.preventDefault();
				});
			$additionalImages.append($additionalImagesTrigger);
			$additionalImagesList.blackout({
				 alternateView : 'filmstrip'
				,showCaption: false
				,showNavigationBar: false
			});
		}
	});
})(jQuery);
