function checkTitle() {
	
	var titleOK = true;
	$("#optionBlocks .title").each( function() {
		var groupID = $(this).attr("id").replace("title","");
		var optionSelected = $("#option"+groupID+" :checked").length;
		if ( !optionSelected ) {
			$("#title"+groupID+" span").html("none");
			titleOK = false;
		}
	});
	
	if ( titleOK == false ) {
		
		$("#quantity").html("");
		
	}
	
}
function getQOH() {
	
	var totalOptions = ($(".optionBlock").length) - 1;
	var totalOptionsSelected = $("#optionBlocks .radioHidden:checked").length;
	
	if ( totalOptionsSelected == totalOptions ) {
		
		$("#quantity").html("<img src='"+ baseImagePath +"/media/images/ajax-loader.gif' border='0'>");

		var allAttributes = "";
		$(".radioHidden:checked").each( function() {
			allAttributes += $(this).val()+",";
		});

		// Get info on the selected product.
		$.getJSON(baseImagePath +"/ajax.php", {
			'action' : 'getQOH',
			'allAttributes' : allAttributes,
			'parentProduct' : parentProduct
		}, function(json) {
			$("#realPrice").text(json.realPrice);
			$("#quantity").html(json.quantitySelect);
		});
		
	}
	
}

function updateTitle(titleID,newTitle) {
	$("#title"+titleID+" span").html(newTitle);
}

function updateOptions(selectedAttribute,selectedAttributeGroup) {

	var allSelectedAttributes = "";
	var selectedAttribute = $("#option"+selectedAttribute).val();
	
	$(".radioHidden:checked").each( function() {
		allSelectedAttributes += $(this).val()+",";
	});
	
	$.getJSON(baseImagePath +"/ajax.php", { "action" : "updateOptions", "parentProduct" : parentProduct, "selectedAttribute" : selectedAttribute, "allSelectedAttributes" : allSelectedAttributes, "selectedAttributeGroup" : selectedAttributeGroup }, function(json){
		
		jQuery.each(json, function(i, val) {
			var jsonOptions = String(val);
			$('#'+i).html(jsonOptions);
		});
		
		getQOH();
		checkTitle();
		
	});
	
}

function mediumImage(imageURL,title) {
	
	
	$("#moreInformationGraphic").html("<a href='"+ imageURL +"&w=1000&h=1000' title='"+ title +"'><img src='"+ imageURL +"' border='0'></a>");
	$("#moreInformationGraphic a").jqzoom({
		zoomWidth : zzoomWidth,
		zoomHeight : zzoomHeight,
		xOffset : zxOffset,
		yOffset : zyOffset
	});
	return false;
}

function updateImages(attributeID) {

	$.getJSON(baseImagePath +"/ajax.php", { "action" : "getThumbnails", "parentProduct" : parentProduct, "attributeID" : attributeID }, function(json){
		if (json.images) {
			$("#moreInformationGraphic").html(json.mainImage);
			//$("#moreInformationThumbs").html(json.images);
			// After updating the images, we have to refire the
			// lightBox plugin to bind them.
			$("#moreInformationGraphic a").jqzoom({
				zoomWidth : zzoomWidth,
				zoomHeight : zzoomHeight,
				xOffset : zxOffset,
				yOffset : zyOffset
			});
			
		}
	});
	
}
function validate() {
	
	var totalOptions = ($(".optionBlock").length) - 1;
	var totalSelectedOptions = $(".radioHidden:checked").length;
	
	if ( totalOptions != totalSelectedOptions ) {
		
		alert("Please select a value for each option");
		
	} else {
		
		$("#addToCartConfirmBtn").fadeIn(1000).fadeOut(1000, function() {
			document.productForm.submit();
		});
		
	}
	
}
$(document).ready( function() {
	
	$("#btnAddToCart").click( function() {
		validate();								   
	});
	
	$("#orderingOptions .optionLabel").livequery('click', function(event) { 
		$(this).parents(".optionBlock").find(".optionLabel").removeClass("optionSelected");
		$(this).addClass("optionSelected");
		$(this).parents("label").children("input").click().change();
	}); 
	$(".radioHidden").change( function() {
		getQOH();
	});
	$("#moreInformationGraphic a").jqzoom({
		zoomWidth : zzoomWidth,
		zoomHeight : zzoomHeight,
		xOffset : zxOffset,
		yOffset : zyOffset
	});
	
});