// JavaScript Document
shallowCopy = new Array();
function myScrollbar(id, obj){
	this.id = id;
	this.width = obj.width;
	this.height = obj.height;
	this.speed = obj.speed;
	
	// imposta altezza e larghezza del div di contenuto
	$(this.id).style.width = this.width + 'px';
	$(this.id).style.height = this.height + 'px';
	
	// HTML scroller
	var html = '<div id="scrollerBar_' + id + '" class="myScollerBar" style="visibility: hidden; float: right; width: 13px; height: ' + this.height + 'px; padding: 0px;">';
	html += '<span id="up_' + id + '" class="myScrollerUp" style="display: block; width: 100%; height: 13px; overflow: hidden;"><span></span></span>';
	html += '<span id="trace_' + id + '" class="myScrollerTrace" style="display: block; position: relative; overflow: hidden; width: 100%; padding: 0px; height: ' + (this.height - 30) + 'px;"><span id="cursor_' + id + '" class="myScrollerCursor" style="display: block; width: 100%; padding: 0px; position: absolute; top: 0px; left: 0px;"><span>&nbsp;</span></span></span>';
	html += '<span id="down_' + id + '" class="myScrollerDown" style="display: block; width: 100%; height: 13px; overflow: hidden;"><span></span></span>';
	html += '</div>';
	
	// HTML container
	html += '<div class="myScrollerContainer" id="container_' + id + '" style="position: relative; width: ' + (this.width - 22) + 'px; height: ' + this.height + 'px; overflow: hidden; padding: 0px; float: left;">';
	html += '<div class="myScrollerContent" id="content_' + id + '" style="position: absolute; width: ' + (this.width - 22) + 'px; left: 0px; top: 0px; padding: 0px;">';
	html += $(this.id).innerHTML;
	html += '</div></div><br style="clear: both;" />';
	
	$(this.id).innerHTML = html; // inserisco tutto nel div di contenuto
	
	
	this.objContainer = $("container_" + id);
	this.objContent = $("content_" + id);
	this.objScroller = $("scrollerBar_" + id);
	this.objUp = $("up_" + id);
	this.objDown = $("down_" + id);
	this.objTrace = $("trace_" + id);
	this.objCursor = $("cursor_" + id);
	
	this.containerHeight = parseInt(this.objContainer.style.height);
	this.contentHeight = this.objContent.offsetHeight;
	
	if(this.contentHeight > this.containerHeight){
		this.traceHeight = parseInt(this.objTrace.style.height); // altezza scrollbar
		this.cursorHeight = Math.ceil((this.traceHeight * this.containerHeight) / this.contentHeight); // altezza cursore
		this.traceVoid = this.traceHeight - this.cursorHeight; // differenza tra altezza scrollbar e altezza cursore
		this.objCursor.style.height = this.cursorHeight + 'px'; // imposta altezza cursore
		
		this.objCursor.style.top = '0px';
		this.objScroller.style.visibility = 'visible';
		
		Drag.init(this.objCursor, null, 0, 0, 0, this.traceVoid); // imposto il drag del cursore
		
		this.t;
		
		shallowCopy[id] = Object.clone(this); // clone dell'oggetto
		
		// Events
		this.objUp.onmouseover = function(){
			shallowCopy[id].moveUp(shallowCopy[id]);
		}
		
		this.objDown.onmouseover = function(){
			shallowCopy[id].moveDown(shallowCopy[id]);
		}
		
		this.objUp.onmouseout = function(){
			clearTimeout(shallowCopy[id].t);
		}
		
		this.objDown.onmouseout = function(){
			clearTimeout(shallowCopy[id].t);
		}
		
		this.objCursor.onDrag = function(x,y) {
			var scrollY = parseInt(shallowCopy[id].objCursor.style.top);
			var docY = 0 - (scrollY * (shallowCopy[id].contentHeight - shallowCopy[id].containerHeight) / shallowCopy[id].traceVoid);
			shallowCopy[id].objContent.style.top = docY + "px";
		}
	}
}

myScrollbar.prototype.moveDown = function(objCopy){
	if(parseInt(objCopy.objContent.style.top) >= (objCopy.contentHeight * (-1) + objCopy.containerHeight)){
		var delTop = parseInt(objCopy.objContent.style.top) - parseInt(objCopy.speed);
		objCopy.objContent.style.top = delTop + "px";
		var addTop = (((0 - delTop) * objCopy.traceVoid) / (objCopy.contentHeight - objCopy.containerHeight));
		objCopy.objCursor.style.top = addTop + 'px';
		
	}
	objCopy.t = setTimeout(function(){ objCopy.moveDown(objCopy); }, 20);
}

myScrollbar.prototype.moveUp = function(objCopy){
	if(parseInt(objCopy.objContent.style.top) <= 0){
		var addTop = parseInt(objCopy.objContent.style.top) + parseInt(objCopy.speed);
		objCopy.objContent.style.top = addTop + "px";
		var delTop = (((0 - addTop) * objCopy.traceVoid) / (objCopy.contentHeight - objCopy.containerHeight));
		objCopy.objCursor.style.top = delTop + 'px';
	}
	objCopy.t = setTimeout(function(){ objCopy.moveUp(objCopy); }, 20);
}


charCount = 0;
function textEffect(id, text){
	len = text.length;
	
	if(charCount == 0) $(id).style.display = 'block';
	
	if(charCount > len - 1) $(id).toggle();
	else if(charCount == len - 1) $(id).innerHTML = text.substring(0, charCount + 1);
	else if(charCount < len) $(id).innerHTML = text.substring(0, charCount + 1) + '_';
	
	if(charCount < len + 6) charCount++;
	else charCount = 0;
	
	if(charCount < len + 6) t = setTimeout('textEffect(\''+id+'\',\''+text+'\')', 100);
	else t = setTimeout('textEffect(\''+id+'\',\''+text+'\')', 1500);
}