/*
 * Utilizes the jquery collapser plugin:
 * http://www.aakashweb.com/jquery-plugins/collapser/
 * 
 * Initialization method for an accordion. Example usage:
 * 
 * 	
    var obj={
		_idStr: ".accordion", 
		_panelIdStr: ".accordion-submenu",  //only required if auto-hiding
		_autoHideBln: true, 
		_expandedClassStr: "leftnav_headerButton-expanded" 
	};		
	var accordion = new DropDown(obj);

 */
function DropDown(obj) {
	
	// ------------------------------------------------------------------------------------------
	// DEFINE PROPERTIES
	// ------------------------------------------------------------------------------------------
	this._idStr;
	this._propsObj;
	this._cls;
	
	// ------------------------------------------------------------------------------------------
	// INTIALIZATION
	// ------------------------------------------------------------------------------------------
	this.init = function(obj) {
		//Common.tracer("DropDown.init");
		this.setupProps(obj);
		this.generate(obj);
		
	};
	
	/** Calls primary initialization methods. */
	this.setupProps = function(obj) {
		//Common.tracer("DropDown.setupProps");
		//save the id of the property
		
		if(obj._idStr.indexOf(".") != -1 ) {
			this._idStr = obj._idStr;
		} else if(obj._idStr.indexOf("#") != -1 ) {
			this._idStr = obj._idStr;
		} else {
			this._idStr = "#"+obj._idStr;	
		}
		
		//Common.tracer("DROPDOWN ID IS: "+this._idStr);
//		if(obj._styleStr == undefined) obj._styleStr = "selectDefault";
//		if(obj._optionsTopNum == undefined) obj._optionsTopNum = 0;
//		if(obj._scrollBln == undefined) obj._scrollBln = 1;
			
		if(obj._hMaxNum == undefined) obj._hMaxNum = 500;
		
		//save the objects to reinit dropdown later
		this._propsObj = obj;
		this._propsObj._originalIdStr = obj._idStr;
		
		
	};
	// ------------------------------------------------------------------------------------------
	// GENERATION
	// ------------------------------------------------------------------------------------------
	/** Calls all primary generation methods. */
	this.generate = function(obj) {
		//Common.tracer("DropDown.generate");
		
		
		var initObj =  {
			maxHeight: obj._hMaxNum,
			positionOptions: {
				my: "left top",
				at: "left top",
				offset: null
			}
		};
		
		this._cls = $(this._idStr).selectmenu(initObj);

		
	};
	// ------------------------------------------------------------------------------------------
	// PUBLIC METHODS 
	// ------------------------------------------------------------------------------------------
	/** refreshes the dropdown by removing the old one, and reinitializing. */
	this.refresh = function(obj) {
		//Common.tracer("DropDown.refresh", this._idStr);
		
		
		
		
		this.destroy({_fullBln: false});
		
		var idStr = this._idStr;
		if(idStr.indexOf("#") != -1) idStr = idStr.substring(1);
		
		
		if(document.getElementById(idStr) != null && document.getElementById(idStr).length != 0 ) {
			this.generate(this._propsObj);
		}
	};
	
	/** triggers the dropdown change event. */
	this.change = function() {
		this._cls.selectmenu("change");
	};
	/** opens the dropdown. */
	this.open = function() {
		this._cls.selectmenu("open");
	};	
	/** closes the dropdown. */
	this.close = function() {
		this._cls.selectmenu("close");
	};	
	/** disables the dropdown. */
	this.disable = function() {
		this._cls.selectmenu("disable");
	};	
	/** enables the dropdown. */
	this.enable = function() {
		this._cls.selectmenu("enable");
	};		
	
	/** get or set the selected index. */
	this.index = function() {
		if (arguments.length) {
			return this._cls.selectmenu("index", arguments);
			
		} else {
			
			return this._cls.selectmenu("index");
		}
		
	};		
	/** get or set the selected value. */
	this.value = function() {
		
		if (arguments.length) {
			return this._cls.selectmenu("value", arguments);
			
		} else {
			
			return this._cls.selectmenu("value");
		}
		
	};		

	/** add a css class. */
	this.addClass = function(str) {
		//Common.tracer("DropDown.addClass");
		$(this._idStr+"-button").addClass(str);
		
	};	
	

	/** remove a css class. */
	this.removeClass = function(str) {
		
		$(this._idStr+"-button").removeClass(str);
		
	};		
	
	this.validate = function() {
		//Common.tracer("DropDown.validate: "+this.value());
		if(this.value() == "")  {
			this.addClass("error");
			return false;
		} else {
			this.removeClass("error");
			return true;
		}
	};

	
	// ------------------------------------------------------------------------------------------
	// DESTROY METHOD
	// ------------------------------------------------------------------------------------------
	/** Prepares for garbage collection by clearing all properties and deregistering all listeners. */
	this.destroy = function(obj) {
		if(this._cls != null) this._cls.selectmenu("destroy", obj);
		
		var fullBln = true;
		if(obj && obj._fullBln) fullBln = obj._fullBln;
		
		//Common.tracer("DropDown.destroy");
		this._cls = null;
		
		if(fullBln) Common.scorch(this._propsObj);		
	};
		
	
	// ------------------------------------------------------------------------------------------
	// CALL TO CONSTRUCTOR - MUST BE AT THE END
	// ------------------------------------------------------------------------------------------
	/** Prepares for garbage collection by clearing all properties and deregistering all listeners. */
	this.init(obj);
	

}




