/* combo boxes */

/*	document.write('<s'+'tyle type="text\/css">');
	document.write('.comboBox_valueDisplay {display: block;}');
	document.write('.comboBox_selectedItems {display: block;}');
	document.write('.comboBox_items {position:relative; display: none;}');
	document.write('.comboBox_itemsList {position: absolute; z-index: 999;}');
	document.write('<\/s'+'tyle'+'>');
	*/
	
/*

//elements.js
//string.js
//object.js
*/


var ComboBoxes = {
	callLater : function(functionName, functionParam){
		return (function(){
			functionName(functionParam);
		});
	},
	applyFilters : function(comboBoxesContainerId,URLPrefix,URLSuffix) { // baseURL - no trailing slash
	//applyFilters : function(URLPrefix,URLSuffix) { // baseURL - no trailing slash
		var filtersURL = new Array();
		var cbs = ComboBoxes.cbObjects[comboBoxesContainerId];
		//var cbs = ComboBoxes.cbObjects;
		for (var key in cbs) {
			var cbObj = cbs[key];
			if (typeof(cbObj)!='function' && cbObj.fieldValues!=null && cbObj.fieldValues!='undefined' && cbObj.fieldLabels!=null && cbObj.fieldLabels!='undefined') {
				for (var fieldId in cbObj.fieldValues) {
					if (typeof(cbObj.fieldLabels[fieldId])=='number' || typeof(cbObj.fieldLabels[fieldId])=='string' || typeof(cbObj.fieldLabels[fieldId])=='boolean') {
						filtersURL.push(cbObj.fieldLabels[fieldId].cleanFileName() + ":" + cbObj.fieldValues[fieldId].cleanFileName());
					}
				}
			}
		}
		document.location.href = URLPrefix+filtersURL.join("/")+URLSuffix;
		return false;
	},
	checkComboBoxItem : function(comboBoxesContainerId,comboBoxId,comboBoxItemObj,fieldId,fieldValue,fieldLabel,noSelectionLabel) {
	//checkComboBoxItem : function(comboBoxId,comboBoxItemObj,fieldId,fieldValue,fieldLabel,noSelectionLabel) {
		var cbObj = ComboBoxes.cbObjects[comboBoxesContainerId][comboBoxId];
		//var cbObj = ComboBoxes.cbObjects[comboBoxId];
				
		if (comboBoxItemObj.className == 'checked') {
			comboBoxItemObj.className = 'unchecked';
			cbObj.fieldLabels[fieldId]=null;
			cbObj.fieldValues[fieldId]=null;
		} else {
			comboBoxItemObj.className = 'checked';
			cbObj.fieldLabels[fieldId]=fieldLabel;
			cbObj.fieldValues[fieldId]=fieldValue;
		}
		
		ComboBoxes.setComboBoxSelectedValues(cbObj,noSelectionLabel);
	},
	showComboBoxItems : function(cbObj) {
		cbObj.cb_i.style.display = 'block';
		cbObj.cb_vd.style.display = 'none';
		cbObj.shown = true;
	},
	hideComboBoxItems : function(cbObj) {
		cbObj.cb_i.style.display = 'none';
		cbObj.cb_vd.style.display = 'block';
		cbObj.shown = false;
	},
	setComboBoxSelectedValues : function(cbObj,noSelectionLabel) {
		var sel_vals_str = joinElements(cbObj.fieldLabels, ', ');
		if (sel_vals_str=='') {sel_vals_str=noSelectionLabel;}
		var sel_vals_si_str = '<a href="#" onclick="return false;"><span class="txt">'+sel_vals_str.cutString(38)+'</span></a>';	
		var sel_vals_vd_str = '<a href="#" onclick="return false;"><span class="txt">'+sel_vals_str.cutString(21)+'</span></a>';	
		cbObj.cb_si.innerHTML = sel_vals_si_str;
		cbObj.cb_vd.innerHTML = sel_vals_vd_str;
	},
	initComboBox : function(comboBoxesContainerId,comboBoxId) { 
	//initComboBox : function(comboBoxId) { 
		var __this = this;
		this.cbObjects[comboBoxesContainerId][comboBoxId] = new Object();
		//this.cbObjects[comboBoxId] = new Object();
		var cbObj = this.cbObjects[comboBoxesContainerId][comboBoxId];
		//var cbObj = this.cbObjects[comboBoxId];
		cbObj.fieldLabels = new Object();
		cbObj.fieldValues = new Object();
		
		cbObj.cb = findElement(comboBoxId);
		cbObj.cb_l = getFirstElementByClassName(cbObj.cb,'comboBox_label');
		cbObj.cb_vd = getFirstElementByClassName(cbObj.cb,'comboBox_valueDisplay');
		cbObj.cb_i = getFirstElementByClassName(cbObj.cb,'comboBox_items');
		cbObj.cb_il = getFirstElementByClassName(cbObj.cb,'comboBox_itemsList');
		
		cbObj.cb_vd.style.display = 'block';
		cbObj.cb_i.style.position = 'relative';
		cbObj.cb_i.style.display = 'none';
		cbObj.cb_il.style.position = 'absolute';
		cbObj.cb_il.style.zIndex = '999';
		
		var noSelectionLabel = getFirstElementByClassName(cbObj.cb_vd,'txt').innerHTML;
		
		var showComboBoxItemsTOut = this.callLater(this.showComboBoxItems, cbObj);
		var hideComboBoxItemsTOut = this.callLater(this.hideComboBoxItems, cbObj);
		
		var cb_il_innerHTML = cbObj.cb_il.innerHTML;
		var cb_vd_innerHTML = cbObj.cb_vd.innerHTML;
		cb_il_innerHTML = '<div class="comboBox_selectedItems">'+cb_vd_innerHTML+'</div>'+cb_il_innerHTML+'<div class="comboBox_closeList"><a href="#">'+this.closeListLabel+'</a></div>';
		cbObj.cb_il.innerHTML = cb_il_innerHTML;
		
		cbObj.cb_si = getFirstElementByClassName(cbObj.cb,'comboBox_selectedItems');
		cbObj.cb_si.style.display = 'block';
		
		cbObj.cb_cl = getFirstElementByClassName(cbObj.cb,'comboBox_closeList');
		
		var comboBoxItems = getElementsByClassName(cbObj.cb_i,'comboBox_item');
		for (var i=0; i<comboBoxItems.length; i++) {
			var itemA = comboBoxItems[i].getElementsByTagName('A')[0];
			var fieldLabel = getFirstElementByClassName(itemA,'item_label').innerHTML;
			var fieldValue = getFirstElementByClassName(itemA,'item_value').innerHTML;
			if (comboBoxItems[i].getElementsByTagName('A')[0].className == 'checked') {
				cbObj.fieldLabels[comboBoxItems[i].id]=fieldLabel;
				cbObj.fieldValues[comboBoxItems[i].id]=fieldValue;
			} else {
				cbObj.fieldLabels[comboBoxItems[i].id]=null;
				cbObj.fieldValues[comboBoxItems[i].id]=null;
			}
			
			itemA.onclick=function(e){return false;}
			comboBoxItems[i].onclick=function(e){
				var itemA = this.getElementsByTagName('A')[0];
				var fieldLabel = getFirstElementByClassName(itemA,'item_label').innerHTML;
				var fieldValue = getFirstElementByClassName(itemA,'item_value').innerHTML;
				__this.checkComboBoxItem(comboBoxesContainerId,comboBoxId,itemA,this.id,fieldValue,fieldLabel,noSelectionLabel);
				//__this.checkComboBoxItem(comboBoxId,itemA,this.id,fieldValue,fieldLabel,noSelectionLabel);
			}
		}
		
		this.setComboBoxSelectedValues(cbObj,noSelectionLabel);
		
		this.hideComboBoxItems(cbObj);
		cbObj.cb.onmouseover=function(e){this.className='comboBox active';}
		cbObj.cb.onmouseout=function(e){this.className='comboBox normal';}
		cbObj.cb_l.onclick=function(e){
			if (!cbObj.shown) {
				__this.showComboBoxItems(cbObj);
				cbObj.tOut = setTimeout(hideComboBoxItemsTOut,1000);						
			} else {
				__this.hideComboBoxItems(cbObj);
			}
		}
		cbObj.cb_vd.onclick=function(e){
			if(cbObj.tOut) (clearTimeout(cbObj.tOut));
			__this.showComboBoxItems(cbObj);
		}
				
		//cbObj.cb_i.style.width = parseInt( getW(cbObj.cb_vd) ) + 'px';
		//cbObj.cb_si.style.width = parseInt( getW(cbObj.cb_vd) - getStylePropNumVal(cbObj.cb_vd,'paddingLeft') - getStylePropNumVal(cbObj.cb_vd,'paddingRight') - getStylePropNumVal(cbObj.cb_vd,'borderLeftWidth') - getStylePropNumVal(cbObj.cb_vd,'borderRightWidth') ) +'px';
		
		cbObj.cb_i.onmouseout = function(e) {
			cbObj.tOut = setTimeout(hideComboBoxItemsTOut,500);
		}
		cbObj.cb_i.onmouseover  = function(e) {
			if(cbObj.tOut) (clearTimeout(cbObj.tOut));
		}
		cbObj.cb_cl.onclick=function(e){
			__this.hideComboBoxItems(cbObj);
			return false;
		}
		
	},
	init : function(comboBoxesContainerId,closeListLabel) {
		if (!findElement(comboBoxesContainerId) || !findElement(comboBoxesContainerId).innerHTML) {return false;}
		this.closeListLabel = closeListLabel;
		if (!this.cbObjects) {this.cbObjects=new Object();}
		if (!this.cbObjects[comboBoxesContainerId]) {this.cbObjects[comboBoxesContainerId]=new Object();}
		var cbContainer = findElement(comboBoxesContainerId);
		var comboBoxes = getElementsByClassName(cbContainer,'comboBox');
		for (var i=0; i<comboBoxes.length; i++) {
			this.initComboBox(comboBoxesContainerId,comboBoxes[i].id); 
			//this.initComboBox(comboBoxes[i].id); 
		}		
	}
}