/* 
 * Copyright (C) 2006 IP Labs GmbH <http://www.iplabs.de/>
 * All rights reserved.
 * 
 * @require jade/HTMLUtils.js
 */
 
 /**
 * @fileoverview
 *
 * Contains the SidebarList.
 *
 * @author Andreas Kornetka <a.kornetka@iplabs.de>
 * @version $Revision: 115064 $
 */

/**
 * The SidebarList class object.
 *
 * @class <p>This class provides the sidebar-list extras.</p>
 *
 * @constructor
 * @param {String} strId
 *            The string of the list id to be reffered to. Inevitable parameter! 
 */
function SidebarList(strId) {
	var i;
	this.__objList   = document.getElementById(strId);
	this.__listItems = new Array();
	this.__countMax  = 0;
	if(typeof(this.__objList) != 'undefined') {
		for(i = 0; i < this.__objList.childNodes.length; i++) {
			if(this.__objList.childNodes[i].tagName == 'LI') {
				this.__objList.childNodes[i].onmouseover = new Function('if(typeof(jade.HTMLUtils) != "undefined") {if(!jade.HTMLUtils.hasClassName(this, "off")) {jade.HTMLUtils.removeClassName(this, "down"); jade.HTMLUtils.addClassName(this, "over");}}');
				this.__objList.childNodes[i].onmouseout  = new Function('if(typeof(jade.HTMLUtils) != "undefined") {if(!jade.HTMLUtils.hasClassName(this, "off")) {jade.HTMLUtils.removeClassName(this, "over"); jade.HTMLUtils.removeClassName(this, "down");} if(typeof(this.__onmouseout) != "undefined") {eval(this.__onmouseout);}}');
				this.__objList.childNodes[i].onmousedown = new Function('if(typeof(jade.HTMLUtils) != "undefined") {if(!jade.HTMLUtils.hasClassName(this, "off")) {jade.HTMLUtils.removeClassName(this, "over"); jade.HTMLUtils.addClassName(this, "down");} if(typeof(this.__onmousedown) != "undefined") {eval(this.__onmousedown);}}');
			}
		}
	}
}

/**
 * Private Method: The internal listItem object.
 * 
 * @param {String} strId
 *            The string of listitem id to be referred to. Inevitable parameter!
 * @param {String} condition
 *            Condition on activation of an listitem. Inevitable parameter! Valid values are:
 *			  '!0'  <=> Always active, except when list empty ('setCountMax' method).
 *			  '0-x' <=> Always active - no exceptions.
 *			  '1'   <=> Active only on one selected item.
 *			  '1-x' <=> Active if one ore more items selected.
 *			  '<x'  <=> Active if not all items selected.
 *            'x'   <=> Only active if all items selected.
 * @param {String} strOnclick
 *            Function (with parameters) that will be called when listitem is active and clicked.
 * @param {String} strOnmousedown
 *            Function (with parameters) that will be called when listitem is active and the left mouse button ist hit.
 * @param {String} strOnmouseup
 *            Function (with parameters) that will be called when listitem is active and the left mouse button is loosen again.
 * @param {String} strOnmouseout
 *            Function (with parameters) that will be called when listitem is active and the mouse left the area.
 */  
SidebarList.prototype.__listItem = function(strId, condition, strOnclick, strOnmousedown, strOnmouseup, strOnmouseout) {
	this.__condition         = condition.toLowerCase();
	this.__obj               = document.getElementById(strId);
	this.__obj.__onclick     = typeof(strOnclick) != 'undefined' ? strOnclick : 'void(0)';
	this.__obj.__onmousedown = typeof(strOnmousedown) != 'undefined' ? strOnmousedown : 'void(0)';
	this.__obj.__onmouseup   = typeof(strOnmouseup) != 'undefined' ? strOnmouseup : 'void(0)';
	this.__obj.__onmouseout  = typeof(strOnmouseout) != 'undefined' ? strOnmouseout : 'void(0)';
}

