/* stupid IE doesnt know the array index function, now it does.... smart programmer */
if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
      if (this[i] == obj) {
        return i;
      }
    }
    return -1;
  }
}

function daisyconSelect( sID, iFixedWidth )
{
	if(null !== document.getElementById(sID+'daisySelect'))
	{
		return false;
	}
	this.bOpen = false;
	this.bMouseIn = false;
	this.tDropdownBoxTimer = null;
	
	this.oSelect = document.getElementById( sID );
	if (null === this.oSelect)
	{
		return;
	}
	this.charBuffer = "";

	this.bFirstItemInvisible = false;
	this.oSelect.style.display = '';
	this.oSelect.style.visibility = 'hidden';
	this.oSelect.daisySelect = this;
	this.bMultiSelect = this.oSelect.getAttribute('multiple') ? true : false;
	this.aCheckBoxes = new Array();

	//add onclick handlers
	var thisRef = this;
	this.oSelect.populate = function() { thisRef.populateList(); }

	this.doActionByClick = function() { if (thisRef.bOpen === true)
									   {
									   	thisRef.collapse();
									   }
									   else
									   {
										thisRef.expand();
									   };
									};
	var expand = function() { if( thisRef.bOpen === true ){ thisRef.expand() } };
	var collapse = function() { if( thisRef.bOpen === true ){ thisRef.bMouseIn = false;
															  clearTimeout( thisRef.tDropdownBoxTimer );
															  thisRef.tDropdownBoxTimer = setTimeout( function() { if ( thisRef.bMouseIn != true ) { thisRef.collapse(); } }, 400 )
															} };
	var mouseover = function() { if ( thisRef.bOpen === true ){ thisRef.bMouseIn = true; } };

	//init the select div
	this.oSelectDiv = document.createElement('div');
	this.oSelectDiv.id = sID+'daisySelect';
	this.oSelectDiv.className = 'daisyconSelect';
	
	this.oSelectDivText = document.createElement('div');
	this.oSelectDivText.className = 'selectText';
	var oThisReference = this;
	this.oSelectDivText.onmouseover		=	function()
											{
												this.bWasHidden = false;
												this.hideFullText();
												this.showFullText();
											};
	this.oSelectDivText.onmouseout		=	function()
											{
												this.hideFullText();
											};

	this.oSelectDivText.onmousemove		=	function()
											{
												if (true === oThisReference.bOpen)
												{
													if ("undefined" !== typeof(this.bIsDisplayed) && true === this.bIsDisplayed)
													{
														this.hideFullText();
													}
												}
												else
												{
													if ("undefined" !== typeof(this.bWasHidden) && false === this.bWasHidden)
													{
														this.showFullText();
													}
												}
											}

	this.oSelectDivText.showFullText	=	function()
											{
												if (false === oThisReference.bOpen)
												{
													this.bIsDisplayed = true;
													if ("undefined" === typeof(this.oldWidth) || null === this.oldWidth)
													{
														this.oldWidth 			= 	this.style.width;
													}
													if ("undefined" === typeof(this.oldClassName) || null === this.oldClassName)
													{
														this.oldClassName		= 	this.className;
													}
													this.className			= 	"selectTextHover";
													this.style.minWidth		=	this.oldWidth;
													this.style.width		=	"auto";
													this.style.marginLeft	=	"5px";
													this.style.marginRight	=	"5px";
													if (parseInt((this.offsetWidth - 2), 10) <= parseInt(this.oldWidth.replace("px", ""), 10))
													{
														this.hideFullText();
														this.bWasHidden = true;
													}
												}
											}

	this.oSelectDivText.hideFullText	=	function()
											{
												this.bIsDisplayed = false;
												if ("undefined" !== typeof(this.oldWidth) && null !== this.oldWidth)
												{
													this.style.width = this.oldWidth;
												}
												if ("undefined" !== typeof(this.oldClassName) && null !== this.oldClassName)
												{
													this.className = this.oldClassName;
												}
												this.style.marginLeft	=	"";
												this.style.minWidth		=	"";
												this.style.marginLeft	=	"";
												this.style.marginRight	=	"";
											}


	if ( !isNaN(iFixedWidth) )
	{
		this.oSelectDiv.style.width = iFixedWidth+"px";
		this.oSelectDivText.style.width = (iFixedWidth-10)+"px";
	}
	else if ( this.oSelect.style.width )
	{
		this.oSelectDiv.style.width = (parseInt(this.oSelect.style.width))+"px";
		this.oSelectDivText.style.width = (parseInt(this.oSelect.style.width)-10)+"px";
	}
	else if (this.oSelect.offsetWidth)
	{
		this.oSelectDiv.style.width = ((this.oSelect.offsetWidth < 10 ? 10 : parseInt(this.oSelect.offsetWidth) ))+"px";
		this.oSelectDivText.style.width = ((this.oSelect.offsetWidth < 10 ? 10 : parseInt(this.oSelect.offsetWidth)) - 10)+"px";
	}
	else
	{
		this.oSelectDiv.style.width = "170px";
		this.oSelectDivText.style.width = "160px";
	}

	this.oSelectDiv.appendChild( document.createElement('div') );
	this.oSelectDiv.appendChild( this.oSelectDivText );

	this.oSelectPanel = document.createElement('div');
	this.oSelectPanel.className = 'daisyconSelectPanel';
	this.oSelectPanel.style.position = 'absolute';
	this.oSelectPanel.onmouseout = collapse;
	this.oSelectPanel.onmouseover = mouseover;

	this.oSelectPanelItems = document.createElement('div');
	this.oSelectPanelItems.className = 'items';
	this.oSelectPanel.appendChild( this.oSelectPanelItems );

	this.oDisableDiv = document.createElement('div');
	this.oDisableDiv.className = 'disabled';
//	this.oDisableDiv.style.width = this.oSelectDiv.style.width;
	this.oDisableDiv.style.height = this.oSelectDiv.style.height;

	this.disabled = false;
	if(!this.oSelect.disabled)
	{
		this.enable();
	}
	else
	{
		this.disable();
	}
	this.oSelectDiv.onmouseout = collapse;
	this.oSelectDiv.onmouseover = mouseover;

	if ( this.bMultiSelect === true )
	{
		this.oSelectAll = document.createElement('a');
		this.oSelectAll.href = 'toggle';
		this.oSelectAll.className = 'selector';
		this.oSelectAll.onclick = function( e )
		{
			if (!e) var e = window.event;
				e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();

			if ( this.checked === true )
			{
				this.checked = false;
				thisRef.unselectAll();
			}
			else
			{
				this.checked = true;
				thisRef.selectAll();
			}
			thisRef.oSelectDivText.innerHTML = thisRef.getMultiSelectedText();
			if ( thisRef.oSelect.onchange )
				thisRef.oSelect.onchange();
			return false;
		}
		this.oSelectPanel.appendChild( this.oSelectAll );
	}

	this.oSelectPanelRoundedCorner = document.createElement('div');
	this.oSelectPanelRoundedCorner.className = 'roundedcorner';
	this.oSelectPanel.appendChild( this.oSelectPanelRoundedCorner );

	//we populate the list based on the select
	this.populateList();

	this.oSelect.style.display = 'none';
	this.oSelect.parentNode.insertBefore( this.oSelectDiv, this.oSelect );
}

