//CHECK FOR AND DECLARE NAMESPACE
try
{
	//DECLARE OUR NAMESPACE
	var iF_AJXPager;
	
	//TEST TO MAKE SURE THAT IT IS NOT ALREADY TAKEN AND CREATE IF NOT TAKEN
	if(!iF_AJXPager) iF_AJXPager = {};
	
	else if(typeof iF_AJXPager != "object")
	{
		throw new Error("ERROR");
	}

}
catch(e){ alert(e);	}


//CLASS CONSTRUCTOR
iF_AJXPager.pager = function(WrapperEL, ContElementID,nextPageLinkID,prevPageLinkID,itemsPerPage)
{
	this.wrapperELe = this.getElement(WrapperEL);
	this.ContEle = this.getElement(ContElementID);
	this.startItem = 1;
	this.endItem = itemsPerPage;
	this.itemsPerPage = itemsPerPage;
	this.nextPageLinkEL = this.getElement(nextPageLinkID);
	this.prevPageLinkEL = this.getElement(prevPageLinkID);
	this.nextButtonDisabled = false;
	this.recordCoungSet = false;
	this.currentPage = 1;
	this.attachBehaviors();

};

//////////////////////////////////////////////////////////////////////////////////////

iF_AJXPager.pager.prototype.getElement = function(ele)
{
	if (ele && typeof ele == "string")
		return document.getElementById(ele);
	return ele;
};

iF_AJXPager.pager.prototype.loadingAni = function(action)
{
	if(action == 'load')
	{
		this.ContEle.className = 'if_MyFavList_trans';
		var loader = document.createElement("img");
		loader.src = 'http://images.ifriends.net/IF_V2/loading_red.gif';
		loader.style.position = 'absolute';
		loader.id = 'loadingAni';
		loader.style.top = '153px';
		loader.style.left = '50px';
		this.wrapperELe.insertBefore(loader,this.ContEle);
	}
	else
	{
		this.ContEle.className = '';
		var ldr = this.getElement('loadingAni');
		ldr.parentNode.removeChild(ldr);		
	}
};

iF_AJXPager.pager.prototype.nextPage = function(e,el)
{
	
	this.startItem += this.itemsPerPage ;
	this.endItem += this.itemsPerPage;
	var cont = this.ContEle;
	var self = this;
	this.prevPageLinkEL.style.visibility = 'visible';
	this.removeBehaviors();
	
	if(self.startItem >= 1)
	{
		this.loadingAni('load');	
	}
	
	Ajax.Execute('membrg/TWRP.dll?pAppCode=TWRP_VIPeliteFaveList&t=site/includes/data/AjaxMyFavoritesList.htm&iStartCounter='+this.startItem+'&iEndCounter='+this.endItem, function(cResponse){
		
		var res = cResponse.match(/<!--(\d+)-->/);
		var recCnt = Number(res[1]);
		if(!self.recordCoungSet)
			self.setRecordCount(recCnt);
		
		if(self.currentPage <= self.totalPages)
		{
			self.loadingAni('finished');
			cont.innerHTML = cResponse;	
			self.attachBehaviors();
			self.currentPage++;
		}
		if(self.endItem >= self.recordCount)
		{
			self.endOfList();
		}
		
		
	});
	
};

iF_AJXPager.pager.prototype.prevPage = function()
{
	this.startItem -= this.itemsPerPage ;
	this.endItem -= this.itemsPerPage;
	var cont = this.ContEle;
	var self = this;
	this.removeBehaviors();
	
	if(self.endItem <= self.recordCount || self.recordCount == undefined)
	{
		this.loadingAni('load');	
	}
	
	if(this.nextButtonDisabled == true)
	{
		this.nextPageLinkEL.style.visibility = 'visible';
		this.nextButtonDisabled = false;
	}

	Ajax.Execute('membrg/TWRP.dll?pAppCode=TWRP_VIPeliteFaveList&t=site/includes/data/AjaxMyFavoritesList.htm&iStartCounter='+this.startItem+'&iEndCounter='+this.endItem, function(cResponse){
		if(self.startItem >= 1)
		{
			self.loadingAni('finished');
			cont.innerHTML = cResponse;
			self.attachBehaviors();
			
		}
		if(self.startItem <= 1)
		{
			self.startOfList();
		}
		
		self.currentPage--;
	});	
};

iF_AJXPager.pager.prototype.setRecordCount = function(recCount)
{
	this.recordCoungSet = true;
	this.recordCount = recCount;
	this.totalPages = Math.ceil(this.recordCount / this.itemsPerPage);	
};

iF_AJXPager.pager.prototype.endOfList = function()
{
	this.nextPageLinkEL.style.visibility = 'hidden';
	this.nextButtonDisabled = true;
};

iF_AJXPager.pager.prototype.startOfList = function()
{
	this.prevPageLinkEL.style.visibility = 'hidden';
};


//METHOD THAT ADDS THE EVENT HANDLERS USED BY CLASS
iF_AJXPager.pager.prototype.attachBehaviors = function()
{
	var self = this;
	this.nextPageLinkEL.onclick = function(e) { return self.nextPage(e,this.nextPageLinkEL); }
	this.prevPageLinkEL.onclick = function(e) { return self.prevPage(e,this.prevPageLinkEL); }	
};

iF_AJXPager.pager.prototype.removeBehaviors = function()
{
	this.nextPageLinkEL.onclick = null;
	this.prevPageLinkEL.onclick = null;
};

//************** CLASS PROPERTIES AND METHODS **************\\

//UTILITY METHOD TO ADD OUR EVENTS. CROSS BROWSER COMPATIBLE.
iF_AJXPager.pager.addEventListener = function(element, eventType, handler, capture)
{
	try
	{
		if (element.addEventListener)
			element.addEventListener(eventType, handler, capture);
		else if (element.attachEvent)
			element.attachEvent("on" + eventType, handler);
	}
	catch (e) {}
};

iF_AJXPager.pager.removeEventListener = function(element, eventType, handler, capture)
{

	try
	{
		if (element.removeEventListener)
		{
			element.removeEventListener(eventType, handler, capture);
		}
		else if (element.detachEvent)
			element.detachEvent("on" + eventType, handler);
	}
	catch (e) {}
};
