// skupicker submit button change transpency if something is selected
$('#skucarter, #skuwisher').change(function(){

	var value = $(this).val();
	var submit	= $(this).parent().find('input.okay');
	
	if( value != '-'){
		submit.addClass('ready');
	}else{
		submit.removeClass('ready');
	}
	
});


// skupicker brings up the sku selection box
function skupicker( cart_or_wish )
{
	var skuselect = document.getElementById('sku');
	cart_or_wish = cart_or_wish_valid( cart_or_wish );
	var multiselect = document.getElementById('sku_1');
	var passthrough = document.getElementById('sku_passthrough');

	if ( ! skuselect )
		return true;

	// Standard drop-down selection for multiple SKUs uses a value of '-' as the default
	if ( skuselect.value == '-' )
	{
		// show the div with the selector in it
		var skupicker	= $('#skuto' + cart_or_wish).attr('id');
		
		$.fancybox({
			href				: '#' + skupicker,
			overlayOpacity		: 0.65,
			overlayColor		: '#000'
		});

		return false;
	}
	// Checkbox-based SKU selection uses the SKU "_multi" to indicate multiple selection is available;
	// however, we update the sku field when we submit, so if the user backed up to this page, sku may
	// still be set to the wrong thing. Testing for the first sku id instead is INVINCIBLE!
	//
	// First we need to verify that the pop-up's button wasn't just pushed; if it was, we need to allow the click
	else if ( multiselect && !$('#skutowish').is(':visible') && passthrough && passthrough.value == 1 )
	{
		// Restore passthrough to 0, so next use of this page (if visitor backs up) behaves normally
		passthrough.value = 0;

		return true;
	}
	// If the pop-up's button wasn't just pushed, pop it up
	else if ( multiselect && !$('#skutowish').is(':visible') )
	{
		// get count of skus
		var skucount = document.getElementById('sku_multimax');

		if ( ! skucount )
			return true;

		var selectedskus = 0;
		// scan through all sku_* fields
		for( var i = 1; i <= skucount.value; i++ )
		{
			var sku_checkbox = document.getElementById('add_sku_' + i);
			if ( sku_checkbox && sku_checkbox.checked )
			{
				selectedskus ++;
			}
		}

		// if we still have 0 selected, pop up
		if ( selectedskus == 0 )
		{				
			// show the div with the selector in it
			var skupicker = $('#skuto' + cart_or_wish ).attr('id');
			
			$.fancybox({
				href				: '#' + skupicker,
				overlayOpacity		: 0.65,
				overlayColor		: '#000'
			});

			return false;
		}
	}

	return true;
}


// skusetter sets the *real* sku drop-down and submits the page
function skusetter( cart_or_wish )
{
	var skuselect = document.getElementById('sku');
	var passthrough = document.getElementById('sku_passthrough');
	var skuchooser;
	cart_or_wish = cart_or_wish_valid( cart_or_wish );

	skuchooser = document.getElementById('sku' + cart_or_wish + 'er'); // skucarter or skuwisher

	// match one sku selector to the other
	skuselect.value=skuchooser.value;

	// when necessary (if in checkbox mode) indicate that ignoring the test for
	// whether we have a sku set is okay temporarily
	if ( passthrough )
		passthrough.value = 1;

	var submitbutton = document.getElementById('submit' + cart_or_wish);
	submitbutton.click();
}

// skucloser closes the sku selection popup
function skucloser( cart_or_wish )
{
	// hide popup
	var skupicker = document.getElementById('skuto' + cart_or_wish );
	skupicker.style.display = 'none';
}

function cart_or_wish_valid( cart_or_wish )
{
	// validate cart/wish mode
	if ( cart_or_wish == 'wish' ) 
	{
		return 'wish';
	}
	else
	{
		return 'cart';
	}
}


// check_all_checkboxes scans the checkbox list (if present), turns them all on
// and toggles the link between states
function check_all_checkboxes( new_state )
{
	// get count of skus
	var skucount = document.getElementById('sku_multimax');

	if ( ! skucount )
		return true;

	// scan through all sku_* fields
	for( var i = 1; i <= skucount.value; i++ )
	{
		var sku_checkbox = document.getElementById('add_sku_' + i);
		if ( sku_checkbox )
			sku_checkbox.checked = ( new_state ? true : false );
	}

	// change link
	var select_link = document.getElementById('sku_checkbox_item');
	if ( select_link )
	{
		if ( new_state )
			select_link.innerHTML = '<a href="?checked=0" onclick="return check_all_checkboxes(0)">Deselect All</a>';
		else
			select_link.innerHTML = '<a href="?checked=1" onclick="return check_all_checkboxes(1)">Select All</a>';
	}

	return false;
}

