$(document).ready(function() {

	$('input:submit.make-pretty').each(function(){ $(this).addClass('new-button'); makePrettyButton( $(this) ); });
	$('input:button.make-pretty').each(function(){ $(this).addClass('new-button'); makePrettyButton( $(this) ); });
	$('a.make-pretty-button').each(function(){ $(this).addClass('anchor-button'); makePrettyAnchor( $(this) ); });

}); // end: document.ready


function makePrettyAnchor( obj ) {

	var text = obj.text();

	var width = obj.outerWidth();
	var newWidth = 30;
	var tempWidth = width;

	if( tempWidth > newWidth ) {
		newWidth = width - 1; // that 'do' will run at least once
		do {
			newWidth = newWidth + 1;
		} while( newWidth % 5 != 0 );
	}
	newWidth = newWidth + 15; // left-right padding for the new anchor-botton

	obj.addClass('bk-' + newWidth);

} // end: makePrettyButton

function makePrettyButton( obj ) {

	// get some info about the input
	var text = obj.attr('value');
	var type = obj.attr('type');
	var id = obj.attr('id');

	objClass = "button";

	var buttonId = "button-" + id;

	$('<button type="'+type+'" class="'+objClass+' new-button" id="'+ buttonId +'"><span>'+text+'</span></button>').insertBefore(obj);

	var width = $('#'+buttonId).outerWidth();
	var newWidth = 30;
	var tempWidth = width;

	if( tempWidth > newWidth ) {
		newWidth = width - 1; // that 'do' will run at least once
		do {
			newWidth = newWidth + 1;
		} while( newWidth % 5 != 0 );
	}

	$('#'+buttonId).addClass('bk-' + newWidth);

	if( type == "submit" ) {
		$('#'+buttonId).click(function(e){
			e.preventDefault();
			$(this).parents('form').submit();
		});
	}

	if( type == "button" ) {
		$('#'+buttonId).click(function(e){
			$('#'+buttonId).attr('onclick',$(obj).attr('onclick'));
			$(obj).copyEventsTo('#'+buttonId);
		});
	}

	// we don't need the orig object any more
	$(obj).remove();

} // end: makePrettyButton