daisyconSelect.prototype.isDisabled = function()
{
	return this.disabled;
}

daisyconSelect.prototype.disable = function()
{
	this.disabled = true;
	this.oSelectDiv.onclick = null;
// IE fix
//	if ( !this.oDisableDiv.parentNode )
	{
		this.oSelectDiv.appendChild(this.oDisableDiv);
	}
}

daisyconSelect.prototype.enable = function()
{
	this.disabled = false;
	this.oSelectDiv.onclick = this.doActionByClick;
	if ( this.oDisableDiv.parentNode )
	{
		this.oDisableDiv.parentNode.removeChild( this.oDisableDiv );
	}
}

daisyconSelect.prototype.showSelect = function()
{
	this.oSelectDiv.style.display = "";
	this.oSelectDiv.style.visibility = "";
}

daisyconSelect.prototype.hideSelect = function()
{
	this.oSelectDiv.style.display = "none";
	this.oSelectDiv.style.visibility = "hidden";

	return this.oSelectedDiv;
}

daisyconSelect.prototype.populateList = function( criteria )
{
	this.oSelectPanelItems.innerHTML = "";
	var thisRef = this;
	thisRef.oSelectDivText.innerHTML = "";
	for( var iLen = this.oSelect.options.length, iPos = 0; iLen > iPos; ++iPos )
	{
		if ( iPos === 0 && true === this.bFirstItemInvisible )
		{
			continue;
		}
	
		if ( criteria )
		{
			if ( -1 == this.oSelect.options[iPos].text.toUpperCase().indexOf( criteria ) )
				continue;
		}
		var oItem = document.createElement('div');
		oItem.className = 'item';
		oItem.selectValue = this.oSelect.options[iPos].value;
		oItem.selectIndex = iPos;

		if ( this.bMultiSelect === true )
		{
			var oCheckBox = document.createElement('input');
			oCheckBox.setAttribute("type", 'checkbox');
			oCheckBox.onkeyup = function(){ return false; }
			oCheckBox.onkeydown = function(){ return false; }
			oCheckBox.onclick = function( e )
			{
				if (!e) var e = window.event;
					e.cancelBubble = true;
				if (e.stopPropagation) e.stopPropagation();

				thisRef.selectSet( this.parentNode.selectValue, this.checked );
				thisRef.oSelectDivText.innerHTML = thisRef.getMultiSelectedText();

				if ( thisRef.oSelect.onchange )
					thisRef.oSelect.onchange();
			};
			oItem.appendChild( oCheckBox );
			oCheckBox.checked = this.oSelect.options[iPos].selected;

			this.aCheckBoxes[iPos] = oCheckBox;
		}

		if ( criteria )
		{
			var oTmp = document.createElement('span');
			oTmp.innerHTML = this.highlightText( this.oSelect.options[iPos].text, criteria );
			oItem.appendChild( oTmp );
		}
		else
		{
			oItem.appendChild( document.createTextNode( this.oSelect.options[iPos].text ) );
		}

		if ( this.bMultiSelect === true )
		{
			oItem.onclick = function( e ){
				this.firstChild.checked = !this.firstChild.checked;
				thisRef.selectSet( this.selectValue, this.firstChild.checked );
				thisRef.oSelectDivText.innerHTML = thisRef.getMultiSelectedText();
				if ( thisRef.oSelect.onchange )
					thisRef.oSelect.onchange();
			};
		}
		else
		{
			oItem.onclick = function(){
										thisRef.selectEnum( this.selectValue );
										thisRef.collapse();
										thisRef.oSelectDivText.innerHTML = thisRef.oSelect.options[this.selectIndex].text;
										if ( thisRef.oSelect.onchange )
											thisRef.oSelect.onchange();
									};
		}

		if (typeof oItem.onselectstart!="undefined") {
  			oItem.onselectstart = function(){ return false; };
		}
		oItem.onmousedown = function(){ return false; }
		oItem.onmouseup = function(){ return true; }

		this.oSelectPanelItems.appendChild( oItem );
	}

	if ( this.bMultiSelect === true )
	{
		this.oSelectDivText.innerHTML = this.getMultiSelectedText();
	}
	else
	{
		if ( this.oSelect.selectedIndex != -1 )
		{
			this.oSelectDivText.innerHTML = this.oSelect.options[this.oSelect.selectedIndex].innerHTML;
		}
	}
}


