//TCssScrollBar Object
function TCssScrollBar() {
	//Properties
	var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	this.scrollBar='';
	if (screen.height>800 ||(is_chrome && screen.height>=800)) this.scrollBackground='img/scrollBg_large.png';
	else this.scrollBackground='img/scrollBg.png';
	this.scrollButton='';
	this.scrollPosition=0;
	if (screen.height>800 || (is_chrome && screen.height>=800)) this.buttonImage='img/scrollButton_large.png';
	else this.buttonImage='img/scrollButton.png';
	this.scrollContent='';
	this.horizontal=false;
	this.startMousePosition='';
	this.oldMouseMove='';
	this.oldMouseUp='';
	this.scrollSize=0;
}

//Return the relative position of ScrollButton
TCssScrollBar.prototype.getScrollPosition=function() {
	return this.scrollPosition;
}

//Get Mouse Positions
TCssScrollBar.prototype.getMouse=function(e) {
	if (!e) e=window.event;
	return {x:(e.pageX||e.clientX + document.body.scrollLeft), y:(e.pageY||e.clientY + document.body.scrollTop)};
}

//ScrollBar Init
TCssScrollBar.prototype.init=function(_scrollContent,_scrollBar,_horizontal) {
	this.scrollContent=document.getElementById(_scrollContent);
	this.horizontal=_horizontal;
	//set scrollBar
	this.scrollBar=document.getElementById(_scrollBar);
	this.scrollBar.innerHTML='<img id="'+_scrollBar+'_button">';
	this.scrollBar.style.background='url('+this.scrollBackground+')';
	this.scrollBar.style.backgroundPosition='center';
	this.scrollBar.style.backgroundRepeat='no-repeat';
	this.scrollBar.style.display='';
	//set ScrollButton
	this.scrollButton=document.getElementById(_scrollBar+'_button');
	this.scrollButton.style.position='relative';
	this.scrollButton.src=this.buttonImage;
	this.scrollButton.style.cursor='pointer';
	this.scrollButton.style.display='';
	if (this.horizontal) this.scrollButton.align='center';
	else this.scrollButton.valign='center';
	//Set Event
	var obj=this;
	//Mouse Scroll event, only for first scrollbar
	if (!this.scrollContent.cssScrollBar && !this.horizontal) {
		if(window.addEventListener) this.scrollContent.addEventListener('DOMMouseScroll', function(e) {TCssScrollBar.prototype.mouseScroll(e,obj);return false;}, false);//Add Scrolling event to Firefox
		else this.scrollContent.onmousewheel =function(e) {TCssScrollBar.prototype.mouseScroll(e,obj)};
		this.scrollContent.cssScrollBar=true;
		this.scrollContent.cssScrollBarObject=this;
	}
	this.scrollButton.onmousedown=function(e) {TCssScrollBar.prototype.mouseDown(e,obj); return false;};
	
}
TCssScrollBar.prototype.mouseMove=function(e,obj) {
	var _pos=0;
	if (obj.horizontal) {//Horizontal
		//Get position
		_pos=obj.scrollPosition+TCssScrollBar.prototype.getMouse(e).x-obj.startMousePosition.x;
		//Validating position
		if (_pos>obj.scrollBar.offsetWidth-obj.scrollButton.offsetWidth) _pos=obj.scrollBar.offsetWidth-obj.scrollButton.offsetWidth;
		else if (_pos<0) _pos=0;
		//Set position
		obj.scrollButton.style.left=_pos;
	} else {//Vertical
		//Get position
		
		_pos=obj.scrollPosition+TCssScrollBar.prototype.getMouse(e).y-obj.startMousePosition.y;
		//Validating position
		if (_pos>obj.scrollBar.offsetHeight-obj.scrollButton.offsetHeight) _pos=obj.scrollBar.offsetHeight-obj.scrollButton.offsetHeight;
		else if (_pos<0) _pos=0;
		//Set position
		obj.scrollButton.style.top=_pos;
	}
	TCssScrollBar.prototype.scroll(_pos,obj);
	return false;
}

