
function ScrollList() {

	this.ScrollBar = null;

	this.CurPage = 1;
	this.TotalPage = 1;

	this.ScrBarStartPosition = 0;
	this.ScrBarFirstLength = 0;
	this.ScrBarTotalLength = 0;

	this.PreviousCallBack = null;
	this.NextCallBack = null;

	this.IsCirculation = true;

	List.call(this);
}

ScrollList.prototype = new List();

ScrollList.prototype.CreateScrBar = function(Ename) {

	this.ScrollBar = new ScrollBar();
	this.ScrollBar.Create(Ename);
}

ScrollList.prototype.SetScrBarImage = function(BgImage, TopImage, CenterImage, BottomImage) {

	this.ScrollBar.SetBGImage(BgImage);
	this.ScrollBar.SetFirstImage(TopImage);
	this.ScrollBar.SetSegmentImage(CenterImage);
	this.ScrollBar.SetLastImage(BottomImage);

}
//2013 add by minseok
ScrollList.prototype.SetScrBGImage = function(BgImageTop, BgImageCenter, BgImageBottom) {
	this.ScrollBar.SetBGImage(BgImageTop, BgImageCenter, BgImageBottom);
}
ScrollList.prototype.SetScrollBarImage = function(TopImage, CenterImage, BottomImage) {
	this.ScrollBar.SetFirstImage(TopImage);
	this.ScrollBar.SetSegmentImage(CenterImage);
	this.ScrollBar.SetLastImage(BottomImage);
}

ScrollList.prototype.SetScrBarStyle = function(style) {

	alert("ScrollList.SetScrBarStyle()");

	var scrollbarBG = this.ScrollBar.GetBGLayer();
	scrollbarBG.SetStyle(style);

}

ScrollList.prototype.SetScrBarShapeStyle = function(style) {

	alert("ScrollList.SetScrBarShapeStyle()");

	this.ShapeStyle = style;
	var ScrollbarShapStyle;

	if(style == LIST_SHAPE_STYLE.HORIZONTAL) {
		ScrollbarShapStyle = SCROLLBAR_SHAPE_STYLE.HORIZONTAL;
	} else {
		ScrollbarShapStyle = SCROLLBAR_SHAPE_STYLE.VERTICAL;
	}

	this.ScrollBar.SetShapeStyle(ScrollbarShapStyle);
}
ScrollList.prototype.GetTotalPage = function() {
	return this.TotalPage;
}
ScrollList.prototype.ResetTotalPage = function() {

	this.TotalPage = parseInt(this.MaxNumOfData / this.MaxNumOfItem);

	if((this.MaxNumOfData % this.MaxNumOfItem) > 0) {
		this.TotalPage++;
	}
	//alert("TotalPage =" + this.TotalPage);

}

ScrollList.prototype.SetScrBarPosition = function(TotalLength, StartPosition, FirstLength) {

	alert("ScrollList.SetScrBarPosition()");
	this.ScrBarStartPosition = StartPosition;
	this.ScrBarFirstLength = FirstLength;
	this.ScrBarTotalLength = TotalLength;

	this.TotalPage = parseInt(this.MaxNumOfData / this.MaxNumOfItem);
	if((this.MaxNumOfData % this.MaxNumOfItem) > 0) {
		this.TotalPage++;
	}
	alert("TotalPage = " + this.TotalPage);
	this.ScrollBar.SetPosition(this.TotalPage, TotalLength, StartPosition, FirstLength);
	if(this.TotalPage > 1)
		this.ScrollBar.ResetScrollBar();

}
//2013 add by minseok
ScrollList.prototype.SetCheckForDataIndex = function(DataIndex, flag) {
	alert("ScrollList.SetCheckForDataIndex(" + DataIndex + ")");
	if(DataIndex >= 0) {
		this.DataArray[DataIndex].SetCheckFlag(flag);
	}
	this.RefreshPage();
}
ScrollList.prototype.RemoveCheckForDataIndex = function(DataIndex) {
	alert("ScrollList.RemoveCheckForDataIndex(" + DataIndex + ")");
	if(DataIndex < 0) {
		this.RemoveAllCheckImage();
	} else {
		this.DataArray[DataIndex].SetCheckFlag(false);
	}
}
ScrollList.prototype.RemoveAllCheckImage = function() {
	alert("ScrollList.RemoveAllCheckImage()");
	for(var i = 0; i < this.DataArray.length; i++) {
		this.DataArray[i].SetCheckFlag(false);
	}
}

