/* Dropdown width fixer for IE (tested with ver 6 and 7) */
/* Written by: Jarrod McGuire - July 2007 */
/* addEvent borrowed from John Resig, changed function name to not conflict with any existing addEvents. */
/* The original addEvent should be used here instead, but this is intended to be standalone code, */
/* it can be dumped pretty much anywhere. */

function setupSelectFix( event )
{
    if( !document.getElementById || !document.insertBefore || !document.createElement || !document.appendChild ) return;
    if( event.target ) return; // mozilla knows how to take care of itself
    
    /* Get all of the dropdowns on the page */
    var selArray = document.getElementsByTagName( "SELECT" );
    
    var el = "";
    var contDiv = "";

    for( var x = 0;x < selArray.length;x++)
    {
        el = selArray[x];
        /* inline attribute can be set on chosen dropdowns */
//        if( !el.getAttribute( "fixWidth" ) ) continue;

        /* Create new element and wrap around the dropdown. Doesn't
           have to be a span, just some inline element */
        contDiv = document.createElement( "SPAN" );
        el.parentNode.insertBefore( contDiv , el );
        contDiv.appendChild( el );
        
        /* Set the styles on the container */
        contDiv.style.height = el.offsetHeight + "px";
        contDiv.style.marginBottom = "-4px"; // This fixes the crazy problem bottom margin thing
                                             // when the dropdown is set to absolute
        contDiv.style.display = "inline-block";
        contDiv.style.position = "relative";

        /* calculate how wide the dropdown actually is */
        var offsetWidth = el.offsetWidth;
        var styleBefore = el.style.width; // in case the dropdown is using inline styles
        contDiv.style.overflow = "hidden";
        contDiv.style.width = 0;
        el.style.width = "auto";

        offsetWidth -= el.offsetWidth;
        
        /* find out if the dropdown will go off the page */
        var offsetLeft = 0;
        var tempEl = el;
        while( tempEl.offsetParent )
        {
            offsetLeft += tempEl.offsetLeft;
            tempEl = tempEl.offsetParent;        
        }
        if( document.body.offsetWidth < offsetLeft + el.offsetWidth ) el.style.right = "0";
        
        /* reset the dropdown width and remove overflow from the container */   
        if( styleBefore ) el.style.width = styleBefore;
        else el.style.removeAttribute( "width" );
        
        contDiv.style.removeAttribute( "overflow" );
        contDiv.style.width = el.offsetWidth;

        /* if the dropdown doesn't need to be fixed then remove container and skip */
        if( offsetWidth >= 0 )
        {
            contDiv.parentNode.insertBefore( el , contDiv );
            contDiv.parentNode.removeChild( contDiv );
            continue;
        }
        
        /* add events for handling fix */
        addSelectFixEvent( el , "mouseover" , selMouseOver );
        addSelectFixEvent( el , "focus" , selMouseOver );
        addSelectFixEvent( el , "mouseout" , selMouseOut );
        addSelectFixEvent( el , "blur" , selMouseOut );
    }
}

function selMouseOver( event )
{
    var el = event.srcElement;
    if( el.style.width != "auto" ) el.origwidth = el.style.width;
    el.style.position="absolute";
    el.style.width = "auto";
    if( event.type == "focus" ) el.gotFocus = true;
}

function selMouseOut( event )
{
    var el = event.srcElement;
    if( !el.gotFocus || event.type == "blur" )
    {
        if( el.origwidth )
            el.style.width = el.origwidth;
        else
            el.style.removeAttribute( "width" );

        el.style.removeAttribute( "position" );
        el.gotFocus = null;
    }
}

function addSelectFixEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

addSelectFixEvent( window , "load" , setupSelectFix );
