// 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];
var doublepane = 350;

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)'});
	}
	
}

// Make sure to add a defualt value for the variable here if you add a new option
$basePrices           = {painted:'6700', hardiplank:'7600', hardimetal:'7400', woodmetal:'8900'};
$raftersPrice         = {rafters:'500'};
$windowPrices         = {glass:'350'}; 
$shippingPrices       = {pacific:'900',mountain:'400',central:'1200',eastern:'1400'};
$foundationPrice      = {foundation:'500'};
$installationPrice    = {DIY:'1025'};
$costBase             = 7400;
$costRafters          = 0;
$costExtraPaint       = 0;
$costDoor             = 0;
$costSwing            = 0;
$costWindows          = 0;
$costRearWindows      = 0;
$costExtraWindow      = 0;
$costFoundation       = 0;
$costDeck             = 0;
$costInstallation     = 0;
$total                = 0;

function getTotal() {
	$total = parseInt($costBase) + parseInt($costRafters) + parseInt($costExtraPaint) + parseInt($costDoor) + parseInt($costSwing) + parseInt($costWindows) + parseInt($costRearWindows) + parseInt($costExtraWindow) + parseInt($costFoundation) + parseInt($costDeck) + parseInt($costInstallation);
	$('#cost_total').html('$' + CurrencyFormatted($total));
	$('#formConfigure_costBase').val($costBase);
	$('#formConfigure_costRafters').val($costRafters);
	$('#formConfigure_costExtraPaint').val($costExtraPaint);
	$('#formConfigure_costDoor').val($costDoor);
	$('#formConfigure_costSwing').val($costSwing);
	$('#formConfigure_costWindows').val($costWindows);
	$('#formConfigure_costRearWindows').val($costRearWindows);
	$('#formConfigure_costExtraWindow').val($costExtraWindow);
	$('#formConfigure_costFoundation').val($costFoundation);
	$('#formConfigure_costDeck').val($costDeck);
	$('#formConfigure_costInstallation').val($costInstallation);
	$('#formConfigure_total').val($total);
	
	$('#cost_rafters').html('$'+ CurrencyFormatted($costRafters));
	$('#cost_extraPaint').html('$'+ CurrencyFormatted($costExtraPaint));
	$('#cost_windows').html('$' + CurrencyFormatted($costWindows));
	$('#cost_extraWindow').html('$'+ CurrencyFormatted($costExtraWindow));
	$('#cost_rearWindows').html('$'+ CurrencyFormatted($costRearWindows));
	$('#cost_foundation').html('$'+ CurrencyFormatted($costFoundation));
	$('#cost_deck').html('$'+ CurrencyFormatted($costDeck));
	$('#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/rafters.php',{ length:$length, width:$width },function(data) {																  			$('raftersDetail').show();
			if (data.rafters == 0)  {
				$('#optionRafters').hide();
			} else {
				$('#costRafters').html(CurrencyFormatted(data.rafters));
			}
			$raftersPrice = data;
			$('#formConfigure_finish3').attr('checked','true');
			$('#formConfigure_finish3').click();
		},'json');
	$.post('/configure/windows.php',{ length:$length, width:$width },function(data) {
  			$('windowsDetail').show();
			if (data.glass == 0)  {
				$('#optionWindows').hide();
			} else {
				$('#costWindows').html(CurrencyFormatted(data.glass));
			}
			$windowPrices = data;
			$('#formConfigure_finish3').attr('checked','true');
			$('#formConfigure_finish3').click();
		},'json');
	$.post('/configure/foundation.php',{ length:$length, width:$width },function(data) {
  			$('foundationDetail').show();
			if (data.foundation == 0)  {
				$('#optionFoundtion').hide();
			} else {
				$('#costFoundation').html(CurrencyFormatted(data.foundation));
			}
			$foundationPrice = data;
			$('#formConfigure_finish3').attr('checked','true');
			$('#formConfigure_finish3').click();
		},'json');
	$.post('/configure/installation.php',{ length:$length, width:$width },function(data) {
  			$('installationDetail').show();
			if (data.DIY == 0)  {
				$('#optionInstallation').hide();
			} else {
				$('#costInstallation').html(CurrencyFormatted(data.DIY));
			}
			$installationPrice = data;
			$('#formConfigure_finish3').attr('checked','true');
			$('#formConfigure_finish3').click();
		},'json');
	$('#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 Basic':
					$('#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 'Collins':
					$('#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 'Collins 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 'Collins-Lat and Metal':
					$('#cost_base').html('$' + CurrencyFormatted($basePrices.woodmetal));
					$costBase = parseInt($basePrices.woodmetal);
					$finish = 'L';
					$('#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_rafters').click(function() {
			if ($(this).attr('checked') == true) {
				$costRafters = parseInt($raftersPrice.rafters);
				$('#option_rafters').html('Yes');
			} else {
				$costRafters = 0;
				$('#option_rafters').html('None');
			}
			getTotal();
		});
	
	$('#formConfigure_extraPaint2').click(function() {
			if ($(this).attr('checked') == true) {
				$costExtraPaint = 75;
				$('#option_extraPaint').html('Yes');
			} else {
				$costExtraPaint = 0;
				$('#option_extraPaint').html('None');
			}
			getTotal();
		});
	
	$('#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('$' + 575);
					$costDoor = 575;
					break;
				case 'Utility Steel':
					$('#cost_door').html('$' + 950);
					$costDoor = 950;
					break;
				case 'Full Lite Glass':
					$('#cost_door').html('$' + 1200);
					$costDoor = 1200;
					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_doorSwing"]').click(function() {
            $('#option_doorSwing').html($(this).val());
			switch ($(this).val()) {
				case 'Inward':
					$('#cost_swing').html('$' + 0);
					$costSwing = 0;
					break;
				case 'Outward':
					$('#cost_swing').html('$' + 75);
					$costSwing = 75;
					break;
			}
			getTotal();
		});
	$('input[name="formConfigure_windows"]').click(function() {
			switch ($(this).val()) {
				case 'Plexiglass':
					$('#option_windows').html('Plexiglass');
					$costWindows = 0;
					break;
				case 'Double-Pane Glass':
					$('#option_windows').html('DP Glass');
					$costWindows = parseInt($windowPrices.glass);
					if ($('#formConfigure_rearWindows').attr('checked') == true) {
					    $costWindows = parseInt($windowPrices.glass) + doublepane;
					}

					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 = 650;
				if ($('#formConfigure_windows2').attr('checked') == true) {
				$costWindows = parseInt($windowPrices.glass) + doublepane;
			   }
			   $('#costWindows').html(CurrencyFormatted(parseInt($windowPrices.glass) + doublepane));
			   $('#option_rearWindows').html('Yes');
			} else {
				$costRearWindows = 0;
				if ($('#formConfigure_windows2').attr('checked') == true) {
				$costWindows = $windowPrices.glass;
			   }
			   $('#costWindows').html(CurrencyFormatted($windowPrices.glass));
				$('#option_rearWindows').html('None');
			}
			getTotal();
		});
	$('#formConfigure_foundation1, #formConfigure_foundation2').click(function() {
			if ($(this).val() == 'Added') {
				$costFoundation = parseInt($foundationPrice.foundation);
				$('.optionaldeck').show();
				$('#option_foundation').html('Added');
			} else {
				$costFoundation = 0;
				$('.optionaldeck').hide();
				$('#option_foundation').html('Existing');
			}
			getTotal();
		});
	$('#formConfigure_deck1').click(function() {
			if ($(this).attr('checked') == true) {
				$costDeck = 950;
				$('#option_deck').html('Yes');
			} else {
				$costDeck = 0;
				$('#option_deck').html('None');
			}
			getTotal();
		});
	$('#formConfigure_deck2').click(function() {
			if ($(this).attr('checked') == true) {
				$costDeck = 1300;
				$('#option_deck').html('Yes');
			} else {
				$costDeck = 0;
				$('#option_deck').html('None');
			}
			getTotal();
		});
	
	$('#formConfigure_installation1, #formConfigure_installation2').click(function() {
			if ($(this).val() == 'Pro Install') {
				$costInstallation = parseInt($installationPrice.DIY);
				$('#option_installation').html('Pro Install');
			} else {
				$costInstallation = 0;
				$('#option_installation').html('None');
			}
			getTotal();
		});
}
