$(document).ready(
	function()
	{		
		// set up page
    	resetOptions();
		
    	// when a colour selection is changed
		$('input[@name=colour]').change(
			function()
			{
				changeColourSelection( $(this).attr( 'value' ) );				
			}
		);
		
		/*
		//
		$('.sizeOption').keyup(
			function()
			{				
				// get quantity entered
				var quantity = $(this).val();
				var sizeId   = $(this).attr('id').substring(4);
								
				// loop through returned stock levels
				for( i = 0; i < sizeStockLevels.length; i++ )
				{					
					// is this the right option?
					if( sizeStockLevels[i][0] == sizeId )
					{						
						// check quantity is less than or equal to stock level
						if( quantity > 0 && quantity <= sizeStockLevels[i][1] )
						{
							// allow add to cart
							$('#addToCart').attr( 'disabled', '' );
						}
						else
						{
							// disallow add to cart
							$('#addToCart').attr( 'disabled', 'disabled' );
						}
					}
				}
			}
		);
		*/
	}
);

/**
 *
 */
function changeColourSelection( colourId )
{
	// get product id
	var productId = $('#p').val();
	
	// reset all size option boxes
	//resetOptions();
	
    // change thumbnail and large image
	updateProductImage( colourId );
				
	// disable add to cart button
	$('#addToCart').attr( 'disabled', '' );
	
	// check size options for stock
	//checkSizeStockLevels( productId, colourId );
}

/**
 *
 */
function checkSizeStockLevels( productId, colourId )
{
	// ajax url
	var requestUrl  = "/ajax/stockcolour.php?p=" + productId + "&c=" + colourId;
	
	// perform ajax request
	performRequest( requestUrl, processResponseSize );
}

/**
 *
 */
function performRequest( requestUrl, successCallback )
{		
	// perform ajax request
	$.ajax(
		{
			type: 		"GET",
			url:		requestUrl,
			dataType: 	"json",
			success:	successCallback,
		}
	);
}

/**
 *
 */
function processResponseSize( response )
{	
	// was a stock level check performed successfully?
	if( response['valid'] == true )
	{
		// disable all size option boxes
		$('.sizeOption').attr( 'disabled', 'disabled' );
		
		// loop through returned stock levels
		for( i = 0; i < response['stockLevels'].length; i++ )
		{
			// get size id
			var sizeId = response['stockLevels'][i][0];
			// get stock level
			var stockLevel = response['stockLevels'][i][1];
			
			// is this size option in stock?
			if( stockLevel > 0 )
			{
				$('#size' + sizeId).attr( 'disabled', '' );
			}
		}
		
		// store stock levels for future checks
		sizeStockLevels = response['stockLevels'];
	}
}

/**
 * Set up on page load
 */
function resetOptions()
{
    /*
	// disable all size option boxes
	$('.sizeOption').attr( 'disabled', 'disabled' );
	
	// reset all size option boxes to 0
	$('.sizeOption').each(
		function()
		{
			$(this).val(0);
		}
	);
	*/
	// disable add to cart button
	$('#addToCart').attr( 'disabled', 'disabled' );
}

/**
 *
 */
function updateProductImage( colourId )
{
	// get path to image
    var imagePath = '/p/image/' + colourId + '/'; 
    			
    // change image src
    $('#productImage img').attr( 'src', '/p/image/' + colourId + '/' );
    
    // change link location
	$('#productImage a').attr( 'href', '/p/image/' + colourId + '/' );
}