/*
SCRIPT DEPENDENCIES:
	/scripts/commonUtils.js
*/
var dropDown = new Object;

dropDown.initialize = function(id){
	//addCSSFileToHead('/styles/dropDown.css');
	var dd = $('dropDownElHood_'+id);
	if (dd.getAttribute('fillType') == 'missingParams'){
		alert( dd.getAttribute('fillType') + '\nDropDown id: ' + dd.id );
	}
	else{
		var cont = document.createElement('div');
		cont.className = 'dropDownListContainer';
		cont.id = 'dropDownList_'+id;
		cont.style.display = 'none';
		cont.setAttribute('ownerDropDown',dd.id);
		dd.setAttribute('dropDownListID',cont.id);
		var ul = document.createElement('ul');
		ul.className = 'dropDownList';
		if ( dd.getAttribute('fillType') == 'stringSplit' ){
			//fill the list by slpitting the string
			var items = dd.getAttribute('stringToSplit').toString().split(dd.getAttribute('pattern'));
			dd.setAttribute('stringToSplit','');
			var values = dd.getAttribute('stringValuesToSplit').toString().split(dd.getAttribute('pattern'));
			dd.setAttribute('stringValuesToSplit','');
			var selectedItem = null;
			for ( var i = 0; i < items.length; i++ ){
				var itm = dropDown.createNewItem(dd, values[i], items[i]);
				if ( dd.getAttribute('selectedValue') == values[i] )
					selectedItem = itm;
					ul.appendChild(itm);
				itm=null;
			}
			values=items=null;
		}
		else if( dd.getAttribute('fillType') == 'lookup' ){
			//fill the list by Ajax look up
		}
		cont.appendChild(ul);
		//if there is a default selectedItem then select it
		if (selectedItem)
			dropDown.itemClick(selectedItem.firstChild);
		document.body.insertBefore(cont, document.body.firstChild);
		
		ul=cont=null;
	}
	dd=null;
}

dropDown.createNewItem = function(dd, value, text){
	var li = document.createElement('li');
	var a = document.createElement('a');
	a.href = '#';
	a.innerHTML = text;
	a.onclick = new Function('dropDown.itemClick(this); ' + dd.getAttribute('functionOnChange') + '; return false;' );
	a.setAttribute('itemValue', value);
	a.style.color	="#FFFFFF";
	li.appendChild(a);
	return li;
}

dropDown.click = function(dd){
	var listCont = $( dd.getAttribute('dropDownListID') );
	if ( listCont.style.display == 'none' ){
		//set position of absolute list div
		var pos = Position.cumulativeOffset(dd);
		listCont.style.top = ( pos[1] + Element.getDimensions(dd).height ) + 'px';
		listCont.style.left = pos[0] + 'px' ;
		pos=null;
		//now make it visible
		listCont.style.display = 'block';
	}
	else
		listCont.style.display = 'none';
	listCont=dd=null;
}

dropDown.itemClick = function(item){
	var listCont = item.parentNode.parentNode.parentNode ;
	var dd = $( listCont.getAttribute('ownerDropDown') );
	listCont.style.display = 'none';
	var inputH = dd.getElementsByTagName('input')[0];
	var selectedDiv = dd.getElementsByTagName('div')[0];
	inputH.value = item.getAttribute('itemValue');
	selectedDiv.innerHTML = item.innerHTML;
	selectedDiv=dd=inputH=listCont=null;
}

dropDown.getSelectedValue = function(dropDownID){
	return $('dropDownElHood_'+dropDownID).getElementsByTagName('input')[0].value  ;
}
