
/*
 * ASC.Chrysler.Pagination
 * 
 */
ASC.namespace('ASC.Chrysler.Pagination');
ASC.Chrysler.Pagination = ASC.extend(Ext.util.Observable,{
	items: null,
	tmpItems: [],
	pageSize: 10,
	pageCount: 0,
	itemCount: 0,
	pageIndex: 1,
	/*
	 * itemsCFG: the json object.
	 * pageSize: the size of the page
	 * itemName(optional): if the itemsCFG is an Array, you do not need to pass itemName.
	 * 			if the Array is a property of the itemsCFG, u have to pass the itemName.
	 * all properties of itemsCFG will be applied to ASC.Chrysler.Pagination.
	 */
	constructor: function (itemsCFG,pageSize,itemName)
	{
		ASC.Chrysler.Pagination.superclass.constructor.apply(this);
		this.addEvents({"indexChanging":true});
		ASC.apply(this, itemsCFG);	
		if(itemName)
		{
			this.items = this[itemName];
		}
		else
		{
			this.items = itemsCFG;
		}
		this.pageSize = pageSize;
		this.itemCount = this.items.length;
		this.pageCount = parseInt(this.itemCount/this.pageSize + 0.99999);
	},
	/*
	 * setPageIndex, and it will fire the event 'indexChanging'.
	 * then pass the pageIndex and items to the event
	 * 
	 */
	setPageIndex: function(pageIndex){
		this.pageIndex = pageIndex;
		this.fireEvent("indexChanging",{pageIndex:this.pageIndex,items:this.getItems(this.pageIndex)});
	},
	setPageBarSelectedIndex:function(offset,pageBarPageIndex,size)
	{
		pageBarPageIndex = pageBarPageIndex+offset;
		pageBarPageIndex = pageBarPageIndex < 1? 1:pageBarPageIndex;
		
		this.setPageIndex((pageBarPageIndex-1)*size+1);
	},
	/*
	 * get the items. it wonn't fire the event 'indexChanging'
	 */
	getItems: function(pageIndex){
		this.pageIndex = pageIndex;
		this.tmpItems = [];
		var beginIndex = (pageIndex-1) * this.pageSize;
		if(beginIndex > this.itemCount)
		{
			return this.tmpItems;
		}
		var endIndex = beginIndex + this.pageSize;
		if(endIndex > this.itemCount)
		{
			endIndex = this.itemCount;
		}
		for(var i=beginIndex; i<endIndex; i++)
		{
			this.tmpItems[this.tmpItems.length] = this.items[i];
		}
		return this.tmpItems;
	},
	/*
	 * getCurrentItems 
	 */
	getCurrentItems: function(){
		return this.tmpItems;
	},
	
	sort: function(colName, sortByTag){
		var strOrderBy
		var tmpVehicle = null;
		var temp = null;
		var exchange;
		for(var i=0;i<this.itemCount - 1;i++)
		{
			for(var j=this.itemCount -2; j>=i; j--) 
			{
				if(sortByTag)
				{
					 if( this.items[j+1][colName] <= this.items[j][colName])
					 {
					     temp = this.items[j+1];
					     this.items[j+1] = this.items[j];
					     this.items[j]= temp;
					     exchange = true;
					 }
				}
				else
				{
					if( this.items[j+1][colName] > this.items[j][colName])
					 {
					     temp = this.items[j];
					     this.items[j] = this.items[j+1];
					     this.items[j+1]= temp;
					     exchange = true;
					 }
				}
			}
			if(!exchange) break;
		}
		return this.getItems(this.pageIndex);
	}	
});