// JB20090420
var $j, asidOptions = new Array();


function setup_combos()
{
	$j = jQuery.noConflict();
	$j.each(hhOptions, function(sidOp, rhOp)
	{
		asidOptions[asidOptions.length] = sidOp;
		$j('#op_'+sidOp).change(function() { update_options(this); });
	});
	update_options();

    	$j('#add2cart').bind('click', function(e)
	{
		var rtnb      = true,
		    hSelected = new Object(),
		    bAllOps   = true,
		    sError    = '';
		// are all required options selected?
		$j.each(asidOptions, function(i, sidOp)
		{
			var sidChild = $j('#op_'+sidOp).val();
			if (sidChild == '-' && hhOptions[sidOp].required) {
				sError = hhOptions[sidOp].name+' is a required field';
				bAllOps = false;
			}
			hSelected[sidOp] = sidChild;
			return bAllOps;
		});
		if (bAllOps) {
			var sidCombo = find_combo(hSelected);
			if (sidCombo) {
				// check stock, etc
				var rhCombo = hhCombos[sidCombo],
				    nQty = $j('#qty').val();
        			if (isNaN(nQty) || nQty < 1)
					sError = 'The quantity is invalid. Please enter a valid number';
				else if (rhCombo.stock_dec && nQty > rhCombo.stock) {
					sError = (rhCombo.stock)
						? ("Sorry! We don't have that quantity in stock at the moment.\r\n"
						   +'The current stock level is '+rhCombo.stock+'.')
						: 'Sorry! This item is currently out of stock.';
				}
			} else {
				sError = 'Sorry, the selected combination of options is currently unavailable.';
			}
		}
		if (sError) {
			alert(sError);
			e.preventDefault();
			rtnb = false;
		}
		return rtnb;
	});
}

function find_combo(rhOps)
{
	var rtnsid = false;
	$j.each(hhCombos, function(sidCombo, rhCombo)
	{
		var bAllOps = true;
		$j.each(rhCombo.options, function(sidOp, sidChild)
		{
			if (rhOps[sidOp] != sidChild)
				bAllOps = false;
			return bAllOps;
		});
		if (bAllOps)
			rtnsid = sidCombo;
		return !bAllOps;
	});
	return rtnsid;
}

function update_options()
{
	// get all selected options, gen arrays of relevant children, grey out those not in the list
	var hSelected  = new Object(),
	    hAvailable = new Object(),
	    sidLastOp;
	$j.each(asidOptions, function(i, sidOp)
	{
		var sidChild = $j('#op_'+sidOp).val();
		if (sidChild != '-' || !hhOptions[sidOp].required)
			hSelected[sidOp] = sidChild;
		hAvailable[sidOp] = new Object();
		sidLastOp = sidOp;
	});
	// go through combo array, for combos containing selected children, add other children to available hash
	$j.each(hhCombos, function(sidCombo, rhCombo)
	{
		var bAllMatch = true;
		$j.each(hSelected, function(sidOp, sidChild)
		{
			if (sidOp != sidLastOp && rhCombo.options[sidOp] != sidChild)
				bAllMatch = false;
			return bAllMatch;
		});
		if (bAllMatch) {
			$j.each(rhCombo.options, function(sidOp, sidChild)
			{
				// add array of related combos to available ops
				if (typeof(hAvailable[sidOp][sidChild]) == 'undefined')
					hAvailable[sidOp][sidChild] = new Array();
				raCombos = hAvailable[sidOp][sidChild];
				raCombos[raCombos.length] = sidCombo;
			});
		}
	});

	// Finally, go through all the options, set class for on or off, show prices in last option if
	// theres only one combo related to each child
	// Note: if all or all but the final option are selected, display prices in the final box
	$j('#op_'+sidLastOp+' option').each(function(i)
	{
		var sidChild = $j(this).val();
		if (sidChild != '-') {
			if (typeof(hAvailable[sidLastOp][sidChild]) != 'undefined') {
				rhCombo = hAvailable[sidLastOp][sidChild];
				if($j(this).attr('selected'))
					$j(this).parent().removeClass();
				$j(this).removeClass().addClass('available');
				if (hAvailable[sidLastOp][sidChild].length == 1) {
					var sStock = 'In stock',
					    rhCombo = hhCombos[rhCombo[0]];
					if (rhCombo.stock_dec)
						sStock = (rhCombo.stock < 1)
							? 'Out of stock'
							: rhCombo.stock+' in stock';
					$j(this).text(hhOptions[sidLastOp].children[sidChild]+' ('+sStock+')');
				} else
					$j(this).text(hhOptions[sidLastOp].children[sidChild]);
			} else {
				$j(this).removeClass().addClass('unavailable');
				if($j(this).attr('selected'))
					$j(this).parent().addClass('unavailable');
				$j(this).text(hhOptions[sidLastOp].children[sidChild]);
			}
		}
	});
}


