dojo.require("dijit.layout.ContentPane");

/**
 * @author DREDroot
 *
 */
dojo.declare("dojom.widgets.PagesNode", null, 
{
	id: null,
	domNode: null,
	_contentPane: null,	
	
	pagesNum : 0,
	currentPage : 0,
	visiblePages: 5,
	
	header:'<div><table width="99%" border="0"><tr><td><table border="0" align="right" cellspacing="5"><tr>',
	footer: '</tr></table></div>',
	
	_prevPage: function (num){
		var str = '<td align="center"  valign="top" class="pagesNav">'
		if(num!=null)str +='<a href="##" onclick="'+this.id+'_onPageClick('+num+')" class="navPage">prev</a>'
		str +='</td>';		
		return str;
	},
	
	_nextPage: function (num){		
		var str = '<td align="center"  valign="top" class="pagesNav">'
		if(num!=null)str +='<a href="##" onclick="'+this.id+'_onPageClick('+num+')" class="navPage">next</a>'
		str +='</td>';		
		return str;
	},
	
	_simplePage: function (num){
		var str = '<td align="center" valign="top" class="pages"><a href="##" onclick="'+this.id+'_onPageClick('
				+num
				+')" class="simplePage">'
				+(num+1)				
				+'</a></td>';		
		return str;
	},
		
	_currentPage: function (num){
		var str = '<td  align="center" valign="top" class="pages"><a href="##" onclick="'+this.id+'_onPageClick('
				+num
				+')" class="currentPage">'
				+(num + 1)
				+'</a></td>';
		return str;
	},
		
	_othersPage: function (num){
		var str = '<td  align="center" valign="top" class="pages"><a href="##" onclick="'+this.id+'_onPageClick('
				+num
				+')" class="simplePage">..</a></td>';
		return str;
	},
	
	constructor: function(arguments)
	{		
		var obj = this;
		this._contentPane = new dijit.layout.ContentPane(arguments);
		this.id = this._contentPane.id;
		this.domNode = this._contentPane.domNode;
		
		window[this.id+'_onPageClick'] = function(num){obj.onPageClick(num)};
		this.startup();
	},
	
	destroy: function()
	{
		this._contentPane.destroy();
	},
	
	startup: function()
	{
		var num = parseInt(this.pagesNum);
		var x = parseInt(this.visiblePages);
		var current = parseInt(this.currentPage);
		
		var pages = '';

		if(num <= 1)
		{
			this._contentPane.setContent(this.header+pages+this.footer);	
			return;
		}
		
		if(current!=0)
			pages +=this._prevPage(current-1);	
		else
			pages +=this._prevPage();	
		
		if(num-1<=x*2)
		{	
			for(var i=0;i<current;i++)
				pages +=this._simplePage(i);
				
			pages +=this._currentPage(current);

			for(var i=current+1;i<num;i++)
				pages +=this._simplePage(i);				   					
		}
		else if(current == 0)
		{
			pages +=this._currentPage(current);
			for(var i=1;i<x*2-1;i++)
			   	pages +=this._simplePage(i);
			
			if(x*2+1!= num) pages +=this._othersPage(x*2-1);
			else pages +=this._simplePage(x*2-1);
			pages +=this._simplePage(num-1);
		}
		else if(current+1==num)
		{
			pages +=this._simplePage(0);
			if(current-(x*2)+1!=1) pages +=this._othersPage(current-(x*2)+1);
			else pages +=this._simplePage(1);
			for(var i=current-(x*2)+2;i<current;i++)
				   		pages +=this._simplePage(i);
				   				
			pages +=this._currentPage(current);
		}
		else{
			pages +=this._simplePage(0);
			
			var offsetl = 0;
			if(current<=x)
				offsetl = x - current;
				
			var offsetr = 0;
			if(num-current-1<=x)
				offsetr = num-current-1 - x;
			
			if(current<=x){				
				for(var i=1;i<current;i++)
				 	pages +=this._simplePage(i);
			}
			else
			{				
				pages +=this._othersPage(current-x+1);
				for(var i=current-x+2 + offsetr;i<current;i++)
					   		pages +=this._simplePage(i);
			}
			
			pages +=this._currentPage(current);			
			
			if(num-current-1<=x){
				for(var i=current+1;i<num-1;i++)
					   		pages +=this._simplePage(i);
			}
			else
			{
				for(var i=current+1;i<current+x-1 + offsetl;i++)
					   		pages +=this._simplePage(i);
				pages +=this._othersPage(current+x-1);
			}
			
			pages +=this._simplePage(num-1);	
		}
		
		if(current+1!=num)
			pages +=this._nextPage(current+1);
		else 
			pages +=this._nextPage();
		
		this._contentPane.setContent(this.header+pages+this.footer);		
	},
	
	onPageClick: function(num){
		this.setCurrentPage(num);
		return true;
	},
	
	setCurrentPage: function(num)
	{
		if(num != null && num >= 0 && parseInt(num) !=null)
		{
			this.currentPage = parseInt(num);
		}
		this.startup();		
	},
	
	setPagesNum: function(num)
	{
		if(num != null && num >= 0 && parseInt(num) !=null)	this.pagesNum = parseInt(num);
		else return;
		this.currentPage = 0;
		this.startup();
	}
	
});
