function tamingselect()
{
	var origTop;
	
	if(!document.getElementById && !document.createTextNode){return;}
	
// Classes for the link and the visible dropdown
	var ts_selectclass='turnintodropdown'; 	// class to identify selects
	var ts_listclass='turnintoselect';		// class to identify ULs
	
	var ts_boxclass='dropcontainer'; 		// parent element
	var ts_triggeron='activetrigger'; 		// class for the active trigger link
	var ts_triggeroff='trigger';			// class for the inactive trigger link
	var ts_dropdownclosed='dropdownhidden'; // closed dropdown
	var ts_dropdownopen='dropdownvisible';	// open dropdown
/*
	Turn all selects into DOM dropdowns
*/
	var count=0;
	var toreplace=new Array();
	var sels=document.getElementsByTagName('select');
	for(var i=0;i<sels.length;i++){
		if (ts_check(sels[i],ts_selectclass))
		{
			var hiddenfield=document.createElement('input');
			hiddenfield.name=sels[i].name;
			hiddenfield.type='hidden';
			hiddenfield.id=sels[i].id;
			hiddenfield.value=sels[i].options[0].value;
			sels[i].parentNode.insertBefore(hiddenfield,sels[i])
			var trigger=document.createElement('a');
			ts_addclass(trigger,ts_triggeroff);
			trigger.href='#';
			trigger.onclick=function(){
				if(this.nextSibling.getElementsByTagName('ul')[0].className==ts_dropdownopen)
					var clickedIsOpen = true;
				var lists = document.getElementById("main").getElementsByTagName('ul');
				for(var x=0;x<lists.length;x++)
				{	// close all dropdowns
					lists[x].parentNode.style.position = '';
					lists[x].parentNode.style.top = '';
					lists[x].parentNode.style.left = '';
					lists[x].parentNode.setAttribute('id', '');
					if(lists[x].className==ts_dropdownopen)
						ts_swapclass(lists[x],ts_dropdownclosed,ts_dropdownopen);
				}
				//ts_swapclass(this,ts_triggeroff,ts_triggeron)
				//ts_swapclass(this.parentNode.getElementsByTagName('ul')[0],ts_dropdownclosed,ts_dropdownopen);
				if(!clickedIsOpen)	// open clicked dropdown if it isn't already
				{
					ts_swapclass(this.nextSibling.getElementsByTagName('ul')[0],ts_dropdownclosed,ts_dropdownopen);
					this.nextSibling.setAttribute('id', 'active_dropdown');
					updateWindowData();
					origTop = dropdownTop;
					this.nextSibling.style.position = 'absolute';
					this.nextSibling.style.left = dropdownLeft;
					updateWindow();
				}
				clickedIsOpen = false;
				return false;
			}
			trigger.appendChild(document.createTextNode(sels[i].options[0].text));
			sels[i].parentNode.insertBefore(trigger,sels[i]);
			var replaceUL=document.createElement('ul');
			for(var j=1;j<sels[i].getElementsByTagName('option').length;j++)
			{
				var newli=document.createElement('li');
				var newa=document.createElement('a');
				newli.v=sels[i].getElementsByTagName('option')[j].value;
				newli.elm=hiddenfield;
				newli.istrigger=trigger;
				newa.href='#';
				newa.appendChild(document.createTextNode(
				sels[i].getElementsByTagName('option')[j].text));
				newli.onclick=function(){ 
					switchData(this.v);
					this.elm.value=this.v;
					//ts_swapclass(this.istrigger,ts_triggeron,ts_triggeroff);
					ts_swapclass(this.parentNode,ts_dropdownopen,ts_dropdownclosed)
					//this.istrigger.firstChild.nodeValue=this.firstChild.firstChild.nodeValue;
					return false;
				}
				newli.appendChild(newa);
				replaceUL.appendChild(newli);
			}
			ts_addclass(replaceUL,ts_dropdownclosed);
			var div=document.createElement('div');
			div.appendChild(replaceUL);
			ts_addclass(div,ts_boxclass);
			sels[i].parentNode.insertBefore(div,sels[i])
			toreplace[count]=sels[i];
			count++;
		}
	}
	
/*
	Turn all ULs with the class defined above into dropdown navigations
*/	

	var uls=document.getElementsByTagName('ul');
	for(var i=0;i<uls.length;i++)
	{
		if(ts_check(uls[i],ts_listclass))
		{
			var newform=document.createElement('form');
			var newselect=document.createElement('select');
			for(j=0;j<uls[i].getElementsByTagName('a').length;j++)
			{
				var newopt=document.createElement('option');
				newopt.value=uls[i].getElementsByTagName('a')[j].href;	
				newopt.appendChild(document.createTextNode(uls[i].getElementsByTagName('a')[j].innerHTML));	
				newselect.appendChild(newopt);
			}
			newselect.onchange=function()
			{
				window.location=this.options[this.selectedIndex].value;
			}
			newform.appendChild(newselect);
			uls[i].parentNode.insertBefore(newform,uls[i]);
			toreplace[count]=uls[i];
			count++;
		}
	}
	for(i=0;i<count;i++){
		toreplace[i].parentNode.removeChild(toreplace[i]);
	}
	function ts_check(o,c)
	{
	 	return new RegExp('\\b'+c+'\\b').test(o.className);
	}
	function ts_swapclass(o,c1,c2)
	{
		var cn=o.className
		o.className=!ts_check(o,c1)?cn.replace(c2,c1):cn.replace(c1,c2);
	}
	function ts_addclass(o,c)
	{
		if(!ts_check(o,c)){o.className+=o.className==''?c:' '+c;}
	}
	
	
	function findPos(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		return [curleft,curtop];
	}
	
	var windowX,windowY,scrollbarY,dropdownX,dropdownY,dropdownLeft,dropdownTop;
	var offset=0;
	var offsetSpeed=0;
	function updateWindowData() {
		if (self.innerHeight) // all except Explorer
		{
			windowX = self.innerWidth;
			windowY = self.innerHeight;
			scrollbarY=window.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			windowX = document.documentElement.clientWidth;
			windowY = document.documentElement.clientHeight;
			scrollbarY=document.documentElement.scrollTop;
		}
		else if (document.body) // other Explorers
		{
			windowX = document.body.clientWidth;
			windowY = document.body.clientHeight;
			scrollbarY=document.body.scrollTop;
		}
		if(document.getElementById('active_dropdown')) {
			dropdownX=document.getElementById('active_dropdown').firstChild.clientWidth;
			dropdownY=document.getElementById('active_dropdown').firstChild.clientHeight;
			dropdownCoords=findPos(document.getElementById('active_dropdown'));
			dropdownLeft=dropdownCoords[0];
			dropdownTop=dropdownCoords[1];
		}
	}
	
	
	// Updates the variable storing the vertical scrollbar co-ordinate
  //function updateVerticalScrollbar(){
  //  if (window.pageYOffset){
  //    scrollbarY=window.pageYOffset;
  //  }else if (document.documentElement && document.documentElement.scrollTop){
  //    scrollbarY=document.documentElement.scrollTop;
  //  }else if (document.body){
  //    scrollbarY=document.body.scrollTop;
  //  }
  //}
	// Updates the window offset and moves the window accordingly
  function updateWindowOffset(){
    //if (offset>scrollbarY){
    //  if (offset-scrollbarY<-offsetSpeed*(1-offsetSpeed)/2) offsetSpeed++;
    //  if (offset-scrollbarY>(1-offsetSpeed)*(2-offsetSpeed)/2) offsetSpeed--;
    //}
    //if (offset<scrollbarY){
    //  if (scrollbarY-offset<offsetSpeed*(offsetSpeed+1)/2) offsetSpeed--;
    //  if (scrollbarY-offset>(offsetSpeed+1)*(offsetSpeed+2)/2) offsetSpeed++;
    //}
    //if (offset==scrollbarY) offsetSpeed=0;
    //offset+=offsetSpeed;
    //document.getElementById('active_dropdown').style.bottom=windowY+offset+'px';
		//if(origTop=="")
			//origTop = ((windowY+scrollbarY)-dropdownY);
		if(document.getElementById('active_dropdown')) {
			if(origTop > ((windowY+scrollbarY)-dropdownY))
				document.getElementById('active_dropdown').style.top=((windowY+scrollbarY)-dropdownY)+'px';
			else
				document.getElementById('active_dropdown').style.top=origTop+'px';
		}
  }
	// Updates the position and size of the window
  function updateWindow() {
		updateWindowData();
    //updateVerticalScrollbar();
    updateWindowOffset();
  }
	
	// Interval to update the window
	window.onscroll = function() {
		updateWindow();
	}
	
	// show all changed selects
	var docSelects = document.getElementsByTagName('SELECT');
	for(var i=0;i<docSelects.length;i++)
	{
		if(docSelects[i].style.display='none')
			docSelects[i].style.display=''
	}
}

window.onload=function()
{
	tamingselect();
	// add more functions if necessary
}

