
//JS¹ö¶¯Ìõ:dansion.com,author:dansion,

function DScroller(id,w,css){
	that=this;
	var objDom=document.getElementById(id);
	if(!objDom.currentStyle){
		objDom.currentStyle=function(){
				return document.defaultView.getComputedStyle(objDom, null);
		}();
	}	
	var currentStyle=objDom.currentStyle;	
	var defaultOverflow=currentStyle.overflowY;
	var widthPadding=parseInt(currentStyle.paddingRight)+parseInt(currentStyle.paddingRight);
	objDom.style.overflow="hidden";
	var obj=objDom.cloneNode(true);
	var isdown=false,downY=0,downX=0,objDown=0,scrollWidth=w==null?15:w,scrollTop=0;
	var css=css==null?"scroller":css;
	var panelWidth=parseInt(obj.clientWidth);	
	var scroller=document.createElement("DIV");
		scroller.style.width=scrollWidth+"px";
		scroller.style.height=obj.clientHeight+"px";
		//scroller.style.background="#00f";
		scroller.style.position="relative";
		scroller.style.styleFloat="right";
		scroller.style.cssFloat="right";		
		scroller.className="scroller";
	var scrollbar=document.createElement("DIV");
		//scrollbar.style.background="#00f";
		scrollbar.style.width=scrollWidth+"px";
		scrollbar.style.position="relative";
		scrollbar.style.top="0px";
		scrollbar.className="scrollbar";
	
	var scrollbarTop=document.createElement("DIV");
		scrollbarTop.style.top="0px";
		scrollbarTop.style.position="absolute";
		//scrollbarTop.style.background="#f66";
		scrollbarTop.className="top";		
	var scrollbarEnd=document.createElement("DIV");
		//scrollbarEnd.style.background="#222";
		scrollbarEnd.style.position="absolute";
		scrollbarEnd.style.bottom="0px";
		scrollbarEnd.className="end";
		
	scrollbar.appendChild(scrollbarTop);
	scrollbar.appendChild(scrollbarEnd);
	scroller.appendChild(scrollbar);

	var panel=document.createElement("DIV");
		if(!panel.contains){
			panel.contains=function(tgt){
				while(tgt!=null&&typeof(tgt.tagName)!="undefind"){ 
					if(tgt==this){return true};
					tgt=tgt.parentNode;
				}   
				return false;   
			}
		}
		//alert(objDom.style.width);
		panel.style.width=currentStyle.width;
		panel.style.height=currentStyle.height;
		
		//alert(panel.style.height);
		//alert(panel.clientHeight);
		obj.appendChild(scroller);
		panel.appendChild(scroller);
		panel.className=obj.className;
		//alert(currentStyle.height);
		obj.style.height=currentStyle.height;
		obj.className="content";
		obj.style.borderWidth="0px";
		obj.style.padding="0px";
		obj.style.styleFloat="left";
		obj.style.cssFloat="left";
		//obj.style.background="#ff0";
		panel.style.overflowY=panel.style.overflowX="hidden";
		panel.appendChild(obj);
		if(objDom.replaceNode){
			objDom.replaceNode(panel);
		}else{
			objDom.parentNode.replaceChild(panel,objDom);
		}		
		initialize();	
	scroller.onmouseover=function(){scroller.style.cursor="default";}
	scroller.onmousedown=function(e){
		e=window.event?window.event:e;
		target=e.srcElement?e.srcElement:e.target;
		if(!scrollbar.contains){
			scrollbar.contains=function(tgt){
				while(tgt!=null&&typeof(tgt.tagName)!="undefind"){ 
					if(tgt==this){return true};
					tgt=tgt.parentNode;
				}   
				return false;   
			}
		}		
		if(scrollbar.contains(target)){
			isdown=true;
			scrollbar.className="scrollbarDown";
			downY=e.clientY-parseInt(scrollbar.style.top);
			downX=e.clientX;
		}else if(target==scroller){			
			var offsetY=e.offsetY?e.offsetY:e.layerY;
			//alert(offsetY<scrollTop);
			if(offsetY<scrollTop){
				scrollTop-=5;
			}else{
				scrollTop+=5;
			}			
			scrollbar.style.top=scrollTop+"px";
			obj.scrollTop=(obj.scrollHeight-obj.clientHeight)*parseInt(scrollbar.style.top)/(parseInt(scroller.style.height)-parseInt(scrollbar.style.height));
		}
	}
	scrollbar.onmouseup=function(){up();}
	scrollbar.onmouseover=function(e){if(!isdown){scrollbar.className="scrollbarOver"};};
	scrollbar.onmouseout=function(e){if(!isdown){scrollbar.className="scrollbar"};};
	scrollbar.onmousemove=function(e){			
		e=window.event?window.event:e;
		if(isdown){
			if(scrollbar.setCapture){
				scrollbar.setCapture();
				if(Math.abs(e.clientX-downX)<100){
					move(e);
				}
			}else if(window.captureEvents) {
				document.addEventListener("mousemove",move,true);
				document.addEventListener("mouseup",up,true);
				window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
            }			
		}
	}
	
	that.scrollNumber=new Number(0);
	//mouse wheel-----------------------------------------------
	function wheel(e){
		e =!e?window.event:e;
		if ( e.wheelDelta <= 0 || e.detail > 0) { 
			that.scrollNumber++;
		}else { 
			that.scrollNumber--; 
		}
		//alert(document.activeElement);
		if(panel.contains(document.activeElement)){
			
			scrollTo(that.scrollNumber);
			//alert(panel.id);
		}
		//window.status=n;
		//
	}
	if(document.attachEvent){
		document.attachEvent("onmousewheel",wheel);
	}else{
		window.addEventListener("DOMMouseScroll", wheel, false);
	}
	//end mouse wheel-----------------------------------------------------------------
	function move(e){		
		if(e.clientY-downY<0){
			scrollTop=0;
		}else if(e.clientY-downY>(parseInt(scroller.style.height)-parseInt(scrollbar.style.height))){
			scrollTop=parseInt(scroller.style.height)-parseInt(scrollbar.style.height);
		}else{
			scrollTop=e.clientY-downY
		}
		scrollbar.style.top=scrollTop+"px";
		obj.scrollTop=(obj.scrollHeight-obj.clientHeight)*parseInt(scrollbar.style.top)/(parseInt(scroller.style.height)-parseInt(scrollbar.style.height));
		that.scrollNumber=parseInt((scrollTop/(parseInt(scroller.style.height)-parseInt(scrollbar.style.height)))*100);
		//alert(scrollNumber);
	}
	function scrollTo(n){
		n=n>100?100:n;n=n<0?1:n;		
		scrollTop=parseInt((parseInt(scroller.style.height)-parseInt(scrollbar.style.height))/100*n);
		scrollbar.style.top=scrollTop+"px";
		obj.scrollTop=(obj.scrollHeight-obj.clientHeight)*parseInt(scrollbar.style.top)/(parseInt(scroller.style.height)-parseInt(scrollbar.style.height));
	}
	function up(e){
		isdown=false;
		scrollbar.className="scrollbar";
		if(scrollbar.releaseCapture){
			scrollbar.releaseCapture();
		}else{
		
		document.removeEventListener("mousemove",move,true);
		document.removeEventListener("mouseup",up,true);
		window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		}
	}
	if(panel.onresize){
		panel.onresize=function(){	initialize();}
	}else{
		window.onresize=function(){initialize();}
	}
	
	/*obj.onmousedownd=function(e){
		e=window.event?window.event:e;
		isdown=true;
		downY=e.clientY;
	}
	obj.onmousemove=function(e){
		e=window.event?window.event:e;		
		if(isdown){
			obj.setCapture();
			move(e);
		}
	}
	obj.onmouseup=function(e){
		isdown=false;
		if(obj.releaseCapture){
			obj.releaseCapture();
		}
	}*/
	obj.onpropertychange=initialize;
	if(obj.addEventListener && obj.nodeName.toLowerCase()=="textarea"){
		//alert("testa");
		//if)
		obj.addEventListener("input",test,false);
		obj.addEventListener("keyup",test,false);
	}
	function test(e){
		//alert("test");
		initialize();
	}
	function initialize(e){
		scroller.style.height=panel.style.height=obj.clientHeight+"px";
		var scrollBarHeihgt=(obj.clientHeight<obj.scrollHeight?parseInt(obj.clientHeight*obj.clientHeight/obj.scrollHeight):obj.clientHeight);
			scrollBarHeihgt=scrollBarHeihgt<(scrollWidth*2)?scrollWidth*2:scrollBarHeihgt;
		scrollBarHeihgt=scrollBarHeihgt%2?scrollBarHeihgt-1:scrollBarHeihgt;
		scrollbar.style.height=scrollBarHeihgt+"px";		
		if(parseInt(scrollbar.style.height)<obj.clientHeight){
			isShow(true);
		}else{
			isShow(false);
		}
		if((parseInt(scrollbar.style.top)+parseInt(scrollbar.style.height))>obj.clientHeight){
			scrollbar.style.top=obj.clientHeight-parseInt(scrollbar.style.height)+"px";
		}
		scrollbarEnd.style.width=scrollbarEnd.style.height=scrollbarTop.style.width=scrollbarTop.style.height=scrollWidth+"px";
	}
	obj.onscroll=function(){
		scrollbar.style.top=obj.scrollTop*(parseInt(scroller.style.height)-parseInt(scrollbar.style.height))/(obj.scrollHeight-obj.clientHeight)+"px";
	}
	
	function isShow(b){
		if(b){
			if(defaultOverflow!="hidden"){
				scrollbar.style.display="block";
				scroller.style.display="block";
				//alert(currentStyle.paddingRight);
				//alert(widthPadding);
				obj.style.width=(panel.clientWidth-scrollWidth-1-widthPadding)+"px";
			}else{
				scrollbar.style.display="none";
				scroller.style.display="none";
				obj.style.width=(panel.clientWidth-1)+"px";
			}
		}else{
			if(defaultOverflow!="scroll"){
				scrollbar.style.display="none";
				scroller.style.display="none";
				obj.style.width=(panel.clientWidth-1)+"px";
			}else{
				scrollbar.style.display="none";
				scroller.style.display="block";
				//alert(panel.clientWidth);
						
				obj.style.width=(parseInt(panel.style.width)-scrollWidth-1)+"px";
			}
		}
	}
	return {
		append:function(s){obj.innerHTML+=s;initialize()},
		panel:panel,
		refresh:initialize,
		width:function(n){obj.style.width=n+"px";initialize();},
		height:function(n){obj.style.height=n+"px";initialize();},
		scrollTo:scrollTo,
		content:obj,
		scroller:scroller
	};
}