/******************************************
* WI_SelectBox script - By Lawrence Carvalho (lawrence@nodetraveller.com)
* http://www.nodetraveller.com
******************************************/
/*WI_SelectBox object
Constructor parameters are:
objName 		= name of instance (str)
containerDiv 	= name of DIV element that the options are to be shown (str)
isNested		= boolean denoting whether the selectBox is nested within 
				  another element. (boolean)

Methods :	
scrollTo()		= scrolls the selectBox to clicked position.
stopScrolling	= stops scrolling the scrollbar
grabBar()		= grabs the scrollbar
scrollUp()		= scroll up
scrollDown)		= scroll down
addOption()		= adds an option to the select box and optionally saves to a cookie
removeOption()	= removes an option to the select box and optionally saves to a cookie
generateOptionHTML = generates HTML for each option
toggleSelect 	= toggles selection of the currently selected option
getLength		= returns the number of options in the select box
sortByText		= sorts the options be text value
sortByValue		= sorts the options by value
updateBox		= reinitialises the scrollbar
redraw			= redraws the options
*/
function selectBox(objName, containerDiv, isNested) {
	this.objName = objName;
	this.containerDiv = document.getElementById(containerDiv);
	this.isNested = isNested;
	
	this.options = new Array();
	this.length = this.options.length;
	this.selectedIndex = -1;
	
	
	this.yScrollBar = new scrollBar(this.objName+".yScrollBar", "Y", "selectTextHolder", "selectText", "selectYTrack", "selectYBar", null, "selectUpArrow", "selectDownArrow", null, null, true);

	this.scrollTo = function(event) {
		this.yScrollBar.clickScroll2(event);
	}
	
	this.grabBar = function(event) {
		this.yScrollBar.grabBar(event);	
	}
	
	this.stopScrolling = function() {
		this.yScrollBar.stopScroll();
	}
	
	this.scrollUp = function() {
		this.yScrollBar.incScroll(-2);
	}
	
	this.scrollDown = function() {
		this.yScrollBar.incScroll(2);
	}
	
	this.addOption = function(object,saveToCookie, fn) {

		for(i=0;i<this.options.length;i++) {
			if(this.options[i].valueOf()==object.valueOf()) {
				return;
			}
		}
		this.options[this.options.length] = object;
		this.length = this.options.length;
		this.generateOptionHTML(this.options[this.options.length-1]);
		this.updateBox();
		if (saveToCookie)
			fn(object.getValue());
	}
	
	this.removeOption = function(doRemoveAll,saveToCookie,fn) {
		var options = this.containerDiv.getElementsByTagName("span");
		if (doRemoveAll) {
			for(i=this.options.length-1;i>=0;i--){
				this.options[i].selected = false;
				this.options[i]=null;
				this.containerDiv.removeChild(options.item(i));
			}
			this.options.length = 0;
			if(saveToCookie)
				fn(doRemoveAll);
		}
		else if (this.selectedIndex>-1) {
			var index = this.selectedIndex;
			var newOptions = this.options.slice(0,index);
			newOptions = newOptions.concat(this.options.slice(index+1,this.options.length));
			this.containerDiv.removeChild(options.item(index));
			this.options = newOptions;
			this.length = this.options.length;
			//update index value in each span
			options = this.containerDiv.getElementsByTagName("span");
			for(i=0;i<options.length;i++){
				options.item(i).index = i;
			}
			if(saveToCookie)
				fn(false);
			this.selectedIndex=-1;
			this.selected.className="selectOff";

			
		}
		this.length = this.options.length;
		this.updateBox();
		
	}
	
	this.getLength = function() {
		return this.length;
	}
	
	
	this.toggleSelect = function(evt) {
		var srcObj = (evt.target) ? evt.target: evt.srcElement;
		//make sure that we have the container 
		while(srcObj.nodeName!="SPAN") {
			srcObj= (evt.target) ? srcObj.parentNode : srcObj.parentElement;
		}
		var index = srcObj.index;
//		turn off all options except the specified one	
		for(i=0;i<this.options.length;i++){
			if(i!=index){
				this.options[i].selected=false;
				if (this.selected!=null)
					this.selected.className="selectOff";			
			}
		}
		this.selected=null;
		this.selectedIndex = -1;
		//toggle specified option on
		if(!this.options[index].selected) {
			this.options[index].selected=true;
			srcObj.className="selectOn";
			this.selected=srcObj;
			this.selectedIndex = index;
		}	
	}
	
	this.generateOptionHTML = function(obj) {
		var span = document.createElement("span");
		span.className = "selectOff";
		span.index = this.options.length-1;
		if (obj.isDefault)
		 	this.defaultOption = obj;
		var thisObj = this;
		//IE

			addEvent(span,"mousedown", function(e){ thisObj.toggleSelect(e) }, false);
		span.innerHTML=obj.getText();
		this.containerDiv.appendChild(span);
	
	}
	
	this.updateBox =  function() {
		this.yScrollBar = new scrollBar(this.objName+".yScrollBar", "Y", "selectTextHolder", "selectText", "selectYTrack", "selectYBar", null, "selectUpArrow", "selectDownArrow", null, null, this.isNested);
		if (this.yScrollBar.active)
			this.yScrollBar.moveBar(100,true);
	}
	
	this.sortByText = function(reverse) {
		this.options.sortByProperty("text", reverse);
		this.redraw();
	}
	
	this.sortByValue = function(reverse) {
		this.options.sortByProperty("value", reverse);
		this.redraw();
	}
	
	this.redraw = function() {
		while(this.containerDiv.hasChildNodes()){
		this.containerDiv.removeChild(this.containerDiv.lastChild);
	}
		for(i=0;i<this.options.length;i++){ 
			this.generateOptionHTML(this.options[i]);
		}
		
	}
	
}


function tDOMOption(text, value, isDefault) {
	this.text = text;
	this.value = value;
	this.isDefault = isDefault;
	
	this.getText = function() {
		return this.text;
	}
	
	this.getValue = function() {
		return this.value;
	}
	
	this.isSelected = function() {
		return this.selected;
	}
	
	this.valueOf = function() {
		return this.getText() + " , " + this.getValue();
	}
	
	this.toString = function() {
		return "\nText : " + this.getText() + " \nValue : " + this.getValue();
	}
}


