﻿/**
* TripleBoxBTD
* 
* @author     banhthidiem <banhthidiem@gmail.com>
* @copyright  2008 Bach Khoa Computer Inc.
* @version    1.0 15/03/2010
*/

var EventNameTB =
{
	click: "click",
	mouseOver: "mouseOver",
	mouseOut: "mouseOut",
	change: "change"
};

var StatusTB =
{
	checked: 1,
	unChecked: 0,
	undefined: -1,

	getTextByStatus: function(status) {
		switch (status) {
			case StatusTB.undefined:
				return "Chưa thao tác";
			case StatusTB.checked:
				return "Chọn";
			case StatusTB.unChecked:
				return "Không chọn";
		}
		return "";
	}
};

TripleBoxBTD = function(idParent, className) {
	if (idParent == null) return;
	this.elContainerParent = idParent;
	this.elContainer = null;
	this.listEvent = new Object();
	this.elTripleBox = null;
	this.isBTD = true;
	this.isReadOnly = false;
	this.css = "tripleBoxBTD";
	if (className != null) {
		this.css += className;
	}
	this.status = StatusTB.undefined;
};

/*****************************************************************************
Create element
*****************************************************************************/

TripleBoxBTD.prototype.createContainer = function() {
	if (typeof this.elContainerParent == "string") {
		this.elContainerParent = utilObj.getElById(this.elContainerParent);
	}
};

TripleBoxBTD.prototype.getCss = function() {
	switch (this.status) {
		case StatusTB.undefined:
			return "undefined";
		case StatusTB.checked:
			return "checked";
		case StatusTB.unChecked:
			return "unChecked";
	}
};

TripleBoxBTD.prototype.getStatus = function() {
	switch (this.status) {
		case StatusTB.undefined:
			return StatusTB.checked;
		case StatusTB.checked:
			return StatusTB.unChecked;
		case StatusTB.unChecked:
			return StatusTB.undefined;
	}
};

TripleBoxBTD.prototype.mouseOver = function() {
	this.elTripleBox.elChild.className = this.getCss() + " over";
	var e = new Object();
	this.executeEvent(e, EventNameTB.mouseOver);
};

TripleBoxBTD.prototype.mouseOut = function() {
	this.elTripleBox.elChild.className = this.getCss();
	var e = new Object();
	this.executeEvent(e, EventNameTB.mouseOut);
};

TripleBoxBTD.prototype.setReadOnly = function(isReadOnly) {
	this.isReadOnly = isReadOnly;
};

TripleBoxBTD.prototype.mouseClick = function(e) {
	if (this.isReadOnly) return;
	var oldValue = this.status;
	this.status = this.getStatus();
	this.elTripleBox.elChild.className = this.getCss() + " over";
	if (oldValue != this.status) {
		this.executeEvent(e, EventNameTB.change);
	}
};

TripleBoxBTD.prototype.getPositionView = function() {
	return utilObj.getElementPositionView(this.elTripleBox);
};

TripleBoxBTD.prototype.getSize = function() {
	return utilObj.getElementSize(this.elTripleBox);
};

TripleBoxBTD.prototype.createTripleBox = function() {
	var elChild = utilObj.createEl("DIV");
	elChild.className = this.getCss();
	var el = utilObj.createEl("DIV");
	this.elContainer = el;
	el.appendChild(elChild);
	el.elChild = elChild;
	el.className = this.css;
	var self = this;
	el["onmouseover"] = function(e) { self.mouseOver(e); };
	el["onmouseout"] = function(e) { self.mouseOut(e); };
	el["onclick"] = function(e) { self.mouseClick(e); };
	this.elTripleBox = el;
	this.elContainerParent.appendChild(el);
};

TripleBoxBTD.prototype.init = function() {
	this.createContainer();
	this.createTripleBox();
};

/*****************************************************************************
Event Start
*****************************************************************************/

TripleBoxBTD.prototype.addEvent = function(eventName, func) {
	if (this.listEvent[eventName] == null) {
		this.listEvent[eventName] = [];
	}
	this.listEvent[eventName].push(func);
};

TripleBoxBTD.prototype.removeEvent = function(eventName, func) {
	if (this.listEvent[eventName] == null) {
		return;
	}
	for (var i = 0; i < this.listEvent[eventName].length; i++) {
		if (func == this.listEvent[eventName][i]) {
			this.listEvent[eventName].splice(i, 1);
			break;
		}
	}
};

TripleBoxBTD.prototype.executeEvent = function(e, eventName) {
	if (this.listEvent[eventName] != null) {
		for (var i = 0; i < this.listEvent[eventName].length; i++) {
			this.listEvent[eventName][i](e);
		}
	}
};

/*****************************************************************************
Event End
*****************************************************************************/

TripleBoxBTD.prototype.setValue = function(v) {
	this.status = parseInt(v, 10);
	this.elTripleBox.elChild.className = this.getCss();
};

TripleBoxBTD.prototype.reset = function() {
	this.setValue(StatusTB.undefined);
};

TripleBoxBTD.prototype.getValue = function() {
	return this.status;
};

TripleBoxBTD.prototype.getText = function() {
	return StatusTB.getTextByStatus(this.status);
};

/*****************************************************************************
Public Method End
*****************************************************************************/