daisyconSelect.prototype.getMultiSelectedText = function()
{
	var selectedText = null;
	var allSelected = true;
	var iTotalSelected = 0;
	for( var iLen = this.oSelect.options.length, iPos = 0; iLen > iPos; ++iPos )
	{
		if ( this.oSelect.options[iPos].selected === true )
		{
			iTotalSelected++;
			if ( selectedText === null )
			{
				selectedText = this.oSelect.options[iPos].innerHTML;
			}
			else if ( selectedText !== null && allSelected === false )
			{
				this.oSelectAll.checked = false;
				this.oSelectAll.innerHTML = this.translate('selectall');
				return this.translate('multipleselected');
			}
		}
		else
		{
			allSelected = false;
		}
	}

	if ( iTotalSelected === 0 )
	{
		this.oSelectAll.innerHTML = this.translate('selectall');
		this.oSelectAll.checked = false;
		return this.translate('noneselected');
	}
	else if ( iTotalSelected === 1 )
	{
		this.oSelectAll.innerHTML = this.translate('selectall');
		this.oSelectAll.checked = false;
		return selectedText;
	}
	else if ( iTotalSelected >= 2 && allSelected === true )
	{
		this.oSelectAll.innerHTML = this.translate('unselectall');
		this.oSelectAll.checked = true;
		return this.translate('allselected');
	}
	else
	{
		this.oSelectAll.innerHTML = this.translate('selectall');
		this.oSelectAll.checked = false;
		return this.translate('multipleselected');
	}
}

daisyconSelect.prototype.select = function( selectID, skipOnchange )
{
	if ( true !== skipOnchange )
	{
		skipOnchange = false;
	}

//	this.selectEnum( this.selectValue );
	this.collapse();
	for ( var i = 0; i < this.oSelect.options.length; ++i )
	{
		if (this.oSelect.options[i].value == selectID)
		{
			this.oSelect.options[i].selected = true;
			this.oSelectDivText.innerHTML = this.oSelect.options[i].text;
		}
		else
		{
			this.oSelect.options[i].selected = false;
		}
	}
	if ( !skipOnchange && this.oSelect.onchange )
		thisRef.oSelect.onchange();
}

