//********************************************************
//********************************************************
//**
//** CLASS :: TSCROLLBOX
//**
//********************************************************
//********************************************************

function TScrollBox(){

	
	this.id_outer = "scrollbox_outer";
	this.id_inner = "scrollbox_inner";
	this.id_true = "scrollbox_true";

	this.rest = 50;
	this.maxspeed = 6;
	

	this.el_outer = null;
	this.el_inner = null;
	this.el_true = null;

	this.el_outer_offset = 0;
	this.el_outer_width = 0;
	this.el_true_width = 0;

	this.direction = "";
	this.speed = 0;
	this.x = 0;
	

	this.interval_id = null;
	this.ts = new Date().getTime();


	//-----------------------------------------------------------------------
	// getOffset (left/top)
	//-----------------------------------------------------------------------
	this.getOffset = function(el, dir){
		
		var offset = 0;
		var offset = (dir == "left") ? el.offsetLeft : el.offsetTop;
		var parent = el.offsetParent;

		while (parent!=null){
			offset = (dir == "left")? (offset+parent.offsetLeft) : (offset+parent.offsetTop);
			parent = parent.offsetParent;
		}

		return (offset);
	}


	//-----------------------------------------------------------------------
	// doInit
	//-----------------------------------------------------------------------

	this.doInit = function(){
						
		this.el_outer = document.getElementById(this.id_outer);

			
		this.el_outer_width = parseInt(this.el_outer.style.width);
		this.el_outer_offset = this.getOffset(this.el_outer, "left");
			
		this.el_inner = document.getElementById(this.id_inner);
		this.el_true = document.getElementById(this.id_true);
				

		var self = this;
		this.el_outer.onmousemove = function(e){ self.doOver(e); }
		this.el_outer.onmouseout = function(e){ self.doOut(e); }

		
	}

	//-----------------------------------------------------------------------
	// doOver
	//-----------------------------------------------------------------------

	this.doOver = function(e){

		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();

	
		
		var x = (window.event) ? (event.clientX) : ( (e.clientX) ? (e.clientX) : ("") );
		this.x = x;
		
		this.el_outer_width = parseInt(this.el_outer.style.width);
		this.el_outer_offset = this.getOffset(this.el_outer, "left");

		this.el_true_width = this.el_true.offsetWidth;
		
		x = x - this.el_outer_offset;
		x = x - document.body.scrollLeft;				


		var rbound = (this.el_outer_width/2)+this.rest;
		var lbound = (this.el_outer_width/2)-this.rest;

		if (this.direction==""){			

			clearInterval(this.interval_id);
			var self = this;
			this.interval_id = window.setInterval(function(){ self.doMove() },10);

		}

		

		if (x>rbound){
			
			this.direction = "right";
			this.speed = ((x - rbound)/((this.el_outer_width/2)-this.rest))*this.maxspeed;
			
		}else{

			if (x<lbound){
				
				this.direction = "left";
				this.speed = ((x - lbound)/((this.el_outer_width/2)-this.rest))*this.maxspeed;
			}else{
				this.doOut();
			}
		}
		

	}


	//-----------------------------------------------------------------------
	// doMove
	//-----------------------------------------------------------------------

	this.doMove = function(){

		this.ts = new Date().getTime();		

		if (this.direction == "right"){
			if (parseInt(this.el_inner.style.left)>-(this.el_true_width-this.el_outer_width)){
				this.el_inner.style.left=parseInt(this.el_inner.style.left)-this.speed+"px";
			}else{
				this.doOut();
			}			
		}

		if (this.direction == "left"){
			if (parseInt(this.el_inner.style.left)<0){
				this.el_inner.style.left=parseInt(this.el_inner.style.left)-this.speed+"px";				
			}else{				
				this.doOut();
			}
		}



	}


	//-----------------------------------------------------------------------
	// contains_ns6
	//-----------------------------------------------------------------------
	this.contains_ns6 = function(a, b) {
		while (b.parentNode) if ((b = b.parentNode) == a) return true;
		return false;
	}



	//-----------------------------------------------------------------------
	// doOut
	//-----------------------------------------------------------------------

	this.doOut = function(e){

		if (window.event){
	
			this.direction = "";
			clearInterval(this.interval_id);			

		}else{
	
			if (e && e.currentTarget){
				
				if (e.currentTarget!= e.relatedTarget && !this.contains_ns6(e.currentTarget, e.relatedTarget)){

					this.direction = "";
					clearInterval(this.interval_id);			

				}
				
			}

		}

	}
	


}


