// 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()
{
	if (arguments.length) {
		//alert('change '+arguments[0].id+' '+$j(arguments[0]).val());
	}

	// 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();
		//console.log(sidOp, sidChild, hhOptions[sidOp]);
		if (sidChild != '-' || !hhOptions[sidOp].required)
			hSelected[sidOp] = sidChild;
		hAvailable[sidOp] = new Object();
		sidLastOp = sidOp;
	});
	//console.log('hAvailable', hAvailable);
	//console.log('hSelected', hSelected);
	// 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)
		{
			//console.log('op '+sidOp+': checking '+rhCombo.options[sidOp]+' against '+sidChild);
			if (sidOp != sidLastOp && rhCombo.options[sidOp] != sidChild) {
				//console.log(rhCombo.options[sidOp]+' is not '+sidChild);
				bAllMatch = false;
			}
			return bAllMatch;
		});
		//console.log(sidCombo+((bAllMatch)?' matches':' does not match'));
		if (bAllMatch) {
			//console.log(rhCombo.options);
			$j.each(rhCombo.options, function(sidOp, sidChild)
			{
				//console.log(sidCombo+': '+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;
			});
		}
	});
	//console.log(hSelected);
	//console.log(hAvailable);

	// 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];
				//console.log('last bit - available '+i, sidLastOp, sidChild);
				if($j(this).attr('selected'))
					$j(this).parent().removeClass();
				$j(this).removeClass().addClass('available');
				if (hAvailable[sidLastOp][sidChild].length == 1)
					$j(this).text(hhOptions[sidLastOp].children[sidChild]+' '+hhCombos[rhCombo[0]].price);
				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]);
			}
		}
	});
}


