$(document).ready(function() {	
	// hide the javascript message and show the configs
	$('.config-category').css('display', '');
	$('#config-price-special').css('display', '');	// show the special when javascript enabled 
	
	// add events to each radio button
	$("#config-form input[type=radio]").click(function() { update_config_selections(); });
	$("#config-form input[type=checkbox]").click(function() { update_config_selections(); });
	
	// update the numbers right off
	update_config_selections();
});

/**
 * Get a product URL that accounts for currently
 * selected configuration options, if any.
 */
function get_product_path()
{
	return document.location.pathname + '?' + get_product_params();
}

function get_product_url()
{
	return document.location.protocol + '//' + document.location.host + (document.location.port ? ':' + document.location.port : '') + get_product_path();
}

function get_product_params()
{
	options = 'p=' + $('input[name=PRODUCT_ID]').val();
	config = jQuery.map($('input[name^=OPT]:checked'), function(option, index) {
		return $(option).val();
	});
	return config.length > 0 ? options + '&o=' + config.join(',') : options;
}

function update_config_selections()
{
	// start out with the base price
	var config_price = Number( $('#starting-price').text() );
	var special_pricing_query =  $('#starting-price-special');
	var config_price_special = Number( special_pricing_query.text() );
	var max_eta_low = 0;
	var max_eta_high = 0;
	
	$(".config-category").each(function(i) {
		
		// find any radio buttons
		var selected_option_id = $("#"+ this.id + " input:checked[type=radio]").attr('id');
		if ( selected_option_id )
		{
			// RADIO BUTTON CATEGORY
			var select_text = $("label[for=" + selected_option_id +"]").text();
			if ( select_text.toLowerCase() != "none" )
				setTextWithFadeEffect( $("#"+ this.id + "_summary"), select_text);
			else
				$("#"+ this.id + "_summary").text('');
						
			var selected_option_price = Number($("#" + selected_option_id + "_price").text())
			var selected_option_price_special = Number($("#" + selected_option_id + "_price_special").text())
			
			// update the price difference on the options
			$("#"+ this.id + " input[type=radio]").each(function(i) {
				var price_diff = Number($("#"+ this.id + "_price").text()) - selected_option_price;
				if ( special_pricing_query.text() )
					price_diff = Number($("#"+ this.id + "_price_special").text()) - selected_option_price_special;
				
				if ( price_diff > 0 )
					$("#"+ this.id + "_price_diff").html( ' ($' + addCommas(Math.abs(price_diff).toFixed(2)) + ' more)' );
				else if ( price_diff < 0 )
					$("#"+ this.id + "_price_diff").html( ' ($' + addCommas(Math.abs(price_diff).toFixed(2)) + ' less)' );
				else
					$("#"+ this.id + "_price_diff").html( '' );
			});
	
			// add the price
			config_price = config_price + selected_option_price;
			config_price_special = config_price_special + selected_option_price_special;
			
			// check if eta is effected
			if ( $("#" + selected_option_id + "_eta_low").text() &&  $("#" + selected_option_id + "_eta_high").text() )
			{
				max_eta_low = Math.max(max_eta_low, Number($("#" + selected_option_id + "_eta_low").text()));
				max_eta_high = Math.max(max_eta_high, Number($("#" + selected_option_id + "_eta_high").text()));
			}
		}
		else
		{
			// CHECKBOX CATEGORY				
			var summary_text = '';
			$("#"+ this.id + " input:checked[type=checkbox]").each(function() {
				//alert('CHECKED ' + this.id );
			
				// add the summary text
				if ( summary_text != '' )
					summary_text += '<br />';
				summary_text += $("label[for=" + this.id +"]").text();
				
				// add to the config price
				var selected_option_price = Number($("#" + this.id + "_price").text())
				var selected_option_price_special = Number($("#" + this.id + "_price_special").text())
				
				config_price = config_price + selected_option_price;
				config_price_special = config_price_special + selected_option_price_special;
				
				// check if eta is effected
				if ( $("#" + this.id + "_eta_low").text() && $("#" +  this.id + "_eta_high").text() )
				{
					max_eta_low = Math.max(max_eta_low, Number($("#" +  this.id + "_eta_low").text()));
					max_eta_high = Math.max(max_eta_high, Number($("#" +  this.id + "_eta_high").text()));
				}
			});
			
			// update the summary display
			setHtmlWithFadeEffect( $("#"+ this.id + "_summary"), summary_text );
			
		}
	});
		
	// update the price
	var config_price_desc = '$' + addCommas(config_price.toFixed(2));
	var config_price_special_desc = '$' + addCommas(config_price_special.toFixed(2));
	var fadeBack = false;
	if ( $('#config-price').text() != config_price_desc || $('#config-price-special').text() != config_price_special_desc)
	{
		$('#config-price').fadeTo(0, 0.3);
		$('#config-price-special').fadeTo(0, 0.3);
		fadeBack = true;
	}
	
	$('#config-price').text(config_price_desc);
	$('#config-price-special').text(config_price_special_desc);
	
	if ( special_pricing_query.text() )
	{
		if ( config_price_special < config_price )
		{
			$('#config-price').addClass("non-special-price");
			$('#config-price-special').addClass("special-price");
			$('#config-price').css("display", "");
		}
		else
		{
			$('#config-price').css("display", "none");
			$('#config-price').removeClass("non-special-price");
			$('#config-price-special').removeClass("special-price");
		}
	}
	
	if ( fadeBack )
	{
		$('#config-price').fadeTo("slow", 1);
		$('#config-price-special').fadeTo("slow", 1);
	}
	
	// default value if none found
	if ( max_eta_low == 0 || max_eta_high == 0 )
	{
		max_eta_low = Number($('#default_eta_low').text());
		max_eta_high = Number($('#default_eta_high').text());
	}

	// update the ETA information	
	var eta_desc = '';
	if ( max_eta_high == 0 )
			eta_desc = 'Call for availability';	
	else if ( max_eta_low == max_eta_high )
	{
		if (max_eta_low % 7 == 0)
			eta_desc = (max_eta_low / 7) + " " + pluralize(max_eta_low/7, "week");	// x weeks
		else
			eta_desc = max_eta_low + " " + pluralize(max_eta_low, "day");	// x days
	}
	else if ( max_eta_low % 7 == 0 && max_eta_high % 7 == 0 )
		eta_desc = (max_eta_low / 7) + " - " + (max_eta_high / 7) + " " + pluralize(max_eta_high/7, "week");	// x - x weeks
	else if ( max_eta_high > 7 && max_eta_high % 7 == 0 )
		eta_desc = max_eta_low + " " + pluralize(max_eta_low, "day") + " - " + (max_eta_high / 7) + " " + pluralize(max_eta_high/7, "week");	// x days - x weeks
	else
		eta_desc = max_eta_low + " - " + max_eta_high + " " + pluralize(max_eta_high, "day");	// x - x days
	
	setTextWithFadeEffect( $('#config-avail'), eta_desc);
	
	// update the return url
	$('#return-url').val(get_product_path());
	
	// Update the AddThis button
	if (addthis)
	{
		addthis.button('#addthis-button', {}, {url: get_product_url()});
	}
	
	// Update mail to friend
	$('#email-friend-button').attr('href', '/share?' + get_product_params());
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function pluralize(count, text_singular)
{
	if (Math.abs(count) == 1)
		return text_singular;
	else if ( text_singular.substring( text_singular.length - 1 ).toLowerCase() == 's' )
		return text_singular + 'es';
	else
		return text_singular + 's';
}

// Set the text on the item with a fade if it has changed
function setTextWithFadeEffect( query_obj, text)
{
	if ( query_obj.text() != text)
	{
			query_obj.fadeTo(0, 0.3);
			query_obj.text( text );
			query_obj.fadeTo("slow", 1);
	}
	else
		query_obj.text( text );
}

// Set the text on the item with a fade if it has changed
function setHtmlWithFadeEffect( query_obj, html)
{
	if ( query_obj.html() != html)
	{
			query_obj.fadeTo(0, 0.3);
			query_obj.html( html );
			query_obj.fadeTo("slow", 1);
	}
	else
		query_obj.html( html );
}
