/**
 * JS custom checkbox control
 * 
 * @author ImPuls$$
 */

/**
 * @var array vm_checkboxes vm_checkbox container
 */
var vm_checkboxes = new Array();

/**
 * vm_checkbox class constructor
 *
 * @param mixed id DOM identifier
 * @param string name Control name
 * @param function onclick On click event
 * @param function onchange On change event
 *
 * @example
 *	new vm_checkbox(
 *			vm_uniq('uniqid'),		// checkbox id
 *			'name2',		// checkbox name
 *			function() {	// checkbox onclick event
 *			},
 *			function() {	// checkbox onchange event
 *			}
 *		)
 *		.render(			// render onclick to document
 *			'label<br />',
 *			false
 *		);
 */
function vm_checkbox(id) {
	this.id = id;
	this.replacer = vm_uniq('img_' + this.id);

	this.name = arguments[1] || this.id;
	if (arguments[2])
		this.onclick = arguments[2];
	else
		this.onclick = null;
	if (arguments[3])
		this.onchange = arguments[3];
	else
		this.onchange = null;
	
	vm_checkboxes[this.id] = this;
	
	return this;
}

/**
 * Replace checkbox specified by id
 *
 * @param string classPostfix Checkbox class name postfix
 */
vm_checkbox.prototype.replace = function() {
	var checkbox = vm_gid(this.id);
	
	var self = this;
	
	if (arguments[0])
		this.classPostfix = arguments[0];
	else
		this.classPostfix = '';
	
	checkbox.style.display = "none";
	
	if (checkbox.className != '')
		classPostfix = '_'+checkbox.className;

	n = document.createElement("span");
	n.id = this.replacer;
	
	if (checkbox.className != '')
		this.classPostfix = checkbox.className;

	var stringBuilder = new StringBuilder("vm_checkbox");
	if(checkbox.checked) {
		stringBuilder.Append("_checked");
	}
	if (this.classPostfix) {
		stringBuilder.Append('_');
		stringBuilder.Append(this.classPostfix);
	}
	n.className = stringBuilder.ToString();

	checkbox.parentNode.insertBefore(n, checkbox.nextSibling);
	
	n.onclick = function() { vm_checkboxes[self.id].toggle(0); return false; }
			
	fn = checkbox.onchange;
	if(typeof(fn) == "function") {
		checkbox.onchange = function() { fn(); vm_checkboxes[self.id].toggle(1); return false; }
	} else {
		checkbox.onchange = function () { vm_checkboxes[self.id].toggle(1); return false; }
	}
	
	return this;
}

/**
 * Write checkbox into document
 *
 * @param string value Checkbox value
 * @param bool checked Checkbox checked attribute
 * @param string label Checkbox label
 * @param string classPostfix Checkbox class name postfix
 */
vm_checkbox.prototype.render = function(value, checked, label) {
	var classPostfix = '';
	if (arguments[3])
		this.classPostfix = arguments[3];
	else
		this.classPostfix = '';

	var stringBuilder = new StringBuilder('<input type="checkbox" id="');
	
	stringBuilder.Append(this.id);
	stringBuilder.Append('"');
	
	if (this.name != '') {
 		stringBuilder.Append(' name="');
 		stringBuilder.Append(this.name);
 		stringBuilder.Append('"');
 	}
 	if (this.classPostfix != '') {
 		stringBuilder.Append(' class="');
 		stringBuilder.Append(classPostfix);
 		stringBuilder.Append('"');
 	}
 	if (value != '') {
 		stringBuilder.Append(' value="');
 		stringBuilder.Append(value);
 		stringBuilder.Append('"');
 	}
 	if (checked) {
 		stringBuilder.Append(' checked');
 	}
 	if (this.onclick != null) {
 		stringBuilder.Append(' onclick="vm_checkboxes[\'');
		stringBuilder.Append(this.id);
		stringBuilder.Append('\'].onclick.call(vm_gid(\'');
		stringBuilder.Append(this.id);
		stringBuilder.Append('\'));"');
	}
 	if (this.onchange != null) {
 		stringBuilder.Append(' onchange="vm_checkboxes[\'');
		stringBuilder.Append(this.id);
		stringBuilder.Append('\'].onchange.call(vm_gid(\'');
		stringBuilder.Append(this.id);
		stringBuilder.Append('\')); vm_checkboxes[\'');
		stringBuilder.Append(this.id);
		stringBuilder.Append('\'].toggle(1);"');
 	} else {
 		stringBuilder.Append(' onchange="vm_checkboxes[\'');
		stringBuilder.Append(this.id);
		stringBuilder.Append('\'].toggle(1);"');
	}
	stringBuilder.Append(' " style="display: none;" /><span id="');
	stringBuilder.Append(this.replacer);

	stringBuilder.Append('" class="vm_checkbox');
 	if (checked) {
		stringBuilder.Append('_checked');
 	}
	if (this.classPostfix) {
		stringBuilder.Append('_');
		stringBuilder.Append(this.classPostfix);
	}
	stringBuilder.Append('" onclick="vm_checkboxes[\'');
	stringBuilder.Append(this.id);
	stringBuilder.Append('\'].toggle(0); return false;" />&nbsp;</span>');

	if (arguments[2]) {
		stringBuilder.Append(' <span style="cursor: arrow;" onclick="vm_checkboxes[\'');
		stringBuilder.Append(this.id);
		stringBuilder.Append('\'].toggle(0);">');
		stringBuilder.Append(arguments[2]);
		stringBuilder.Append('</span>');
	}
	
	document.write(stringBuilder.ToString());
	
	return this;
}


/**
 * Checkbox event handler
 *
 * @param bool caller If false change chackbox state
 */
 vm_checkbox.prototype.toggle = function(caller) {
	owner = vm_gid(this.id);
	imgObj = vm_gid(this.replacer);
 
	if(!caller) {
		owner.checked = !owner.checked?true:false;
		if (typeof(owner.onclick) == "function")
			owner.onclick.call(owner);
	}

	var stringBuilder = new StringBuilder("vm_checkbox");
	if(owner.checked) {
		stringBuilder.Append("_checked");
	}
	if (this.classPostfix) {
		stringBuilder.Append('_');
		stringBuilder.Append(this.classPostfix);
	}
	imgObj.className = stringBuilder.ToString();
	
	return this;
}


/**
 * Replace all document checkboxes
 *
 * @param string classPostfix Checkbox class name postfix
 */
function vm_checkbox_replace_all() {
	var classPostfix = '';
	if (arguments[0])
		classPostfix = '_'+arguments[0];
		
	c = document.getElementsByTagName("input");
	var start = c.length - 1;
	for(j = start; j--;) {
		if(c[j].getAttribute("type") == "checkbox") {
			c[j].id = c[j].id || vm_uniq(c[j].name || 'checkbox');
			new vm_checkbox(c[j].id).replace(classPostfix);
		}
	}
}

/**
 * Generate unique id
 *
 * @param mixed id DOM identifier
 */
function vm_uniq(id) {
	if (!vm_gid(id))
		return id;
	else
		return vm_uniq(id + Math.round(Math.random()*1000))
}

/**
 * Get DOM element by id
 *
 * @param mixed id DOM identifier
 */
function vm_gid(id) {
	var element = document.getElementById(id);
	if (element)
		return element;
	else
		return false;
}