var Product = {
		variations: null,
		selected: 0,

		init: function() {
			Product.variations = variations;
			Product.buildOptions($("#variationColor").val());
			Product.selectVariation();
			
			$("#variation").live('change', Product.refresh);
			$("#variationColor").live('change', Product.buildOptions);
			$("#variationSize").live('change', Product.selectVariation);
		},
		
		buildOptions: function(color) {
			if( typeof(color) === "string" ) {
				$("#variationSize").removeOption(/./);
				Product.variations.sort(function(a, b) {
					return a.size - b.size;
				});
				
				for(var i = 0; i < Product.variations.length; i++) {
					if(Product.variations[i].color == color) {
						$("#variationSize").addOption(Product.variations[i].size, Product.variations[i].size);
					}
				}
				$("#variationSize option:first").attr('selected', true);
			}
			Product.selectVariation();
		},
		
		selectVariation: function() {
			Product.color = $("#variationColor").val();
			Product.size = $("#variationSize").val();
			
			for(var i = 0; i < Product.variations.length; i++)
				if(Product.variations[i].color == Product.color && Product.variations[i].size == Product.size)
					Product.selected = i;
			
			Product.refresh();
		},
		
		refresh: function() {
			
			Product.sku = $("#variation").val();
			
			for(var i = 0; i < Product.variations.length; i++)
				if(Product.variations[i].sku == Product.sku)
					Product.selected = i;
			
			$('#sku>code').html(Product.variations[Product.selected].sku);
			$('input[name=sku]').val(Product.variations[Product.selected].sku);
			$('input#currency').val(Product.variations[Product.selected].currency);
			if(Product.variations[Product.selected].currency=='POINTS') {
				$('li.price').html(Math.round(Product.variations[Product.selected].price)+'pts');
				$('ul.payment>li>label>strong').html(Math.round(Product.variations[Product.selected].price)+'pts');
			}
			else {
				$('li.price').html('$'+Product.variations[Product.selected].price);
				$('ul.payment>li>label>strong').html('$'+Product.variations[Product.selected].price);
			}
			if(Product.variations[Product.selected].quantity>0) Product.available(true);
			else Product.available(false);
		},
		
		
		available: function(status) {
			if(status) {
				$('#availability').removeClass('out').addClass('in').html('In Stock');
				$("button[type=submit]").html('<span>Add to cart</span>');
				$("button[type=submit]").attr('disabled', false);
				$("#method").attr('disabled', false);
				$("#quantity").attr('disabled', false);
			}
			else {
				$('#availability').removeClass('in').addClass('out').html('Out Of Stock');
				$("button[type=submit]").html('<span>Out of stock</span>');
				$("button[type=submit]").attr('disabled', 'disabled');
				$("#method").attr('disabled', 'disabled');
				$("#quantity").attr('disabled', 'disabled');
			}
		}
		
};

