/* http://www.hedgerwow.com/360/dhtml/ui_select_with_fixed_width/ */
YAHOO.namespace( 'YAHOO.Hack' ).FixIESelectWidth = function(el)
{
	this.YUE = YAHOO.util.Event;
	this.YUD = YAHOO.util.Dom;
	this.ie7 = !!(document.uniqueID  &&   typeof(XMLHttpRequest)!='undefined' );
	this.nStartWidth = null;
	this.nEndWidth = null;
	this.poppedUp = false;
	this.el = el;
	
	//Only fix for IE55 ~ IE7	
	if(document.uniqueID && window.createPopup )
	{
		this.YUE.onAvailable(this.el,this.init,this,true);
	}else{
		return false
	};
	
};


YAHOO.namespace( 'YAHOO.Hack' ).FixIESelectWidth.prototype.init = function()
{	
		if( this.el.tagName.toLowerCase() != 'select')
		{
			throw Error('element [' + this.el.id + '] is not <select>');
			return;
		};	
		
		if(!this.YUD.hasClass( this.el.parentNode, 'select-box'))
		{
			throw Error('className select-box is not included for element [' + this.el.id + ']');
			return;
		};
		
		this.YUE.removeListener( this.el, 'mouseover' , this.onMouseOver);
		this.YUE.removeListener( document, 'mousedown' ,this.onMouseDown);
		this.YUE.removeListener( this.el, 'change' ,this.collapseSelect);
		this.YUE.addListener( this.el, 'mouseover' , this.onMouseOver, this, true);
		this.YUE.addListener( document, 'mousedown' ,this.onMouseDown , this, true);
		this.YUE.addListener( this.el, 'change' ,this.collapseSelect , this, true);
};

YAHOO.namespace( 'YAHOO.Hack' ).FixIESelectWidth.prototype.setupThing = function(){
			
		var oRs = this.el.style;
		var oPRs = this.el.parentNode.style;
		oPRs.fontSize = 0;
		
		this.nStartWidth = this.el.offsetWidth;
		
		var sDisplay = this.el.parentNode.style.display.toLowerCase() ;
		if(  sDisplay=='' ||  sDisplay=='inline' ||  sDisplay=='inline-block' )
		{
			oPRs.display = 'inline-block';
			oPRs.width = this.el.offsetWidth + 'px';
			oPRs.height = this.el.offsetHeight + 'px';
			oPRs.position = 'relative';
			oRs.position = 'absolute';
			oRs.top = 0;
			oRs.left = 0;
		};
		
		this.el.style.width = 'auto';
		this.nEndWidth  = this.el.offsetWidth;
		
		// If the text is smaller than the select box, don't do anything
		if (this.nEndWidth <= this.nStartWidth)
		{
			this.el.style.width = this.nStartWidth + 'px';
			return;
		}
		
		this.el.style.width = this.nEndWidth + 'px';
		
	};
	
	
YAHOO.namespace( 'YAHOO.Hack' ).FixIESelectWidth.prototype.collapseSelect = function(e)
{
	this.poppedUp = false;
	this.el.style.width = this.nStartWidth + 'px';
};
	

YAHOO.namespace( 'YAHOO.Hack' ).FixIESelectWidth.prototype.onMouseOver = function(e)
{
	if(!this.poppedUp){
	
		this.poppedUp = true;
		this.setupThing();
		
	}	
};


YAHOO.namespace( 'YAHOO.Hack' ).FixIESelectWidth.prototype.onMouseDown = function(e,el)
{
	
	if(!this.poppedUp){
		return false;
	}
		
	el = ( e.srcElement || e.target );
	
	if( el != this.el ){
		this.collapseSelect();
	}
	
};
