/**
 * @author donghee80.lee
 */

var SCROLLBAR_SHAPE_STYLE = {

	HORIZONTAL : 0,
	VERTICAL : 1,
}

 
function ScrollBar() {

	this.FirstLayerIndex = -1;
	this.SegmentLayerIndex = -1;
	this.LastLayerIndex = -1;

	this.ShapeStyle = SCROLLBAR_SHAPE_STYLE.VERTICAL;

	this.TotalNum = 0;
	this.CurrentNum = 0;

	this.StartPosition = 0;
	this.FirstLength = 0;
	this.TotalLength = 0;

	this.objId = null;

	CompBase.call(this);
}

ScrollBar.prototype = new CompBase();

ScrollBar.prototype.Create = function(eName) {

	alert("ScrollBar.Create()");

	// added 7.30
	this.objId = eName;

	this.CreateBG(eName)

	var ElementStr;
		
	ElementStr = eName + "First";
	this.FirstLayerIndex = this.LayerArray.length;
	this.AddImageLayer(ElementStr);

	ElementStr = eName + "Segment";
	this.SegmentLayerIndex = this.LayerArray.length;
	this.AddImageLayer(ElementStr);

	ElementStr = eName + "Last";
	this.LastLayerIndex = this.LayerArray.length;
	this.AddImageLayer(ElementStr);

}

ScrollBar.prototype.SetShapeStyle = function(nShapeStyle) {

	this.ShapeStyle = nShapeStyle;
}


ScrollBar.prototype.SetPosition = function(totalNum, TotalLength_, StartPosition_, FirstLength_) {

	this.TotalNum = totalNum;
	this.CurrentNum = 1;
	this.TotalLength = TotalLength_;
	this.StartPosition = StartPosition_;
	this.FirstLength = FirstLength_;
}


ScrollBar.prototype.SetTotalNum = function(nTotalNum) {

	this.TotalNum = nTotalNum;
}


ScrollBar.prototype.SetCurrentNum = function(nCurNum) {

	this.CurrentNum = nCurNum;

	//alert("ScrollBar.CurrentNum = " + nCurNum);
}


ScrollBar.prototype.SetStartPosition = function(nPos) {

	this.StartPosition = nPos;

}

ScrollBar.prototype.SetFirstLength = function(nLength) {

	this.FirstLength = nLength;

}


ScrollBar.prototype.SetTotalLength = function(nLength) {

	this.TotalLength = nLength;

}

ScrollBar.prototype.ResetScrollBar = function() {

	alert("ScrollBar.ResetScrollBar()");

	if(this.TotalNum > 1)
	{
		//set block length
		var BlockLengh = parseInt(this.TotalLength / this.TotalNum);

		var RestLength;
		var adjustRest;
		var FirstPosition;
		var SegmentPositon;
		var SegmentLength;
		var LastPosition;

		//BlockLengh = this.FirstLength * 2 + 2;
		if(BlockLengh < this.FirstLength * 2 + 2) {
			
			BlockLengh = this.FirstLength * 2 + 2;

			var MtotalLength = this.TotalLength - BlockLengh;
			var MblockLength = parseInt(MtotalLength / (this.TotalNum - 1));
			
			RestLength = parseInt(MtotalLength % (this.TotalNum - 1));
			adjustRest = parseInt(RestLength * this.CurrentNum / this.TotalNum);

			//first
			FirstPosition = this.StartPosition + MblockLength * (this.CurrentNum - 1);
			FirstPosition += adjustRest;

			//segment
			SegmentPositon = FirstPosition + this.FirstLength;
			SegmentLength = 2;

			//last
			LastPosition = FirstPosition + this.FirstLength + SegmentLength;

		}
		else {

			RestLength = parseInt(this.TotalLength % this.TotalNum);
			adjustRest = parseInt(RestLength * this.CurrentNum / this.TotalNum);

			//first
			FirstPosition = this.StartPosition + BlockLengh * (this.CurrentNum - 1);
			FirstPosition += adjustRest;

			//segment
			SegmentPositon = FirstPosition + this.FirstLength;
			SegmentLength = BlockLengh - this.FirstLength * 2;

			//last
			LastPosition = FirstPosition + this.FirstLength + SegmentLength;
		}

		/*
		 alert("BlockLengh = " + BlockLengh);
		 alert("adjustRest = " + adjustRest);
		 alert("FirstPosition = " + FirstPosition);
		 alert("SegmentPositon = " + SegmentPositon);
		 alert("SegmentLength = " + SegmentLength);
		 alert("LastPosition = " + LastPosition);
		 */

		if(this.ShapeStyle == SCROLLBAR_SHAPE_STYLE.VERTICAL) {
			$("#" + this.LayerArray[this.FirstLayerIndex].Obj).css('top', FirstPosition + 'px');
			$("#" + this.LayerArray[this.SegmentLayerIndex].Obj).css('top', SegmentPositon + 'px');
			$("#" + this.LayerArray[this.SegmentLayerIndex].Obj).css('height', SegmentLength + 'px');
			$("#" + this.LayerArray[this.LastLayerIndex].Obj).css('top', LastPosition + 'px');
		}
		else{
			$("#" + this.LayerArray[this.FirstLayerIndex].Obj).css('left', FirstPosition + 'px');
			$("#" + this.LayerArray[this.SegmentLayerIndex].Obj).css('left', SegmentPositon + 'px');
			$("#" + this.LayerArray[this.SegmentLayerIndex].Obj).css('width', SegmentLength + 'px');
			$("#" + this.LayerArray[this.LastLayerIndex].Obj).css('left', LastPosition + 'px');
		}


		if(this.Showstate == false)
		{
			this.Show();
		}
	}
	else{
		alert("ResetScrollBar is TotalNum <= 1 ");

		if(this.Showstate == true) {
			//		alert("111111ResetScrollBar Hide");
			this.Hide();
		}
	}

}


