

function $(id){ return document.getElementById(id); }

// die scrollbox 'klasse'
function ScrollBox(box_id,height,repeat){

	this.box  		= $(box_id);
	this.height 	= height;
	this.msg  		= new Array();
	this.time 		= new Array();
	this.buffer_use = 0;
	this.current_id = 0;
	this.last_id 	= 0;
	this.debug_mode = 0;
	this.debug_box 	= 0;
	this.block_next = 0;
	this.interval 	= 2000;
	this.speed    	= 30;
	this.user_mod   = 0;
	this.repeat     = repeat;

	this.msg.push('Init System .... OK');
	this.time.push(this.get_time());
	
	this.box.innerHTML = this.create_entry(0);
	
	if(this.debug_mode) this.debug('Inititalisierung OK');
}

ScrollBox.prototype.set_debug_mode = function(id){
	this.debug_mode = 1;
	this.debug_box  = $(id);
}


ScrollBox.prototype.set_speed = function(speed){
	this.speed    = speed;
}
ScrollBox.prototype.set_interval = function(interval){
	this.interval = interval;
}

ScrollBox.prototype.add_msg = function(html,usedate){

	if(this.user_mod){
		this.user_mod = 0;
		this.current_id = this.msg.length - 1;
	}
	
	if(this.debug_mode) this.debug('Neue Msg: ' + html);
	this.msg.push(html);
	if(usedate)
		this.time.push(this.get_time());
	else
		this.time.push('NULL');
	
	if(!this.buffer_use){
		if(this.current_id + 1 < this.msg.length){
			if(this.debug_mode) this.debug('nächste msg');
			this.next_msg(++this.current_id);
		}
		if(this.debug_mode) this.debug('Starte process_buffer');
		window.setTimeout(process_buffer,this.interval);
		this.buffer_use = 1;
	}
	
	var _this = this;
	function process_buffer(){
		if(!_this.block_next){
			if(_this.debug_mode) _this.debug('process_buffer:');
			if(_this.current_id + 1 < _this.msg.length){
				if(_this.debug_mode) _this.debug('nächste msg');
				_this.next_msg(++_this.current_id);
				window.setTimeout(process_buffer,_this.interval);
			} else {
				if(_this.repeat){
					_this.current_id = 1;
					_this.next_msg(_this.current_id);
					window.setTimeout(process_buffer,_this.interval);
				} else {
					if(_this.debug_mode) _this.debug('letze msg erreicht');
					_this.buffer_use = 0;
				}
			}
		} else {
			window.setTimeout(process_buffer,_this.speed);
		}
	}

}

ScrollBox.prototype.next_msg = function(id){
	this.block_next	= 1;
	if(id >= this.msg.length) return;
	if(this.debug_mode) this.debug('next_msg id: ' + id);
	
	// neue msg anhängen
	this.box.innerHTML = this.create_entry(id) + this.box.innerHTML;
	if(id == 0) $('msg_' + id).style.top = this.height + 'px;';
	var old_msg = $('msg_' + this.last_id);
	var new_msg = $('msg_' + id);
	this.last_id = id;
	var old_y   = 0;
	var new_y   = this.height; 
	var _this   = this;
	
	// blende starten
	var timer = window.setInterval(blend,this.speed);
	function blend(){
		old_y -= 5;
		new_y -= 5;
		old_msg.style.top = old_y + 'px';
		new_msg.style.top = new_y + 'px';
		if(new_y == 0){
			window.clearInterval(timer);
			_this.box.removeChild(old_msg);
			_this.block_next = 0;
		}
	}
}

ScrollBox.prototype.prev_msg = function(id){
	this.block_next	= 1;
	if(id >= this.msg.length) return;
	if(this.debug_mode) this.debug('next_msg id: ' + id);
	
	// neue msg anhängen
	this.box.innerHTML = this.create_entry(id) + this.box.innerHTML;
	$('msg_' + id).style.top = '-' + this.height + 'px';
	var old_msg = $('msg_' + this.last_id);
	var new_msg = $('msg_' + id);
	this.last_id = id;
	var old_y   = 0;
	var new_y   = -this.height; 
	var _this   = this;
	
	// blende starten
	var timer = window.setInterval(blend,this.speed);
	function blend(){
		old_y += 5;
		new_y += 5;
		old_msg.style.top = old_y + 'px';
		new_msg.style.top = new_y + 'px';
		if(new_y == 0){
			window.clearInterval(timer);
			_this.box.removeChild(old_msg);
			_this.block_next = 0;
		}
	}
}

ScrollBox.prototype.prev = function(){
	if(this.current_id > 0){
		if(!this.block_next){
			this.prev_msg(--this.current_id);
			this.user_mod = 1;
		}
	}
	
}
ScrollBox.prototype.next = function(){
	if(this.current_id + 1 < this.msg.length){
		if(!this.block_next){
			this.next_msg(++this.current_id);
			this.user_mod = 1;
		}
	}
}


ScrollBox.prototype.create_entry = function(id){

	if(id == 0) style = 'top:0px;';
	else	    style = '';

	var code;
	code  = '<div class="entry" id="msg_' + id + '" style="' + style + '">';
	if(this.time[id] != 'NULL')
		code += '<table><tr><th>' + this.time[id] + '</th><td><div>' + this.msg[id] + '</div></td></tr></table>';
	else
		code += '<table><tr><td><div>' + this.msg[id] + '</div></td></tr></table>';
	code += '</div>';
	return code;
}

ScrollBox.prototype.get_time = function(){

	var date = new Date();
	var dd   = date.getDate();
	var mm   = date.getMonth() + 1;
	var yy   = date.getYear() % 100;
	var HH   = date.getHours();
	var MM   = date.getMinutes();
	var SS   = date.getSeconds();
	
	if(dd<10) dd = "0" + dd;
	if(mm<10) mm = "0" + mm;
	if(yy<10) yy = "0" + yy;
	if(HH<10) HH = "0" + HH;
	if(MM<10) MM = "0" + MM;
	if(SS<10) SS = "0" + SS;
	
	//var jetzt = dd + '.' + mm + '.' + yy + ' ' + HH + ':' + MM + ':' + SS;
	var jetzt = HH + ':' + MM + ':' + SS;
	
	return jetzt;
}

ScrollBox.prototype.debug = function(msg){
	this.debug_box.innerHTML += msg + '<br/>';

}
