/*************************************************************************************************************
	
	developer : idw1@naver.com
	date: 2006-07-04

*************************************************************************************************************/

/*************************************************************************************************************
	
	LED_TIMER
		- start
		- stop
		- init

*************************************************************************************************************/

LED = function(id, parent_id) {
	LED.superclass.constructor.call(this, id, parent_id);
}

Class(
	LED, OBJECT,

	"thickness", 2,

	"init", function(id, parent_id) {
		LED.superclass.init.call(this, id, parent_id);
		this.default_color = "#333";
		this.width = this.thickness * 8;
		this.height = this.thickness * 15;
	},

	"set_xy", function(x, y) {
		DW_UTIL.setXY(this.container, x, y);
	},

	"createLEDPart", function(id, x, y, w, h) {
		var el = DW_UTIL.addDiv(id, this.container);
		DW_UTIL.setXYWH(el, x, y, w, h);
		DW_UTIL.setBackground(el, this.default_color);

		return el;
	},

	"create", function(x, y) {
		if( this.container ) {
			return;
		}

		x = x || 0;
		y = y || 0;
		var t = this.thickness;
		var w = this.thickness * 4;
		var h = this.thickness * 5;

		this.container = DW_UTIL.addDiv(this.id, this.parent_id);
		DW_UTIL.setXYWH(this.container, x, y, w + this.thickness*2, h*2 + this.thickness*3);
		DW_UTIL.setBackground(this.container, "#000");
		YAHOO.util.Dom.setStyle(this.container, "border", t + "px solid #000");

		this._0 = this.createLEDPart(this.get_uid("0"), t, 0, w, t);
		this._1 = this.createLEDPart(this.get_uid("1"), 0, t, t, h);
		this._2 = this.createLEDPart(this.get_uid("2"), w + t, t, t, h);
		this._3 = this.createLEDPart(this.get_uid("3"), t, h + t, w, t);
		this._4 = this.createLEDPart(this.get_uid("4"), 0, h + 2 * t, t, h);
		this._5 = this.createLEDPart(this.get_uid("5"), w + t, h + 2 * t, t, h);
		this._6 = this.createLEDPart(this.get_uid("6"), t, h * 2 + t * 2, w, t);
	},

	"destroy", function() {
		for( var i=0; i<7; i++ ) {
			DW_UTIL.safeDestroy(eval("this._" + i), this.container);
		}
		DW_UTIL.safeDestroy(this.id, this.parent_id);

		delete this._0;
		delete this._1;
		delete this._2;
		delete this._3;
		delete this._4;
		delete this._5;
		delete this._6;
		delete this.container;
	},

	"init_color", function() {
		for( var i=0; i<7; i++ ) {
			DW_UTIL.setBackground(eval("this._" + i), this.default_color);
		}
	},

	"set_part", function(color) {
		if( arguments.length <= 1 ) {
			return;
		}

		for( var i=1; i<arguments.length; i++ ) {
			DW_UTIL.setBackground(eval("this._" + arguments[i]), color);
		}
	},

	"set_char", function(number) {
		this.init_color();

		switch( number.toString() ) {
		case "0":
			this.set_part("#f00", 0, 2, 5, 6, 4, 1);
			break;
		case "1":
			this.set_part("#f00", 2, 5);
			break;
		case "2":
			this.set_part("#f00", 0, 2, 3, 4, 6);
			break;
		case "3":
			this.set_part("#f00", 0, 2, 3, 5, 6);
			break;
		case "4":
			this.set_part("#f00", 1, 3, 2, 5);
			break;
		case "5":
			this.set_part("#f00", 0, 1, 3, 5, 6);
			break;
		case "6":
			this.set_part("#f00", 0, 1, 4, 6, 5, 3);
			break;
		case "7":
			this.set_part("#f00", 0, 2, 5);
			break;
		case "8":
			this.set_part("#f00", 0, 1, 2, 3, 4, 5, 6);
			break;
		case "9":
			this.set_part("#f00", 3, 1, 0, 2, 5, 6);
			break;
		}
	}
);

LEDS = function(id, parent_id) {
	LEDS.superclass.constructor.call(this, id, parent_id);
}

Class(
	LEDS, OBJECT,

	"init", function(id, parent_id) {
		LEDS.superclass.init.call(this, id, parent_id);
		this.count = 3;
		this.led = [];
		this.value = 0;
	},

	"set_xy", function(x, y) {
		DW_UTIL.setXY(this.container, x, y);
	},

	"add_led", function() {
		var count = this.led.length;
		var led = new LED(this.get_uid(count), this.id);
		led.create(led.width * count, 0);
		this.led.unshift(led);

		DW_UTIL.setWH(this.container, led.width*(count + 1), led.height);
	},

	"create", function(x, y) {
		x = x || 0;
		y = y || 0;
		this.container = DW_UTIL.addDiv(this.id, this.parent_id);
		DW_UTIL.setXYWH(this.container, x, y, new LED().thickness * 8, new LED().thickness * 15);
		DW_UTIL.setBackground(this.container, "#369");

		for( var i=0; i<this.count; i++ ) {
			this.add_led();
		}
	},

	"set", function(text) {
		if( typeof text != "string" ) {
			return;
		}

		this.set_to_zero();
		for( var i=0; i<text.length && i<this.led.length; i++ ) {
			this.led[i].set_char(text.substr(text.length - 1 - i, 1));
		}
	},

	"set_number", function(number) {
		if( typeof number != "number" ) {
			return;
		}

		this.value = number;

		for( var i=0; i<this.count; i++ ) {
			this.led[i].set_char(number % 10);
			number = parseInt(number / 10);
		}
	},

	"set_to_zero", function() {
		for( var i=0; i<this.count; i++ ) {
			this.led[i].set_char(0);
		}
	},

	"init_value", function() {
		this.value = 0;
		for( var i=0; i<this.count; i++ ) {
			this.led[i].set_char("x");
		}
	},

	"decrease", function() {
		if( this.value > 0 ) {
			this.value--;
		}
		this.set_number(this.value);
	},

	"increase", function() {
		this.value++;
		this.set_number(this.value);
	},

	"get_value", function() {
		return this.value;
	}
);


LED_TIMER = function(id, parent_id) {
	this.id = id;
	this.parent_id = parent_id;
	this.timer = 0;
	this.timer_count = 0;

	this.create = function(x, y) {
		if( this.led ) {
			return;
		}

		this.led = new LEDS(this.id, this.parent_id);
		this.led.create(x, y);
	}

	this.get_value = function() {
		return this.timer_count;
	}

	this.set_value = function(value) {
		this.timer_count = value;
		this.led.set_number(this.timer_count);
	}

	this.start = function() {
		YAHOO.namespace("LED_TIMER");
		YAHOO.LED_TIMER.obj = this;

		this.led.set_number(this.timer_count++);
		this.timer = setTimeout("YAHOO.LED_TIMER.obj.start()", 1000);

		if( this.timer_count >= Math.pow(10, this.led.count) ) {
			this.stop();
		}
	}

	this.stop = function() {
		clearTimeout(this.timer);
	}

	this.init = function() {
		this.led.set_to_zero();
		clearTimeout(this.timer);
		this.timer = 0;
		this.timer_count = 0;
	}
}
