/**
 * StyledSelect - old rSelect ported from Prototype
 *
 * @version 1.0
 * @author Ryan Brill, Adam Brill
 *
 * @requires yahoo-dom-event.js
 **/

YAHOO.namespace("SolutionSet.widget");


YAHOO.SolutionSet.widget.StyledSelect = function(el, s) {
	if (!YAHOO.util.Dom.inDocument(el)) {
		return;
	}
	this.items = [];									// array of list items
	this.el = el;										// <select class="rSelect">
	this.label = document.createElement('div');			// the label of our dropdown
	this.labelText = document.createElement('span');	// the text node inside our label
	this.container = document.createElement('div');		// the container element
	this.ulcontainer = document.createElement('div');	// Generated UL mirroring the <select> box
	this.imask = document.createElement('iframe');		// iframe fix for IE
	this.actualCols = 0;								// Actual number of columns that get created
	this.ulInit = false;								// Keeps track of if ul is initialized
	var title = ((this.el.title)? this.el.title : 'No Values Selected').split("||"),
	defaultLabel = title[0],
	defaultAllLabel = ((typeof(title[1]) != "undefined") ? title[1] : false),
	alwaysOpen = YAHOO.util.Dom.hasClass(this.el, "alwaysOpen");
	this.s = {
		fixedWidth: true,					// set the width to the size of the original <select> box
		className: 'StyledSelect',			// Class name to apply to the <div> tag
		selectedClass: 'selected',			// Class name for the seleted item in the select box
		hoverClass: 'hover',				// Class name for the hover of the items in the list
		toggleClass: 'toggle',				// Class name for the toggle all items in the list
		disabledClass: 'disabled',			// Class name for when the select box is disabled
		openClass: 'open',					// Class name added to the container when the box is open
		isMultiSelect: false,				// whether or not this is a multiple select box. It will look for the attribute multiple="multiple" on the select box
		defaultLabel: defaultLabel,			// Default value for when no options are selected in a multi-select box
		defaultAllLabel: defaultAllLabel,	// Default value for when no options are selected in a multi-select box
		pxPerChar: 6.5,						// Number of pixels to allow for each character in the label
		maxHeight: 300,						// Maximum height of the dropdown for multi selects
		maxCols: 1,							// Maximum number of columns - will be fewer colums if fewer fit in maxHeight
		pxPerRow: 22,						// Number of pixels to allow for each row
		processed: 'StyledSelectReplaced',	// Flag to tell whether a select has been processed yet
		alwaysOpen: alwaysOpen,				// Sets the StyledSelect to render itself and always stay open
		width: this.el.offsetWidth+24
	};
	for (var key in s) {
		this.s[key] = s[key];
	}
	
	if(YAHOO.util.Dom.hasClass(this.el, this.s.processed)) {
		return 0;
	}
	YAHOO.util.Dom.addClass(this.el, this.s.processed);
	
	var multi = (this.el.attributes['multiple']) ? this.el.attributes['multiple'].value : this.el.getAttribute('multiple');
	if (multi && (multi.toLowerCase() == "multiple" || multi.toLowerCase() == "true")){
		this.s.isMultiSelect = true;
	}
	
	YAHOO.util.Dom.addClass(this.container, this.s.className);
	if(this.el.className != ''){
		YAHOO.util.Dom.addClass(this.container, this.el.className);
	}
	
	/* Event observers */
	YAHOO.util.Event.addListener(this.label, 'click', this.clickLabel, this, true);
	
	this.createContainer();
	this.setDisabled(this.el.disabled);
	this.el.style.display = "none";
	
	this.el.styledSelectInstance = this;
	
	this.form = YAHOO.util.Dom.getAncestorByTagName(this.el, "form");
	YAHOO.util.Event.addListener(this.form, "reset", this.onResetForm, this, true);
	
	if (this.el.options) {
		this.defaultSelectedItems = [];
		for (var i=0, option; option = this.el.options[i]; i++) {
			if (option.selected === true) {
				this.defaultSelectedItems.push(option);
			}
		}
	}
	
	if (this.s.alwaysOpen) {
		this.findElements();
		this.createUL();
		this.initializeULEvents();
		this.show();
	}
	
	this.updateLabel();
};
YAHOO.SolutionSet.widget.StyledSelect.initSelectors = [
	{
		'tagName': 'select',
		'className': 'StyledSelect',
		's': {}
	}
]; 
YAHOO.SolutionSet.widget.StyledSelect.addInitSelector = function(init) {
	if (!init.tagName) init.tagName = "*";
	if (!init.className) init.className = false;
	if (!init.s) init.s = {};
	
	YAHOO.SolutionSet.widget.StyledSelect.initSelectors.push(init);
}

