function openMenu(menu, start, end, el) {
	if(!this.anim && $(menu) != null){
		this.anim = true;
		var myEffects = new Fx.Styles(menu, {duration: 500,transition: Fx.Transitions.linear, onComplete: function(){location.href = el.href;}});
		
		myEffects.start({'right': [start, end]});
	}else{
		location.href = el.href;
	}
}

function onMenuSelect(menu) {
	location.href = menu.value;
}

expendSearch = {
	openContainer : function(el){
		$(el).style.display = 'block';
		containerHeight = $(el).getFirst().getCoordinates()['height'];
		var slider = new Fx.Style(el, 'height', {duration:500});
		slider.custom(0, containerHeight + 1);
	},
	
	closeContainer : function(el){
		if($(el).getFirst()){

			currentHeight = $(el).getFirst().getCoordinates()['height'];
		
			var slider = new Fx.Style(el, 'height', {duration:500, onComplete: function(){$(el).style.display = 'none';}});
			slider.custom(currentHeight, 0);
		}
	},
	
	hideContainer : function(el){
		$(el).style.height = 0;
		$(el).style.display = 'none';
	},
	
	hideButton : function(el){
		$(el).style.visibility = 'hidden';
	},
	showButton : function(el){
		$(el).style.visibility = 'visible';
	}
}


var toggleItem = {
	openBlock : function(which) {
		
		var blockContainer = "";
		
		if($$('div.toggleDiv')){
			
			//Needs to define which block is being toggled, then hide the elements corresponding to that block
			for(i=1; i<10; i++){
				if($(which).hasClass('block' + i)){
					
					// Dectect if the block is nested
					// Also detect if the browser sucks (IE), node tree is not the same in IE had to make different selectors
					if(window.ie){
						// Browser is IE
						if($(which).getFirst().getParent().getParent().getParent().getParent().getTag()){
							if($(which).getFirst().getParent().getParent().getParent().getParent().getTag() == "fieldset"){
								// The block is nested, need to get the container height, to reduce the container height.
								blockContainer = $(which).getFirst().getParent().getParent().getParent().getParent();
								blockContainerHeight = blockContainer.getCoordinates();
								blockContainerHeight = blockContainerHeight['height'];
							}
						}
					}else{
						// Browsers other than IE
						if($(which).getParent().getFirst().getTag() == 'li'){
							// The block is nested, need to get the container height, to reduce the container height.
							blockContainer = $(which).getParent().getParent().getParent().getFirst();
							blockContainerHeight = blockContainer.getCoordinates();
							blockContainerHeight = blockContainerHeight['height'];
						}
					}
					
					// Close the elements part of the block.
					$$('div.block' + i).each( function(el){
						if(el.id != which){ toggleItem.closeBlock(el); } // Close the blocks other than the one the user cliked on.
					} );
				}
			}
		}
		
		if($(which).style.display != "block"){ // Don't reopen the block if it's already open

			$(which).style.display = "block";
			if(window.ie){ $$('#' + which + " select").each(function(el){ $(el).style.display = "inline-block"; }); }

			coordinates = $(which).getCoordinates();
			divHeight = coordinates['height'];

			coordinates = $(which).getFirst().getCoordinates();
			fieldsetHeight = coordinates['height'];

			if(divHeight != fieldsetHeight){
				var slider = new Fx.Style($(which), 'height', {duration:500});
				slider.custom(0,fieldsetHeight);
			}

			// If the element is nested, I need to strech the container.
			if(blockContainer){
				if(window.ie){
					if($(blockContainer) == 'fieldset'){
						var slider = new Fx.Style($(blockContainer).getParent().getParent().getParent().getParent(), 'height', {duration:500});
					}else{
						var slider = new Fx.Style($(blockContainer).getParent(), 'height', {duration:500});
					}
					slider.custom(blockContainerHeight, blockContainerHeight + fieldsetHeight);
				}else{
					var slider = new Fx.Style($(blockContainer).getParent(), 'height', {duration:500});
					slider.custom(blockContainerHeight, blockContainerHeight + fieldsetHeight);	
				}
			}
			
		}else{
			this.closeBlock(which);
		}
		
	},
	
	closeBlock : function(which) {
		
		blockContainer = "";

		coordinates = $(which).getCoordinates();
		divHeight = coordinates['height'];
		
		if(divHeight != 0){
			var slider = new Fx.Style($(which), 'height', {duration:500, onComplete : function(){ $(which).style.display = "none" } } );
			slider.custom(divHeight,0);
		}
		
		if(window.ie){
			// Browser is IE
			if($(which).getFirst().getParent().getParent().getParent().getParent().getTag()){
				if($(which).getFirst().getParent().getParent().getParent().getParent().getTag() == "fieldset"){
					// The block is nested, need to get the container height, to reduce the container height.
					blockContainer = $(which).getFirst().getParent().getParent().getParent().getParent();
					blockContainerHeight = blockContainer.getCoordinates();
					blockContainerHeight = blockContainerHeight['height'];
				}
			}
		}else{
			// Browsers other than IE
			if($(which).getParent().getFirst().getTag() == 'li'){
				// The block is nested, need to get the container height, to reduce the container height.
				blockContainer = $(which).getParent().getParent().getParent().getFirst();
				blockContainerHeight = blockContainer.getCoordinates();
				blockContainerHeight = blockContainerHeight['height'];
			}
		}
		
		// If the element is nested, I need to strech the container.
		if(blockContainer){
			// If the div that needs to be closed is still open, close it and reduce the containers height.
			if(divHeight != 0){
				var slider = new Fx.Style($(blockContainer).getParent(), 'height', {duration:500});
				slider.custom(blockContainerHeight, blockContainerHeight - divHeight);
			}
		}
		
	},
	
	pageLoad : function(){
		// Hide the "toggling divs" onload, this way user with javascript disabled will still see them.
		$$('.toggleDiv').each( function(el){ $(el).addClass('toggleDivHidden'); } );
		
		// Loop throught the checkboxes/radio to verify if they are checked.
		// If they are checked, call the function associated to it and open the depedant blocks if any.
		$$('label input').each( function(el){
			if($(el).checked && $(el).getProperty('onclick')){
				// Need to be treated a string, or else there are compatibility problems
				myFunction = new String($(el).getProperty('onclick'));
				
				// I need to get the parameter passed in the function
				startOfParameter = myFunction.indexOf("('");
				endOfParameter = myFunction.indexOf("')");				
				theParameter = myFunction.substring(startOfParameter + 2, endOfParameter);
				
				// I then need to rebuild the function and execute it
				if(myFunction.indexOf('closeBlock') != -1){
					toggleItem.closeBlock(theParameter);
				}else if(myFunction.indexOf('openBlock') != -1){
					toggleItem.openBlock(theParameter);
				}
			}
		} );
	}
}
