// JavaScript Document
function CurrencyFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount;
	var i = parseInt(a);
	if(isNaN(i)) { return 0; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	amount = a.join(delimiter);
	amount = minus + amount;
	return amount;
}

var stepIndex   = 1;
var progressWidth = 0;
var progressBar = [0];

function slide($index) {
	if ($index == 'next') {
		stepIndex++;
		$('#steps').animate({'left': $('#steps').position().left - 625});
		$('#progress').animate({'backgroundPosition':'(' + (-627 + progressBar[stepIndex]) + 'px 0)'});
	} else if ($index == 'prev') {
		stepIndex--;
		$('#steps').animate({'left': $('#steps').position().left + 625});
		$('#progress').animate({'backgroundPosition':'(' + (-627 + progressBar[stepIndex]) + 'px 0)'});
	} else {
		stepIndex = $index;
		$('#steps').animate({'left': (-625 * $index) + 625});
		$('#progress').animate({'backgroundPosition':'(' + (-627 + progressBar[stepIndex]) + 'px 0)'});
	}
	
}

$basePrices           = {painted:'6200', hardiplank:'7600', hardimetal:'7400', woodmetal:'8900'};
$windowPrices         = {glass:'650',extra:'600'};
$shippingPrices       = {pacific:'900',mountain:'400',central:'1200',eastern:'1400'};
$foundationPrice      = 500;
$installationDiscount = -400;
$costBase             = 7400;
$costDoor             = 0;
$costWindows          = 0;
$costRearWindows      = 0;
$costExtraWindow      = 0;
$costFoundation       = 0;
$costInstallation     = 0;
$total                = 0;

function getTotal() {
	$total = parseInt($costBase) + parseInt($costDoor) + parseInt($costWindows) + parseInt($costRearWindows) + parseInt($costExtraWindow) + parseInt($costFoundation) + parseInt($costInstallation);
	$('#cost_total').html('$' + CurrencyFormatted($total));
	$('#formConfigure_costBase').val($costBase);
	$('#formConfigure_costDoor').val($costDoor);
	$('#formConfigure_costWindows').val($costWindows);
	$('#formConfigure_costRearWindows').val($costRearWindows);
	$('#formConfigure_costExtraWindow').val($costExtraWindow);
	$('#formConfigure_costFoundation').val($costFoundation);
	$('#formConfigure_costInstallation').val($costInstallation);
	$('#formConfigure_total').val($total);
	
	$('#cost_windows').html('$' + CurrencyFormatted($costWindows));
	$('#cost_extraWindow').html('$'+ CurrencyFormatted($costExtraWindow));
	$('#cost_rearWindows').html('$'+ CurrencyFormatted($costRearWindows));
	$('#cost_foundation').html('$'+ CurrencyFormatted($costFoundation));
	$('#cost_installation').html('$'+ CurrencyFormatted($costInstallation));
}

function setDimensions() {
	$length = $('#formConfigure_length').val();
	$width  = $('#formConfigure_width').val();
	if ($length != '' && $width != '') {
		$('#shedPreview').attr('src','/_images/configure/previews/' + $width + 'x' + $length + '_M_L.jpg');
	} else {
		$('#shedPreview').attr('src','/_images/configure/previews/no-preview-available.jpg');
	}
	$.post('/configure/dimensions.php',{ length:$length, width:$width },function(data) {
			$('#finishDetail td').show();
			if (data.painted == 0) {
				$('#optionFinishPainted').hide();
			} else {
				$('#costPainted').html(CurrencyFormatted(data.painted));
			}
			if (data.hardiplank == 0) {
				$('#optionFinishHardi').hide();
			} else {
				$('#costHardi').html(CurrencyFormatted(data.hardiplank));
			}
			if (data.hardimetal == 0) {
				$('#optionFinishHardiMetal').hide();
			} else {
				$('#costHardiMetal').html(CurrencyFormatted(data.hardimetal));
			}
			if (data.woodmetal == 0){
				$('#optionFinishWoodMetal').hide();
			} else {
				$('#costWoodMetal').html(CurrencyFormatted(data.woodmetal));
			}
			$basePrices = data;
			$('#formConfigure_finish3').attr('checked','true');
			$('#formConfigure_finish3').click();
		},'json');
	$.post('/configure/windows.php',{ length:$length, width:$width },function(data) {
			$('#costGlass').html(CurrencyFormatted(data.glass));
			$('#costRearWindows').html(CurrencyFormatted(data.extra));
			$windowPrices = data;
		},'json');
	$.post('/configure/foundation.php',{ length:$length, width:$width },function(data) {
			$foundationPrice = data;
		});
	$.post('/configure/installation.php',{ length:$length, width:$width },function(data) {
			$installationDiscount = data * -1;
		});
	$('#option_length').html($length + 'ft.');
	$('#option_width').html($width+ 'ft.');
	getTotal();
}

