var MSIE = navigator.userAgent.indexOf('MSIE') > 0;

var defaultShrinkWidth = "100px";
var defaultGrowthWidth = "300px";

// Init function
function initShrinkSelects() {
	var allSelects = document.getElementsByTagName('select');
	for (i=0; i < allSelects.length; i++) {
		if (allSelects[i].getAttribute("className") == "shrinkCombo" ||
			allSelects[i].getAttribute("class") == "shrinkCombo" ) {
			addEvent(allSelects[i],"mouseenter",growEvent,false);
			addEvent(allSelects[i],"mouseleave",mayShrinkEvent,false); 
			addEvent(allSelects[i],"focus",markEvent,false); 
			addEvent(allSelects[i],"change",mayShrinkEvent,false); 
			addEvent(allSelects[i],"blur",shrinkEvent,false); 
			if (getStyleAttribute(allSelects[i], "growWidth") == null)
				setStyleAttribute(allSelects[i], "growWidth", defaultGrowthWidth);
			if (getStyleAttribute(allSelects[i], "shrinkWidth") == null)
				setStyleAttribute(allSelects[i], "shrinkWidth", defaultShrinkWidth);
		}
	}
}

// Auxiliary functions
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}
function getStyleAttribute(elm, attName) {
	if (MSIE) {
		return elm.style.getAttribute(attName);
	} else {
		return elm.style.getPropertyCSSValue(attName);
	}
}
function setStyleAttribute(elm, attName, value) {
	if (MSIE) {
	   elm.style.setAttribute(attName, value);
	} else {
	   elm.style.setProperty(attName, value, true);
	}
}

function getTarget(aEvent) {
  // if aEvent is null, means the Internet Explorer event model, 
  // so get window.event. 
  var myEvent = aEvent ? aEvent : window.event; 
  var targ;
  if (myEvent.target != null) targ = myEvent.target;
  else if (myEvent.srcElement) targ = myEvent.srcElement;
  if (targ.nodeType == 3) targ = targ.parentNode;
  return targ;
}

// Event Wrappers
function growEvent(aEvent) {
  grow(getTarget(aEvent));		
}

function markEvent(aEvent) {
  mark(getTarget(aEvent));
}

function mayShrinkEvent(aEvent) {
  mayShrink(getTarget(aEvent));
}

function shrinkEvent(aEvent) {
  shrink(getTarget(aEvent));
}

// Real Functions

function grow(selObj) {
	if (getStyleAttribute(selObj, "growWidth") == null)
		setStyleAttribute(selObj, "width", defaultGrowthWidth);
	else
		setStyleAttribute(selObj, "width", getStyleAttribute(selObj, "growWidth"));
}

function mark(selObj) {
	setStyleAttribute(selObj, "hasFocus", "true");
	if (! MSIE)	grow(selObj);
	   
}

function mayShrink(selObj) {
    if (getStyleAttribute(selObj,"hasFocus") == null ||
    	getStyleAttribute(selObj,"hasFocus") == "" ||
    	getStyleAttribute(selObj,"hasFocus") == "false") {
    	if (getStyleAttribute(selObj, "shrinkWidth") == null)
			setStyleAttribute(selObj, "width", defaultShrinkWidth);
    	else
			setStyleAttribute(selObj, "width", getStyleAttribute(selObj, "shrinkWidth"));
    }
}

function shrink(selObj) {
	setStyleAttribute(selObj, "hasFocus", "false");
    	if (getStyleAttribute(selObj, "shrinkWidth") == null)
			setStyleAttribute(selObj, "width", defaultShrinkWidth);
    	else
			setStyleAttribute(selObj, "width", getStyleAttribute(selObj, "shrinkWidth"));
}
