// JavaScript Document

/*
 * This script finds selectboxes that have options
 * that are too long and replaces the select box function
 * with a div replacement.
 *
 * Only Targets IE
 *
 * Enjoy
 *
 */

function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}


function changeValue(elem) {
	var theBox = elem
	var theOpt = theBox.options
	var theBoxFont = Array()
	theBoxFont['size'] = theBox.currentStyle.fontSize
	theBoxFont['face'] = theBox.currentStyle.fontFamily
		
	var check;
	var maxLength = theBox.offsetWidth /(parseInt(theBoxFont['size'])*0.95)

	for(i=0;i<theOpt.length;i++) {
		if ( theOpt[i].text.length >= maxLength ) 
			check = true
	}
	if(check) {
		var coverdiv = document.createElement('div')
		coverdiv.style.width = (theBox.offsetWidth-5) + 'px'
		coverdiv.style.height = (theBox.offsetHeight-4) + 'px'
		coverdiv.style.paddingTop = '4px'
		coverdiv.style.paddingLeft = '5px'
		coverdiv.style.whiteSpace = 'nowrap'
		coverdiv.style.overflow = 'hidden'
    	var ver = getInternetExplorerVersion();
        if (ver >= 8.0)
			coverdiv.style.background = "url(/skin/frontend/default/acustom/images/selBox_ie8.jpg)"
		else
			coverdiv.style.background = "url(/skin/frontend/default/acustom/images/selBox_ie7.jpg)"
		coverdiv.innerHTML = 'Choose Option...'
		coverdiv.onclick = function() {
			var el = this.nextSibling
			el.style.display = 'block'
			el.focus()
		}
		theBox.parentNode.insertBefore(coverdiv,theBox)
		
		theBox.style.position = 'absolute'
		theBox.style.zIndex = '1'
		theBox.style.width = 'auto'
		theBox.size = theOpt.length
		theBox.style.display = 'none'
		theBox.onblur = theBox.onmouseup = function() {
			this.style.display = 'none'
			var selIndex = this.selectedIndex;
			this.previousSibling.innerHTML = this.options[selIndex].text 
		}
	}
}
function fixSelects() {
	var sB = document.getElementsByTagName('select')
	for(j=0;j<sB.length;j++){
			changeValue(sB[j])	
	}
}
/*@cc_on @*/
/*@if (@_win32)

is_ie = true;

document.write("<script id=__ie_onload_funtions defer src=//:><\/script>");
var script = document.getElementById("__ie_onload_funtions");
script.onreadystatechange = function() {
	if (this.readyState == "complete") {
		fixSelects();
	}
};

/*@end @*/