var products = {};


// ***********************************************
//
// AJAX method to retrieve product skus,
// variations, and quantities. 
//
//***********************************************
function AJAX_REQ_getInventory(collection) {
	
	$.ajax({
		type: "POST",
		url: "/ajax/store/product",
		data: {"f":"getInventory", "collection":collection},
		dataType: "json",
		success: AJAX_RES_getInventory
	});
}


function AJAX_RES_getInventory(data, status) {
}
//***********************************************

// Rebuilds the sizes pulldown based on the selected color
function getApparelColorSizes(color, callback) {
	//console.log("getApparelColorSizes(" + color + ")");
	
	// 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) {
	//console.log("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 button
		$("#cashMethod").attr('disabled', false);		
		
		// Change the status of availability
		$("#availability").html("In Stock");
		$("#availability").removeClass("fail").addClass("success");
		
		// Update the sku
		$("#sku").html("SKU #" + sku);
		
	// Sold out
	} else {
		
		// Disable the add to cart button(s)
		$("button[type=submit]").html('<span>Out of stock</span>');
		$("button[type=submit]").attr('disabled', true);
		
		// Disable the radio button
		$("#cashMethod").attr('disabled', true);
		
		// Change the status of availability
		$("#availability").html("Out Of Stock");
		$("#availability").removeClass("success").addClass("fail");
		
		// Update the sku
		$("#sku").html("SKU #" + sku);
	}
}

$(document).ready(function() {
	
	if(products !== undefined) {
	
		// Build the pulldowns
		getApparelColorSizes($("#variationsColor").val());
		
		// 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());
		});

		// Add event listener for size change
		$("#variationsSize").change(function() {
			
			// Check availability for color and size selected
			checkApparelAvailability($("#variationsColor").val(), $("#variationsSize").val());
		});
	}
	
	//adds fancybox/lightbox functionality for product previews
	$('body.store .product .figure a').fancybox(
		{
			'padding': 0,
			'overlayColor': '#000',
			'overlayOpacity': 0.6,
			'zoomOpacity' : true,
			'zoomSpeedIn' : 500,
			'zoomSpeedOut' : 500
		}
	);
	
});