/*------------------------------------------------------
cd_scalebox
Add vertical scroll Bar to div Elements(given ID)

version		0.81(Beta)
build date	2009/07/01
author		Yoshihisa Kato/YAH!-cyberstudio
Copyright	2009 YAH!-cyberstudio. All rights reserved.
-------------------------------------------------------*/

/*Global variable*/
var CD_mouseX=0;	//mouse X position
var CD_mouseY=0;	//mouse Y position
var CD_scaleBoxObj=null;	//exec scale box based object ID

document.onmousemove=function(e){
	var m=new CD_coreFunc._getWindowAttr(e);
	CD_mouseX=m._mouseX(e);
	CD_mouseY=m._mouseY(e);
	CD_scalebox.dragScroll(e,CD_scaleBoxObj);
}

document.onmouseup=function(){
	CD_scalebox.stopDragScroll(CD_scaleBoxObj);
}

/*Global variable*/
var scrollY=0;
var scrollSpeed=4;
var plusScrollEvent;
var minusScrollEvent;
var dragMinHeight=0;
var dragMaxHeight=0;

var CD_scalebox={

	Setting:function(){
		this.viewAreaWidth=265;
		this.viewAreaHeight=260;

		this.arrowUp="image/arrow_up.gif";
		this.arrowDw="image/arrow_dw.gif";
		this.arrowWidth=15;
		this.arrowHeight=15;

		this.scrollBar="image/scrollbar.gif";
		this.scrollBarWidth=15;
		this.scrollBarHeight=230;

		this.scrollButton="image/dragbutton.gif";
		this.scrollButtonWidth=15;
		this.scrollButtonHeight=15;
		this.withinAreaBar=5;
	},

	initObj:function(obj){
		CD_scaleBoxObj='';
		this.boxViewArea[obj]='';
		this.boxContent[obj]='';
		this.scrollBar[obj]='';
		this.scrollArrowU[obj]='';
		this.scrollArrowB[obj]='';
		this.scrollArea[obj]='';
		this.scrollDrag[obj]='';
//		this.resizeBar[obj]='';
	},

	getObj:function(obj){
		CD_scaleBoxObj=obj;
		this.boxViewArea[obj]=new CD_coreFunc._getObjPos(('scaleBox-view'+obj),0,0,0,0,0);
		this.boxContent[obj]=new CD_coreFunc._getObjPos(('scaleBox-content'+obj),0,0,0,0,0);
		this.scrollBar[obj]=new CD_coreFunc._getObjPos(('scaleBox-scrollBar'+obj),0,0,0,0,0);
		this.scrollArrowU[obj]=new CD_coreFunc._getObjPos(('scaleBox-scrollArrowU'+obj),0,0,0,0,0);
		this.scrollArrowB[obj]=new CD_coreFunc._getObjPos(('scaleBox-scrollArrowB'+obj),0,0,0,0,0);
		this.scrollArea[obj]=new CD_coreFunc._getObjPos(('scaleBox-scrollDragArea'+obj),0,0,0,0,0);
		this.scrollDrag[obj]=new CD_coreFunc._getObjPos(('scaleBox-scrollDragButton'+obj),0,0,0,0,0);
//		this.resizeBar[obj]=new CD_coreFunc._getObjPos(('scaleBox-resizebar'+obj),0,0,0,0,0);
	},

	Config:new Array(),

	boxViewArea:new Array(),
	boxContent:new Array(),
	scrollBar:new Array(),
	scrollArrowU:new Array(),
	scrollArrowB:new Array(),
	scrollArea:new Array(),
	scrollDrag:new Array(),
	resizeBar:new Array(),

	scrollStartX:new Array(),
	scrollStartY:new Array(),
	scrollAreaHeight:new Array(),

	Set:function(obj,setting){

		if(!setting){
			this.Config[obj]=new this.Setting();
		}else{
			this.Config[obj]=setting;
		}

		var baseCode=$(obj).innerHTML;
		var minHeight=this.Config[obj].scrollButtonHeight;
		var maxHeight=this.Config[obj].scrollBarHeight;
		var boxCode='';

		var boxViewArea_style="width:"+this.Config[obj].viewAreaWidth+"px;height:"+this.Config[obj].viewAreaHeight+"px;overflow:hidden;position:relative;float:left;display:inline;";
		var boxContent_style="width:"+this.Config[obj].viewAreaWidth+"px;position:absolute";
		var scrollBar_style="width:"+this.Config[obj].scrollBarWidth+"px;height:"+this.Config[obj].viewAreaHeight+"px;margin-left:"+this.Config[obj].withinAreaBar+"px;padding:0px;float:right;display:inline;";
		var scrollArrowU_style="width:"+this.Config[obj].arrowWidth+"px;height:"+this.Config[obj].arrowHeight+"px;cursor:pointer;background-image:url("+this.Config[obj].arrowUp+");background-position:bottom;background-repeat:no-repeat;";
		var scrollArrowB_style="width:"+this.Config[obj].arrowWidth+"px;height:"+this.Config[obj].arrowHeight+"px;cursor:pointer;background-image:url("+this.Config[obj].arrowDw+");background-position:bottom;background-repeat:no-repeat;";
		var scrollDragArea_style="width:"+this.Config[obj].scrollBarWidth+"px;height:"+this.Config[obj].scrollBarHeight+"px;-moz-user-select:none;-khtml-user-select:none;user-select:none;;background-image:url("+this.Config[obj].scrollBar+");background-repeat:repeat-y;";
		var scrollDragButton_style="width:"+this.Config[obj].scrollButtonWidth+"px;height:"+this.Config[obj].scrollButtonHeight+"px;background-image:url("+this.Config[obj].scrollButton+");background-position:bottom;background-repeat:no-repeat;font-size:1px;line-height:1;cursor:pointer;position:relative;";

		boxCode+='<div id="scaleBox-view'+obj+'" style="'+boxViewArea_style+'" class="scaleBoxView">';
		boxCode+='<div id="scaleBox-content'+obj+'" style="'+boxContent_style+'" class="scaleBoxContent">'+baseCode+'</div></div>';

		boxCode+='<div id="scaleBox-scrollBar'+obj+'" style="'+scrollBar_style+'" class="scaleBoxScroll">';
		boxCode+='<div id="scaleBox-scrollArrowU'+obj+'" style="'+scrollArrowU_style+'" onmousedown="CD_scalebox.setminusScroll(\''+obj+'\','+minHeight+','+maxHeight+');" onmouseup="CD_scalebox.resetminusScroll(\''+obj+'\');"></div>';
		boxCode+='<div id="scaleBox-scrollDragArea'+obj+'" style="'+scrollDragArea_style+'" unselectable="on" onSelectStart="return false;">';
		boxCode+='<div id="scaleBox-scrollDragButton'+obj+'" style="'+scrollDragButton_style+'" onmousedown="CD_scalebox.startDragScroll(\''+obj+'\','+minHeight+','+maxHeight+')"></div></div>';
		boxCode+='<div id="scaleBox-scrollArrowB'+obj+'" style="'+scrollArrowB_style+'" onmousedown="CD_scalebox.setplusScroll(\''+obj+'\','+minHeight+','+maxHeight+');" onmouseup="CD_scalebox.resetplusScroll(\''+obj+'\');"></div>';
		boxCode+='</div><div class="clear"></div>';
/*
<!--
<div id="scaleBox-resizebarSg" style="width:285px;height:15px;font-size:1px;line-height:1;background-image:url(image/resizebutton.gif);background-position:bottom right;background-repeat:no-repeat;background-color:#d5d5d5;cursor:se-resize;border-top:solid 1px #fff;clear:both;" onmousedown="CD_startResize('Sg')" onmouseup="CD_stopResize('Sg')"></div>
-->
*/
//		alert(boxCode);
		$(obj).innerHTML=boxCode;
	},

	startDragScroll:function(obj,min,max){
		this.getObj(obj);

		this.scrollStartX[obj]=0;
		this.scrollStartY[obj]=CD_mouseY;

		dragMinHeight=min;
		dragMaxHeight=max;

		this.getStartPos(obj);
	},

	stopDragScroll:function(obj){
		this.initObj(obj);
	},

	dragScroll:function(e,obj){
		if(obj){
			var moveY=this.scrollDrag[obj]._height+(CD_mouseY-this.scrollStartY[obj]);

			if(moveY>=dragMinHeight && moveY<=dragMaxHeight){
				CD_coreFunc._setObjPos(this.scrollDrag[obj]._name,"height",moveY);
				CD_coreFunc._setObjPos(this.boxContent[obj]._name,"y",Math.floor(((moveY-dragMinHeight)/(dragMaxHeight-dragMinHeight))*this.scrollAreaHeight[obj],0));
			}else{
				if(moveY<dragMinHeight){
					CD_coreFunc._setObjPos(this.scrollDrag[obj]._name,"height",dragMinHeight);
					CD_coreFunc._setObjPos(this.boxContent[obj]._name,"y",0);
				}
				if(moveY>dragMaxHeight){
					CD_coreFunc._setObjPos(this.scrollDrag[obj]._name,"height",dragMaxHeight);
					CD_coreFunc._setObjPos(this.boxContent[obj]._name,"y",this.scrollAreaHeight[obj]);
				}
			}
		}
	},

	getStartPos:function(obj){
		this.scrollAreaHeight[obj]=this.boxViewArea[obj]._height-($(this.boxContent[obj]._name).clientHeight||$(this.boxContent[obj]._name).offsetHeight);
		var scrollY=parseInt($(this.boxContent[obj]._name).style.top);
		scrollY=(!scrollY)?0:scrollY;
		return scrollY;
	},

	plusScroll:function(obj){
		var scrollY=this.getStartPos(obj);	
	//	if(parseInt($(this.boxContent[obj]._name).style.top.replace(/px/,''))>this.scrollAreaHeight[obj]){
			scrollY-=scrollSpeed;
			if(scrollY<this.scrollAreaHeight[obj] && scrollY<0){
				CD_coreFunc._setObjPos(this.boxContent[obj]._name,"y",this.scrollAreaHeight[obj]);
				this.scrollDragButton(obj)
				this.resetplusScroll(obj);
			}else{
				CD_coreFunc._setObjPos(this.boxContent[obj]._name,"y",scrollY);
				this.scrollDragButton(obj)
			}
	//	}
	//	$("objPos").innerHTML=scrollY+"/"+this.scrollAreaHeight[obj]+"/"+objPosPer;
	},

	minusScroll:function(obj){
		var scrollY=this.getStartPos(obj);	
	//	if(parseInt($(this.boxContent[obj]._name).style.top.replace(/px/,''))<0){
			scrollY+=scrollSpeed;
			if(scrollY>0){
				CD_coreFunc._setObjPos(this.boxContent[obj]._name,"y",0);
				this.scrollDragButton(obj)
				this.resetminusScroll(obj);
			}else{
				CD_coreFunc._setObjPos(this.boxContent[obj]._name,"y",scrollY);
				this.scrollDragButton(obj)
			}
	//	}
	//	$("objPos").innerHTML=scrollY+"/"+this.scrollAreaHeight[obj]+"/"+objPosPer;
	},

	scrollDragButton:function(obj){
		var objPosPer=Math.floor((parseInt($(this.boxContent[obj]._name).style.top.replace(/px/,""))/this.scrollAreaHeight[obj])*100,0);
		objPosPer=(objPosPer/100>=1)?1:objPosPer/100;
		CD_coreFunc._setObjPos(this.scrollDrag[obj]._name,"height",Math.floor(((dragMaxHeight-dragMinHeight)*objPosPer),0)+dragMinHeight);
	},

	setplusScroll:function(obj,min,max){
		this.getObj(obj);
		dragMinHeight=min;
		dragMaxHeight=max;
		
		startScroll=function(obj){
			CD_scalebox.plusScroll(obj);
		}
		plusScrollEvent=setInterval("startScroll('"+obj+"')",100);
	},

	setminusScroll:function(obj,min,max){
		this.getObj(obj);
		dragMinHeight=min;
		dragMaxHeight=max;

		startScroll=function(obj){
			CD_scalebox.minusScroll(obj);
		}
		minusScrollEvent=setInterval("startScroll('"+obj+"')",100);
	},

	resetplusScroll:function(obj){
		clearInterval(plusScrollEvent);
		this.initObj(obj);
	},

	resetminusScroll:function(obj){
		clearInterval(minusScrollEvent);
		this.initObj(obj);
	}
}

