(function($) {
	$.extend({
		fixIESelectWidth: new function() {

			var oTimer = {};
			var oAnim = {};
			var nTimerId =  0 ;
			var dLastFocalItem;
			var ie7 = !!(document.uniqueID  &&   typeof(XMLHttpRequest)!='undefined' )

			function log(s) {
				if (typeof console != "undefined" && typeof console.debug != "undefined") {
					console.log(s);
				} else {
					alert(s);
				}
			}
			
			function init(el) {
				var oSelf = this;
				var wrapped = false;

				el = el || this;
				
				if(el.className.indexOf('wrapped') != -1) wrapped = true;

				if(wrapped)	var oRs = el.parentNode.runtimeStyle;
				else		var oRs = el.runtimeStyle;
				if(wrapped)	var oPRs = el.parentNode.parentNode.runtimeStyle;
				else		var oPRs = el.parentNode.runtimeStyle;
				
				oPRs.fonSize = 0;
				
				if(wrapped)	var sDisplay = el.parentNode.parentNode.currentStyle.display.toLowerCase() ;
				else		var sDisplay = el.parentNode.currentStyle.display.toLowerCase() ;
				if(  sDisplay=='' ||  sDisplay=='inline' ||  sDisplay=='inline-block' ) {
					oPRs.display = 'inline-block';
					oPRs.width = el.offsetWidth + 'px';
					oPRs.height =el.offsetHeight + 'px';
					oPRs.position = 'relative';
					oRs.position = 'absolute';
					oRs.top = 0;
					oRs.left = 0;
				};

				el._timerId = ( nTimerId+=1 );
		
				el.selectedIndex = Math.max( 0 , el.selectedIndex );
				
				oTimer[ '_' + el._timerId ] = setTimeout('void(0)',0);
				oAnim [ 'A' + el._timerId ] = setTimeout('void(0)',0);

				$(el).mouseover(onMouseOver);
				$(el).click(onMouseDown);
				$(el).change(collapseSelect);
				$(el).blur(collapseSelect);

			}
			
			function collapseSelect(e) {
				// log('blurred or changed');
				this.runtimeStyle.width = '';			
			}

			function onMouseOver(e ) {
				var el = this;	
				if(dLastFocalItem && dLastFocalItem !=el) {
					onMouseDown.call( dLastFocalItem , e );
				};

				var sTimerId ='_' +  el._timerId ;
				var sAniId = 'A' + el._timerId ;
				clearTimeout( oTimer[ sTimerId ] );

				var onTween = function() {
					clearTimeout( oAnim [  sAniId  ] );
					if( Math.abs( nEndWidth - nStartWidth ) > 3 ) {
						nStartWidth += (nEndWidth - nStartWidth ) /3;
						el.runtimeStyle.width = nStartWidth + 'px';
						oAnim [  sAniId  ] = setTimeout( onTween ,0 );
					} else {
						el.runtimeStyle.width = 'auto';
						el.selectedIndex = Math.max( 0 , el.selectedIndex );
					}
				}

				var nStartWidth =  el.offsetWidth ;
				el.runtimeStyle.width = 'auto';
				var nEndWidth  = el.offsetWidth;

				clearTimeout( oAnim [  sAniId  ] );
				if(nStartWidth <= nEndWidth) onTween();
				else el.runtimeStyle.width = '';

				if(!dLastFocalItem || dLastFocalItem != el) el.focus();	
				dLastFocalItem = el;
			}

			function onMouseDown(e , el ) {
				el = ( e.srcElement || e.target );

				if( el == this && e.type!='mouseover' ) {
					return false;
				};

				el = this;

				clearTimeout( oAnim [ 'A' + el._timerId ] );

				var sTimerId ='_' +  el._timerId ;
				var doItLater = function() {
					el.runtimeStyle.width = '';			
				};
				if( e.type=='mouseover') {
					doItLater();
				} else {
					oTimer[ sTimerId ] = setTimeout(doItLater,100);
				}
			}

			this.construct = function(settings) {
				return this.each(function() {
					if(!document.uniqueID && !window.createPopup ) {
						return this;
					}
					init(this);
				})
			};
		}
	});

	// extend plugin scope
	$.fn.extend({
        fixIESelectWidth: $.fixIESelectWidth.construct
	});
	
})(jQuery);