var ELA = {
	accordionMenu: function() {
			var level_one_items = $$('#menu > div > ul > li');
			var level_two       = $$('#menu > div > ul > li > ul');
			var level_two_items = $$('#menu > div > ul > li > ul > li');
			var level_three     = $$('#menu > div > ul > li > ul > li > ul');
	
		if ( typeof Vx == 'undefined' ) {
			// hide all submenus
			level_three.invoke('hide');
			level_two.invoke('hide');
	
			level_one_items.each(function(li) {
				if ( li.down('ul') ) { li.down('a').insert({ bottom: '&nbsp;&raquo;'}); }
			
				li.observe('mouseover', function() {
					level_two.invoke('hide');
					if ( this.down('ul') ) { this.select('ul').invoke('show'); }
				});
			});
	
			level_two_items.each(function(li) {
				if ( li.down('ul') ) { li.down('a').insert({ bottom: '&nbsp;&raquo;'}); }
			
				li.observe('mouseover', function() {
					level_three.invoke('hide');
					if ( this.down('ul') ) { this.select('ul').invoke('show'); }
				});
			});
		}
		
		$('content').observe('mouseover', function() {
			level_three.invoke('hide');
			level_two.invoke('hide');
		});
	},
	
	fadeNews: function() {
		if ( typeof Vx == 'undefined' ) {
			// declare the variables, 'news_items' and 'counter'
				var news_items = $$('#side_text .vx_block'),
					counter = 0;

			// if there is a column and news
			if ( $('side_text') && news_items.length > 1 ) {
				var max_height = news_items.invoke('getHeight').max();
				$('side_text').setStyle({ position: 'relative', height: (max_height)+'px', marginBottom: '30px' });

				news_items.invoke('hide').invoke('setStyle', { position: 'absolute', height: (max_height)+'px' }).first().show();

				new PeriodicalExecuter(function(pe) {
					Effect.toggle(news_items[counter], 'appear');
					counter = ( counter == news_items.length-1 ) ? 0 : counter+1;
					Effect.toggle(news_items[counter], 'appear');
				}, 8);
			}
		}
	}
};

document.observe('dom:loaded', function() {
	if (typeof Vx === 'undefined') {
		ELA.accordionMenu();
		ELA.fadeNews();
	}
	
	$$('a[rel=webdesign]').invoke('observe', 'click', function(event) {
		event.stop();
		window.open(this.href, this.readAttribute('rel'));
	});
	
	var results = $$('div.information li');
	var titles = $$('div.information h1');
	var information = $$('div.information');
	
	if (document.getElementById('search_form')) {
		$('search_form').observe('form:searched', function () {
			
			var filter = $('search').getValue().toLowerCase();
		
			if (filter.blank()) {
				titles.invoke('show');
				results.invoke('show');
				information.invoke('show');
			} else {
				information.invoke('hide');
				titles.invoke('hide');
				results.invoke('hide').each(function (result) {
					if (result.innerHTML.toLowerCase().indexOf(filter) > 0) {
						result.show();
						if (result.up('li')) { result.up('li').show(); }
						if (result.down('li')) { result.select('ul', 'li').invoke('show'); }
						if (result.up('ul').previous('h1')) {
							result.up('ul').previous('h1').show();
						}
					}
				});
			
				information.invoke('appear');
			}
		});
		
		$('search_form').observe('submit', function (event) {
			event.stop();
			this.fire('form:searched');
		});
		
		$('search-cancel').observe('click', function (event) {
			event.stop();
			$('search').setValue('');
			$('search_form').fire('form:searched');
		})
	}
});