/* -------------------- */
/*   Option Functions   */
/* -------------------- */

function selectProductOption(productId, changed) {
	// initialize variables
	var link       = "http://" + domain + "/store/remote/cart.cfm/mode/getNextOptionType";
	var params     = "json=1&productId=" + productId;
	var numOptions = parseInt(jQuery("form#productForm input#optioncount").val());
	var selOptions = "";
	var thisOption = "";

	changed = parseInt(changed);

	// Did we select the last option or a previous?
	if(numOptions > changed) {
		for( var d = numOptions; d > changed; d-- ) {
			jQuery("table#option" + d + "table").remove();
		}

		numOptions = changed;

		jQuery("form#productForm input#optioncount").val(numOptions);
	}

	// If we selected a blank option, do nothing.
	if(jQuery("form#productForm select#option" + changed).val() == "") {
		return false;
	}

	for( var o = 1; o <= changed; o++ ) {
		thisOption = jQuery("form#productForm select#option" + o).val();

		if(thisOption != "") {
			if (selOptions == "") {
				selOptions = thisOption;
			} else {
				selOptions += "," + thisOption;
			}
		}
	}

	params = params + "&selected=" + selOptions;

	jQuery.post(link, params, function(json) {
		if(json.recordcount > 0) {
			// Update the option count.
			numOptions += 1;
			jQuery("form#productForm input#optioncount").val(numOptions);

			var newOptionBlock = jQuery("div#optionblock").html();
			var optionType     = "";
			var optionHTML     = "";
			var iRegEx         = new RegExp("%i%", "g");
			var idRegEx        = new RegExp("%id%", "g");
			var typeRegEx      = new RegExp("%type%", "g");

			newOptionBlock = newOptionBlock.replace(iRegEx, numOptions);
			newOptionBlock = newOptionBlock.replace(idRegEx, productId);

			for(var i = 0; i < json.recordcount; i++) {
				optionType = json.data.option_type[i];

				// Build the options as a string...
				optionHTML += "<OPTION VALUE='" + json.data.option_id[i] + "'>" + json.data.option_name[i] + "</OPTION>\n";
			}

			newOptionBlock = newOptionBlock.replace(typeRegEx, optionType);

			jQuery("div#optionDiv").append(newOptionBlock);
			jQuery("form#productForm select#option" + numOptions).append(optionHTML);
		} else {
			// If results are blank, there are no more options.
			checkOptionCombo();
		}
	}, "json");
}

function checkOptionCombo() {
	// initialize variables
	var link       = "http://" + CGI["http_host"] + "/store/remote/product.cfm/mode/validateOptions";
	var productId  = jQuery("form#productForm input#productId").val();
	var params     = "json=1&productId=" + productId;
	var numOptions = parseInt(jQuery("form#productForm input#optioncount").val());
	var selOptions = "";
	var thisOption = "";

	for(var o = 1; o <= numOptions; o++) {
		if(selOptions == "") {
			selOptions = jQuery("form#productForm #option" + o).val();
		} else {
			selOptions = selOptions + "," + jQuery("form#productForm #option" + o).val();
		}
	}

	params = params + "&optionIds=" + selOptions;

	// Json Post
	jQuery.post(link, params, function(json) {
		if(json.recordcount == 1) {
			var result  = json.data.result[0];
			var picture = json.data.picture[0];
			var price   = json.data.price[0];

			if(result == 1) {
				jQuery("a.representative").attr("href", "/images/store/_original/" + picture);
				jQuery("img.representative").attr("src", "/images/store/_large/" + picture);

				// Update any spans.
				jQuery("#productPrice").html("$" + price.toFixed(2));

				// Run Event
				onPriceChange(jQuery("#productPrice"));

				enableAddCartButton();
			} else {
				alert("That is an invalid option combination. Please select another.");
			}
		} else {
			alert("There was an error. If this occurs frequently, please contact the site administrator.");
		}
	}, "json");
}

/* ------------------- */
/*   Event Functions   */
/* ------------------- */

function onPriceChange(c) { }

/* --------------------------------- */
/*   Button Manipulation Functions   */
/* --------------------------------- */

function disableAddCartButton() {
	var formtag = $("#productForm");
	jQuery("#cart_add",formtag).attr("disabled",true).css("cursor","default").animate({opacity:0.4},"fast");
}

function enableAddCartButton() {
	var formtag = $("#productForm");
	jQuery("#cart_add",formtag).removeAttr("disabled").css("cursor","pointer").animate({opacity:1},"fast");
}

/* ------------------------- */
/*   jQuery Load Functions   */
/* ------------------------- */

jQuery(document).ready(function(){
	var formtag = $("#productForm");

	jQuery("#cart_add",formtag).click(function() {
		var baseLink   = "http://" + CGI["http_host"] + "/store/cart.cfm/mode/add";
		var finalLink  = "";
		var productId  = $("#productId",formtag).val();
		var numOptions = parseInt($("#optioncount",formtag).val());
		var qty        = ($("#qty",formtag).length > 0) ? $("#qty",formtag).val() : 1;
		var selOptions = "";
		var selAddOns  = "";

		disableAddCartButton();

		for(var o = 1; o <= numOptions; o++) {
			var thisVal = $("#option" + o,formtag).val();

			if(thisVal) {
				if(selOptions != "") { selOptions = selOptions + "," }
				selOptions = selOptions + $("#option" + o,formtag).val();
			}
		}

		jQuery("input[name='addons']::checked",formtag).each(function() {
			$(this).trigger("addon.beforesubmit");
			if(selAddOns != "") { selAddOns = selAddOns + "," }
			selAddOns = selAddOns + $(this).val();
		});

		finalLink = baseLink + "/id/" + productId;

		if(selOptions != "") { finalLink = finalLink + "/options/" + selOptions; }
		if(selAddOns != "") { finalLink = finalLink + "/addons/" + selAddOns; }
		if(qty > 1) { finalLink = finalLink + "/qty/" + qty; }

		location.href = finalLink;
	});

	jQuery("img[piclink][piclarge]",formtag).click(function() {
		var imgSrc  = $(this).attr("piclarge");
		var imgLink = $(this).attr("piclink");

		$("a.representative").attr("href",imgLink);
		$("img.representative").attr("src",imgSrc);
	});

	if(jQuery("#cart_add",formtag).attr("disabled") == true) { disableAddCartButton(); }
});