YAHOO.SolutionSet.widget.StyledSelect.eleIsVisible = function(ele) {
	if (YAHOO.util.Dom.getStyle(ele, "display") == "none") {
		return false;
	}
	if (ele.parentNode && ele.parentNode !== document.body) {
		return YAHOO.SolutionSet.widget.StyledSelect.eleIsVisible(ele.parentNode);
	}
	return true;
}

YAHOO.SolutionSet.widget.StyledSelect.init = function(container, inits) {
	if (!container) container = document.body;
	if (!inits) inits = YAHOO.SolutionSet.widget.StyledSelect.initSelectors;
	
	for (var i=0, init; init = inits[i]; i++) {
		var eles = YAHOO.util.Dom.getElementsByClassName(init.className, init.tagName, container);
		
		for (var j=0, ele; ele = eles[j]; j++) {
			if (typeof(ele.styleSelectInstance) != "undefined" || (init.s.forceLoad !== true && typeof(init.s.width) == "undefined" && !YAHOO.SolutionSet.widget.StyledSelect.eleIsVisible(ele))) {
				continue;
			}
			
			new YAHOO.SolutionSet.widget.StyledSelect(ele, init.s);
		}
	}
}

YAHOO.SolutionSet.widget.StyledSelect.prototype.setDisabled = function(disabled) {
	if (disabled === true) {
		YAHOO.util.Dom.addClass(this.container, this.s.disabledClass);
		this.disabled = true;
	} else {
		YAHOO.util.Dom.removeClass(this.container, this.s.disabledClass);
		this.disabled = false;
	}
	this.el.disabled = this.disabled;
}

