;( function( $ ) {
	$.fn.customButton = function( ) // TODO optimize for hidden elements, make them transform on show
	{
		return this.each( function() 
		{
			var $this = $(this);
			if( $this.data("transformed") ) // if it was transformed already exit
				return false;
			
			var sty = $this.attr('style');
			$this.hide(); // hide original button
			
			var cls = $this.attr('class'); // get the class of the button
			
			var href = $this.attr("href"); // get the href
			
			if( href == undefined ) // if it's undefined put javascript:void(0) 
				href = "javascript:void(0)"; 
			
			var clk = this.onclick; // get the click for attribute
			if( clk !== undefined && clk !== null ) // then bind event from jquery
			{
				$this.bind( "click" , clk );
				$this.onclick = undefined;
			}
			
			var areplace = $(document.createElement("A"));  // replacement A tag
			$this.data( "a", areplace );
			
			if( $this.hasClass("confirm") ) // custom confirm button, need to open $.dialog
			{
				$this.unbind( 'click' );
				var diaBtn = $this.attr('btn');			// button text
				var diaTitle = $this.attr('title');		// title of window
				var diaContent = $this.attr('content');	// content of window
				if( !$this.data('cDialog') ) // if it's not transformed yet let's do it!
				{
					var cDialog = $( document.createElement("DIV") );
					$this.data('dialog')
					cDialog.attr( 'title', diaTitle );
					cDialog.html( "<div>"+diaContent+"</div>" );
					cDialog.hide();
					$('body').append(cDialog);
					cDialog
					.data( 'actionBtn', diaBtn )
					// .data( 'href', href )
					.data( 'parent', $this )
					.dialog(
					{
						bgiframe	: true,
						resizable	: false,
						modal		: true,
						dialogClass	: 'confirm',
						overlay	:
						{
							backgroundColor: '#000',
							opacity: 0.5
						},
						buttons	:
						{
							actionBtn : function( event )
							{
								$(this).dialog('close');
								var theBtn = $(this).data("parent");
								theBtn.unbind( 'click', theBtn.data( 'dialogFnc' ) );
								event.target = theBtn.data( 'a' );
								theBtn.data( 'a' ).trigger( event );
								if( href !== undefined ) //$(this).data('href')
									document.location = href; // $(this).data('href')
								return true;
							},
							Cancel	: function()
							{
								$(this).dialog('close');
							}
						},
						open : function( event )
						{
							if( this.beenOpen ) return false;
							var buttons = $(this).siblings("div.ui-dialog-buttonpane").find("button");
							buttons.removeClass();
							buttons.filter("*:contains('actionBtn')").eq(0).text( $(this).data('actionBtn') ).addClass('icon-ok')
							buttons.filter("*:contains('Cancel')").eq(0).addClass('icon-cancel')
							buttons.addClass("fr small").each( LiveRail.global.transformButton );
							event.preventDefault();
							event.stopPropagation();
							this.beenOpen = true;
						}
					}).dialog('close')
	
					$(this).data('cDialog',cDialog);
				}
				var diaFnc = function()
				{
					$(this).data('cDialog').dialog('open');
					return false;
				};
				$this.data('dialogFnc', diaFnc ).bind('click', diaFnc );
			}
	
	
			/*var acontainer = $(document.createElement("DIV"));
			acontainer.css(
			{
				"position"		: "relative",
				"margin-left"	: "-5px",
				"margin-right"	: "-5px"
			});*/ // custom container for button
			
			areplace.data( "type", $(this).attr("type") );
			areplace.attr( "href", typeof href == 'undefined' ? 'javascript:void(0)' : href );
			areplace.data( "button", $this );
			areplace.click( function( event )
			{
				var b = $(this).data( "button" );
				event.target = b;
				event.preventDefault();
				event.stopImmediatePropagation();
				event.stopPropagation();
				b.trigger(event);				
				ret = event.result!=undefined ? event.result : true ;
				// if we have a form to submit
				if( ret && $(this).data( 'type' ) == 'submit' && ( parentForm = b.parents('form').eq(0) ).length > 0 )
				{
					var event = $.Event("submit");
					parentForm.trigger( event );
					return event.result
				}
				if( ret )
					document.location = href;
				return ret;
			});
	
			areplace.html( "<span>"+$this.html()+"</span>" );
			areplace.addClass("button");
			areplace.addClass(cls);
			if( sty != undefined )
				areplace.attr( "style", sty );
			//acontainer.append(areplace)
	
			$(areplace).insertAfter( this );
			$this.data("transformed",true)
		})
	}
})( $ )
