var ProductID;
$(document).ready(function()
{
	ProductID = $('input[name=ProductID]').val();
	
	var flashvars = {xml: 'flash/gallery_xml.new.asp?id=' + ProductID};
	var params = {wmode:"opaque", bgcolor: "#ffffff"};
	var attributes = {id:'productImage', name:'productImage'};
	swfobject.embedSWF("flash/gallery.new.swf", "productImage", '300', '395', "9.0.0", "expressInstall.swf", flashvars, params, attributes);
	
	$('#swatchesPopup').click(function(e)
	{
		e.preventDefault();
		
		$('#productSwatches ul').toggleClass('expanded');
		$(this).text($(this).text() == 'View all the colours' ? 'Minimise' : 'View all the colours');
		
		//TINY.box.show('swatches.asp?pid=' + ProductID, 1, 414, 650, 1);
	});
	
	/*$('#makeAnEnquiryButton').click(function(e)
	{
		e.preventDefault();
		GB_showCenter('', $(this).attr('href'), 580, 655);
	});*/

// Colour Options.
$('#coloursOption a').click(function(e)
{
	e.preventDefault();
	
	// Remove active state on all colour options.
	$(this).parents('#coloursOption').find('a').removeClass('active');
	
	// Set active state on selected option.
	$(this).addClass('active');
	
	// Set value of colour option.
	var colourValue = $(this).attr('href').split('_');
	colourValue = colourValue[colourValue.length - 1];

	$('input[name=Colour]').val(colourValue);
	
	// Trigger stock availability check.
	$(this).parents('form').trigger('updateavailablestock');
});

// Stock availability checks.
$('#addToCart').bind('updateavailablestock', function()
{
	// Prepare data.
	var data = {};
	data.id = $('input[name=ProductID]').val();
	data.c1 = $('input[name=Colour]').val();
	
	// Get stock.
	$.ajax(
	{
		url: rootFolder + '/ajax/getShoppingCartVariationStock.asp', 
		dataType: 'html', 
		type: 'GET', 
		
		data: data, 
		
		success: function(html)
		{
			if ($('select[name=ProductSizes]').length > 0) {
				// Get size select box.
				var $oldSizes = $('select[name=ProductSizes]');
				var $newSizes = $(html);
				
				// Make updates.
				$newSizes.find('option').each(function()
				{
					var value    = $(this).val();
					var cssClass = $(this).attr('class');
					var disabled = $(this).is(':disabled');
					var text     = $(this).text();
					
					var option = $oldSizes.find('option[value=' + value + ']');
					option.attr('class', cssClass);
					option.attr('disabled', disabled);
					option.text(text);
				});
				
			} else { 
				if ($(html).find('option').size() == 0) {
					$('#addBtnDisabled').css('display', 'block');
					$('#addBtn').css('display', 'none');
				}
				else
				{
					$('#addBtnDisabled').css('display', 'none');
					$('#addBtn').css('display', 'block');
				}
			}
		}
	});
});

// Colour hovers.
/*	$('#coloursOption a').tooltip(
{
	track: true, 
	delay: 0,
	showURL: false, 
	bodyHandler: function()
	{
	return $('<img/>').attr('src', $(this).find('img').attr('src')); 
}
});
*/	
$('#addToCart').submit(function(e) {
	if ( ($('input[name=Colour]').length && $('input[name=Colour]').val() == '') || ($('select[name=ProductSizes]').length && $('select[name=ProductSizes] option:selected').attr('class') != '') ) {
		alert('Please select all options.');
		e.preventDefault();
	}
});

});
