/*																																										*/
if( typeof pln == 'undefined' || typeof nvi == 'undefined' ){

	/**
	 @requires pln.js
	 @requires nvi.core.js
	 @requires class.nvi.basic.panel.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." );
	if( typeof class_nvi_basic_panel == 'undefined' ) alert( "This file required the Class Nvi Basic Panel and it need to be loaded before this file." );

}else{



	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 @name 			Pagination Panel Class
	 @version		1.00
	*/
	function class_nvi_pagination_panel(){
	
		var _currentParams = null ;
		var _linkCollection ;
		var _controlViewId ;
		var _disablePaginationUpdateOnViewUpdate = false ;
		var _host = this ;
		
		this.__constructor = function( $id ){
			__super.__constructor( $id ) ;
		}	
		
		this.toString = function(){ return "Nvi Pagination Panel Class" ; }
		
		/**
		 @public
		 @description	Initialize the panel, parse all the link and add 'click' event on each of the link
		 @return		Nothing
		*/		
		this.initialize = function(){

			// parse each pagination and apply action...
			var element = this.getElement() ; 
			if( !pln.isHtmlElement( element ) ) return ;
			
			_linkCollection = element.nodeName.toLowerCase() == 'a' ? [ element ] : pln.node.getByName( 'a' , this.getElement() ) ;
			if( pln.isArray( _linkCollection ) ){
				if( _linkCollection.length == 0 ){
					nvi.logManager.log( 'There is no A node in the following pagination container: ' + this.getId() , 'Report' , 'class_nvi_pagination_panel' , 'initialize' );
				}
				for( var i = 0 ; i < _linkCollection.length ; i++ ){
					var element = _linkCollection[ i ] ;
					if( pln.isHtmlElement( element ) ){
						var url = element.href ;
						var params = null ;
						try{
							// Get the url param from the href attibute
							params = url.match( /\?.+/gi )[ 0 ].replace( '?' , '' );

							// if there is no params, we do not add js events
							element.href = "javascript:void(0);" ; // This work for all browsers but it is done for ie6, cancel the href and there is no race condition with the onclick browser event
							element.title = params ;
							pln.events.addListener( element , 'click' , null , function( eventObject , parameters ){
								// Assigns current params
								if( pln.isString( parameters ) )_currentParams = parameters ;
								if( pln.isString( _controlViewId ) ){
									var gridInstance = nvi.panelManager.getInstance( _controlViewId ) ;
									if( !pln.isObject( gridInstance ) ){
										nvi.logManager.log( 'The panel view linked with this pagination instance ( ' + _host.getId() + ' ) is not a panel instance, it may be null or undefined' , 'Error' , 'class_nvi_pagination_panel' , 'initialize' );
									}else{
										gridInstance.updateView( _host.getSharedRessources() ) ;
										nvi.eventManager.dispatchEvent( _host.getId() , nvi.eventManager.events.__changed ) ;
									}
								}
							} , params );
						}catch( errorObject ){}
					}
				}
			}
			
			__super.initialize() ;

		}
		
		this.terminate = function(){
			__super.terminate() ;
		}
		
		/**
		 @public
		 @description	Register the panel id of the view that this panel will control
		 @return		Nothing
		*/		
		this.controlViewId = function( id ){
			if( pln.isString( id ) ){
				_controlViewId = id ;
				nvi.eventManager.addListener( _controlViewId , nvi.eventManager.events.__updating , this , this.updateView ) ;
			}
		}

		/**
		 @public
		 @description	Validate if this pagination panel instance if included in the view that this panel control.
						It is used to disable refreshing this panel because it will be refreshed anyway when its view will
						be refreshed since it is part of the view...
		 @return		Boolean ;
		*/		
		this.isChildOfView = function(){
			var isChild = false ;
			var controledViewElement = null ;
			try{
				controledViewElement = nvi.panelManager.getInstance( _controlViewId ).getElement() ;
			}catch( errotObject ){}
			var paginationElement = this.getElement() ;
			if( pln.isHtmlElement( controledViewElement ) && pln.isHtmlElement( paginationElement ) ){
				if( pln.node.isChildOf( paginationElement , controledViewElement ) ) isChild = true ;
			}
			return isChild ;
		}
		
		/**
		 @public
		 @description	We initialize this module each time the view is updated since we have to assign mouse event and parse links
		 @return		Nothing
		*/		
		this.onUpdatedView = function( success , serverData , message ){
			this.initialize() ;
		}

		/**
		 @public
		 @description	We remove mouse listener on each link and update this panel only if it 
						is not a part the view it will update since the view will update also...
		 @return		Nothing
		*/	
		this.updateView = function(){
			if( _disablePaginationUpdateOnViewUpdate ) return ;
			if( pln.isString( _currentParams ) ){
				for( var i = 0 ; i < _linkCollection.length ; i++ ){
					var element = _linkCollection[ i ] ;
					if( pln.isHtmlElement( element ) ) pln.events.removeListener( element , 'click' , this , this.updateView ) ;
				}
				if( pln.isString( _controlViewId ) && !this.isChildOfView() ){
					_hideDefaultWaitingPanel = false ;
					__super.updateView( _currentParams ) ;
				}
			}
		}
		
		/**
		 @public
		 @description	Each time a link is clicked, we get its params and update a vaiable, to get the 
						latest param, we use the following method.
		 @return		String ; A serialized variable name / value pair
		*/
		this.getCurrentParams = function(){
			return _currentParams ;
		}
		
		/**
		 @private
		 @description	This will tell the instance to not refresh itself when it refresh it's assigned view(s)
		 @return		Nothing ;
		*/	
		this.disablePaginationUpdateOnViewUpdate = function(){
			_disablePaginationUpdateOnViewUpdate = true ;
		}
		
		/**
		 @public
		 @description	Get the desired data and returned it( when called) to 
						the Nvi Shared Ressources Manager
		 @returns		Object ;
		*/
		this.getSharedRessources = function(){
			return {
				type : 'pagination' , 
				id : this.getId() ,
				parameters : {
					currentParams : this.getCurrentParams()
				}
			} ;
		}		

	}
	/**
	 @definition	We are extending this class with the basic panel class
	*/
	class_nvi_pagination_panel.extend = class_nvi_basic_panel ;
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
}