/* -------------------- */
/*   Option Functions   */
/* -------------------- */

function selectProductOption(productId, changed) {
	// initialize variables
	var link       = urlPrefix + "://" + CGI["http_host"] + "/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 && thisOption != "") {
			if(selOptions != "") selOptions += ","
			selOptions += thisOption;
		}
	}

	params += "&selected=" + selOptions;

	jQuery.ajaxSetup({ cache: false });

	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();
		}
	
		jQuery.ajaxSetup({ cache: true });
	}, "json");
}

function checkOptionCombo() {
	// initialize variables
	var link       = urlPrefix + "://" + 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 += ","
		selOptions += jQuery("form#productForm #option" + o).val();
	}

	params += "&optionIds=" + selOptions;

	jQuery.ajaxSetup({ cache: false });

	// 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").empty().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.");
		}

		jQuery.ajaxSetup({ cache: true });
	}, "json");
}

/* ------------------- */
/*   Event Functions   */
/* ------------------- */

function onPriceChange(c) { }

/* --------------------------------- */
/*   Button Manipulation Functions   */
/* --------------------------------- */

function disableAddCartButton() {
	var formtag = jQuery("#productForm");
	jQuery("#cart_add",formtag).attr("disabled",true).css("cursor","default").animate({opacity:0.4},"fast");
}

function enableAddCartButton() {
	var formtag = jQuery("#productForm");
	jQuery("#cart_add",formtag).removeAttr("disabled").css("cursor","pointer").animate({opacity:1},"fast");
}

/* ------------------------- */
/*   jQuery Load Functions   */
/* ------------------------- */

jQuery(document).ready(function(){
	var formtag = jQuery("#productForm");

	jQuery("#cart_add",formtag).click(function() {
		var baseLink   = urlPrefix + "://" + CGI["http_host"] + "/store/cart.cfm/mode/add";
		var finalLink  = "";
		var productId  = jQuery("#productId",formtag).val();
		var numOptions = parseInt(jQuery("#optioncount",formtag).val());
		var qty        = (jQuery("#qty",formtag).length > 0) ? jQuery("#qty",formtag).val() : 1;
		var selOptions = "";
		var selAddOns  = "";

		disableAddCartButton();

		for(var o = 1; o <= numOptions; o++) {
			var thisVal = jQuery("#option" + o,formtag).val();

			if(thisVal && thisVal != "") {
				if(selOptions != "") selOptions += ","
				selOptions += thisVal;
			}
		}

		jQuery("input[name='addons']::checked",formtag).each(function() {
			jQuery(this).trigger("addon.beforesubmit");
			if(selAddOns != "") selAddOns += ","
			selAddOns += jQuery(this).val();
		});

		finalLink = baseLink + "/id/" + productId;

		if(selOptions != "") finalLink += "/options/" + selOptions;
		if(selAddOns != "") finalLink += "/addons/" + selAddOns;
		if(qty > 1) finalLink += "/qty/" + qty;

		location.href = finalLink;
	});

	jQuery("img[piclink][piclarge]",formtag).click(function() {
		var imgSrc  = jQuery(this).attr("piclarge");
		var imgLink = jQuery(this).attr("piclink");

		jQuery("a.representative").attr("href",imgLink);
		jQuery("img.representative").attr("src",imgSrc);
	});

	if(jQuery("#cart_add",formtag).attr("disabled") == true) disableAddCartButton();
});
