	var jsa = new Array();

	var currentGalleryImg = null;

	var defaultAddToBagText = "";
	var defaultOutfitNoSizeText = "";

	var chosenProducts = new Array();

	$(document).ready(function() {
		
		/*
			Handle Color Click
		*/		
		$('.productColors li').click(function(){
			
			// If there is an image associated with the color 
			if(currentGalleryImg){
				$('#imageColumn ul li.active').children('img').stop().fadeTo("fast", 1);	
				$('#imageColumn ul li.active').removeClass('active');
				$('#imageColumn ul li.' + $(this).attr("class").replace(" hover", "") ).addClass('active');
				currentGalleryImg = null;
			}
			
			var clClass = $(this).find("img").attr("class");
			var item = $(this).parents('.item').attr("id");
		
			// If a chosen size is not available in this new color, send up a warning!
			if( $('#' + item + ' .productSizes li:not(.' + clClass + ')').hasClass('chosen') ){
				$('#' + item + ' .sizeText').text( $('#' + item + ' .productSizes li.chosen').text() + " is not available in " + $('#' + item + ' .colorText').text());
				$('#' + item + ' .sizeText').addClass('notavailable');
				$('#' + item + ' .productSizes li:not(.' + clClass + ')').removeClass('chosen');
			}
			else{
				if( $('#' + item + ' .productSizes li').hasClass('chosen') ){
					$('#' + item + ' .sizeText').removeClass('notavailable');
					$('#' + item + ' .sizeText').text($(this).siblings('.chosen').find("input").attr("value"));
				}
				else {
					$('#' + item + ' .sizeText').addClass('notavailable');
					$('#' + item + ' .sizeText').html("Choose a size before purchase");
				}
			}
		
			$('#' + item + ' li.' + clClass).removeClass('inactive');
			$('#' + item + ' .productSizes li').not("." + clClass).addClass('inactive');
									
			$('#' + item + ' .colorText').html($(this).find("img").attr("alt"));
			$(this).siblings('.chosen').removeClass('chosen');
			$(this).addClass('chosen');
			
			itemSelectionsChanged(item);
		});
		
		/*
			Product Colors Rollover
		*/ 
		$('.productColors li').hover(function(){
				var item = $(this).parents('.item').attr("id");
			
				$(this).toggleClass("hover");
				$('#' + item + ' .colorText').html($(this).find("img").attr("alt"));
				
				var pid = $(this).attr("class").replace(' hover', '').replace(' chosen', '');
				if( $('#imageColumn ul li').not('.active').hasClass( pid ) ){
					$('#imageColumn ul .' + pid + ' img').stop().fadeTo("fast", .7);
					currentGalleryImg = $('#imageColumn #theImageSpot').find("img").attr("src");
					$('#imageColumn #theImageSpot').find("img").attr("src", $('#imageColumn ul .' + pid + ' img').attr("rel"));
				}
				
			}, function(){
				var item = $(this).parents('.item').attr("id");				
				
				if( currentGalleryImg ){
					var pid = $(this).attr("class").replace(' hover', '').replace(' chosen', '');
					$('#imageColumn #theImageSpot').find("img").attr("src", currentGalleryImg);
					$('#imageColumn ul .' + pid + ' img').stop().fadeTo("fast", 1);
					currentGalleryImg = null;
				}
				
				$(this).toggleClass("hover");
				$('#' + item + ' .colorText').html($(this).siblings('.chosen').find("img").attr("alt"));
			}
		);

		$('.productSizes li').hover(function(){
				var item = $(this).parents('.item').attr("id");
				if( $(this).hasClass("inactive") ) { 
					$('#' + item + ' .sizeText').addClass('notavailable');
					$('#' + item + ' .sizeText').text( $(this).text() + " is not available in " + $('#' + item + ' .colorText').text());
				}
				else{
					$(this).toggleClass("hover");
					$('#' + item + ' .sizeText').removeClass('notavailable');
					$('#' + item + ' .sizeText').text( $(this).find( "input" ).attr("value") );
				}

			}, function(){
				var item = $(this).parents('.item').attr("id");
				
				if( $(this).hasClass("inactive") ) {} //$('#' + item + ' .sizeText').removeClass('notavailable');
				else $(this).toggleClass("hover");
				
				if( $('#' + item + ' .productSizes li').hasClass('chosen') ){
					$('#' + item + ' .sizeText').removeClass('notavailable');
					$('#' + item + ' .sizeText').text($(this).siblings('.chosen').find("input").attr("value"));
				}
				else{
					$('#' + item + ' .sizeText').addClass('notavailable');
					$('#' + item + ' .sizeText').html("Choose a size before purchase");
				}
			}
		);

		$('.productSizes li').click(function(){
			var item = $(this).parents('.item').attr("id");

			if( $(this).hasClass('inactive') ){ }
			else{
				$(this).siblings('.chosen').removeClass('chosen');
				$(this).addClass('chosen');
				
				itemSelectionsChanged(item);
			}
		});

		$('#addToBagButton img').click(function(){
			if($('#product').length){
				var qty = $('.product-qty select').val();
			
				var iid = jsa[$('.item .productColors li.chosen').attr("class").replace(' chosen', '') + $('.item .productSizes li.chosen input').attr("class") + $('.item').attr("id")].id;

				var pid = $('.item').attr("id");

				var items = "["+pid+","+iid+","+qty+"]";

			}
			else{
				var items = "[";
				
				$('.item').each(function(){
					
					if( $(this).find('.included-text').hasClass('included') ){ 
							var pid = $(this).attr("id");
							var psize = $(this).find('.productSizes li.chosen input').attr("class");
							var clid = $(this).find('.productColors li.chosen').attr("class").replace(' hover','').replace(' chosen','');
							
							if(items!="[") items += ":";
							items += pid+","+jsa[clid + psize + pid].id + ",1";
					}
			
				});
				
				items += "]";
			}		

			$('#addToBagButton img').hide()
			
			$.ajax({
				url: '/action/shop/addtocart/?items=' + items,
				type: 'GET',
				dataType: 'html',
				timeout: 1000,
				error: function(){ alert('Error adding to cart.'); },
				success: function(html){			
					$("#addedToBag").append(html);
					$("#addedToBag div.addedItem").slideDown("slow");

					$('#addToBagButton img').toggle();
					updateBag($(".itemsadded:last").attr("value"));
				}
			});
		});
		
		defaultAddToBagText = $('#addToBagText').text();
		defaultOutfitNoSizeText = $('.item .choose:first').text();

		$('.item').each(function(){ 
			$(this).find('.productColors li:first').click();
			if( $(this).find('.productSizes ul').children().length == 1) $(this).find('.productSizes li:first').click();
		});

		$('#outfit .item a').click(function(e){ e.preventDefault() });
		
		$('#outfit .include-link').click(function(){
			var item = $(this).parents('.item').attr("id"); 
			
			$(this).toggle();
			$(this).siblings().toggleClass('included');
			$('#' + item + ' .include-options').toggle();
			updateOutfitAddToBag()
		});

		// "dis-include" an item from an outfit purchase
		$('#outfit .included-text a').click(function(){
			$(this).parents('.included-text').toggleClass('included');
			$(this).parents('.included-text').siblings().toggle();
			$(this).parents('.item').find('.include-options').toggle();
			updateOutfitAddToBag()
		});
		
	});
	
	/*
	
    function countChecked() {
      var n = $("input:checked").length;
      $("div").text(n + (n == 1 ? " is" : " are") + " checked!");
    }
    countChecked();
    $(":checkbox").click(countChecked);
	
	*/

	
	
	function updateProductAddToBag(){
		if( $('.item .productSizes li').hasClass('chosen') ){
			$('#addToBagButton img').show();
			$('#addToBagText').html("&nbsp;");
		}
		else{
			$('#addToBagButton img').hide();
			$('#addToBagText').html(defaultAddToBagText);
		}
	}
	
	function outfitItemSizeChange(item){
		if( $('#' + item + ' .productSizes li').hasClass('chosen') ){
			$('#' + item + ' .includeItem').attr("checked", "yes");
			$('#' + item + ' .choose').text($('#' + item + ' .productSizes li.chosen input').attr("value"));
			$('#' + item + ' .choose').css("color","#333");
		}
		else{
			$('#' + item + ' .includeItem').attr("checked", "");
			$('#' + item + ' .choose').text(defaultOutfitNoSizeText);
			$('#' + item + ' .choose').css("color","#F00");
		}
		
		
	}
	
	function updateQuantitySelect(){
		$(".product-qty select").children().remove();

		var options = '';

		if( $('.item .productSizes li').hasClass('chosen') ){
			var pcolor = $('.item .productColors li.chosen').attr("class").replace(' chosen', '').replace(' hover', '');
			var psize = $('.item .productSizes li.chosen input').attr("class");
			var pid = $('.item').attr("id");

			for(var i=1; i <= jsa[ pcolor + psize + pid ].qty; i++){
				options += '<option>'+i+'</option>';
			}
		}
		else{
			options += '<option value="-">First, Choose a Size</option>';
		}
		
		$(".product-qty select").html(options);
	}
	
	function itemSelectionsChanged(item){
		if( $("#product").length ){
			updateQuantitySelect();
			updateProductAddToBag();
		}
		else if( $('#outfit').length ){
			outfitItemSizeChange(item);
			updateOutfitAddToBag();
		}
		else{
//			outfitItemSizeChange(item);
		}		
	}
	
	function updateOutfitAddToBag(){
		var errorText = "";
		
		var included = 0;
		var totalprice = 0;
		
		$('.item').each(function(){
					
			if( $(this).find('.included-text').hasClass('included') ){ 
				totalprice += Number($(this).find('.outfit-price-field').val());
				
				if( $(this).find('.productSizes li').hasClass('chosen') ){
					included ++;
//					var pid = $(this).attr("id");
//					var psize = $(this).find('.productSizes li.chosen input').attr("class");
//					var clid = $(this).find('.productColors li.chosen').attr("class").replace(' hover','').replace(' chosen','');
				}
				else errorText = "Please choose a size on all items.";
				
			}
			
		});
		
		if(totalprice == 0) errorText = "Choose at least one item to include.";

		if(totalprice > 0 && errorText == ""){
			$('#addToBagButton img').show();

			var itemtext = "items";
			if(included==1) itemtext = "item";

			$('#outfit-error').hide();
			$('#outfit-price-summary').show().html(included + " " + itemtext + ", <strong>$" + totalprice.toFixed(2) + "</strong>");
		}
		else{
			$('#addToBagButton img').hide();
			$('#outfit-price-summary').hide();
			$('#outfit-error').show().html("<em>" + errorText + "</em>");
		}
	}
	
	
	/*
		Called after an item is added to cart successfully - updates the bag details in the upper right corner
	*/
	function updateBag(qty){
		var current = Number($("#shoppingBag input").attr("value"));
		if(qty && current<1){
			$("#shoppingBag #checkoutNow").show();
			$("#shoppingBag #viewBagLink").show();
		}
		if(qty){
			$("#shoppingBag input").attr("value", qty);
		}
		$("#shoppingBag #bagItemz").text(qty);
	}
	