YAHOO.SolutionSet.widget.StyledSelect.prototype.findElements = function() {
	var alleles = this.el.childNodes;
	var eles = new Array();
	var totalEles = 0;
	var makeSelected = false;
	var currentRow = 0;
	var ul = document.createElement('ul');
	
	// add top level <option> and <optgroups> to eles, and count all rows (totalEles) that will get outputted
	for (var i=0; i<alleles.length; i++) {
		switch (String(alleles[i].tagName).toLowerCase()) {
			case 'optgroup':
				eles.push(alleles[i]);
				var opts = alleles[i].getElementsByTagName('option');
				if (alleles[i].label) {
					totalEles++;
				}
				totalEles = totalEles + opts.length;
			case 'option':
				if (alleles[i].childNodes.length == 1) {
					eles.push(alleles[i]);
					totalEles++;
				}
		}
	}
	
	// Check how many rows we are expecting
	var expectedCols = (Math.ceil(totalEles * this.s.pxPerRow / this.s.maxHeight) > this.s.maxCols ? this.s.maxCols : Math.ceil(totalEles * this.s.pxPerRow / this.s.maxHeight));
	
	for (var i=0; i<eles.length; i++) {
		
		// Close our current UL and start a new one
		if (currentRow > Math.ceil(totalEles / expectedCols) - 1) {
			YAHOO.util.Dom.addClass(ul, 'col');
			this.ulcontainer.appendChild(ul);
			ul = document.createElement('ul');
			currentRow = 0;
			this.actualCols++;
		}
					
		// Fix disabled options in IE
		if(eles[i].selected && eles[i].disabled){
			makeSelected = true;
		}
		if(makeSelected && !eles[i].disabled && String(eles[i].tagName).toLowerCase() == "option"){
			makeSelected = false;
			eles[i].selected = true;
		}
		
		switch (String(eles[i].tagName).toLowerCase()) {
			case 'optgroup':
				var li = document.createElement('li');
				var optgroupUl = document.createElement('ul');
				
				if (eles[i].label) {
					YAHOO.util.Dom.addClass(li, 'optlabel');
					if (eles[i].disabled) {
						YAHOO.util.Dom.addClass(li, 'disabled');
						YAHOO.util.Dom.addClass(optgroupUl, 'disabled');
					}
					YAHOO.util.Dom.addClass(optgroupUl, eles[i].className);
					li.innerHTML = eles[i].label;
					ul.appendChild(li);
					currentRow++;
				}
				
				var opts = eles[i].getElementsByTagName('option');
				currentRow = currentRow + opts.length;

				for (var j=0; j<opts.length; j++) {
					// Fix disabled options in IE
					if(opts[j].selected && (eles[i].disabled || opts[j].disabled)){
						makeSelected = true;
					}
					if(makeSelected && !eles[i].disabled && !opts[j].disabled){
						makeSelected = false;
						opts[j].selected = true;
					}
					
					this.createElement(opts[j], eles[i].disabled, optgroupUl);
				}
				ul.appendChild(optgroupUl);
				break;
			case 'option':
				if (eles[i].childNodes.length == 1) {
					this.createElement(eles[i], eles[i].disabled, ul);
					currentRow++;
				}
				break;
		}
	}
	this.updateLabel();
	this.actualCols++;
	if (this.actualCols > 1) {
		YAHOO.util.Dom.addClass(ul, 'col');
	}
	this.ulcontainer.appendChild(ul);
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.createElement = function(el, state, parent) {
	// Fix disabled options in IE
	var elSelected = (el.selected && (!el.disabled && (!el.parentNode || !el.parentNode.disabled))) ? true : false;
	
	var itemvar = {};
	itemvar.value = el.value;
	itemvar.el = el;
	itemvar.selected = elSelected;
	
	itemvar.disabled = (el.disabled || el.parentNode.disabled) ? true : false;
	itemvar.text = el.text;
	itemvar.li = document.createElement('li');
	if(itemvar.el.className != ''){
		YAHOO.util.Dom.addClass(itemvar.li, itemvar.el.className);
	}
	if (state == true) {
		itemvar.li.innerHTML = el.text;
		YAHOO.util.Dom.addClass(itemvar.li, 'disabled');
	}
	else {
		itemvar.a = document.createElement('a');
		itemvar.a.href = 'javascript:void(0);';
		itemvar.text = el.text;
		if(this.s.isMultiSelect){
			itemvar.a.innerHTML = '<input type="checkbox" class="checkbox" ' + ((itemvar.selected)? ' checked="checked"' : '' ) + '> ' + el.text;
		}else{
			itemvar.a.innerHTML = el.text;
		}
		itemvar.li.appendChild(itemvar.a);
	}
	parent.appendChild(itemvar.li);
	this.items.push(itemvar);
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.createContainer = function(){
	YAHOO.util.Dom.addClass(this.label, 'label');
	
	var icon = document.createElement('div');
	YAHOO.util.Dom.addClass(icon, 'icon');
	icon.appendChild(this.labelText);
	this.label.appendChild(icon);
	if (!this.s.alwaysOpen) {
		this.container.appendChild(this.label);
	}
	
	// container width
	if (this.s.fixedWidth == true && this.s.width) {
		this.container.style.width = this.s.width + 'px'; // set the width of the original <select>
	}
	
	var wrapper = document.createElement("div");
	wrapper.className = "StyledSelectContainer";
	wrapper.appendChild(this.container);
	var classes = this.el.className.split(" "),
	newclasses = [];
	for (var i=0, newclass; newclass=classes[i]; i++) {
		if (newclass != "StyledSelect" && newclass != "StyledSelectReplaced" && newclass != "") {
			newclasses.push(newclass);
		}
	}
	if(newclasses.length > 0){
		YAHOO.util.Dom.addClass(wrapper, newclasses.join(" "));
	}
	this.el.parentNode.insertBefore(wrapper, this.el.nextSibling);
	
	this.updateLabel();
	return;
	var text = [];
	var opts = this.el.getElementsByTagName('option');
	for(var i=0, len=opts.length; i<len; i++)
		if(opts[i].selected)
			text.push(opts[i].text);
	text = text.join(', ');
	if(text == '')
		text = this.s.defaultLabel;
	this.setLabel(text);
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.createUL = function() {
	YAHOO.util.Dom.addClass(this.ulcontainer, 'ulcontainer');
	this.container.appendChild(this.ulcontainer);
	this.ulcontainer.style.display = "";;
	
	var width = this.container.offsetWidth;
	var uldims = {width:this.ulcontainer.offsetWidth, height:this.ulcontainer.offsetHeight};
	
	var uls = this.ulcontainer.childNodes;
	var ulswidth = 0;
	for (i=0; i<uls.length; i++) {
		if (uls[i].tagName.toLowerCase() == 'ul') {
			ulswidth += uls[i].offsetWidth;
			
			// Add class for IE styling
			if (this.s.isMultiSelect) {
				YAHOO.util.Dom.addClass(uls[i], 'multiple');
			}
		}
	}
	
	
	// dropdown width
	if (ulswidth > width) {
		// options longer than select
		this.ulcontainer.style.width = ulswidth + ((!isNaN(this.s.maxHeight) && uldims.height > this.s.maxHeight) ? 17 : 0) + 'px';
		//alert ('2');
	}
	else if (this.s.fixedWidth == true) {
		// set the width of the original <select>
		this.ulcontainer.style.width = width + 'px';
		this.ulcontainer.style.width = width + (width - this.ulcontainer.offsetWidth) + 'px';
		//alert ('3');
	}
	
	if (!isNaN(this.s.maxHeight) && uldims.height > this.s.maxHeight) {
		this.ulcontainer.style.height = this.s.maxHeight + 'px';
		this.ulcontainer.style.overflow= 'auto';
		// Corrects IE overflow issue
		if (YAHOO.env.ua.ie) {
			for (var i=0, ul; ul = uls[i]; i++) {
				if (ul.tagName.toLowerCase() == 'ul') {
					// 17px is the width of the scroll bar in IE
					ul.style.width = this.ulcontainer.style.width.substr(0, this.ulcontainer.style.width.length-2) - 17 + 'px';
				}
			}
		}
	}
	
	
	var left = String(YAHOO.util.Dom.getX(this.ulcontainer));
	left = Number(left.substring(0, left.indexOf(',')));
	left += this.ulcontainer.offsetWidth;
	if(document.getElementsByTagName('body')[0].offsetWidth < left){
		this.ulcontainer.style.right = "0px";
	}
	
	var uldims = {width:this.ulcontainer.offsetWidth, height:this.ulcontainer.offsetHeight};
	
	if (document.all) { 
		/* create an iFrame to fix IE bug */
		this.imask.scrolling = 'no';
		this.imask.frameborder = '0';
		this.imask.src = 'javascript:void(0);';
		this.imask.style.display = 'none';
		this.imask.style.position = 'absolute';
		this.imask.style.marginTop = '-1';
		this.imask.style.zIndex = 10;
		this.ulcontainer.style.zIndex = 11;
		this.imask.style.width = uldims.width + 'px';
		this.imask.style.height = uldims.height + 'px';
		this.container.appendChild(this.imask);
		this.imask.style.top = this.ulcontainer.style.top;
		if(document.getElementsByTagName('body')[0].offsetWidth < left){
			this.imask.style.right = "0px";
		}
	}
	
	this.ulInit = true;
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.clickLabel = function(e) {
	//is disabled?
	if (this.disabled === true) {
		return;
	}
	if(!this.ulInit){	//ul not initialized?
		this.findElements();
		this.createUL();
		this.initializeULEvents();
		this.show();
	}
	else
		this.toggle();
	for (var i=0; i<this.items.length; i++) {
		YAHOO.util.Dom.removeClass(this.items[i].li, this.s.hoverClass);
		YAHOO.util.Dom.removeClass(this.items[i].li, this.s.selectedClass);
		if (this.items[i].selected == true) {
			YAHOO.util.Dom.addClass(this.items[i].li, this.s.hoverClass);
			YAHOO.util.Dom.addClass(this.items[i].li, this.s.selectedClass);
		}
	}
	if (typeof(this.el.onclick) == 'function') {
		this.el.onclick();
	}
	if(this.ulcontainer.style.display == "none"){
		this.hide();
		if (typeof(this.el.onclose) == 'function') {
			this.el.onclose();
		}
		/* old - skipping this */
		/*if (typeof(Event.fire) == 'function') {
			Event.fire(this.el, "close");
		}*/
	}
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.toggle = function() {
	if(this.ulcontainer.style.display != "none"){
		this.hide();
	}else{
		this.show();
	}
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.hide = function() {
	if (this.s.alwaysOpen) {
		return;
	}
	this.ulcontainer.style.display = "none";
	if (document.all) {
		this.imask.style.display = "none";
	}
	
	YAHOO.util.Dom.removeClass(this.container, this.s.openClass);
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.show = function() {
	this.ulcontainer.style.display = "";
	if (document.all) {
		this.imask.style.display = "";
	}
	var left = String(YAHOO.util.Dom.getX(this.ulcontainer));
	left = Number(left.substring(0, left.indexOf(',')));
	left += this.ulcontainer.offsetWidth;
	if(document.getElementsByTagName('body')[0].offsetWidth < left){
		this.ulcontainer.style.right = "0px";
	}
	
	YAHOO.util.Dom.addClass(this.container, this.s.openClass);
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.checkItem = function(itemvar, index){
	itemvar.selected = true;
	itemvar.el.selected = true;
	if(itemvar.li.getElementsByTagName('input')[0]){
		itemvar.li.getElementsByTagName('input')[0].checked = true;
	}
	YAHOO.util.Dom.addClass(itemvar.li, this.s.selectedClass);
	if(index){
		this.el.selectedIndex = index;
	}
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.uncheckItem = function(itemvar){
	itemvar.selected = false;
	itemvar.el.selected = false;
	if(itemvar.li.getElementsByTagName('input')[0]){
		itemvar.li.getElementsByTagName('input')[0].checked = false;
	}
	YAHOO.util.Dom.removeClass(itemvar.li, this.s.selectedClass);
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.onClick = function(e) {
	var ele = YAHOO.util.Event.getTarget(e);
	while(ele && ele != this.container){
		if(ele.tagName.toLowerCase() == 'a'){
			break;
		}
		ele = ele.parentNode;
	}
	if(ele.tagName.toLowerCase() != 'a'){
		return;
	}
	
	for (var i=0; i<this.items.length; i++) {
		if (this.items[i].a == ele) {
			if(YAHOO.util.Dom.hasClass(this.items[i].li, "disabled")){
				this.uncheckItem(this.items[i]);
				return;
			}
		}
	}
	
	if(this.s.isMultiSelect){
		for (var i=0; i<this.items.length; i++) {
			if (this.items[i].a == ele) {
				if(this.items[i].selected == true){
					this.uncheckItem(this.items[i]);
				}else{
					this.checkItem(this.items[i]);
				}
				break;
			}
		}
	}else{
		for (var i=0; i<this.items.length; i++) {
			if (this.items[i].a == ele) {
				this.checkItem(this.items[i], i);
			} else {
				this.uncheckItem(this.items[i]);
			}
		}
		this.hide();
	}
	var allchecked = true;
	for(var x=0; x<this.items.length; x++){
		if(this.items[x].a == ele){
			var itemvar = this.items[x];
		}
		if(this.items[x].selected == false && !YAHOO.util.Dom.hasClass(this.items[x].li, this.s.toggleClass) && !this.items[x].disabled){
			allchecked = false;
		}
	}
	if(YAHOO.util.Dom.hasClass(itemvar.li, this.s.toggleClass)){
		for(var x=0; x<this.items.length; x++){
			if(allchecked){
				//uncheck all
				this.uncheckItem(this.items[x]);
			}else{
				//check all
				if (!this.items[x].disabled) {
					this.checkItem(this.items[x]);
				}
			}
		}
	}else if(allchecked){
		for(var x=0; x<this.items.length; x++){
			if(YAHOO.util.Dom.hasClass(this.items[x].li, this.s.toggleClass)){
				//alert ('ran');
				this.checkItem(this.items[x]);
			}
		}
	}else{
		for(var x=0; x<this.items.length; x++){
			if(YAHOO.util.Dom.hasClass(this.items[x].li, this.s.toggleClass)){
				this.uncheckItem(this.items[x]);
			}
		}
	}
	this.updateLabel();
	if (typeof(this.el.onclick) == 'function') {
		this.el.onclick();
	}
	if (typeof(this.el.onchange) == 'function') {
		this.el.onchange();
	}
	/* old - */
	/*
	if (typeof(Event.fire) == 'function') {
		Event.fire(this.el, "change");
	}
	*/
	this.fireEvent("change");
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.onMouseOver = function (e) {
	var ele = YAHOO.util.Event.getTarget(e);
	if(ele.tagName.toLowerCase() != 'a' && ele.tagName.toLowerCase() != 'li'){
		return;
	}
	for (var i=0; i<this.items.length; i++) {
		YAHOO.util.Dom.removeClass(this.items[i].li, this.s.hoverClass);
		if (this.items[i].a == ele) {
			YAHOO.util.Dom.addClass(this.items[i].li, this.s.hoverClass);
		}
	}
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.initializeULEvents = function() {
	YAHOO.util.Event.addListener(this.ulcontainer, 'click', this.onClick, this, true);
	YAHOO.util.Event.addListener(this.ulcontainer, 'mouseover', this.onMouseOver, this, true);
	YAHOO.util.Event.addListener(document, 'click', this.onBlur, this, true);
	YAHOO.util.Event.addListener(window, 'blur', this.onBlur, this, true);
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.onBlur = function (e) {
	var el = YAHOO.util.Event.getTarget(e);
	var found = false;
	do {
		if (el == null || el == window || el == document.body) break;
		if(el == this.container){
			found = true;
			break;
		}
	} while(el = el.parentNode);
	if(!found && this.ulcontainer.style.display != "none"){
		this.hide();
		if (typeof(this.el.onclose) == 'function') {
			this.el.onclose();
		}
		/* old */
		/*
		if (typeof(Event.fire) == 'function') {
			Event.fire(this.el, "close");
		}*/
	}
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.updateLabel = function() {
	var selectedItems = [],
	allSelected = true,
	items = this.el.getElementsByTagName('option');
	for(var x=0; x<items.length; x++){
		if(items[x].selected && !YAHOO.util.Dom.hasClass(items[x].li, this.s.toggleClass)){
			selectedItems.push(items[x].text);
		} else if(!items[x].selected) {
			allSelected = false;
		}
	}
	
	if (this.s.defaultAllLabel !== false && allSelected) {
		var text = this.s.defaultAllLabel;
	} else {
		var text = selectedItems.join(', ');
	}
	if(text == ''){
		text = this.s.defaultLabel;
	}
	this.setLabel(text);
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.setLabel = function(text) {
	//shorten text if necessary
	var shorttext = text.substring(0, Math.round(this.s.width/this.s.pxPerChar));
	if(shorttext != text)
		text = shorttext.substring(0, shorttext.length-2) + '\u2026'; // truncate text and replace last character with ellipsis
		
	//this.labelText.innerHTML = text; // This brakes in IE
	var text = document.createTextNode(text);
	this.labelText.innerHTML = '';
	this.labelText.appendChild(text);
	//old - I think the above line is correct - this.labelText.insertBefore(text, this.labelText.firstChild);
};

YAHOO.SolutionSet.widget.StyledSelect.prototype.fireEvent = function(type) {
	var listeners =YAHOO.util.Event.getListeners(this.el, type);
	
	if (listeners) {
		for (var i=0, listener; listener = listeners[i]; i++) {
			listener.fn.call(listener.scope, {
				type: "change",
				target: this.el,
				currentTarget: this.el,
				explicitOriginalTarget: this.el,
				eventPhase: 2,
				timeStamp: 0,
				bubbles: true,
				cancelable: true,
				isTrusted: true
			});
		}
	}
}

YAHOO.SolutionSet.widget.StyledSelect.prototype.onResetForm = function(e) {
	var text = [];
	for (var i=0, item2; item2 = this.items[i]; i++) {
		for (var j=0, option2; option2 = this.defaultSelectedItems[j]; j++) {
			if (item2.el == option2) {
				this.checkItem(item2);
				break;
			}
		}
		if (!this.defaultSelectedItems[j]) {
			this.uncheckItem(item2);
		}
	}
	this.updateLabel();
	
	if (typeof(this.el.onchange) == 'function') {
		this.el.onchange();
	}
	this.fireEvent("change");
}

YAHOO.SolutionSet.widget.StyledSelect.prototype.destroy = function() {
	var parentNode = this.container.parentNode;
	parentNode.parentNode.removeChild(parentNode);
	
	YAHOO.util.Dom.removeClass(this.el, this.s.processed);
	
	YAHOO.util.Event.removeListener(this.label, this.clickLabel);
	YAHOO.util.Event.removeListener(this.form, this.onResetForm);
	
	this.el.style.display = "";
	
	this.el.styledSelectInstance = null;
	//delete(this.el.styledSelectInstance);
}
