YAHOO.namespace("eleganthomes");

YAHOO.eleganthomes.StateRegionUI = function(regionStateArray,regionID,stateID) {
	YAHOO.util.Event.addListener(regionID,"change",function(e) {
		var regionDomEl = YAHOO.util.Dom.get(regionID);
		var stateDomEl = YAHOO.util.Dom.get(stateID);
		var szRegion = regionDomEl.options[regionDomEl.options.selectedIndex].value;
		var currIndex = 0;
		var currentArray = [];
		stateDomEl.options.length = 0;
		stateDomEl.options[currIndex++] = new Option("- Select -","",true);
		for (var i = 0; i < regionStateArray.length; i++) {
			currentArray = regionStateArray[i].split(",");
			if (szRegion === "") {
				stateDomEl.options[currIndex++] = new Option(currentArray[0],currentArray[1],false);
			}
			else {
				for (var j = 2; j < currentArray.length; j++) {
					if (szRegion == currentArray[j]) {
						stateDomEl.options[currIndex++] = new Option(currentArray[0],currentArray[1],false);
					}
				}
			}
		}
	});
};

YAHOO.eleganthomes.CategoryBoxUI = (function () {
	var catUnselected = null;
	var catSelected = null;
	var szCategoryEl = null;
	
	
	
	return (function(catUnselected,catSelected,szCategory,leftArrow,rightArrow) {
		catUnselected = YAHOO.util.Dom.get(catUnselected);
		catSelected = YAHOO.util.Dom.get(catSelected);
		szCategoryEl = YAHOO.util.Dom.get(szCategory);
		// left arrow click event
		YAHOO.util.Event.addListener(leftArrow,"click",function(e) {
			moveItems(catSelected,catUnselected);
			YAHOO.util.Event.preventDefault(e);
		}); 
		
		// right arrow click event
		YAHOO.util.Event.addListener(rightArrow,"click",function(e) {
			moveItems(catUnselected,catSelected);
			YAHOO.util.Event.preventDefault(e);
		});
		
		//double click events		
		YAHOO.util.Event.addListener(catUnselected,"dblclick",function(e) {
			moveItems(catUnselected,catSelected);
			YAHOO.util.Event.preventDefault(e);
		});
		
		YAHOO.util.Event.addListener(catSelected,"dblclick",function(e) {
			moveItems(catSelected,catUnselected);
			YAHOO.util.Event.preventDefault(e);
		});
		
		YAHOO.util.Event.addListener("add-all","click",function(e) {
			moveAll(catUnselected,catSelected);
			YAHOO.util.Event.preventDefault(e);
		});
		
		YAHOO.util.Event.addListener("remove-all","click",function(e) {
			moveAll(catSelected,catUnselected);
			YAHOO.util.Event.preventDefault(e);
		});
		
		var moveAll = function(fromBox,toBox) {
			var optionItems = YAHOO.util.Selector.query("option",fromBox);
			fromBox.options.length = 0;
			for (var i = 0; i < optionItems.length; i++) {
				toBox.options[toBox.options.length] = new Option(optionItems[i].text,optionItems[i].value);
			}
			var szCategory = "";
			for (i = 0; i < catSelected.options.length; i++) {
				if (i === 0) {
					szCategory = catSelected.options[i].value;
				}
				else {
					szCategory += "," + catSelected.options[i].value;
				}
			}
			szCategoryEl.value = szCategory;
		}
		
		var moveItems = function(fromBox,toBox) {
			var optionItems = YAHOO.util.Selector.query("option",fromBox);
			fromBox.options.length = 0;
			var currIndex = 0;
			for (var i = 0; i < optionItems.length; i++) {
				if (optionItems[i].selected) {
					toBox.options[toBox.options.length] = new Option(optionItems[i].text,optionItems[i].value);
				}
				else {
					fromBox.options[currIndex++] = new Option(optionItems[i].text,optionItems[i].value);
				}
			}
			var szCategory = "";
			for (i = 0; i < catSelected.options.length; i++) {
				if (i === 0) {
					szCategory = catSelected.options[i].value;
				}
				else {
					szCategory += "," + catSelected.options[i].value;
				}
			}
			szCategoryEl.value = szCategory;
		};
	});
	
})();