/**
 * Paginator Class
 */
function PaginatorTable(){}

PaginatorTable.prototype =
{
		
	/**
	 * keys = column position, values = property column name
	 * 
	 * @param object|null
	 */
	_cols: null,
	
	/**
	 * Instance of the paginator
	 * 
	 * @param Paginator
	 */
	_paginator: null,
	
	_popUpId: 'popup_confirmation',
	
	getPopUpId: function(){
		return this._popUpId;
	},
	
	/**
	 * Constructor of the class
	 * 
	 * @param	paginator	Paginator
	 * @param	cols		object
	 * @return 	void
	 */
	init: function(paginator, cols)
	{
		this._paginator = paginator; 
		this._cols = cols;
		this._registerColumnLinks();
		
		var thisInstance = this;
		paginator.callbackAfterLoadAjax = function(){
			thisInstance.afterLoadAjax();
		};
	},
	
	/**
	 * in each column of _col:
	 * register a link to interact with the paginator.
	 * 
	 * @return void
	 */
	_registerColumnLinks: function()
	{
		var $container = $('#' + this._paginator.id);
		var thisInstance = this;
	    $('table thead tr th', $container[0]).each(function(index, element) {
	    	if(thisInstance._cols[index + 1] != undefined){
	    		var $th = $(element);
	    		$th.attr('class', 'order');
	    		if(thisInstance._cols[index + 1] === thisInstance._paginator.getQueryParam('order_by')){
	    			if(thisInstance._paginator.getQueryParam('asc') == 'true'){
	    				$th.attr('class', 'order asc');		    				
	    			} else {
	    				$th.attr('class', 'order desc');	
	    			}
	    		}
	    		$th.bind('click', function(){
    				thisInstance._onClickLinks(this, index);
				});
	    	}
	    });				
	},
	
	/**
	 * onClick event listener in order asc/desc
	 * 
	 * @return void
	 */
	_onClickLinks: function(thElement, index)
	{		
		if (this._paginator._activeAjaxRequest){
			return;
		}
		
		var $thElement = $(thElement); 
		var orderBy = this._cols[index + 1];
		var className = $thElement.attr('class');
		if(className.length > 5){
			var asc = className.substring(6);
			if(asc == 'desc'){
				asc = 'true';
			} else {
				asc = 'false';
			}			
		} else {
			asc = 'true';
		}
		this._paginator.setQueryParam('order_by', orderBy);
		this._paginator.setQueryParam('asc', asc);
		this._paginator._loadPage(1);
	},
	
	/**
	 * Callback after load the paginator ajax
	 * 
	 * @return void
	 */
	afterLoadAjax: function()
	{
		this._registerColumnLinks();
	},
	
	/**
	 * Create a hidden box to confirm or cancel delete item
	 * 
	 * @return void
	 */
	
	popUp_confirmation: function()
	{
		var popUpId = this.getPopUpId();
		if (!$('#' + popUpId).length > 0){
			var html = '<div class="box" id=' + popUpId + ' style="width:300px;display:none">' +				
				'<h2>Delete</h2>'+						        
		        '<h5>Do you want to delete the item?</h5>'+
		        '<div>'+ 
		        	'<a id="confirm" class="btn">Confirm</a>'+
		        	'<a id="cancel" class="btn">Cancel</a>'+
		        '</div>'+
	        '</div>';		        			
			
			$('body').append(html);			
				
			$('#'+ popUpId).overlay({
				mask: {
					color: '#ffffff',
					opacity: 0.8						
				},
				left:'center',					
				top: '40%',
				closeOnClick:false,
				closeOnEsc:false
			});			
		}
	},
	
	/**
	 * Register confirm and cancel buttons of the popup and show it.
	 * 
	 * @param	Number				id	
	 * @param	Number | undefined	nonprofitId 
	 * @return void
	 */
	
	popUpDeleteItem: function (id, nonprofitId){		
		
		var popUpId = this.getPopUpId();		
		
		$('#confirm').unbind('click');
		$('#cancel').unbind('click');
		
		$('#confirm').bind('click', function(){							
			paginatorTable.delete_item(id, nonprofitId);
			$('#' + popUpId).overlay().close();
		});
		
		$('#cancel').bind('click', function(){			
			$('#' + popUpId).overlay().close();
		});
		
		$('#' + popUpId).overlay().load();
	},
	
	/**
	 * Delete an item. You can overdrive this method, 
	 * to extend functionallity
	 * 
	 * @param	Number				id	
	 * @param	Number | undefined	nonprofitId 
	 * @return	void
	 */
	
	delete_item: function(id, nonprofitId){}
};

