/*																																										*/
if( typeof pln == 'undefined' || typeof nvi == 'undefined' ){

	/**
	 @requires pln.js
	 @requires nvi.core.js
	 @description if not found, alert the developers and do nothing;
	*/
	if( typeof pln == 'undefined' ) alert( "This file required the PLN Javascript Library ( pln.js ) and it need to be loaded before this file." );
	if( typeof nvi == 'undefined' ) alert( "This file required the NVI CORE ( nvi.core.js ) and it need to be loaded before this file." );

}else{



	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 @name 			Basic Panel Class
	 @version		1.00
	*/
	function class_nvi_basic_panel(){
		
		var _id ;
		var _automaticUpdateTimeout ;
		var _automaticUpdateDelay = 5 ;
		var _automaticUpdate = false ;
		var _customWaitingPanelId = null ;
		
		this.__constructor = function( $id ){
			_id = $id ;
		}		
		
		this.toString = function(){ return "Nvi Basic Panel Class" ; }		
		
		this.initialize = function(){
		}
		
		this.terminate = function(){
			// By default, we clear any ajax call
			nvi.panelManager.cancelUpdateView( this ) ;
		}
		
		/**
		 @public
		 @description	Return the id of the panel instance.
		 @return		The element id or null
		*/		
		this.getId = function(){
			return _id ;
		}

		/**
		 @public		
		 @description	Acquire an html element by it's ID and we do not create a pointer to it. 
		 @return		The element or null
		*/
		this.getElement = function(){
			/*
			 We always use element ID to get the element and we never create a pointer to it since we dont know
			 if the element have been cloned, modified, moved away or deleted.
			*/
			return pln.node.getById( this.getId() );
		}

		/**
		 @public		
		 @description	The basic ajax handler of the updateView method.
		 @return		Nothing
		*/		
		this.onUpdatedView = function( success , serverData , message ){
			if( _automaticUpdate ) this.startAutomaticUpdate() ;
		}

		/**
		 @public		
		 @description	The basic ajax method to refresh the view of a panel.
		 @return		Nothing
		*/
		this.updateView = function( data ){
			nvi.panelManager.updateView.apply( nvi.panelManager , [ this.getId() , data ] ) ;
		}

		/**
		 @public		
		 @description	All panel that extend the basic class can register a waiting panel with custom view to 
						replace the default panel view provided by the framework
		 @return		Nothing
		*/	
		this.registerWaitingPanel = function( $id ){
			if( !pln.isString( $id ) ) return ;
			_customWaitingPanelId = $id ;
			nvi.eventManager.addListener( _id , nvi.eventManager.events.__updating , null , function(){
				pln.node.setProperty( $id , 'display' , '' );  ; // This should not be 'block' because we dont know if the element was of type inline or block
			} );
			nvi.eventManager.addListener( _id , nvi.eventManager.events.__ready , null , function(){
				pln.node.setProperty( $id , 'display' , 'none' );
			} );
		}
		
		/**
		 @public		
		 @description	Start the automatic update process
		 @return		Nothing ;
		*/
		this.startAutomaticUpdate = function(){
			_automaticUpdate = true ;
			clearTimeout( _automaticUpdateTimeout ) ;
			_automaticUpdateTimeout = setTimeout( pln.delegate( this , this.updateView ) , _automaticUpdateDelay ) ;
		}
		
		/**
		 @public		
		 @description	Stop the automatic update process
		 @return		Nothing ;
		*/		
		this.stopAutomaticUpdate = function(){
			_automaticUpdate = false ;
			clearTimeout( _automaticUpdateTimeout ) ;
		}
		
		/**
		 @public		
		 @description	Set the delay for the automatic update process, if it is smaller than 1 , 1 is used.
		 @return		Nothing ;
		*/		
		this.setAutomaticUpdateDelay = function( delay ){
			if( !pln.isNumber( delay ) ){
				nvi.logManager.log( 'The delay is undefined or not an integer for the ' + this.getId() + ' panel' , 'Error' , 'class_nvi_basic_panel' , 'setAutomaticUpdateDelay' );
			}else{
				_automaticUpdateDelay = Math.max( Math.abs( delay ) , 1 ) ;
			}
		}

		this.getCustomWaitingPanel = function(){
			return pln.node.getById( _customWaitingPanelId ) ;
		}
		
		this.getDefaultWaitingPanel = function(){
			return pln.node.getById( nvi.getDefaultWaitingPanelPrefixId() + this.getId() ) ;
		}		

		/**
		 @public
		 @description	Get the desired data and returned it( when called) to 
						the Nvi Shared Ressources Manager
		 @returns		Object ;
		*/		
		this.getSharedRessources = function(){
			return {
				type : 'basic' ,
				id : this.getId() ,
				parameters : {}
			} ;
		}
		
	}
	/**
	 @definition	We defined here which other class we extend.
	*/
	class_nvi_basic_panel.extend = null ;

	
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
}