// assumes g_flatInventory filled - see getProductInventory.php
var inventoryHash = {};
function buildInvHash() {
 for(invItem in g_flatInventory) {
 	var type = g_flatInventory[invItem].sleeve;
 	var typeItems = inventoryHash[type];
 	if(typeItems == null) {
 	  typeItems = {};
 	  inventoryHash[type] = typeItems;
 	}
 	var size = g_flatInventory[invItem].size;
 	var color = g_flatInventory[invItem].color;
 	var qty = g_flatInventory[invItem].qty;
 	var variantId = g_flatInventory[invItem].variantId;
 	var colors = typeItems[size];
 	if(colors == null) {
 		colors = {};
 		typeItems[size] = colors;
 	}
 	colors[color] = [variantId, qty];
 }
}

var colorSel;
var typeSel;
var sizeSel;

function init() {
	sizeSel = document.getElementById("sizeSel");
	colorSel = document.getElementById("colorSel");
	qtySel = document.getElementById("qtySelection");
    buildInvHash();
	clearOptions(sizeSel);
	clearOptions(colorSel);
	clearOptions(qtySel);
	sizeSel[0] = new Option("Choose Size", "none");
	var index = 1;
	var type;
	var size;
	for(type in inventoryHash) {
       for(size in inventoryHash[type]) {
       	var typeSz = type + " - " + size;
	   	sizeSel[index] = new Option(typeSz, typeSz);
	   	index++;
       }
	}
}

function changeSize(sizeSel) {
	if(sizeSel.value == "none") {
		clearOptions(colorSel);
		return;
	}
	var tmp = sizeSel.value.split(" - ");
	var type = tmp[0];
	var size = tmp[1];
	var currentColor = colorSel.value;
	clearOptions(colorSel);
	colorSel[0] = new Option("Choose Color", "none");
	var index = 1;
	var colors = inventoryHash[type][size];
	var match;
	for(var color in colors) {
	    var variantId = colors[color][0];
		match = (currentColor == color);
		colorSel[index++] = new Option(color, variantId, false, match);
	}
}

function changeColor(colorSel) {
	if(colorSel.value == "none") {
		clearOptions(qtySel);
		return;
	}
	var color = colorSel.options[colorSel.selectedIndex].text;
	var currentQty = qtySel.value;
	clearOptions(qtySel);
	var index = 0;
	var tmp = sizeSel.value.split(" - ");
	var type = tmp[0];
	var size = tmp[1];
	var qty = inventoryHash[type][size][color][1];
	var match;
	while(index < qty) {
		match = (currentQty == index + 1);
		qtySel[index] = new Option(index + 1, index + 1, false, match);
		index++;
	}
}

function changeQty(qtySel) {
	
}

function clearOptions(sel) {
  for(i = sel.options.length - 1; i >= 0; i--)
  {
     sel.remove(i);
  }
}

// show an image in a pop-up window
function zoom(zoomImage) {
    windowName = "Zoom";
    windowURL = zoomImage;
    myWindow = window.open(windowURL, windowName,'location=0,menubar=0,toolbar=0,status=0,resizable=1,scrollbars=1,width=600,height=470');
    myWindow.focus();
} 

// show a different shirt image in the current window
function showColor(color) {
    img = document.getElementById("shirtImage");
    img.src = color;
}

// add to shopping cart
function addToCart() {
    //alert("add to cart");
    var form = document.getElementById("addToCart");
    form.size.value  = sizeSel.value;
    form.color.value = colorSel.options[colorSel.selectedIndex].text;
    form.variantId.value = colorSel.value;
    form.qty.value   = qtySel.value;
    form.submit();
}