TCssScrollBar.prototype.mouseUp=function(e,obj) {
	var _pos=0;
	//Load original functions
	document.onmousemove=obj.oldMouseMove;
	document.onmouseup=obj.oldMouseUp;
	//Save position
	if (obj.horizontal) {
		//Get position 
		_pos=obj.scrollPosition+TCssScrollBar.prototype.getMouse(e).x-obj.startMousePosition.x;
		//Validating position
		if (_pos>obj.scrollBar.offsetWidth-obj.scrollButton.offsetWidth) _pos=obj.scrollBar.offsetWidth-obj.scrollButton.offsetWidth;
		else if (_pos<0) _pos=0;
		//Set position
		obj.scrollPosition=_pos;
	} else {
		//Get position
		_pos=obj.scrollPosition+TCssScrollBar.prototype.getMouse(e).y-obj.startMousePosition.y;
		//Validating position
		if (_pos>obj.scrollBar.offsetHeight-obj.scrollButton.offsetHeight) _pos=obj.scrollBar.offsetHeight-obj.scrollButton.offsetHeight;
		else if (_pos<0) _pos=0;
		//Set position
		obj.scrollPosition=_pos;
	}
	return false;
}

TCssScrollBar.prototype.mouseDown=function(e,obj) {
	//Save Current Mouse position
	obj.startMousePosition=TCssScrollBar.prototype.getMouse(e);
	//Save original functions
	obj.oldMouseMove=document.onmousemove;
	obj.oldMouseUp=document.onmouseup;
	//Set ScrollBar Functions
	document.onmousemove=function(e) {TCssScrollBar.prototype.mouseMove(e,obj);return false;};
	document.onmouseup=function(e) {TCssScrollBar.prototype.mouseUp(e,obj);return false;};
	return false;
}

//Set Background Image
TCssScrollBar.prototype.setBackground=function(src) {
	this.scrollBackground=src;
	this.scrollBar.style.background='url('+src+')';
	this.scrollBar.style.backgroundRepeat='no-repeat';
	this.scrollBar.style.backgroundPosition='center';
}

//Set Button Image
TCssScrollBar.prototype.setButton=function(src) {
	this.buttonImage=src;
	this.scrollButton.src=src;
}

//Scroll the content
TCssScrollBar.prototype.scroll=function(_pos,obj) {
	if (obj.horizontal) {
		obj.scrollContent.scrollLeft=(obj.scrollContent.scrollWidth-obj.scrollContent.offsetWidth)*_pos/(obj.scrollBar.offsetWidth-obj.scrollButton.offsetWidth);
	} else {
		obj.scrollContent.scrollTop=(obj.scrollContent.scrollHeight-obj.scrollContent.offsetHeight)*_pos/(obj.scrollBar.offsetHeight-obj.scrollButton.offsetHeight);
	}
}

//onmouse Scroll event
TCssScrollBar.prototype.mouseScroll=function(e,obj) {
	var delta = 0;
	if (!e) e = window.event;
	if (e.wheelDelta) {
		delta = e.wheelDelta / 120;
	} else if (e.detail) {
		delta = -e.detail / 3;
	}
	if (delta>0) {
		obj.scrollContent.scrollTop=obj.scrollContent.scrollTop-obj.scrollSize;
	} else if (delta<0) {
		obj.scrollContent.scrollTop=obj.scrollContent.scrollTop+obj.scrollSize;
	}
	obj.scrollContent.cssScrollBarObject.scrollPosition=obj.scrollContent.scrollTop/(obj.scrollContent.scrollHeight-obj.scrollContent.offsetHeight)*(obj.scrollBar.offsetHeight-obj.scrollButton.offsetHeight);
	obj.scrollContent.cssScrollBarObject.scrollButton.style.top=obj.scrollPosition+'px';
}

//Set ScrollSize
TCssScrollBar.prototype.setScrollSize=function(_size) {
	this.scrollSize=_size;
}