ScrollList.prototype.SetHighlightForDataIndex = function(DataIndex) {

	alert("ScrollList.SetHighlightForDataIndex");

	this.Flag_Focus = true;

	var CurPage_ = parseInt(DataIndex / this.MaxNumOfItem);
	var rest = parseInt(DataIndex % this.MaxNumOfItem);

	this.StartDataIndex = CurPage_ * this.MaxNumOfItem;
	this.CurrentDataIndex = DataIndex;
	//this.CurrentItemIndex = rest;

	this.CurPage = CurPage_ + 1;
	alert("CurPage = " + this.CurPage);

	this.UpdatePage();
	this.SetCurrentItem(rest);

}

ScrollList.prototype.DeleteDataAll = function() {

	alert("ScrollList.DeleteDataAll ");

	if(this.MaxNumOfData == 0) {
		alert("can't delete data because no data!!! ");
		return;
	}

	this.DataArray.splice(0, this.MaxNumOfData);

	this.MaxNumOfData = 0;
	this.CurrentDataIndex = -1;
	this.StartDataIndex = -1;
	this.TotalPage = 0;
	this.CurPage = 0;

	for(var i = 0; i < this.NumOfCreatedItem; i++) {

		this.ItemArray[i].SetFlagForExistData(false);
	}

	this.NumOfVisibleItem = 0;
}

ScrollList.prototype.Next = function(){
	
	if(this.IsCirculation==false){
		this.Next2();
		return;
	}
	alert("ScrollList.Next()");
	var CurItemIndex = 	this.CurrentItemIndex + 1;

	this.CurrentDataIndex++;
	
	//to next page	
	if(CurItemIndex == this.NumOfVisibleItem ) {
		
		alert("move to top");
		CurItemIndex = 0;		
			
		//case: Multi Page 
		if(this.MaxNumOfItem <  this.MaxNumOfData ) 
		{
			alert("Page scroll ");
			this.StartDataIndex += this.MaxNumOfItem;
			this.CurPage++;
			
			//from last page to 1st page 
			if(this.StartDataIndex > this.MaxNumOfData -1 ) {
		
				this.StartDataIndex = 0;
				this.CurrentDataIndex = 0;
				this.CurPage = 1;
			}
			
			this.UpdatePage();						
		}
		else
		{
			this.CurrentDataIndex = 0;
		}		
	}

	alert("CurrentDataIndex = " + this.CurrentDataIndex);		
	this.SetCurrentItem(CurItemIndex);	
}


ScrollList.prototype.Previous = function(){
	if(this.IsCirculation==false){
		this.Previous2();
		return;
	}
	alert("ScrollList.Previous()");

	if(this.MaxNumOfData < 2){
		alert(" this.MaxNumOfData < 2 ");
		return;
	}
	
	var CurItemIndex = 	this.CurrentItemIndex - 1;
	//set data index
	this.CurrentDataIndex--;
			
	if(CurItemIndex == -1 ) {				
		alert("Page scroll");
		
		//case1 : Multi Page 
		if (this.MaxNumOfItem < this.MaxNumOfData) {
						
			this.StartDataIndex -= this.MaxNumOfItem;
			this.CurPage--;
			
			//from 1st page to last page
			if(this.StartDataIndex < 0 ) {
			
				var NumOfDataInLastPage = this.MaxNumOfData % this.MaxNumOfItem;
				
				if(NumOfDataInLastPage > 0) {
					
					this.StartDataIndex = this.MaxNumOfData - NumOfDataInLastPage;
				}
				else {
					
					this.StartDataIndex = this.MaxNumOfData - this.MaxNumOfItem;
				}
												
				this.CurrentDataIndex = this.MaxNumOfData - 1;
				this.CurPage = this.TotalPage;
			}		
			
			//Set NumOfVisibleItem
			this.UpdatePage();
			//Set CurItemIndex after call UpdatePage
			CurItemIndex = this.NumOfVisibleItem - 1;	
				
		
		}
		//case2 : Single page
		else
		{
			CurItemIndex = this.NumOfVisibleItem - 1;
			this.CurrentDataIndex = CurItemIndex;
		}				
	}
	
	alert("CurrentDataIndex = " + this.CurrentDataIndex);		
	this.SetCurrentItem(CurItemIndex);		
}