ScrollBar.prototype.SetFirstImage = function(ImagePath) {

	this.LayerArray[this.FirstLayerIndex].SetImage(ImagePath);

}

ScrollBar.prototype.SetSegmentImage = function(ImagePath) {

	this.LayerArray[this.SegmentLayerIndex].SetImage(ImagePath);

}

ScrollBar.prototype.SetLastImage = function(ImagePath) {

	this.LayerArray[this.LastLayerIndex].SetImage(ImagePath);

}
//added 7.30
// modify 8.20
ScrollBar.prototype.SetBGImage = function(first, segment, last) {
	if(this.ShapeStyle == SCROLLBAR_SHAPE_STYLE.VERTICAL) {
		$("#" + this.objId).append("<div id='" + this.objId + "_first_bg'></div><div id='" + this.objId + "_segment_bg'></div><div id='" + this.objId + "_last_bg'></div>");	
		var width = $("#" + this.objId).css('width');
		var scroll_width = parseInt(width.substring(0, width.indexOf("px")))+1;
		$("#" + this.objId + "_first_bg").css('width', scroll_width+"px");
		$("#" + this.objId + "_segment_bg").css('width', scroll_width+"px");
		$("#" + this.objId + "_last_bg").css('width', scroll_width+"px");

		$("#" + this.objId + "_first_bg").css('height', this.FirstLength+"px");

		$("#" + this.objId + "_segment_bg").css('top', this.FirstLength+"px");
		$("#" + this.objId + "_segment_bg").css('height', this.TotalLength - (this.FirstLength * 2) +"px");

		$("#" + this.objId + "_last_bg").css('top', this.TotalLength - this.FirstLength +"px");
		$("#" + this.objId + "_last_bg").css('height', this.FirstLength+"px");
		
		$("#" + this.objId + "_first_bg").css("background-image", "url('" + first + "')");
		$("#" + this.objId + "_segment_bg").css('background-image', "url('" + segment + "')");
		$("#" + this.objId + "_last_bg").css('background-image', "url('" + last + "')");

	} else if(this.ShapeStyle == SCROLLBAR_SHAPE_STYLE.HORIZONTAL) {
		$("#" + this.objId + "_BG_First").css("background-image", "url('" + first + "')");
		$("#" + this.objId + "_BG_Segment").css('background-image', "url('" + segment + "')");
		$("#" + this.objId + "_BG_Last").css('background-image', "url('" + last + "')");
	}
	
}