var Paging = function(parameters){

	var _page_per_view = 0;
	var _rows_per_page = 0;
	var _current_page=1; 
	var _record_count = 0;
	var _goto_page= 'gotoPage';
	var _render_top = null;
	var _render_bottom = null; 
	
	//constructor function
	var _Paging = function(parameters){
		
		if (parameters.page_per_view !=null){
			_page_per_view = parameters.page_per_view;
		}
		
		if (parameters.rows_per_page !=null){
			_rows_per_page = parameters.rows_per_page;
		}
		
		if (parameters.render_top != null){
			_render_top = parameters.render_top;
		}
		
		if (parameters.render_bottom != null){
			_render_bottom = parameters.render_bottom;
		}
		
		if (parameters.record_count > 0){
			_record_count = parameters.record_count; 
		}
		
		if (parameters.current_page){
			_current_page = parameters.current_page; 
		}
		
		if (parameters.on_success){
			_goto_page = parameters.on_success; 
		}

	}
	
	this.getCurrentPage  = function(){ return _current_page; }
	this.setCurrentPage = function(current_page){ _current_page = current_page };
	
	this.getRecordCount  = function(){ return _record_count; }
	this.setRecordCount = function(record_count){ _record_count = record_count };

	this.getRowsPerPage  = function(){ return _rows_per_page; }
	this.setRowsPerPage = function(rows_per_page){ _rows_per_page = rows_per_page };
	
	this.getGotoPage = function(){ return _goto_page; }
	this.setGotoPage = function(goto_page){ _goto_page = goto_page };
	
	this.getRowStart = function(){ 
		return (_current_page * _rows_per_page) - _rows_per_page + 1;
	}
	
	this.getRowEnd = function(){
		return _current_page * _rows_per_page;
	}
	
	this.getNextPage = function(){ 
		return _current_page  + 1;
	}
	
	this.getPreviousPage = function(){ 
		return _current_page  - 1;
	}

	this.getTotalPages = function(){ 
		
		//var result = (_record_count / _rows_per_page) +  (_record_count % _rows_per_page == 0? 0: 1); 
		var result = _record_count / _page_per_view;
		result = Math.ceil(result); 
		//result = parseInt(result,10);	//disregard the decimal points

		return result;
	}
	
	this.getCountStart = function(){ 
		
		var page_no = _current_page;
		
		page_no = parseFloat(page_no); 
		
		page_no = Math.ceil( (page_no / (_page_per_view)))  -1 ;
		
		page_no = (page_no * _page_per_view) + 1;

        //alert("From Function--start--"+page_no);

		//return page_no ;
		
		//return Math.ceil( (page_no / (_page_per_view))) ;

		return  (((_current_page - 1 )/ _page_per_view) * _page_per_view) + 1 ;
	}
	
	this.getCountEnd = function(){ 
		
		var count_end = Number(this.getCountStart()) + Number(10);
		//alert("count_start:"+this.getCountStart()+" count_end:"+count_end+" this.getTotalPages():"+this.getTotalPages());
		if(count_end > Number(this.getTotalPages())){
			
			count_end = Number(this.getTotalPages()) + 1;
		}
		//alert("From Function--end--"+count_end);
		return count_end;
	}
	//Output: [<< < Page 1 2 3 4 5 6 7 8 9 10 11 > >>]			
	this.getPagingStyle2 = function(){
		
		var goto_page = this.getGotoPage();
		var current_page = this.getCurrentPage();
		
		var count_start = this.getCountStart();
		var count_end = this.getCountEnd();

		var previous_page = Number(this.getCurrentPage()) - 1;
		var next_page = Number(this.getCurrentPage()) + 1 ; 
		
		var first_page = 1;
		var last_page = this.getTotalPages(); 
		
		var paging ='<b>Page</b> ';
		var link_page='';
		var link_first_page='';
		var link_prev_page='';
		var link_last_page ='';
		var link_next_page=''
		
		var link_param_others = '"' + this.getRecordCount()  + '","' + goto_page + '","' + 
								_render_top + '","' + _render_bottom + '"';
		
		var params = 'render_top:"' + _render_top + '",' +
								'render_bottom:"' + _render_bottom + '",' +
								'goto_page:"' + goto_page + '",' +
								'record_count:"' + this.getRecordCount()  + '"' ;
								
		for (var i=count_start; i< count_end;i++){
			
			if (i ==current_page){
				paging  = paging + ' <b>' + i + '</b>';
			}else{
					//alert("--page"+current_page)
				link_page = 'javascript:' + goto_page + '({on_success:"' + i + '",'+ params +  '})';
				paging  = paging + " <a href='" + link_page + "'>" + i + "</a>";

			}
		}
		
		
		if (current_page>1){

			
			link_page = 'javascript:' + goto_page + '({on_success:"' + first_page + '",' + params +  '})';
			
			
			link_first_page = "<a href='" + link_page + "'class='alpha smallbutton' title='First page'><<</a>";
			
			link_page = 'javascript:' + goto_page + '({on_success:"' + previous_page + '",' + params +  '})';
			link_prev_page = "<a href='" + link_page + "' class='alpha smallbutton' title='Previous page'><</a>";

			//add first and last page
			paging =  link_first_page + ' ' + link_prev_page +'  ' +  paging+ '  '  ;
            //paging =  paging + "current_page>1";

					
		}
		
		if (current_page !=last_page){
			
			link_page = 'javascript:' + goto_page + '({on_success:"' + last_page + '",' + params +  '})';
			
			link_last_page = "<a href='" + link_page + "'class='alpha smallbutton' title='Last page'>>></a>";
			
			link_page = 'javascript:' + goto_page + '({on_success:"' + next_page + '",' + params +  '})';
			
			link_next_page = "<a href='" + link_page + "' class='alpha smallbutton' title='Next page'>></a>";
			
			paging = paging + '  ' + link_next_page + '  ' + link_last_page;

		}
		
		paging = '<span class="bullet"></span>  ' + paging + '  <span class="bullet"></span>';
		//alert(paging);
		//render the paging (at the top)
		if (_render_top !=null){
			
			document.getElementById(_render_top).innerHTML = paging;
		}

		//render the paging (at the bottom)
		//if (_render_bottom !="null"){
		var bottom_page_exist = document.getElementById(_render_bottom);
		if (bottom_page_exist){	
			//alert(_render_bottom);
			document.getElementById(_render_bottom).innerHTML = paging;

		}else{
			//alert(document.getElementById(_render_bottom) );			
		}
	}
		
	//call the constructor
	_Paging(parameters);

	if (_record_count>0){
		this.getPagingStyle2();
	}else{

		if (_render_top){
			document.getElementById(_render_top).innerHTML = '';
		}
		
		//render the paging (at the bottom)
		if (_render_bottom){
			document.getElementById(_render_bottom).innerHTML ='';
		}
	}
	
}