ScrollList.prototype.Next2 = function() {
	alert("ScrollList.Next2()");
alert("befor CurrentDataIndex = " + this.CurrentDataIndex);	
	var CurItemIndex = 	this.CurrentItemIndex + 1;
	if(this.StartDataIndex+CurItemIndex>=this.MaxNumOfData){
		if(this.NextCallBack) {
			this.NextCallBack();
		}
		return;
	}
	this.CurrentDataIndex++;
	
	//to next page	
	if(CurItemIndex == this.NumOfVisibleItem ) {
		
		alert("move to top");
		CurItemIndex = 0;		
			
		//case: Multi Page 
		if(this.MaxNumOfItem <  this.MaxNumOfData ) 
		{
			alert("Page scroll ");
			this.StartDataIndex += this.MaxNumOfItem;
			this.CurPage++;
			
			//from last page to 1st page 
			if(this.StartDataIndex > this.MaxNumOfData -1 ) {
		
				this.StartDataIndex = 0;
				this.CurrentDataIndex = 0;
				this.CurPage = 1;
			}
			
			this.UpdatePage();						
		}
		else
		{
			this.CurrentDataIndex = 0;
		}		
	}

	alert("after CurrentDataIndex = " + this.CurrentDataIndex);		
	this.SetCurrentItem(CurItemIndex);	

}

