/*
====================================================

  http://www.GlobalWebCart.com - Version 2

  Copyright 2009 Internet Specialty Services, LLC.

  ANY UN-AUTHORIZED USE OR DISTRIBUTION OF THIS
  FILE OR IT'S CONTENTS IS STRICTLY PROHIBITED

====================================================
*/
/*
version: 2.0.2
Date: 2011-01-03 22:29
*/

var FlipIt = function(tID){
	this.tbl = document.getElementById(tID);
	if (!this.tbl){
		alert("Can't finid the object!");
		return;
	}
	this.iCountPerPage = 5; // record count/page
	this.flip = 5000; // page flip time
	this.itemFlip = 50; // 
	this.iItemCount = 0; // count of cells for this table
	this.iPauseArea = 1;
	this.bFirstShow = true; // if that's the first show data
	this.bShowByPage = false; // if show data page by page

	this.iCurrentIndex = 0; // current index

	this.aContentItems = new Array();
	this.aThisPageContent = new Array();
	this.aCell = new Array();
	this.iCurrentCellIndex = -1;

	// only when show products page by page use these variables
	this.iCurrentPageNo = 1;
	this.iTotalPageCount = 0;
	this.iItemCountOfThisPage = -1;

	this.bFirstLoad = true; // if this is the first loaded
	this.sSkipItem = ""; // if skip the first and/or the last cell of row
	this.sIDToShowPageNoInfo = ""; // 

	this.iFirstIndex = -1; // the first index of products
	this.iLastIndex = -1; // the last index of products

	this.iLeftPercentWidth = 100;

	this.bCanFlip = true;
	this.flipAlso = null;
	this.oShowItemInterval = null;
}
FlipIt.prototype = {
	// get the settings in the rel tag
	SplitRelValue : function(oObj, iIndex){
		var sResult = "";
		if (oObj){
			var aArray = oObj.getAttribute("rel").split(";");
			if (aArray){
				if (iIndex < aArray.length){
					sResult = aArray[iIndex];
					}
				}
			}
		return sResult;
	},
	
	getIntPercentWidth : function(sPercentWidth){
		var iResult = 0;
		if (sPercentWidth.indexOf("%") >= 0 && sPercentWidth.length > 1){
			iResult = parseInt(sPercentWidth.replace("%", ""));
		}
		return iResult;
	},
	
	ShowOneItem : function(){
		if (this.aThisPageContent.length > 0){
			this.aCell[this.iCurrentCellIndex].innerHTML = this.aThisPageContent.shift();
			this.iCurrentCellIndex ++;
		}else{
			clearInterval(this.oShowItemInterval);
			this.oShowItemInterval = null;
		}
	},
	
	flipData : function(tdDir, byHand, iMovCol){
		//_this = this;
		var _this = this;

		//debugger
		if (this.bCanFlip == false){
			return;
		}
		
		var bNeedAddEvent = false;
		if (!byHand || byHand == true){
			if (this.flipAlso){
				clearInterval(this.flipAlso);
				this.flipAlso = null;
				bNeedAddEvent = true;
			}
		}
		
		// clear something when want to flip to a new page
		clearInterval(this.oShowItemInterval);
		this.bCanFlip = false;
		this.aThisPageContent = new Array();
	
		if (this.bFirstLoad){
			var tCell = this.tbl.rows[0].cells;
			// set the value for some global variables
			this.sSkipItem = this.SplitRelValue(this.tbl, 0);
			this.iCountPerPage = parseInt(this.SplitRelValue(this.tbl, 1));
			this.flip = parseInt(this.SplitRelValue(this.tbl, 2));//alert(flip);
			this.itemFlip = parseInt(this.SplitRelValue(this.tbl, 3));
			var sShowByPage = this.SplitRelValue(this.tbl, 4);
			if (sShowByPage && sShowByPage == "P"){
				this.bShowByPage = true;
			}
			this.sIDToShowPageNoInfo = this.SplitRelValue(this.tbl,5);
			this.iPauseArea = parseInt(this.SplitRelValue(this.tbl, 6));
			
			this.iItemCount = tCell.length;
			this.iFirstIndex = 0;
			this.iLastIndex = tCell.length - 1;
			if (this.sSkipItem.indexOf("L") != -1){
				this.iItemCount --;
				this.iFirstIndex = 1;
				//this.iCurrentIndex = 1;
				
				this.iLeftPercentWidth -= this.getIntPercentWidth(tCell[0].width);
			}
			if (this.sSkipItem.indexOf("R") != -1){
				this.iItemCount --;
				this.iLastIndex --;
				
				this.iLeftPercentWidth -= this.getIntPercentWidth(tCell[tCell.length - 1].width);
			}
			
			// get all content from those cells
			var iCellIndex = 0;
			for (var i = this.iFirstIndex;i <= this.iLastIndex;i++){
				iCellIndex ++;
				this.aContentItems.push(tCell[i].innerHTML);
				if (iCellIndex <= this.iCountPerPage){
					this.aCell.push(tCell[i]);
					tCell[i].style.width = (this.iLeftPercentWidth / this.iCountPerPage) + "%";
				}else{
					tCell[i].style.display = "none";
				}
			}
			
			if (this.bShowByPage){
				var iAdd = this.iItemCount % this.iCountPerPage == 0 ? 0 : 1;
				this.iTotalPageCount = Math.floor(this.iItemCount / this.iCountPerPage) + iAdd;
			}
//alert(this.flip + '|' + byHand);
			
			if (this.flip > 0) {
				if (!byHand){
					callback = function(){_this.flipData(1);};
					this.flipAlso = setInterval(callback, this.flip)

					// pause flip action when mouse over an area
					if (this.iPauseArea == 1){
						this.tbl.onmouseover = function(){
							clearInterval(this.flipAlso);
							this.flipAlso = null;
						}
						this.tbl.onmouseout = function(){
							callback = function(){_this.flipData(1, false);};
							this.flipAlso = setInterval(callback, this.flip);
						}
					}else{
						for (var i = this.iFirstIndex;i <= this.iLastIndex;i++){
							tCell[i].onmouseover = function(){
								clearInterval(this.flipAlso);
								this.flipAlso = null;
							}
							tCell[i].onmouseout = function(){
								callback = function(){_this.flipData(1, false);};
								this.flipAlso = setInterval(callback, this.flip);
							}
						}
					}
				}
			}
		}
			this.iFirstIndex = 0;
			this.iLastIndex = this.aContentItems.length - 1;
			this.iCurrentCellIndex = 1;
			//debugger
			// show those products
			if (this.bShowByPage){
				this.iCurrentPageNo += tdDir;
				
				if (this.iCurrentPageNo < 1){
					this.iCurrentPageNo = this.iTotalPageCount;
				}
				if (this.iCurrentPageNo > this.iTotalPageCount){
					this.iCurrentPageNo = 1;
				}
				
				this.iItemCountOfThisPage = (this.iCurrentPageNo >= this.iTotalPageCount) ? this.iItemCount % this.iCountPerPage: this.iCountPerPage;
				
				if (this.iItemCountOfThisPage <= 0) this.iItemCountOfThisPage = this.iCountPerPage;
				
				this.iCurrentIndex = (this.iCurrentPageNo - 1) * this.iCountPerPage + this.iFirstIndex;
				
				var oShowPageInfo = document.getElementById(this.sIDToShowPageNoInfo);
				if (oShowPageInfo){
					oShowPageInfo.innerHTML = this.iCurrentPageNo + "/" + this.iTotalPageCount;
				}
			}else{
				this.iItemCountOfThisPage = this.iCountPerPage;
				/*if (tdDir < 0){
					for (var tmp = 0; tmp < 2;tmp ++){
						this.iCurrentIndex += this.iCountPerPage * (tdDir > 0 ? 0: tdDir);
						
						if (this.iCurrentIndex < 0)
							this.iCurrentIndex += this.iItemCount;
						else if (this.iCurrentIndex >= this.iItemCount)
							this.iCurrentIndex -= this.iItemCount;
					}
				}*/
				if (!iMovCol)
					iMovCol = 1;
				if (!this.bFirstLoad){
					var iMoveColCount = iMovCol *  tdDir;
					this.iCurrentIndex -= this.iCountPerPage;
					this.iCurrentIndex += iMoveColCount;
				}
			}
			
			if (iMovCol){
				//this.iCurrentIndex += (this.iCountPerPage - iMovCol);
				if (this.iCurrentIndex < 0)
					this.iCurrentIndex += this.iItemCount;
				else if (this.iCurrentIndex >= this.iItemCount)
					this.iCurrentIndex -= this.iItemCount;
			}
			
			var iNowItemCount = 0;
			for (var i = 0; i < this.iCountPerPage;i++){
				if (this.bFirstLoad){
					this.iCurrentIndex += this.iItemCountOfThisPage;
					if (this.iCurrentIndex < 0)
						this.iCurrentIndex += this.iItemCount;
					else if (this.iCurrentIndex >= this.iItemCount)
						this.iCurrentIndex -= this.iItemCount;
					
					break;
				}else{
					if (i == 0){
						this.aCell[i].innerHTML = this.aContentItems[this.iCurrentIndex];
						this.iCurrentIndex ++;
						if (this.iCurrentIndex < 0){
							this.iCurrentIndex += this.iItemCount;
						}else if (this.iCurrentIndex >= this.iItemCount){
							this.iCurrentIndex -= this.iItemCount;
						}
					}else{
						if (iNowItemCount <  this.iItemCountOfThisPage - 1){
							this.aThisPageContent.push(this.aContentItems[this.iCurrentIndex]);
							this.iCurrentIndex ++;
							if (this.iCurrentIndex < 0){
								this.iCurrentIndex += this.iItemCount;
							}else if (this.iCurrentIndex >= this.iItemCount){
								this.iCurrentIndex -= this.iItemCount;
							}
						}else{
							this.aThisPageContent.push("&nbsp;&nbsp;&nbsp;&nbsp;");
						}
						iNowItemCount ++;
					}
				}
			}
			
			// setInterval
			if (!this.bFirstLoad){
				callback = function(){_this.ShowOneItem();};
				this.oShowItemInterval = window.setInterval(callback, this.itemFlip);

			}
			
			if (bNeedAddEvent){
				callback = function(){_this.flipData(1, false);};
				this.flipAlso = setInterval(callback, this.flip);
			}
			
			this.bFirstLoad = false;
		
		this.bCanFlip = true;
	}
}

