window.addEvent('domready', function() {
	var mainnavTriggers = $$('#mainnav-1 a.nav');
	var mainnavFxSubnav = Array();

	// set the visibility now, to reduce flicker in IE7
	$('mainnav-1').setStyle('visibility', 'visible');


	mainnavTriggers.each( function(el, i) {
		
		// get the subnav element
		el_subnav = el.getParent().getElement('.subnav');

		// skip the Slide Fx creation if there is no subnav
		if ( el_subnav != null ) {
			el.addClass('has-subnav');
			thisFx = new Fx.Slide( el_subnav, {
				duration: 350
			} ).hide();
			mainnavFxSubnav[i] = thisFx;
		}
		
		// lets handle some clicking
		el.addEvent('click', function(e) {
			e.stop();

			// do not allow the currently selected trigger to be closed (toggled)
			if ( el.hasClass('on') ) {
				return false;
			}

			// close the other submenus and deselect the "on state" for the trigger
			mainnavTriggers.each( function(trigger, i2) {
				if ( trigger.hasClass('has-subnav') && mainnavFxSubnav[i2].open ) {
					mainnavFxSubnav[i2].slideOut();
				}
				trigger.removeClass('on');
			});
			
			// if this trigger has a subnav, show or hide it
			if ( this.hasClass('has-subnav') ) {
				mainnavFxSubnav[i].slideIn();
			}
			
			// set the "on state" for the trigger
			if ( mainnavFxSubnav[i] != null ) {
				if ( mainnavFxSubnav[i].open ) {
					this.removeClass('on');
				} else {
					this.addClass('on');
				}
			}
			
			// after expanding a section, redirect the user to the first link in the subnav
			( function() {
				subnav_links = el.getParent().getElements('.subnav a');
				document.location.href = subnav_links[0].get('href');
			}).delay(400);
		});
	});

	// set the default mainnav trigger states for when the page is loaded
	setDefaultMenu( $('pagebody').get('class') );

	function setDefaultMenu( classname )
	{
		mainnavTriggers.each( function(el, i) {
			if ( el.getParent().hasClass( classname ) ) {
				el.addClass('on');
				mainnavFxSubnav[i].show();
			}
		});
	}


	$('stocks').addEvents({
		'mouseenter': function(e) {
			this.addClass('stocks-hover');
		},
		'mouseleave': function(e) {
			this.removeClass('stocks-hover');
		},
		'click': function(e) {
			document.location.href = 'index.php?page=investors&subpage=stockquote';
		}
	});

	$('logos').addEvent('click', function(e) {
		document.location.href = 'index.php?page=home';
	});
	
	
	var faqScroll = new Fx.Scroll( $('pagebody'), {
		duration: 500
	});

	var faq_anchor_index;
	$$('.faq-questions span.question a').each( function(trigger, index) {
		trigger.addEvent('click', function(e){
			e.stop();
			
			$$('div.faq .question').each( function(el_title, index2) {
				el_title.removeClass('highlight');
			});
			
			faq_anchor_index = index + 1;
			
			$('faq'+faq_anchor_index).getParent().getElement('.question').addClass('highlight');
			faqScroll.toElement('faq' + faq_anchor_index);
		});
	});

	$$('.backtotop a').addEvent('click', function(e){
		e.stop();
		
		$$('div.faq .question').each( function(el_title, index2) {
			el_title.removeClass('highlight');
		});
		
		faqScroll.toTop();
	});


	/*
	http://davidwalsh.name/mootools-form-field-default-plugin
	*/
	var dwDefaults = new Class({
		//implements
		Implements: [Options],
	
		//options
		options: {
			collection: $$('input[type=text]')
		},
		
		//initialization
		initialize: function(options) {
			//set options
			this.setOptions(options);
			this.defaults();
		},
		
		//a method that does whatever you want
		defaults: function() {
			this.options.collection.each(function(el) {
				el.set('value',el.get('alt'));
				el.addEvent('focus', function() { if(el.get('value') == el.get('alt')) { el.set('value',''); } });
				el.addEvent('blur', function() { if(el.get('value') == '') { el.set('value',el.get('alt')); } });
			});
		}
		
	});

	var defs = new dwDefaults({
		collection: $$('#joinmailinglist .defaultfield')
	});

});

