$(function(){
	/*
	 * Book order form
	 */
	
	$('.book').each( function() {
		theBook = $(this);
		thePrice = $(this).find('input.price').val();
		$(this).find('input.quantity').change( function(){
			theQuantity = $(this).val();
			theSubtotal = theQuantity * thePrice;
			formattedSubtotal = addCommas(theSubtotal.toFixed(2));
			$(theBook).find('span.subtotal').html('$' + formattedSubtotal);
		});
	});
	
	/*
	 * Blur on focus
	 */	 
	$('a').focus(function() { this.blur(); });
	$('input[@type*=submit]').focus(function() { this.blur(); });
	$('input[@type=text], input[@type=password]').addClass("input");
	
	/*
	 * Pretty forms
	 */
	$('form.prettyform input[@type*=radio]').css('border','none');
	$('form.prettyform input[@type*=checkbox]').css('border','none');
	if($.browser.mozilla) { prettyform(); }
});

/*
 * Pretty forms
 */
function prettyform(){
  /*
   * Hide forms
   */
  $( 'form.prettyform' ).hide().end();
  /*
   * Label & list formatting
   */
  $( 'form.prettyform' ).find( 'label' ).not( '.noprettyform' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = '';
    this.appendChild( labelSpan );
  } ).end();
  
  /*
   * Show forms
   */
  $( 'form.prettyform' ).show().end();
}

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