$(document).ready(function(){
	Product.init();

    var largePreview = $('#large-preview');

	$('.GC_figure .mini').bind('click',function(e){
		e.preventDefault();
		
		var source = $(this).attr('href');
		
		largePreview
			.attr( 'src', source )
			.parent('a')
			    .attr('href', source );
	});

	// Fancy Effects For Event Details
	$('input[type="radio"]')
		.each(function() {
			var $line = $(this).closest('li');
			
			this.checked ? $line.addClass('checked') : $line.removeClass('checked');
		})
		.live('change', function() {
			var $input = $(this),
				$siblings = $('input[name="' + $input.attr('name') + '"]').closest('li'),
				$line = $input.closest('li');
			
			$siblings.removeClass('checked');
			$input.is(':checked') ? $line.addClass('checked') : $line.removeClass('checked');
	});
	
	
});
/*
// Rebuilds the sizes pulldown based on the selected color
function getApparelColorSizes(color, callback) {
	
	// First, empty out the pulldown
	$("#variationsSize").removeOption(/./);
	
	// Sort sizes in numerical order
	products.sort(function(a, b) {
		return a.size - b.size;
	});
	
	// Now, loop through the master list of SKUs and build the sizes based on the selected color
	for(var i = 0; i < products.length; i++)
		if(products[i].color == color) $("#variationsSize").addOption(products[i].size, products[i].size);
}

function checkApparelAvailability(color, size) {
	
	var qty = 0;
	var sku = '';
	
	// Loop through the array and find the quantity
	for(var i = 0; i < products.length; i++)  {
		if(products[i].color == color && products[i].size == size) {
			qty = products[i].cash_quantity + products[i].point_quantity;
			sku = products[i].sku;
		}
	}
	
	// Enough inventory?
	if(qty > 0) {
		
		// Enable the add to cart button(s)
		$("button[type=submit]").html('<span>Add to cart</span>');
		$("button[type=submit]").attr('disabled', false);
		
		// Disable the radio and qty input
		$("#cashMethod").attr('disabled', false);
		$("#quantity").attr('disabled', false);
		
		// Change the status of availability
		$("#availability").html("In Stock");
		$("#availability").removeClass("out").addClass("in");
		
		// Update the sku
		$("#sku").html("Sku: <code>" + sku + "</code>");
		
	// Sold out
	} else {
		
		// Disable the add to cart button(s)
		$("button[type=submit]").html('<span>Out of stock</span>');
		$("button[type=submit]").attr('disabled', 'disabled');
		
		// Disable the radio and qty input
		$("#cashMethod").attr('disabled', 'disabled');
		$("#quantity").attr('disabled', 'disabled');
		
		// Change the status of availability
		$("#availability").html("Out Of Stock");
		$("#availability").removeClass("in").addClass("out");
		
		// Update the sku
		$("#sku").html("Sku: <code>" + sku + "</code>");
	}
}

var gc_itemAvailability = {
	
	init: function(){
		if(gc_productsItems && typeof(gc_productsItems) === 'object'){
			this.build();
		}
	},
	
	listen: function(){
		$('input[type=radio]', '.payment').live('change', function(){
			var sku = $(this).attr('data-sku');
			$('#sku').html("Sku: <code>" + sku + "</code>");
			$('input[name=sku]', '.payment').attr('disabled', true);
			$('input[name=sku][value='+sku+']', '.payment').attr('disabled', false);
		});
	},
	
	set: function(){
		var sku = $('input[type=radio]:checked', '.payment').attr('data-sku');
		
		if(!sku){ return false; }
		
		$('#sku').html("Sku: <code>" + sku + "</code>");
		$('input[name=sku]', '.payment').attr('disabled', true);
		$('input[name=sku][value='+sku+']', '.payment').attr('disabled', false);
		
	},
	
	build: function(){
	
		var $payment = $('.payment', '.purchase_form');

		if(gc_productsItems.length > 1){
			$payment.empty();
			
			this.listen();
			
			$.each(gc_productsItems, function(i){
			
				var value_type = value = null;
				
				switch(this.value_type){
					case 'POINT':
						value_type = 'Points';
						value = Math.round(this.value) + ' pts';
						break;
					case 'CASH':
						value_type = 'Cash';
						value = '$'+this.value;
						break;
					default: break;
				}
			
				$('<li class="radio">'+
						'<label for="'+value_type.toLowerCase()+'Method">'+
							'<input type="hidden" name="sku" value="'+this.sku+'" disabled="disabled"/>'+
							'<input data-sku="'+this.sku+'" id="'+value_type.toLowerCase()+'Method" type="radio" value="'+this.value_type+'" name="paymentMethod" />'+
							' Purchase With '+value_type+' - '+
							'<strong>'+value+'</strong>'+
						'</label>'+
						'<span> This item can be purchased with '+value_type.toLowerCase()+'.</span>'+
					'</li>')
				.appendTo($payment);
				
				if(i === 0){ 
					$('input:radio[data-sku='+this.sku+']', '.payment').attr('checked', 'checked');
					gc_itemAvailability.set();
				 }
			});
			

		}
		
		return false;
	}
};

$(document).ready(function() {

	if(gc_productsItems 
	&& gc_productsItems !== undefined 
	&& typeof(gc_productsItems) === 'object'){ 
		gc_itemAvailability.init(); 
	}
	
	if(products !== undefined) {
	
		// Build the pulldowns
		getApparelColorSizes($("#variationsColor").val());
		
		
		
		if($('#variationsColorPoints').length && $('#variationsSizePoints').length){
		
			var $pointColor = $('#variationsColorPoints'),
				$pointSize = $('#variationsSizePoints');
				
				getApparelColorSizes($pointColor.val());
				
			//for points
			$pointColor.change(function() {
				getApparelColorSizes($pointColor.val());
				checkApparelAvailability($pointColor.val(), $pointSize.val());
	
				if($.isFunction($.fn.uniform)) {
					$.uniform.update();
				}
			});
	
			$pointSize.change(function() {
				checkApparelAvailability($pointColor.val(), $pointSize.val());
				if($.isFunction($.fn.uniform)) {
					$.uniform.update();
				}
			});
		
		}

		
		
		// Check availability for default selection
		if ( ($('#pointMethod') != []) && ($('#pointMethod').attr('checked')) ) {
			checkApparelAvailability($("#variationsColorPoints").val(), $("#variationsSizePoints").val());
		} else {
			checkApparelAvailability($("#variationsColor").val(), $("#variationsSize").val());
		}
		
		
		// Add event listener for color change
		$("#variationsColor").change(function() {
			// Get available sizes for selected color
			getApparelColorSizes($("#variationsColor").val());
			
			// Check availability for color and size selected
			checkApparelAvailability($("#variationsColor").val(), $("#variationsSize").val());
			
			if($.isFunction($.fn.uniform)) {
				$.uniform.update();
			}
		});

		// Add event listener for size change
		$("#variationsSize").change(function() {
			
			// Check availability for color and size selected
			checkApparelAvailability($("#variationsColor").val(), $("#variationsSize").val());
			
			if($.isFunction($.fn.uniform)) {
				$.uniform.update();
			}
		});
	}
});
*/
