function jsSelect(sel, w, h, align )	{

	this.sel = sel;
	this.selParentNode = this.sel.parentNode;
	
	this.optionsArr = [];
	
	this.w = w;
	this.h = h;
	this.align = align;
	
	// create divs
	this.container = document.createElement('div');
	
	this.selectDivContainer = this.createDiv('selectDivContainer');
	this.selectDiv          = this.createDiv('selectDiv');
	this.selectButtonDiv    = this.createDiv('selectButtonDiv');
	this.optionsDiv         = this.createDiv('optionsDiv');
	this.optionsList        = document.createElement('ul');
	this.selectDivSpan      = document.createElement('span');
	
	this.iframe             = document.createElement('iframe');
		
	this.selectDivContainer.style.width =  this.sel.offsetWidth;
	this.selectDivContainer.style.height = this.sel.offsetHeight;
	
	this.optionsDiv.style.width = this.w;
	this.optionsDiv.style.height = this.h;
	this.iframe.style.width = this.w;
	this.iframe.style.height = this.h;
	
	this.selectDivContainer.obj = this;
	this.selectDivContainer.onclick = function()	{	this.obj.toggleOptions();	}
	
	this.replaceOptions();
	this.replaceSelect();
	
	this.optionsOpened = false;

}
//
jsSelect.prototype.createDiv = function(id)	{
	var div = document.createElement('DIV');
	div.setAttribute('id', id);
	return div;
}
//
jsSelect.prototype.replaceSelect = function()	{
	this.selectDivContainer.appendChild(this.selectDiv);
	this.selectDivContainer.appendChild(this.selectButtonDiv);
	this.selectDiv.appendChild(this.selectDivSpan);
	
	this.selParentNode.insertBefore(this.selectDivContainer, this.sel);
	this.selParentNode.insertBefore(this.optionsDiv, this.sel);
	this.selParentNode.insertBefore(this.iframe, this.sel);
	
	this.selectDivContainer.className = 'jsSelectContainer';
	this.selectDiv.className = 'jsSelectSelectDiv';
	this.selectButtonDiv.className = 'jsSelectButton';
	this.optionsDiv.className = 'jsSelectOptionsContainer';
	this.iframe.className = 'jsSelectIframe';
	
	this.selectDiv.style.width = this.selectDivContainer.offsetWidth - this.selectButtonDiv.offsetWidth - 5+'px';
	this.selectDiv.style.height = this.sel.offsetHeight;
	this.selectDivContainer.height = this.sel.offsetHeight;
	
	// position the option elements
	if(this.align == null || this.align == 'left')	{
		this.optionsDiv.style.marginLeft = 
		this.iframe.style.marginLeft     = '-' + this.selectDivContainer.offsetWidth + 'px';
	}
	else	{
		this.optionsDiv.style.marginLeft = 
		this.iframe.style.marginLeft     = '-' + this.w + 'px';
	}
	this.optionsDiv.style.marginTop  = 
	this.iframe.style.marginTop      = (this.selectDivContainer.offsetHeight - 1) + 'px';
	
	this.sel.style.display = 'none';
	
	//resize the options div if we have spare room
	if(this.optionsArr.length * 18 + 6 < this.h)	{
		this.optionsDiv.style.height = 
		this.iframe.style.height     = (this.optionsArr.length * 18 + 6) + 'px';
	}

}
//
jsSelect.prototype.createOption = function(val, label, i)	{
	var option = document.createElement('a');
	option.innerHTML = label;
	option.obj = this;
	option.i = i;
	option.onclick = function()	{
		this.obj.selectValue(this.i, true);
	}
	option.href="javascript:;";
	this.optionsArr[i] = [option, val, label];
}
//
jsSelect.prototype.replaceOptions = function()	{
	for(var i = this.sel.options.length-1; i >= 0; i--)	{
		this.createOption(this.sel.options[i].value, this.sel.options[i].innerHTML, i);
		// check if this option is selected
		if(this.sel.options[i].selected)	{
			this.selectValue(i, false);
		}
	}
	for(var i = 0; i < this.optionsArr.length; i++)	{
		this.optionsDiv.appendChild(this.optionsArr[i][0]);
	}
}
//
jsSelect.prototype.selectValue = function(num, fireOnchange)	{
	// close if open
	if(this.optionsOpened) this.toggleOptions();

	if(this.selectedValue == this.optionsArr[num][1]) return;
	
	this.selectedValue = this.optionsArr[num][1];
	// set label
	this.selectDivSpan.innerHTML = this.optionsArr[num][2];
	// select the option in the original select
	this.sel.options[num].selected = true;
	// check if there's an onchange handler, dont run this on page load
	if(fireOnchange)	{
		alert(this.sel.onchange);
		if(typeof(this.sel.onchange) == 'function')	{
			this.sel.onchange();
		}
	}
}
//
jsSelect.prototype.toggleOptions = function()	{
	if(document.jsSelectOpenedOptions)	{
		if(document.jsSelectOpenedOptions[0].obj != this)	document.jsSelectOpenedOptions[0].obj.toggleOptions();
	}	
	if(this.optionsOpened)	{
		this.optionsDiv.style.display = 'none';
		this.iframe.style.display = 'none';
		document.jsSelectOpenedOptions = false;
		this.optionsOpened = false;
	}
	else	{
		this.optionsDiv.style.display = 'block';
		this.iframe.style.display = 'block';
		document.jsSelectOpenedOptions = [this.selectDivContainer, this.optionsDiv];
		this.optionsOpened = true;
	}
}
//
jsSelect.prototype.timeoutCloseOptions = function()	{
	if(this.optionsOpened)	{
		this.toggleOptions();
	}
}
//

function findPos(obj)	{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}
//
document.onclick=check;
function check(e){
	if(document.jsSelectOpenedOptions)	{
		var target = (e && e.target) || (event && event.srcElement);
		
		if(checkParent(target))	{
			document.jsSelectOpenedOptions[0].obj.toggleOptions();
		}
	}
}
function checkParent(t){
	while(t.parentNode){
		if(t==document.jsSelectOpenedOptions[0] || t==document.jsSelectOpenedOptions[1])	{
			return false
		}
		t=t.parentNode
	}
	return true
} 

//////////////////

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

