/*
	Objekt: LED()
	
	Autor: J. Strübig

	Version 0.4 / Datum: 11:25 22.06.2010
	* Parameter von Konstruktor und .create() vertauscht
    * Feinarbeiten
    * scrollOut() Funktion

	Version 0.3 / Datum: 11:25 15.02.2009
	* Vererbung über prototype

	Version 0.2 / Datum: 11:24 14.02.2009
	
	Methoden:
	
	seText(string)
	charAt(top, left, char)
	print(string)

	stop()
	start()
	
	pause(millisekunden)
	up(anzahl)
	blink(anzahl)
	flash(anzahl)
    scrollOut() 

*/
function LED(h, w, px, pad) {

	var bitmap = LED_Chars;
	if(h < bitmap.length) h  = bitmap.length;

	// LED erbt von LED_Panel
	LED_Panel.call(this, h, w, px, pad);
	
	//this.speed = 100;
	this.speed = 1;

	this.blinkInterval = 350;
	this.blinkAmount = 3;

	this.scrollAmount = 1;
	this.scrollSpeed = 150;

	
	this.pause_ms = 500;
	
	// lokale var
	var text = '';
	var columnPointer;
	var messageArray;
	var char_offset;
	var cursor = 0;
    
    var doScroll = false;
	var stop = false; // Flag um Aktionen stoppen zu können

	var _this = this;

	var wait = function(status) {
		doScroll = !doScroll;
		stop = !stop;
		if(stop)scroll();
	};

	this.start = function() {
		if(!stop) return;
		doScroll = true;
		stop = true;
		scroll();
	};
	this.stop = function() {
		doScroll = false;
		stop = true;
	};

	this.setText = function(txt) {
		if(typeof txt == 'undefined' || !txt.length) return;
		this.stop();
		char_offset = parseInt(((this.height() - bitmap.length) / 2));
		
		text = txt.toUpperCase();
		messageArray = [];
		for (var i = 0; i < text.length; ++i){
            var c = text.substr(i, 1);
			var letter = LED_Chars.get(c);
   			
            if(!letter) {
				// Sonderfunktionen.
				// Zahl in einer geschweiften Klammer dahinter, 
				// wird der zugehörigen Funktion übergeben
				var zahl = '';
				var k1 = text.indexOf('{', i) + 1;
				var k2 = text.indexOf('}', i);
				if(k1 && k1 - i == 2) { zahl = text.substring(k1, k2); i += k2 - k1 + 2;} 
				messageArray.push(c + zahl); 
			} else  {
				// Das Bitmap-Array erzeugen
				for (var j = 0; j < letter.length; ++j) messageArray.push(letter[j] << char_offset);
				messageArray.push(0); // Abstand zwischen Buchstaben
			}
		}
        
		columnPointer = 0;
	};

	this.pause = function(c) {
		wait();
		window.setTimeout( function() {wait(false)}, c || this.pause_ms);
	};

	this.up = function(loop) {
		wait();
		var c = this.height() + 1;
		if(!loop) loop = this.scrollAmount;

		window.setTimeout(function() {
			if(--c) {
				_this.scrollUp(true);
				window.setTimeout(arguments.callee, _this.scrollSpeed);
			}
			else if(!stop) {
				if(--loop) {
					c = _this.height() + 1;
					window.setTimeout(arguments.callee, _this.scrollSpeed);
				}
				else wait();
			}
		}, this.scrollSpeed);
	};
	this.blink = function(c) {
		wait();
		var on = this.dots(true);
		if(!c) c = this.blinkAmount;
		c *= 2;
		
		window.setTimeout(function() {
			if(c--) {
				for(var i = 0; i < on.length; ++i) on[i].toogle();
				window.setTimeout(arguments.callee, _this.blinkInterval);
			}
			else if(!stop) wait();
		}, this.blinkInterval);
	};
	this.flash = function(c) {
		wait();
		var on = this.dots(false);
		if(!c) c = this.blinkAmount;
		c *= 2;
		window.setTimeout(function() {
			if(c--) {
			for(var i = 0; i < on.length; ++i) on[i].flash();
			window.setTimeout(arguments.callee, _this.blinkInterval);
			}
			else if(!stop)wait();
		}, this.blinkInterval);
	};
    
	this.charAt = function(t, l, c) {
		if(!t || t < 0) t = 0;
		if(!l || l < 0) l = 0;
		
        c = bitmap.get(c.toUpperCase());
        if(!c) return;
        
		var off = char_offset - t;
		if(off < 0) off = 0;
		for(var i = 0; i < c.length; i++) {
			var tmp = c[i] << off;
			this.setByte(l + i, tmp);
			++cursor;
		}
		++cursor;
		if(cursor > this.width()) cursor = 0;
	};
	this.print = function(txt) {
		for(var i = 0; i < txt.length; ++i) {
			this.charAt(0, cursor, txt.substr(i, 1));
		}
	};
	this.scrollOut = function() {
		wait(); 
		var w = this.width() ;

		window.setTimeout(function() {
			if(--w) {
				_this.scrollLeft();
				window.setTimeout(arguments.callee, _this.speed);
			}
			else if(!stop)wait();
		}, this.speed);
        
    }
    function scroll() {
		// Textausgabe
		var temp = messageArray[columnPointer++];
		if(columnPointer == messageArray.length) columnPointer = 0;
		if(isNaN(temp)) {
			// Sonderzeichen
			switch(temp.substr(0, 1)) {
				case '#': _this.blink(temp.substring(1)); break;
				case '^': _this.up(temp.substring(1)); break;
				case '_': _this.flash(temp.substring(1)); break;
				case '@': _this.pause(temp.substring(1)); break;
                case '*': _this.scrollOut(); break;
			}
		} else {
			_this.scrollLeft();
			_this.setByte(_this.width() - 1, temp);
		}
		// setTimeout verbrät weniger Resourcen als interval im FF 
		if(doScroll)window.setTimeout(scroll, _this.speed);
	}
    
    var clear = this.clear; //sichern
    this.clear = function(){
        cursor = 0;
        return clear.apply(this, arguments);
        
    }
    this.setText(' ');
}
LED.prototype = LED_Panel.prototype;

