
jQuery(function($) {

	/**
	 * Top navigation 
	 */
	
	$('ul.sf-menu').superfish();

	/**
	 * Product Page Stuff
	 */
	
	/**
	 * Update some hidden inputs to reflect the specified sku, and show
	 * the price of the sku. Disable the add to cart button if the combination
	 * of options isn't available (when sku isn't a string).
	 */
	function setInputsAndPrice(sku, color_not_selected) {
			
		var data = getSkuData(sku),	
			submit = $('#product-details .product-options input[type=submit]'),
			price = $('#price'),
			skuId = $('#product-details input[name=txtSkuID]');
		
		if(data) {
			price.html(data.price);
			submit.removeAttr('disabled');
			skuId.val(data.id);
		} else {			
			if (typeof color_not_selected !== 'undefined' && color_not_selected)
				price.html('Choose a color');
			else
				price.html('Combination Not Available');
			submit.attr('disabled', 'disabled');		
			skuId.val('');
		}			
	}
	
	/**
	 * Find data from the global object cws, which should have a member skus. It
	 * is put on the page by clsShopProd.
	 */
	function getSkuData(name)	{		
		
		for(var i = 0; i < cws.skus.length; i++) {
			if(cws.skus[i].sku === name) {
				return cws.skus[i];
			}
		}
		return false;
	}
	
	/**
	 * Loop over skus/dimensions and remove failed candidates until only
	 * one sku remains.
	 */
	function getCurrentSku() {
	
		// This copies cws.skus, we don't want to affect cws.skus 
		var candidates = cws.skus.slice();

		$('#product-details select').each(function() {
				
			var select = $(this),
				value = select.val(),
				dimension = select.attr('name'),
				successful_candidates = [];

			for(var i = 0; i < candidates.length; i++) {
				if(candidates[i].options[dimension] == value)
					successful_candidates.push(candidates[i]);
			}

			candidates = successful_candidates;
		});

		if(candidates.length < 1)
			return false;
		
		return candidates[0].sku;
	}
	
	/**
	 * Update the product image 
	 */
	function setImage(sku) {
				
		var data = getSkuData(sku);
		
		if(!data)
			return false;
		
		if(data.image) {
			$('a#product-image img').attr('src', 'img/prod/'+data.image);
			$('a#product-image').attr('href', 'img/prod/'+data.big_image);
		}
	}
	
	/**
	 * Call setInputsAndPrice and setImage.
	 */
	function setCurrentSku(sku, color_not_selected) {

		var sku_data = getSkuData(sku);

		if (sku_data)
		{
			for(var i = 0; i < cws.dimensions.length; i++) {
				$('select[name='+cws.dimensions[i]+']').val(sku_data.options[cws.dimensions[i]]);
			}
		}
		
		setInputsAndPrice(sku, color_not_selected);
		setImage(sku);
	}

	if($('#product-details').length) {
		
		/**
		 * If updating a cart item set options to that sku, otherwise 
		 * set options to whatever sku happens to be selected initially.
		 */		
		
		var matches = document.location.toString().match(/cart-sku,([^;]+)/);

		if(matches && matches.length === 2)
			setCurrentSku(matches[1]);
		else
			setInputsAndPrice(getCurrentSku(), true);
		
		/**
		 * Add event listeners
		 */
		
		$('#product-details select').change(function() {

			var color_select = $('select[name=color_name]'),
				initial = false;

			if (color_select.length && color_select.val() == '')
				initial = true;

			setCurrentSku(getCurrentSku(), initial);
		});
		
		$('#product-details .swatch a').click(function() {
			
			var sku = $(this).parent().get(0).id,			
				sku_data = getSkuData(sku);
			
			// Change the swatch dimension select to match the clicked swatch and 
			// get the new current sku based on that change.
			
			$('select[name='+cws.swatch_dimension+']').val(sku_data.options[cws.swatch_dimension]);
			
			setCurrentSku(getCurrentSku());
			
			return false;
		});
		
		/**
		 * Enable lightbox on product image
		 */
		
		$('a#product-image').lightBox();
	}
});

