Element.extend({
	hide: function() {
			return this.setStyle('display', 'none');
	},
	show: function(top) {
			// get the parent's coordiantes
			var coord = this.getParent().getCoordinates();
//			var coord = this.getNext().getCoordinates();
			this.setStyles({
				'position': 'absolute',
				'width': 250,
//				'top': top,
//				'top': ( top.length > 0 ? top : coord['top']+24 ),
				'padding-top': top,
				'z-index': 10000,
				'left': coord['left']
			});	
			if( top < 1 ) this.setStyle( 'top', coord['top']+24 );
			return this.setStyle('display', 'block');
	}
});

var DropdownMenu = new Class({	
	initialize: function(element, sub) {
			$A( $(element).childNodes).each( function(el) {
				if(el.nodeName.toLowerCase() == 'li') {
					$(el).addEvents({
						'mouseover': function() {
							this.setStyle( 'background-color', '#9c9571' );
						},
						'mouseout': function() {
							this.setStyle( 'background-color', '#B0A87D' );
						}
					});
					$A($(el).childNodes).each(function(el2) {
						if(el2.nodeName.toLowerCase() == 'ul') {
							$(el2).setStyle( 'opacity', 0.9 );
							$(el2).hide();
							
							el.addEvent('mouseover', function(e) {
								this.setStyle( 'background-color', '#9c9571' );
								el2.show( ( sub == 'y' ? 20 : 0 ) );
								return false;
							});
	
							el.addEvent('mouseout', function() {
								this.setStyle( 'background-color', '#B0A87D' );
								el2.hide();
							});
							
							var tmp = new DropdownMenu(el2, 'y');
						}
					});
				}
			});
		return this;
	}
});

window.addEvent( 'domready', function(e) {

	//new DropdownMenu( 'dropdownMenu' );	
	//var myMenu = new menu("dropdownMenu");
	if( $('dropdownMenu' ) ) menu = new DropMenu('dropdownMenu');
	
	// add the mouse overs to the main menu
	$$( 'div.main_menu img' ).addEvents({
		'mouseover': function(e) {
			this.src = this.src.replace( ".gif", "_hover.gif" );
		},
		'mouseout': function(e) {
			this.src = this.src.replace( "_hover.gif", ".gif" );
		}
	});
	
	// see if there is a services dropdown image
	if( $( 'services_dropdown' ) ) {
		// add the cursor to the image
		$( 'services_dropdown' ).setStyle( 'cursor', 'pointer' );
		// add the events
		$( 'services_dropdown' ).addEvent( 'click', function(e) {
			// stop the default
			new Event(e).stop();
			// get the posision of the image
			var coords = this.getCoordinates();
			// show and position the box
			$( 'ddservices' ).setStyles({
				'display': ( $( 'ddservices' ).getStyle( 'display' ) == 'none' ? '' : 'none' ),
				'position': 'absolute',
				'left': coords['left'],
				'top': coords['top']+15,
				'z-index': 10000
			}).addEvent( 'mouseleave', function(e) {
				this.setStyle( 'display', 'none' );
			});
		});
		
		// add the mouseenter and mouseleave to the li elements
		$$( 'ul#ddservices li' ).addEvents({
			'mouseenter': function(e) {
				this.setStyles({
					'background-image': 'url( images/sub_menu_bg_blue_hover.gif )'
				});
			},
			'mouseleave': function(e) {
				this.setStyles({
					'background-image': 'none'
				});
			}
		});
	}
});