/**
 * Add a new listItem object.
 * 
 * @param {String} strId
 *            The string of listitem id to be referred to. Inevitable parameter!
 * @param {String} condition
 *            Condition on activation of an listitem. Inevitable parameter! Valid values are:
 *			  '!0'  <=> Always active, except when list empty ('setCountMax' method).
 *			  '0-x' <=> Always active - no exceptions.
 *			  '1'   <=> Active only on one selected item.
 *			  '1-x' <=> Active if one ore more items selected.
 *			  '<x'  <=> Active if not all items selected.
 *            'x'   <=> Only active if all items selected.
 * @param {String} strOnclick
 *            Function (with parameters) that will be called when listitem is active and clicked.
 * @param {String} strOnmousedown
 *            Function (with parameters) that will be called when listitem is active and the left mouse button ist hit.
 * @param {String} strOnmouseup
 *            Function (with parameters) that will be called when listitem is active and the left mouse button is loosen again.
 * @param {String} strOnmouseout
 *            Function (with parameters) that will be called when listitem is active and the mouse left the area.
 */ 
SidebarList.prototype.add = function(strId, condition, strOnclick, strOnmousedown, strOnmouseup, strOnmouseout) {
	var newItem;
	this.__listItems.push(new this.__listItem(strId, condition, strOnclick, strOnmousedown, strOnmouseup, strOnmouseout));
	newItem = this.__listItems[this.__listItems.length - 1];
	if(newItem.__condition == '0-x' || (newItem.__condition == '<x' && this.__countMax) || (newItem.__condition == 'x' && this.__countMax) || (newItem.__condition == '!0' && this.__countMax)) {jade.HTMLUtils.removeClassName(newItem.__obj, 'off');} 
	else {jade.HTMLUtils.addClassName(newItem.__obj, 'off');}
}

/**
 * Sets a getter for active items (can be a function with return or a variable).
 * 
 * @param {String} strGetter
 *            Can be a function with an integer return or an integer variable, that will be called later. Inevitable Parameter!
 */ 
SidebarList.prototype.setCountGetter = function(strGetter) {
	this.__countGetter = strGetter;
}

/**
 * Sets a the amount of total items that can be selected.
 * 
 * @param {Integer} max
 *            The Maximum number of selectable items. Inevitable Parameter!
 */ 
SidebarList.prototype.setCountMax = function(max) {
	this.__countMax = max;
}

/**
 * Updates the Sidebar list.
 * 
 * @param {Integer} forceActive
 *            If this optional parameter ist set, an update with the specifed amount of active items is forced.
 */ 
SidebarList.prototype.update = function(forceActive) {
	var i, active;
	if(typeof(forceActive) == 'undefined') {
		try {active = eval(this.__countGetter);}
		catch(e) {active = 0;}
	} else {active = forceActive;}
	for(i = 0; i < this.__listItems.length; i++) {
		if(this.__listItems[i].__condition == '1') {
			if(active == '1') {jade.HTMLUtils.removeClassName(this.__listItems[i].__obj, 'off');}
			else {jade.HTMLUtils.addClassName(this.__listItems[i].__obj, 'off');}
		}
		if(this.__listItems[i].__condition == '1-x') {
			if(active > 0) {jade.HTMLUtils.removeClassName(this.__listItems[i].__obj, 'off');}
			else {jade.HTMLUtils.addClassName(this.__listItems[i].__obj, 'off');}
		}
		if(this.__listItems[i].__condition == '<x') {
			if(active < this.__countMax || this.__countMax == 0) {jade.HTMLUtils.removeClassName(this.__listItems[i].__obj, 'hidden');}
			else {jade.HTMLUtils.addClassName(this.__listItems[i].__obj, 'hidden');}
		}
		if(this.__listItems[i].__condition == 'x') {
			if(active < this.__countMax || this.__countMax == 0) {jade.HTMLUtils.addClassName(this.__listItems[i].__obj, 'hidden');}
			else {jade.HTMLUtils.removeClassName(this.__listItems[i].__obj, 'hidden');}
		}
	}	
}

/**
 * Main initialization method (all 'add' calls must be done before). 
 */ 
SidebarList.prototype.init = function() {
	var i;
	this.update();
	for(i = 0; i < this.__listItems.length; i++) {
		this.__listItems[i].__obj.onclick   = new Function('if(typeof(jade.HTMLUtils) != "undefined") {if(!jade.HTMLUtils.hasClassName(this, "off")) {eval(this.__onclick);}}');
		this.__listItems[i].__obj.onmouseup = new Function('if(typeof(this.__onmouseup) != "undefined") {eval(this.__onmouseup);}');
	}
}