daisyconSelect.prototype.selectEnum = function( selectID )
{
	this.select( selectID, true );
}

daisyconSelect.prototype.selectSet = function( selectID, checked )
{
	for( var iLen = this.oSelect.options.length, iPos = 0; iLen > iPos; ++iPos )
	{
		if ( this.oSelect.options[iPos].value === selectID )
		{
			this.oSelect.options[iPos].selected = checked;
		}
	}
}

daisyconSelect.prototype.unselectAll = function()
{
	for( var iLen = this.oSelect.options.length, iPos = 0; iLen > iPos; ++iPos )
	{
		this.oSelect.options[iPos].selected = false;
		this.aCheckBoxes[iPos].checked = false;
	}
}

daisyconSelect.prototype.selectAll = function()
{
	for( var iLen = this.oSelect.options.length, iPos = 0; iLen > iPos; ++iPos )
	{
		this.oSelect.options[iPos].selected = true;
		this.aCheckBoxes[iPos].checked = true;
	}
}

daisyconSelect.prototype.collapse = function()
{
	document.body.onkeydown = null;
	this.bOpen = false;
	if ( this.oSelectPanel.parentNode )
	{
		this.oSelectPanel.parentNode.removeChild( this.oSelectPanel );
	}
}

daisyconSelect.prototype.highlightText = function( text, matchingtext )
{
	var pos = 0;
    pos = text.toUpperCase().indexOf( matchingtext );
    if( pos == -1 )
    {
        return false;
    }
    else
    {
    	var textReturn = '';
    	var len = matchingtext.length;

    	if ( pos > 0)
	    	textReturn += text.substr( 0, pos );

	    textReturn += '<b id="highlight">'+text.substr( pos, len )+'</b>';


	    if ( pos+len < text.length )
	    	textReturn += text.substr( pos+len );

	   	return textReturn;
    }
}

daisyconSelect.prototype.expand = function()
{
	var thisRef = this;
	this.populateList();
	this.charBuffer = "";

	document.onkeydown = function( e )
	{

		var event;
		var keycode;
		if (window.event) {event = window.event; keycode = event.keyCode}
		else if (e) { event = e; keycode = e.which;}

		if ( (keycode >= 65 && keycode <= 90) || (keycode >= 48 && keycode <= 57) || keycode == 32 || keycode == 8 )
		{
			if ( keycode == 8 )
			{
				thisRef.charBuffer = thisRef.charBuffer.substr(0,thisRef.charBuffer.length-1);
			}
			else
			{
				thisRef.charBuffer+= String.fromCharCode(keycode);
			}

			thisRef.populateList( thisRef.charBuffer );
		}
		return true;
	}

	var iCurLeft = iCurTop = 0;
	var obj = this.oSelectDiv;
	if (obj.offsetParent)
	{
		//IE 7 fix for elements that "have no layout"
		if (undefined !== obj.style.zoom)
		{
			oObjZoom = obj;
			do
			{
				oObjZoom.style.zoom = 1;
			} while (oObjZoom = oObjZoom.offsetParent);
		}

		do
		{
			iCurLeft += parseInt( obj.offsetLeft );
			iCurTop += parseInt( obj.offsetTop );
		} while (obj = obj.offsetParent);
	}

	this.oSelectPanel.style.left = (iCurLeft + 3) + 'px';
	this.oSelectPanel.style.top = ( iCurTop + parseInt(this.oSelectDiv.offsetHeight) ) + 'px';

	this.bOpen = true;
	document.body.appendChild( this.oSelectPanel );

	if (this.oSelectPanel.offsetWidth < this.oSelectDiv.offsetWidth-31)
	{
		this.oSelectPanel.style.width = parseInt( this.oSelectDiv.offsetWidth-31 ) + 'px';
	}
}

daisyconSelect.prototype.setFirstItemInvisible = function()
{
	this.bFirstItemInvisible = true;
}

daisyconSelect.prototype.getSelectedCount = function()
{
	var iCounted = 0;
	for( var iLen = this.oSelect.options.length, iPos = 0; iLen > iPos; ++iPos )
	{
		if ( this.oSelect.options[iPos].selected == true )
		{
			iCounted++;
		}
	}
	
	return iCounted;
}

daisyconSelect.prototype.translate = function( code )
{
	if ("function" === typeof(translate))
	{
	    return translate(code);
	}
	return code;
}

