// Initialise dropdown variables

var isToggled   = new Array();
var isExpanded   = new Array();
var effectArray  = new Array();
var sizeArray   = new Array();
var duration   = 300;
var hoverCssClass  = 'jsHighlight';
var visibleArray  = new Array();
var CssClass = 'jsDropDown';
var CssClassArray = new Array();
// timeout object to allow us to stop timed closure of dropdown
var closeDropdownTimeout = null;
// seconds until timeout fires
var closeDropdownCountdown = 1;
// flag to stop multiple dropdown toggles - i.e. when user clicks on an item, click fires first then mouseout fires second
var preventDropdownTimeout = false;
 
// Style filter dropdown menu
function styleFilter(containerID, action, newClass)
{
	var containerId  = containerID;
	var filter = '';
		
	if( document.getElementById(containerId) )
	{
		filter = document.getElementById(containerId);
	}
	else
	{
		filter = document.head;
	}
	
	var list = filter.getElementsByTagName('select');
	var listLength = list.length;
	
	for(var j = 0; j < listLength; j++)
	{
		var cur_list = list[j];
	
		var optionLength = cur_list.options.length;
		
		if(optionLength > 0){
		
		var id = '';
		
		var selectedOption = cur_list.selectedIndex;   
		
		if(cur_list.id){
		var elId = cur_list.id;
		
		id = 'js'+elId;
		
		CssClassArray[id] = CssClass;
		
		if( typeof( newClass ) != 'undefined' && newClass != '' )
		{
			CssClassArray[id] = newClass;
		}
		
		var container = new Element('div', {
		 'class': CssClassArray[id],
		 'id': id
		});
		
		var titleContainer = new Element('div', {
		 'class': CssClassArray[id]+'-title',
		 'id': id+'Title'
		}).inject(container);
		
		var titleContainerInner = new Element('span', {
		 'class': CssClassArray[id]+'-titleInner',
		 'id': id+'TitleInner'
		}).set('html',cur_list.options[selectedOption].innerHTML).inject(titleContainer);
		
		var contentWrapper = new Element('div', {
		 'class': CssClassArray[id]+'-wrapper',
		 'id': id+'ContentWrapper',
		 'styles': {
		  'height': '0px'
		 }
		}).inject(titleContainer);
		
		var clear = new Element('div', {
		 'class': 'clear-both'
		}).set('html', '&nbsp;').inject(container, 'bottom');
	
		isToggled[id+'ContentWrapper'] = 0;
		
		var contentContainer = new Element('div', {
		 'class': CssClassArray[id]+'-content',
		 'id': id+'Content'
		}).inject(contentWrapper).addEvents({
				 'mouseover': function(e) {
					 	clearCloseDropdown();
				 },
				 'mouseout': function(e) {
					 // flag is set if mouseout was caused by user clicking on an item in the list,
					 // the dropdown has already gone, reset this flag
					 if(preventDropdownTimeout)
					 {
						 preventDropdownTimeout = false;
					 }
					 // if no flag, schedule a disappearance
					 else
					 {
					 	scheduleCloseDropdown(e, this.id);
					 }
				 }
		});
		
		var ul = new Element('ul', {
		 'class': CssClassArray[id]+'-ul',
		 'id': id+'Ul'
		}).inject(contentContainer);
		
		var clear = new Element('div', {
		 'class': 'clear-both'
		}).set('html', '&nbsp;').inject(contentContainer, 'bottom');
		
		for(var i = 0; i < optionLength; i++)
		{
		 
		 var li = new Element('li', {
			  'class': CssClassArray[id]+'-li '+CssClassArray[id]+'-li-'+i
			 }).set('html',cur_list.options[i].innerHTML).addEvents({
				 'mouseover': function(e) {
				  mouseOver(e, containerId);
				 },
				 'focus': function(e) {
				  mouseOver(e, containerId);
				 },
				 'mouseout': function(e) {
				  mouseOut(e);
				 },
				 'blur': function(e) {
				  closeItem(e, containerId);
				 },
				 'click': function(e) {
					// user clicked, set flag to prevent second toggle of dropdown once item selected
					preventDropdownTimeout = true;
				  selectItem(e, containerId, action);
				 },
				 'keypress': function(e) {
				  selectItem(e, containerId, action);
				 }
			}).inject(ul);
		}
	
		effectArray[id+'ContentWrapper'] = new Fx.Tween(contentWrapper, {duration: duration, transition: Fx.Transitions.linear});     
		
		$(container).inject(elId, 'before');
		
		$(elId).style.display = 'none';
		$(id+'ContentWrapper').style.display = 'none';
		
		$(titleContainer).addEvent('click', function(e){    
			toggleDropDown(e, '');
			
			var el = new Event(e).target;
			var id = el.id;
			id = id.replace('Title', 'Content');
			var wrapperId = id.replace('Content', 'ContentWrapper');
			/*
			if(isToggled[wrapperId] == 1)
			{
				//it's expanded, so listen to see if the mouse leaves it
				$(id).addEvent('mouseleave',function()
				{
					$(wrapperId).style.display = 'none';
					isToggled[wrapperId] = 0;
					$('buttons').removeClass('zindex-fix');
				});
			}
			*/
		});
	
	}
	else
	{
		alert('IDs must be applied to dropdowns');
	}
	
	}
	
	}
 
}
 
