$(document).ready(function(){
	//resize large images
	$(".productImage").each(function(){
		//resize
		if($(this).width > maxWidth){
			$(this).animate({width: maxWidth});
		}
		
		//FUTURE: add lightboxing capability
	});
	
	//resize small images
	$(".productSmallImage").each(function(){
		if($(this).width > maxThumbWidth){
			$(this).animate({width: maxThumbWidth});
		}
		//also add click functionality
		$(this).click(function(){
			var mainImage = $("img#mainImg");
			mainPicture = (mainImage.size() == 1) ? mainImage : $(this).closest(".productWrapper").find(".mainPicture img");
			mainSrc = $(mainPicture).attr('src');
			$(mainPicture).attr({"src":$(this).attr("src")});
			/*
			FUTURE: hide small version?
			$(this).siblings("img").show();
			$(this).hide();*/
		});
	});
	
	//hide selects when their only value is "One Color" or "One Size"
	$("select.colorSelect, select.sizeSelect").each(function(){
		if($(this).children().length == 1){
			selectVal = $(this).children(":first").val();
			hideTheseValues = new Array("One Color", "One Size");
			if(-1 != $.inArray(selectVal, hideTheseValues)){
				//hide stuff
				if("vertical" == cartType){
					select = "tr";
				} else {
					select = "td";
				}
				$(this).parents(select).prev().remove(); //css("border","1px solid red");
				$(this).parent().remove();
			}
		}
		
	});
	
	//update product on change of color
	$(".colorSelect").bind('change', function(){
		id = $(this).attr("id").replace("color",'');
		updateProduct(id, "color");
	});
	
	//update product on change of size
	$(".sizeSelect").bind('change', function(){
		id = $(this).attr("id").replace("size",'');
		updateProduct(id, "size");
	});
	
	//update product on change of quantity	
	$(".quantitySelect").bind('change', function(){
		id = $(this).attr("id").replace("quantity",'');
		updateProduct(id, "quantity");
	});
	
	//add product to cart
	$(".addToCart").bind('click', function(){
		submitForm($(this).attr("id"));
	});
	
});

function updateProduct(id, type){
	//updates all product information: sizes, quantities, skus
	//selects
	colorInput = $("#color" + id);
	color = $("#color" + id + " option:selected").text();
	sizeInput = $("#size" + id);
	size = $("#size" + id + " option:selected").text();
	skuInput = $("#sku" + id);
	sku = '';
	quantityInput = $("#quantity" + id);
	//data array
	productData = productObject[id];
	
	switch(type){
		case "color":
			//update size, continue for quantity, sku
			sizesArray = array_keys(productData[color]);
			sizeInput.html(makeOptions(sizesArray,''));
			
		case "size":
			//update quantity, continue for sku
			size = $("#size" + id + " option:selected").text();
			inventory = productData[color][size].inventory;
			quantities = fillArray(inventory);
			//print_r(quantities);
			quantityInput.html(makeOptions(quantities,''));
			
		case "quantity":
			//double check sku is updated
			sku = productData[color][size].sku;
			skuInput.val(sku)
		break;
	}
}

function makeOptions(options){
	htmlOptions = '';
	//if(isArray(options)){
		for(i in options){
			htmlOptions += "<option>" + options[i] + "</option>";
		}
		return htmlOptions;
	//}
}

function fillArray(length){
	retArray = new Array();
	for(i=1;i<=length;i++){
		retArray[i] = i;
	}
	return retArray;
}

function updateSize(color, id){
	var options;
	sizeArray = window['sizeArray'+id];
	optionsArray = sizeArray[color];

	if(1 < sizeArray.length){
		options = "<option></option>";
	}
	sizeSelect = $('#size' + id);
	options = '';
	for(i in optionsArray){
		options += "<option>" + optionsArray[i] + "</option>";
	}
	sizeSelect.html(options);
}

//update inventory based on the sku
function updateInventory(sku, id){
	quantityArray = window['inventoryList'+id];
	quantitySelect = $('#quantity' + id);
	options = '';
	for(i=1; i<=quantityArray[sku];i++){
		options += "<option>" + i + "</option>";
	}
	quantitySelect.html(options);	
}

//submit foxycart form
function submitForm(id){
	$("#productForm" + id).submit();
}

//get array/object keys
function array_keys(object) {
	keys = [];
	for(i in object){
		keys.push(i);
	}
	return keys;
}

function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}