function onReady() {
	$stepCount = 8;
	$('#steps').width(625 * $stepCount);
	$('#progress td').each(function() {
		progressWidth += $(this).innerWidth() + 1;
		progressBar.push(progressWidth);
	});
	$('#progress').css({'backgroundPosition': (-627 + progressBar[1]) + 'px 0'});
	$placement = 'L';
	$finish    = 'M';
	$('.tipped').hover(function(event) {
			$('#tip').show();
		},function(event) {
			$('#tip').hide();
		});
	$('.tipped').mousemove(function(event) {
			$('#tip .bubble').html($(this).attr('alt'));
			$tipHeight = $('#tip').outerHeight();
			$('#tip').css({
					'top'  : (event.pageY - $tipHeight - 60) + 'px',
					'left' : (event.pageX) + 'px'
				});
			if ($.browser.msie) {
				$('#tip').css({
						'top'  : (event.pageY - $tipHeight - 5) + 'px',
						'left' : (event.pageX) + 'px'
					});
			}
		});
	$('#formConfigure_finish1, #formConfigure_finish2, #formConfigure_finish3, #formConfigure_finish4').click(function() {
			$('#option_finish').html($(this).val());
			switch ($(this).val()) {
				case 'Painted OSB':
					$('#cost_base').html('$' + CurrencyFormatted($basePrices.painted));
					$costBase = parseInt($basePrices.painted);
					$finish = 'P';
					$('#shedPreview').attr('src','/_images/configure/previews/' + $('#formConfigure_width').val() + 'x' + $('#formConfigure_length').val() + '_' + $finish + '_' + $placement + '.jpg');
					break;
				case 'HardiPlank':
					$('#cost_base').html('$' + CurrencyFormatted($basePrices.hardiplank));
					$costBase = parseInt($basePrices.hardiplank);
					$finish = 'H';
					$('#shedPreview').attr('src','/_images/configure/previews/' + $('#formConfigure_width').val() + 'x' + $('#formConfigure_length').val() + '_' + $finish + '_' + $placement + '.jpg');
					break;
				case 'HardiPlank and Metal':
					$('#cost_base').html('$' + CurrencyFormatted($basePrices.hardimetal));
					$costBase = parseInt($basePrices.hardimetal);
					$finish = 'M';
					$('#shedPreview').attr('src','/_images/configure/previews/' + $('#formConfigure_width').val() + 'x' + $('#formConfigure_length').val() + '_' + $finish + '_' + $placement + '.jpg');
					break;
				case 'Wood and Metal':
					$('#cost_base').html('$' + CurrencyFormatted($basePrices.woodmetal));
					$costBase = parseInt($basePrices.woodmetal);
					$finish = 'W';
					$('#shedPreview').attr('src','/_images/configure/previews/' + $('#formConfigure_width').val() + 'x' + $('#formConfigure_length').val() + '_' + $finish + '_' + $placement + '.jpg');
					break;
			}
			getTotal();
		});
	$('#formConfigure_width').change(function() {
		$.post('/configure/sizes.php',{'width':$(this).val()},function(data) {
			$lengths = data;
			$('#formConfigure_length option').hide();
			for($x = 0; $x < $lengths.length; $x++) {
				$('#formConfigure_length option[value="' + $lengths[$x] + '"]').show();
			}
			if ($.inArray(parseInt($('#formConfigure_length').val()),$lengths) < 0) {
				$('#formConfigure_length').val($lengths[0]);
			}
			setDimensions();
		},'json');
	});
	$('#formConfigure_length, #formConfigure_width').change(setDimensions);
	$('input[name="formConfigure_door"]').click(function() {
			$('#option_door').html($(this).val());
			switch ($(this).val()) {
				case 'Painted Metal':
					$('#cost_door').html('$' + 0);
					$costDoor = 0;
					break;
				case 'Birch Wood':
					$('#cost_door').html('$' + 400);
					$costDoor = 400;
					break;
				case 'Glass':
					$('#cost_door').html('$' + 600);
					$costDoor = 600;
					break;
			}
			getTotal();
		});
	
	$('input[name="formConfigure_doorPlacement"]').click(function() {
			if ($(this).val() == 'Left') {
				$('#option_placement').html('L');
				$placement = 'L';
			} else {
				$('#option_placement').html('R');
				$placement = 'R';
			}
			$('#shedPreview').attr('src','/_images/configure/previews/' + $('#formConfigure_width').val() + 'x' + $('#formConfigure_length').val() + '_' + $finish + '_' + $placement + '.jpg');
		});
	
	$('input[name="formConfigure_windows"]').click(function() {
			switch ($(this).val()) {
				case 'Plexiglass':
					$('#option_windows').html('Plexiglass');
					$costWindows = 0;
					if ($('#formConfigure_rearWindows').attr('checked') == true) {
						$costRearWindows = $windowPrices.extra;
					}
					$('#costRearWindows').html(CurrencyFormatted($windowPrices.extra));
					break;
				case 'Double-Pane Glass':
					$('#option_windows').html('DP Glass');
					$costWindows = $windowPrices.glass;
					if ($('#formConfigure_rearWindows').attr('checked') == true) {
						$costRearWindows = $windowPrices.extra * 2;
					}
					$('#costRearWindows').html(CurrencyFormatted($windowPrices.extra * 2));
					break;
			}
			getTotal();
		});
	
	$('#formConfigure_extraWindow').click(function() {
			if ($(this).attr('checked') == true) {
				$costExtraWindow = 400;
				$('#option_extraWindow').html('Yes');
			} else {
				$costExtraWindow = 0;
				$('#option_extraWindow').html('None');
			}
			getTotal();
		});
	
	$('#formConfigure_rearWindows').click(function() {
			if ($(this).attr('checked') == true) {
				$costRearWindows = $windowPrices.extra;
				if ($('#formConfigure_windows2').attr('checked') == true) {
					$costRearWindows = $windowPrices.extra * 2;
				}
				$('#option_rearWindows').html('Yes');
			} else {
				$costRearWindows = 0;
				$('#option_rearWindows').html('None');
			}
			getTotal();
		});
	$('#formConfigure_foundation1, #formConfigure_foundation2').click(function() {
			if ($(this).val() == 'Added') {
				$costFoundation = $foundationPrice;
				$('#option_foundation').html('Added');
			} else {
				$costFoundation = 0;
				$('#option_foundation').html('Existing');
			}
			getTotal();
		});
	$('#formConfigure_installation1, #formConfigure_installation2').click(function() {
			if ($(this).val() == 'Included') {
				$costInstallation = 0;
				$('#option_installation').html('Included');
			} else {
				$costInstallation = $installationDiscount;
				$('#option_installation').html('DIY');
			}
			getTotal();
		});
}