// Toggle dropdown status (expanded/contracted)
function toggleDropDown(e, elId)
{
	// tidy up the event
	clearCloseDropdown();
	
	if(e)
	{
		var el = new Event(e).target;
		var id = el.id;
		id = id.replace('TitleInner', 'Content');
		
	}
	
	if(elId != '')
	{
		id = elId;
	}
	
	var containerId = id.replace('Content', '');
	
	var items = $$('.'+CssClassArray[containerId]+'-content');
	
	for( i=0, maxi=items.length; i < maxi; i++ )
	{
		if( id != items[i].id )
		{
			expandConntractElement( items[i].id, 'close' );
		}
	}
	
	
	expandConntractElement( id, '' );
}

function expandConntractElement( id, action )
{
	var wrapperId = id.replace('Content', 'ContentWrapper');
	wrapperId = wrapperId.replace('Title', 'ContentWrapper');
	
	if( $(id) != null )
	{
		if(!sizeArray[wrapperId])
		{
			sizeArray[wrapperId] = $(id).getCoordinates();
		}
		
		if(!isToggled[wrapperId])
		{
			isToggled[wrapperId] = 0;
		}
		
		if(!isExpanded[wrapperId])
		{
			isExpanded[wrapperId] = 0;
		}
		
		if( isToggled[wrapperId] == 0 )
		{
			if( action != 'close' )
			{
				var tempHeight = $(wrapperId).getStyle('height');
				$(wrapperId).style.display = 'block';
				
				//alert($(wrapperId).getStyle('height'));
				
				isToggled[wrapperId] = 1;
				
				if($('let-us-know'))
				{
					$('let-us-know').addClass('ie6-hide');
				}
				
				$('buttons').addClass('zindex-fix');
			}
		}
		else
		{
			if( action != 'open' )
			{
				$(wrapperId).style.display = 'none';
				isToggled[wrapperId] = 0;
				
				if($('let-us-know'))
				{
					$('let-us-know').removeClass('ie6-hide');
				}
				
				$('buttons').removeClass('zindex-fix');
			}
		}
	}
}

// Add class on mouse over dropdown list
function mouseOver(e)
{
	var el = new Event(e).target;
	
	el.addClass(hoverCssClass);
}
 
function closeItem(e, containerId)
{
	if(e.type == 'mouseout')
	{
		elId = containerId;
		e = null;
	}
	else
	{
		var el = new Event(e).target;
	
		elId = el.getParent().id.replace('Ul', 'Content');
		elIdTitle = elId.replace('Content', 'Title');
		var elContent = el.innerHTML;
	}
	
	toggleDropDown(e, elId);
}
 
// Remove class on mouse out of dropdown list
function mouseOut(e, containerId)
{
	var el = new Event(e).target;
	
	if( el.hasClass(hoverCssClass) )
	{
		el.removeClass(hoverCssClass);
	}
}
 
// Select dropdown item
function selectItem(e, containerId, action)
{
	var el = new Event(e).target;
	
	elId = el.getParent().id.replace('Ul', 'Content');
	elIdTitle = elId.replace('Content', 'Title');
	
	var elBg = el.getStyle('background-image');
	if( elBg != 'none' )
	{
		$(elIdTitle).setStyle('background-image', elBg);
	}
	
	var elContent = el.innerHTML;
	
	toggleDropDown(e, elId);
	
	selectId = elId.replace('Content', '');
	selectId = selectId.replace('js', '');
	selectElem = $(selectId)
	
	var option = selectElem.options;
	var optionCount = option.length;
	
	for( var i = 0; i < optionCount; i++ )
	{
		if( option[i].innerHTML == elContent )
		{
			selectElem.selectedIndex = i;
			
			var form = selectElem.form;
			
			$(elIdTitle+'Inner').set('html',option[i].innerHTML);
			/******************************************************/
			if(action != 'nosubmit')
			{
				form.submit();
			}
		}
	}  
}
 

// Close dropdowns
function closeDropDowns(e, scope)
{
	var eve = new Event(e);
	var el = eve.target;
	
	var dropDownContainers = $$('.'+CssClass);
	var dropDownContainersLength = dropDownContainers.length;
	
	for(var i = 0; i < dropDownContainersLength; i++)
	{
		dropContainerId = dropDownContainers[i].id;
		var wrapperId = dropContainerId+'ContentWrapper';
		 
		if( scope.isToggled[wrapperId] == 1 )
		{
		
			var elId = wrapperId.replace('ContentWrapper', 'Content');
			
			var children = [[0, dropContainerId+'ContentWrapper'],[0, dropContainerId+'Title']];
			
			var notChild = 0;
			
			for( var j = 0, maxj = children.length; j < maxj; j++ )
			{
				if( el.id == children[j][1] )
				{
					notChild = 1;
				}
			}
			
			if( notChild == 0 )
			{
				scope.toggleDropDown(e, elId);
			}
		
		}
	}
}
// Close dropdowns
function scheduleCloseDropdown(e, scope)
{
	closeDropdownTimeout = setTimeout( function()
		{
			closeItem(e, scope);
		}, parseInt(closeDropdownCountdown)*1000 );
}
// Cancel close dropdowns
function clearCloseDropdown()
{
	if(closeDropdownTimeout != null)
	{
		clearTimeout( closeDropdownTimeout );
		closeDropdownTimeout=null;
	}
}