

function Label( id, parent, opt, debugLog )
{

	this.construct = function( id, parent, opt, debugLog )
	{
		this._id = id;		
		this._debugLog = debugLog;
		this._parent = parent;

		var loc = window.location + "";
		var ch = '';
/*
		if( loc.indexOf( '?', 0 ) == -1 ) {
			ch = '?';
		}
		else {
			ch = '&';
		}

		this._url = loc + ch + 'ctlId=' + this._id;
		this._patchUrl = this._url + '&get=patch';
*/

		if( loc.indexOf( '?', 0 ) != -1 ) {
			var tmp = new Array();
			tmp = loc.split( '?' );
			loc = tmp[ 0 ];
		}

		this._url = loc;
		//this._patchUrl = this._url;
	}

	this._sfWait = null;
	this._sfCtl = null;
	this._debugLog = null;
	this._id = '';
	this._text = '';
	this._url = '';
	this._parent = null;
	this._cssClass = '';
	

	this.setText = function( text )
	{
		this._text = text;

		this._sfCtl.onUnlock( function( sfCtl, self ) {
				
			sfCtl.setCssClass( self._cssClass );
			sfCtl.setText( text );

			sfCtl.expand( true, true );

			sfCtl.lock();

/*
			sfCtl.onShow( function() {
				
			});
*/
			//sfCtl.mode( sfMode_normal );
			sfCtl.show( 'slow' );

			sfCtl.paint();

			//sfCtl.unlock();
					
		}, this );

		this._sfCtl.unlock();

	}

	this.getText = function()
	{
		return this._text;
	}
	
	this.setCssClass = function( cssClass )
	{
		this._cssClass = cssClass;
	}

	this.getCssClass = function()
	{
		return this._cssClass;
	}

	this.getId = function()
	{
		return this._id;
	}

	this._renderLoadingScreen = function()
	{

		this._sfWait = new Surface( 'sf_' + this._parent + '_wait', this._parent, debugLog );			

		this._sfWait.onUnlock( function( sfWait ) {
			sfWait.setCssClass( "small-wait-control" );

			
			sfWait.expand( true, true );

			sfWait.lock();
			sfWait.update();
		});
	
		this._sfWait.unlock();

		this._sfWait.paint( true );

	}

	this._doWait = function( show, callback )
	{

		var self = this;


		if( show ) {
	
			

			if( callback ) {
				this._sfWait.onShow( function( sfWait ) {
					if( callback != null ) callback( self );
				});
			}

			this._sfWait.show( 'slow' );
		}
		else {
			

			if( callback ) {

				this._sfWait.onHide( function( sfWait ) {
					if( callback != null ) callback( self );
				});
			}

			this._sfWait.hide( 'slow' );
		}

		this._sfWait.paint( true );
	
		//callback( self );
	}

	this._render = function( refreshOptions )
	{

		var self = this;

		this._renderLoadingScreen();

		this._doWait( true, function( self ) {
		
			var tmp = null;

			if( refreshOptions != null ) {
				tmp = refreshOptions;
			}
			else {
				tmp = new Object();
			}

			tmp.ctlId = self.getId();
			tmp.get = 'json';
			tmp.structId = 'label';

			$.get( self._url, tmp, function( ret, code ) {


				self._doWait( false, function( self ) {

					self._sfCtl.onUnlock( function( sfCtl, self ) {

						var labelInfo = parseJSON( ret );
		
						if( labelInfo != null ) {

							if( labelInfo.clientErrorCode == 0 ) {

								self.setCssClass( labelInfo.cssClass );
								self.setText( labelInfo.text );
							}
							else {
								eval( labelInfo.clientErrorHandler + '(' + labelInfo.clientErrorCode + ');' );
							}
						}
					
					}, self );

					self._sfCtl.unlock();
					self._sfCtl.paint();
				});

			});

		});

	}

	this.refresh = function( refreshOptions )
	{
		var self = this;


		this._sfCtl.onHide( function() {

			// first do some clean up
			//sfCleanUp();

			//self._sfCtl.kill();

			// now render the label
			self._render( refreshOptions );

			//self._sfCtl.mode( sfMode_layout );
		});
		
		this._sfCtl.hide( 'slow' );
		this._sfCtl.paint();	
	}

	this.paint = function( options )
	{
		this._sfCtl = new Surface( 'sf_' + this._parent, this._parent, this._debugLog );

		this._render( options );

	}

	this.construct( id, parent, opt, debugLog );
}