ScrollList.prototype.Previous2 = function() {
	alert("ScrollList.Previous2()");
	alert("befor CurrentDataIndex = " + this.CurrentDataIndex);	
	
	var CurItemIndex = 	this.CurrentItemIndex - 1;
	//set data index
	
	if(this.StartDataIndex+CurItemIndex<0){
		if(this.PreviousCallBack) {
			this.PreviousCallBack();
		}
		return;
	}
	
	this.CurrentDataIndex--;
			
	if(CurItemIndex == -1 ) {				
		alert("Page scroll");
		
		//case1 : Multi Page 
		if (this.MaxNumOfItem < this.MaxNumOfData) {
						
			this.StartDataIndex -= this.MaxNumOfItem;
			this.CurPage--;
			
			//from 1st page to last page
			if(this.StartDataIndex < 0 ) {
			
				var NumOfDataInLastPage = this.MaxNumOfData % this.MaxNumOfItem;
				
				if(NumOfDataInLastPage > 0) {
					
					this.StartDataIndex = this.MaxNumOfData - NumOfDataInLastPage;
				}
				else {
					
					this.StartDataIndex = this.MaxNumOfData - this.MaxNumOfItem;
				}
												
				this.CurrentDataIndex = this.MaxNumOfData - 1;
				this.CurPage = this.TotalPage;
			}		
			
			//Set NumOfVisibleItem
			this.UpdatePage();
			//Set CurItemIndex after call UpdatePage
			CurItemIndex = this.NumOfVisibleItem - 1;	
				
		
		}
		//case2 : Single page
		else
		{
			CurItemIndex = this.NumOfVisibleItem - 1;
			this.CurrentDataIndex = CurItemIndex;
		}				
	}
	
	alert("after CurrentDataIndex = " + this.CurrentDataIndex);		
	this.SetCurrentItem(CurItemIndex);		
}
//only update page.. inner function
ScrollList.prototype.UpdatePage = function() {

	alert("ScrollList.UpdatePage(" + this.StartDataIndex + ")");
	//alert("MaxNumOfData = " + this.MaxNumOfData);

	var temp;
	var DataSetCount = 0;

	//MaxNumOfItem
	for(var i = 0; i < this.NumOfCreatedItem; i++) {
		temp = this.StartDataIndex + i;

		if(temp > (this.MaxNumOfData - 1)) {

			this.ItemArray[i].SetFlagForExistData(false);
			//alert("SetFlagForExistData[" + i + "]");
		} else {
			this.ItemArray[i].SetItem(this.DataArray[temp]);
			DataSetCount++;
			//alert("SetItem[" + i + "]");
		}
	}

	this.NumOfVisibleItem = DataSetCount;

	this.ShowItem();

	this.ScrollBar.SetTotalNum(this.TotalPage);
	this.ScrollBar.SetCurrentNum(this.CurPage);
	this.ScrollBar.ResetScrollBar();

}
//refresh current page
//after changing data....
ScrollList.prototype.RefreshPage = function() {
	alert("ScrollList.RefreshPage");
	/*

	 <<page>>
	 this.StartDataIndex

	 <<scroll>>
	 this.CurPage

	 <<highliht>>
	 this.CurrentDataIndex
	 this.CurrentItemIndex

	 */

	if(this.MaxNumOfData == 0) {
		this.ShowItem();
		this.ScrollBar.SetTotalNum(this.TotalPage);
		this.ScrollBar.SetCurrentNum(this.CurPage);
		this.ScrollBar.ResetScrollBar();
	} else {

		//1.Set StartDataIndex
		if(this.StartDataIndex > this.MaxNumOfData - 1) {
			this.StartDataIndex = (this.TotalPage - 1) * this.MaxNumOfItem;
		}

		//2.this.CurPage
		if(this.CurPage > this.TotalPage) {
			this.CurPage = this.TotalPage;
		} else if(this.CurPage == 0) {
			this.CurPage = 1;
		}

		//check Invalid
		var FirstIndexOfCurPage = (this.CurPage - 1) * this.MaxNumOfItem;
		if(this.StartDataIndex != FirstIndexOfCurPage) {

			alert("Error StartDataIndex!!!!");
			this.StartDataIndex = FirstIndexOfCurPage;
		}

		//3. highliht
		//auto set info when focusing list
		/*
		if(this.CurrentDataIndex > this.MaxNumOfData - 1)
				{
					
					this.CurrentDataIndex = this.MaxNumOfData - 1;
					this.CurrentItemIndex = this.MaxNumOfItem-1;
					//this.StartDataIndex = this.CurrentDataIndex;
				}*/
		

		this.UpdatePage();
	}

}

ScrollList.prototype.SetCurPage = function(page) {

	this.CurPage = page;
}

ScrollList.prototype.GetCurPage = function() {

	return this.CurPage;
}

ScrollList.prototype.PrintListInfo = function() {

	alert("######################################");
	alert("Print Information of Item");
	alert("######################################");
	alert("NumOfVisibleItem = " + this.NumOfVisibleItem);
	alert("MaxNumOfItem = " + this.MaxNumOfItem);
	alert("NumOfCreatedItem = " + this.NumOfCreatedItem);
	alert("CurrentItemIndex = " + this.CurrentItemIndex);
	alert("PrevItemIndex = " + this.PrevItemIndex);

	alert("######################################");
	alert("Print Information of Data");
	alert("######################################");
	alert("MaxNumOfData = " + this.MaxNumOfData);
	alert("CurrentDataIndex = " + this.CurrentDataIndex);
	alert("StartDataIndex = " + this.StartDataIndex);

	alert("######################################");
	alert("Print Information of Page");
	alert("######################################");
	alert("TotalPage = " + this.TotalPage);
	alert("CurPage = " + this.CurPage);

}

