function CurrencyFormatted(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num);
}

$(document).ready(function(){
  //make the lists tiger pretty
  $('.builderList li:nth-child(odd)').addClass('alternate');
  $('.optionList li:nth-child(odd)').addClass('alternate2');
  //3 steps
  $('fieldset').hide();
  $('fieldset:eq(0)').slideToggle();
  $('.buildStep:eq(0)').toggleClass('idle');
  $('.buildStep').click(
   function() {
       $(this).next().slideToggle('normal').siblings('fieldset').slideUp('normal');
       $(this).toggleClass('idle').siblings().removeClass('idle');	
   });
  //collapse all but the first of the engine options lists
  $('fieldset:eq(0) .builderList').hide();
  $('fieldset:eq(0) h4').toggleClass('idle');
  $('.builderList:eq(0)').slideToggle();
  $('h4:eq(0)').toggleClass('idle');
  $('h4').click(
   function() {
       $(this).next().slideToggle('normal');
       $(this).toggleClass('idle');	
   });
  //because I'm too lazy to copy and paste the same values
  $('.builderList li input').each( function(){
	var myPrice = $(this).val();
	$(this).next('.price').text("$"+CurrencyFormatted(myPrice));
	});
  
  
  //form handling biz
  
  // Get Defaul Values
  var total_value = 0;
  var engine_value = 0;
  var packages_value = 0;

  engine_value = parseFloat($('#engine input:radio:checked').val());
  $("p.engineOpt").text( $('#engine input:radio:checked').attr('id') );
  $("p.colorOpt").text( $('#color input:radio:checked').attr('id') );

  $('#options input:checkbox').each( function() {
  	if (this.checked)
  	{
		if( $(this).attr('name') == "options" ){
    		total_value = total_value + parseFloat($(this).val());
			var id = "li_" + $(this).attr('id')
			$('ul.optionList').append('<li id="'+id+'">' + $(this).attr('id') + '</li>');
		}
    }
  });
  
  $('#options input:checkbox').each( function() {
  	if (this.checked)
  	{
		if( $(this).attr('name') == "packages" ){
			packages_value = packages_value + parseFloat($(this).val());
			var id = "li_" + $(this).attr('id')
			$('ul.optionList').append('<li id="'+id+'">' + $(this).attr('id') + '</li>');
			
			var nodes = $(this).attr('alt').split("::");
			for(i=0;i<nodes.length;i++) 
			{
				var id = nodes[i];
				if($('[id='+id+']').is(':checked')){
					total_value = total_value - parseFloat($('[id='+id+']').val());
					var id2 = "li_" + $('[id='+id+']').attr('id');
					$('[id='+id2+']').remove();
				} else {
					$('[id='+id+']').attr('checked', true);
				}
				$('[id='+id+']').attr("disabled", true)
			}
		}
    }
  });

  
  $("span.msrpPrice").text("$" + (  CurrencyFormatted(total_value+engine_value+packages_value) ) + "*" );
  
  
  
  $("input").click(function () {

	// engines
	if( $(this).attr('name') == "engine" ){
		engine_value = parseFloat($(this).val());
		$("p.engineOpt").text( $(this).attr('id') );
	}
	
	// colors
	if( $(this).attr('name') == "Color" ){
		$("p.colorOpt").text( $(this).attr('id') );
	}
	
	// options
	if( $(this).attr('name') == "options" ){	
		if( $(this).is(':checked') ){
			// add to the price	
			total_value = total_value + parseFloat($(this).val());
			var id = "li_" + $(this).attr('id')
			$('ul.optionList').append('<li id="'+id+'">' + $(this).attr('id') + '</li>');
		} else {
			// remove from the price
			total_value = total_value - parseFloat($(this).val());
			var id = "li_" + $(this).attr('id');
			$('[id='+id+']').remove();
		}
	}
	
	// options
	//PACKAGES - to get pagages working the name must change to packages, and items that are included should be contained in an alt, with "::" separating multiple items
	if( $(this).attr('name') == "packages" ){	
	
		if( $(this).is(':checked') ){
			// add to the price	
			packages_value = packages_value + parseFloat($(this).val());
			var id = "li_" + $(this).attr('id')
			$('ul.optionList').append('<li id="'+id+'">' + $(this).attr('id') + '</li>');
			
			var nodes = $(this).attr('alt').split("::");
			for(i=0;i<nodes.length;i++) 
			{
				var id = nodes[i];
				if($('[id='+id+']').is(':checked')){
					total_value = total_value - parseFloat($('[id='+id+']').val());
					var id2 = "li_" + $('[id='+id+']').attr('id');
					$('[id='+id2+']').remove();
				} else {
					$('[id='+id+']').attr('checked', true);
				}
				$('[id='+id+']').attr("disabled", true)
			}
		} else {
			// remove from the price
			packages_value = packages_value - parseFloat($(this).val());
			var id = "li_" + $(this).attr('id');
			$('[id='+id+']').remove();
			
			var nodes = $(this).attr('alt').split("::");
			for(i=0;i<nodes.length;i++) 
			{
				var id = nodes[i];
				$('[id='+id+']').attr('checked', false);
				$('[id='+id+']').attr("disabled", false)
			}
		}
	}
	
	// add totals together
	$('.optionList li:nth-child(odd)').addClass('alternate2');
  	$("span.msrpPrice").text("$" + (  CurrencyFormatted(total_value+engine_value+packages_value) ) + "*" );
	
	
  });
  
});