var CD_coreFunc={

	_getObjPos:function(objID,adjustX,adjustY,adjustW,adjustH,setStyle){
		obj=$(objID);

		this._name=objID;
		this._type=obj.type;

		if(obj.style.left){
			this._x=parseInt(obj.style.left.replace(/px/i,''));
		}else{
			this._x=obj.offsetLeft+adjustX;
			if(setStyle){obj.style.left=this._x;}
		}

		if(obj.style.top){
			this._y=parseInt(obj.style.top.replace(/px/i,''));
		}else{
			this._y=obj.offsetTop+adjustY;
			if(setStyle){obj.style.top=this._y;}
		}

		if(obj.style.width){
			this._width=parseInt(obj.style.width.replace(/px/i,''));
		}else{
			this._width=obj.offsetWidth+adjustW;
			if(setStyle){obj.style.width=this._width;}
		}

		if(obj.style.height){
			this._height=parseInt(obj.style.height.replace(/px/i,''));
		}else{
			this._height=obj.offsetHeight+adjustH;
			if(setStyle){obj.style.height=this._height;}
		}
	},

	_setObjPos:function(objID,Attr,Val){
		obj=$(objID);
		if(Attr == "x"){
			obj.style.left=obj.style.pixeLeft=Val+"px";
		}else if(Attr == "y"){
			obj.style.top=obj.style.pixelTop=Val+"px";
		}else if(Attr == "width"){
			obj.style.width=obj.style.pixelWidth=Val+"px";
		}else if(Attr == "height"){
			obj.style.height=obj.style.pixelHeight=Val+"px";
		}
	},


	_getWindowAttr:function(e){
		this._name=window.self.name;

		this._mouseX=function(e){
			if(navigator.appName == "Opera"){
				return e.clientX;
			}else if(navigator.appName == "Microsoft Internet Explorer"){
				return document.body.scrollLeft+event.clientX;
			}else{
				return e.pageX;
			}
		}
		this._mouseY=function(e){
			if(navigator.appName == "Opera"){
				return e.clientY;
			}else if(navigator.appName == "Microsoft Internet Explorer"){
				return document.body.scrollTop+event.clientY;
			}else{
				return e.pageY;
			}
		}

		this._scrollX=document.body.scrollLeft||document.documentElement.scrollLeft;
		this._scrollY=document.body.scrollTop||document.documentElement.scrollTop;
	}
}



function scaleBoxGetObjects(obj){


}



function $(ids){
	return document.getElementById(ids);
}

