/*
	Objekt: LED_Panel
	
	Version: 0.3
	Datum: 11:21 15.02.2009
	* Funktionen in prototype ausgelagert
	* Parameter von each() geändert
		
	create(height, width, pixel, padding)
	set(top, left, on/off)
	get(top, left)
	
	scrollUp(circle)
	scrollDown(circle)
	scrollLeft(circle)
	scrollDown(circle) circle  gibt an, ob "im Kreis" gescrollt werden soll
	
	scrollCol(col, dir) true = up
	scrollRow(row, dir) true = left
	
	copy_led(LED_Panel)
	clear([backgroundcolor] [, color]);
	
	setByte(col, byte)
	
	dots([status])
	
	width(), height(), pixel()
	
*/

function LED_Panel() {
	var width, height, pixel, top, left, panel, dots, padding;
	var color = '#F00';
	var backcolor = '#000';
	var bits = [];
	var _this = this;


	// Panel erzeugen
	this.create = function(h, w, px, p) {
		width = w || 8;
		height = typeof h != 'undefined' && h > 0 ? h : 8;
		pixel = px > 0 ? px : 1;
		padding = typeof p == 'undefined' ? 1 : p;
		dots = [];
		
		panel = document.createElement('div');
		panel.style.position = 'relative';
		panel.className = 'LED';
		var offset = pixel + padding;
		for(var i = 0; i < this.height(); i++) {
			if(!dots[i]) dots[i] = [];
			for(var j = 0; j < this.width(); j++) {
				dots[i][j] = new Dot( i * offset + padding,  j * offset + padding, panel);
			}
		}
		panel.onclick = function(e) { 
			e = (e || window.event);
			var target = e.srcElement || e.target;
			var search;
			var row, col;
			_this.each( 
				function(dot, r, c) {
					if(dot.is(target)) {
						search = dot;
						row = r;
						col = c;
						return false;
					}
				}
			);
			_this.onclick(search, row, col); 

		};
		for(var i = 0; i < height; i++) bits[i] = Math.pow(2, i);
		bits.reverse()

		panel.style.width = 1 + (width * offset) + 'px';
		panel.style.height = 1 +  (height * offset) + 'px';
		
		cursor = 0;

		return panel;
	};
	this.set = function(t, l, on){
		if(dots[t] && dots[t][l]) dots[t][l].set(on);
	};
	this.get = function(t, l){
		if(dots[t] && dots[t][l]) return dots[t][l].on;
	};

	this.setByte = function(col, c) {
		if(col < 0 || col >= this.width()) return;
		//panel.style.display = 'none';
		for(var i = 0; i < this.height(); i++) {
			this.set(i, col, (c & bits[i]));
		}
		//panel.style.display = '';
	};
	this.getByte = function(col) {
		if(col < 0 || col >= this.width()) return;
		var b = 0;
		for(var i = 0; i < this.height(); i++) {
			if(this.get(i, col)) b +=  bits[i];
			//alert(i + '(' + bits[i] + ')=' + (bits[i] & this.get(i, col)));
		}
		return b;
	};
	this.each = function(f) {
		for(var i = 0; i < this.height(); i++) 
		for(var j = 0; j < this.width(); j++) 
		if(false == f(dots[i][j], i, j)) break;
	};
	this.copy_led = function(led) {
		for(var i = 0; i < this.height(); i++) 
		for(var j = 0; j < this.width(); j++) 
		led.set(i, j, this.get(i, j));
	}; 
	this.width = function() {return width;};
	this.height = function() {return height;};
	this.pixel = function() {return pixel;};
	

	// Dot-Objekt
	function Dot(t, l, p) {
		var e = document.createElement('span');
		e.style.position = 'absolute';
		e.style.backgroundColor = backcolor;
		e.style.top = t + 'px';
		e.style.left = l + 'px';
		e.style.width = pixel + 'px';
		e.style.height = pixel + 'px';
		e.style.overflow = 'hidden';
		e.style.margin = e.style.padding = 0;
		e.style.border = 'none';
		
		if(p) p.appendChild(e);
		
		this.on = false;
		this.off = function() {
			e.style.backgroundColor = backcolor; 
			this.on = false;
		};
		this.set = function(on) { 
			if(this.on == on) return;
			e.style.backgroundColor = on ? color : backcolor;
			this.on = on ? true : false;
		};
		this.toogle = function() {
			this.set(!this.on);
		};
		this.is = function(obj) {
			return obj == e;
		};
		var tmp;
		this.flash = function() {
			if(!tmp) {
				tmp = e.style.backgroundColor;
				e.style.backgroundColor = '#fff'; 
			} else {
				e.style.backgroundColor = tmp; 
				tmp = null;
			}
		}
	}
}
LED_Panel.prototype = {
	scrollLeft: function(circle) {
		for(var i = 0; i < this.height(); i++) {
			var tmp = this.get(i, 0);
			this.scrollRow(i, true);
			this.set(i, this.width() - 1, circle ? tmp : false);
		}
	},
	scrollUp: function(circle) {
		for(var j = 0; j < this.width(); j++) {
			var tmp = this.get(0, j);
			this.scrollCol(j, true);
			this.set(this.height() - 1, j, circle ? tmp : false);
		}
	},
	scrollDown: function(circle) {
		for(var j = 0; j < this.width(); j++) {
			var tmp = this.get(this.height() - 1, j);
			this.scrollCol(j, false);
			this.set(0, j, circle ? tmp : false);
		}
	},
	scrollRight: function(circle) {
		for(var i = 0; i < this.height(); i++) {
			var tmp = this.get(i, this.width() - 1);
			this.scrollRow(i, false);
			this.set(i, 0, circle ? tmp : false);
		}
	},
	scrollCol: function(col, dir) { // dir: true = up
		if(dir) {
			for(var i = 0; i < this.height() - 1; i++)
			this.set(i, col, this.get(i + 1, col));
		} else {
			var i = this.height();
			while(--i) {
				this.set(i, col, this.get(i - 1, col));
			}
		}
		this.onscroll(col, dir);
	},
	scrollRow: function(row, dir) { // dir: true = left
		if(dir) {
			for(var i = 1; i < this.width(); i++)  
			this.set(row, i - 1, this.get(row, i));
		} else {
			var i = this.width();
			while(--i) {
				this.set(row, i, this.get(row, i - 1));
			}
		}
		this.onscroll(row, dir);
	},
	dots: function(status) {
		var tmp = [];
		var get_dot = typeof status == 'undefined' ? 
		function(d) {tmp.push(d); } :
		function(d) {if(d.on == status) tmp.push(d);}
		;
		this.each(get_dot);
		return tmp;
	},
	clear: function(bc, c) {
		if(bc) backcolor = bc;
		if(c) color = c;
		this.each(function(d) {d.off();});
	},
	onclick:function () {},
	onscroll: function () {}

};