ScrollList.prototype.SetLastPageInfo = function() {

	alert("ScrollList.SetLastPageInfo()");

	var newPositionItem = -1;
	//1. Set StartDataIndex
	alert("1. MaxNumOfData = " + this.MaxNumOfData);
	alert("1. MaxNumOfItem = " + this.MaxNumOfItem);
	var NumOfDataInLastPage = this.MaxNumOfData % this.MaxNumOfItem;

	if(NumOfDataInLastPage == 0) {
		NumOfDataInLastPage = this.MaxNumOfItem;
	}

	var lastItemIndex = NumOfDataInLastPage - 1;

	this.StartDataIndex = this.MaxNumOfData - NumOfDataInLastPage;

	alert("2. NumOfDataInLastPage = " + NumOfDataInLastPage);
	alert("2. StartDataIndex = " + this.StartDataIndex);

	//2. Set CurrentDataIndex
	if(this.CurrentItemIndex > lastItemIndex) {
		//over index
		this.CurrentDataIndex = this.MaxNumOfData - 1;
		newPositionItem = lastItemIndex;
		alert("3. CurrentDataIndex = " + this.CurrentDataIndex);
		alert("3. newPositionItem = " + newPositionItem);
	} else {
		//under index
		this.CurrentDataIndex = this.StartDataIndex + lastItemIndex;
		alert("3. CurrentDataIndex = " + this.CurrentDataIndex);
	}

	return newPositionItem;

}
//down - next
ScrollList.prototype.NextPage = function() {
	alert("ScrollList.NextPage()");

	if((this.TotalPage < 2) || (this.CurPage >= this.TotalPage)) {
		alert("TotalPage < 2 or (this.CurPage >= this.TotalPage)");
		return;
	}

	var CurItemPos = -1;
	var BackupCurPage = this.CurPage;

	//Change Value!!
	this.CurrentDataIndex += this.MaxNumOfItem;
	this.StartDataIndex += this.MaxNumOfItem;
	this.CurPage++;

	if(BackupCurPage == this.TotalPage) {
		//1. Set CurPage
		this.CurPage = 1;

		//2. Set StartDataIndex
		this.StartDataIndex = 0;

		//3. Set CurrentDataIndex
		this.CurrentDataIndex = this.CurrentItemIndex;
		alert("first page >> CurrentDataIndex = " + this.CurrentDataIndex);

	} else if(BackupCurPage == this.TotalPage - 1) {
		CurItemPos = this.SetLastPageInfo();
	}

	//Set NumOfVisibleItem
	this.UpdatePage();

	if(CurItemPos > -1) {
		this.SetCurrentItem(CurItemPos);
	}

}
//up - previous
ScrollList.prototype.PreviousPage = function() {
	alert("ScrollList.PreviousPage()");

	if((this.TotalPage < 2) || (this.CurPage <= 1)) {
		alert("TotalPage < 2 or (this.CurPage <= 1)");
		return;
	}

	var CurItemPos = -1;
	var BackupCurPage = this.CurPage;

	//Change Value!!
	this.CurrentDataIndex -= this.MaxNumOfItem;
	this.StartDataIndex -= this.MaxNumOfItem;
	this.CurPage--;

	if(BackupCurPage == 1) {

		// Set CurPage
		this.CurPage = this.TotalPage;
		CurItemPos = this.SetLastPageInfo();
	}

	//Set NumOfVisibleItem
	this.UpdatePage();

	if(CurItemPos > -1) {
		this.SetCurrentItem(CurItemPos);
	}
}
ScrollList.prototype.SetCirculation = function(flag) {
	this.IsCirculation = flag;
}
ScrollList.prototype.NextAtLastItemCallBack = function(func) {
	if(func)
		this.NextCallBack = func;
}
ScrollList.prototype.PreviousAtFirstItemCallBack = function(func) {
	if(func)
		this.PreviousCallBack = func;
}

