// JavaScript Document

var ProductDetailCustomizer = Class.create(
{
	dom: {},
	
	basePrice: null,	
	
	initialize: function()
	{
		this.initDOM();
		
		if( this.dom.inputs.length == 0 && this.dom.productModel === null )
			return; // no customization available for this product
			
		this.initOther();
		this.initEvents();
	},
	
	initDOM: function()
	{
		this.dom.productBasePrice = $( "product-base-price" );
		this.dom.productPrice = $( "product-price-calculated" ); // display price including features.
		this.dom.form = $( "product-detail-form" );
		this.dom.productModel = $( "product-model" );
		this.dom.inputs = [];
		
		var inputs = this.dom.form.getElementsByTagName( "input" );
		
		for( var i = 0; i < inputs.length; i++ )
			this.dom.inputs.push( $( inputs[i] ) );
	},
	
	initOther: function()
	{
		this.basePrice = parseFloat( this.dom.productBasePrice.getValue() ) || 0.0;
	},
	
	initEvents: function()
	{
		this.dom.form.observe( "change", this.handleChangeForm.bind( this ) );
		
		if( typeof IEVersion == "number" )
		{
			if( this.dom.productModel !== null )
				this.dom.productModel.observe( "change", this.handleChangeForm.bind( this ) );
			
			for( var i = 0; i < this.dom.inputs.length; i++ )
				this.dom.inputs[i].observe( "click", this.handleChangeForm.bind( this ) );
		}
	},
	
	handleChangeForm: function( e )
	{
		//Event.stop( e );
		
		var price = this.calculatePrice();
		
		this.dom.productPrice.update( "$" + this.numberFormat( price ) );
	},
	
	calculatePrice: function()
	{
		var price = this.basePrice;
		
		//alert( typeof this.dom.productModel );
		if( this.dom.productModel !== null )
		{
			var selectedModel = this.dom.productModel.options[ this.dom.productModel.selectedIndex ];
			var title = selectedModel.getAttribute( "title" ) || selectedModel.title;
			var priceModifier = parseFloat( title.substr( 1 ) ) || 0.0;
			if( title.substr( 0, 1 ) == "-" )
				price -= priceModifier;
			else
				price += priceModifier;
		}
		
		for( var i = 0; i < this.dom.inputs.length; i++ )
		{
			var title = this.dom.inputs[i].getAttribute( "title" ) || this.dom.inputs[i].title;
			
			if( title == "" || title == 0 || title === null )
				continue;
			
			if( this.dom.inputs[i].checked === true || this.dom.inputs[i].getAttribute( "checked" ) == "checked" )
			{
				var priceModifier = parseFloat( title.substr( 1 ) ) || 0.0;
				price += priceModifier;
			}
		}
		
		return price;
	},
	
	numberFormat: function( number )
	{
		//alert( "The number you are trying to format as a price is:\n\n" + number + "\n\n and it is a " + typeof number );
		
		number = number.toFixed(2).toString();
		var left = number.split(".")[0];
		var mod = left.length%3;
		var remnant = mod > 0 ? left.substr( 0, mod ) : "";
		var formatThis = left.substr( mod );
		var formatted = "";
		
		var hits = "";
		
		for( var i = 0; i < formatThis.length; i++ )
		{
			var char = formatThis.substr( i, 1 );
			formatted += ( i%3 == 0 ) ? ( mod == 0 && i == 0 ? '' : ',' ) + char : char;
		}
			
		/*var breakdown = "BREAKDOWN\n\n-----------------------------------------\n\nNumber (toFixed, toString): " + number + "\n"
					  + "Left (left of decimal): " + left + " (" + typeof left + ")\n"
					  + "Mod:                    " + mod + " (" + typeof mod + ")\n"
					  + "Remnant:                " + remnant + " (" + typeof remnant + ")\n"
					  + "formatThis:             " + formatThis + " (" + typeof formatThis + ")\n"
					  + "Formatted:              " + formatted + " (" + typeof formatted + ")\n";
		
		alert( breakdown );*/
		
		var formattedNumber = ""+remnant+formatted+"."+number.substr( number.length-2,2);
		//alert( "We formatted your number to this:\n\n" + formattedNumber );
		return formattedNumber;
	}
});

document.observe( "dom:loaded", function(){ new ProductDetailCustomizer() } );