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

	// Video popup
	if( $$( '.video-link').length ) {
		try{
			new videoPopup( 'video-link', {
				movieWidth: 880,
				movieHeight: 487,
				showNumbers: false,
				showControls: false
			});
		} catch ( err ){}

		//new VideoPlayer('video-pop');
	}

	if ( $$( 'form[name=academy_reg]' ).length) {
		new AcademyRegistration( { removeStudentPadding: false } );
	}

	// Form validation
	$$( '.error-message' ).each( function( elm ){
		elm.setProperty( 'style', '' );
	});

	// Handle error fields
	var error_label = $$( 'label.error-field' );
	for ( var i = 0; i < error_label.length; i++ ) {
		try {
			error_label[ i ].setProperty( 'style', '' );
			error_label[ i ].getParent( 'div.field' ).addClass( 'error-field' );
			var fields = error_label[ i ].getParent().getChildren( '.field-wrap, select, textarea' );
			if ( ! fields.length ) {
				fields = error_label[ i ].getParent().getChildren( 'select' );
			}
			if ( ! fields.length ) {
				fields = error_label[ i ].getParent().getChildren( 'textarea' );
			}

			if ( fields[ 0 ].hasClass( 'field-wrap' ) ) {
				fields[ 0 ].getChildren( 'input' )[ 0 ].setStyles( { 'border': 'none' } ); // ie fix
				fields[ 0 ].getChildren( 'input' )[ 0 ].setProperty( 'style', '' );
			} else {
				fields[ 0 ].setStyles({
					'background-color': '1px solid #FFC785',
					border: '1px solid #FFC785'
				}); // ie fix
				fields[ 0 ].setProperty( 'style', '' );
			}
		} catch ( err ){}
	}
	// Testimonials
	if ( $( 'sidebar-testimonial' ) )
		if ( $( 'sidebar-testimonial' ).getChildren( 'p' )[ 0 ].getChildren( 'a.show-more' ).length) {
			$( 'sidebar-testimonial' ).getChildren( 'p' )[ 0 ].getChildren( 'a.show-more' )[ 0 ].addEvent( 'click', function( event ){
				event.stop();
				var linkfx = new Fx.Tween( this );
				var contentfx = new Fx.Tween( this.getNext( '.more-content' ) );
				contentfx.start( 'display', 'none', 'inline' );
				linkfx.start( 'display', 'inline', 'none' );
			});
		}

	if ( $( 'stage' ) ) {
		new Carousel( 'stage', {
			child_el: 'div',
			auto_run: 6,
			active_name: 'slide-active',
			active_navigation: 'slide-nav-active'
		});
	}

	if ( $$( '.enews-signup' ).length) {
		$$( '.enews-signup' ).each( function( elm ){
			elm.addEvent( 'click', function( event ){
				event.stop();
				new HTMLBox( '', 'temp.aoa_enews.php', {
					cssFile: 'style-aoa_enews',
					onContentLoad: showContent,
					onLoad: setContent
				});
			});
		});
	}

	// Search bar
	if ( $( 'search' ) ) {
		$( 'search' ).addEvent( 'focus', function(){
			if ( this.value == 'SEARCH THIS SITE...' ){
				this.value = '';
			} else {
				this.select();
				this.focus();
			}
		});

		$( 'search' ).addEvent( 'blur', function(){
			if ( this.value == '' ){
				this.value =' SEARCH THIS SITE...';
			}
		});
	}

	if ( $$( 'form[name=searchform]' ).length){
		$$( 'form[name=searchform]' ).each( function( elm ){
			elm.addEvent( 'submit', function( event ){
				if ( $( 'search' ).value == 'SEARCH THIS SITE...' )
					event.stop();
			});
		});
	}

	// Tracking Events
	// pdfs & files
	if ( $$( '.tracker-file' ).length){
		$$( '.tracker-file' ).each( function( elm ){
			elm.addEvent( 'click', function( event ){
				var page = document.location.href;
				_gaq.push( [ '_trackPageview', page + '?page=' + elm.getProperty( 'href' ) ] );
				_gaq.push( [ 'glg._trackPageview', page + '?page=' + elm.getProperty( 'href' ) ] );
				//console.log(page+'?page='+elm.getProperty('href'));
			});
		});
	}

	// videos
	if ( $$( '.tracker-video' ).length ){
		$$( '.tracker-video' ).each( function( e ){
			e.addEvent( 'click', function( event ){
				label = e.getProperty( 'title' ).replace( ' ', '_' );
				if ( window.loginPopup )
					label = 'NOT_LOGGED_IN_' + label;
				_gaq.push( [ '_trackEvent', 'videos_benefit', 'play', label ] );
				_gaq.push( [ 'glg._trackEvent', 'videos_benefit', 'play', label ] );
			});
		});
	}

	// slide show
	if ( $$( '#slide-nav li' ).length){
		var slider_nav_i = 1;
		$$( '#slide-nav li' ).each( function( elm ){
			elm.addEvent( 'click', function( event ){
				_gaq.push( [ '_trackEvent', 'network_ad', 'click_button', slider_nav_i ] );
				_gaq.push( [ 'glg._trackEvent', 'network_ad', 'click_button', slider_nav_i ] );
				//console.log('network_ad'+'click_button'+slider_nav_i);
			});
			slider_nav_i++;
		});
	}

	// testimonial expand button
	if ( $$( '#sidebar-testimonial .show-more' ).length ){
		$$( '#sidebar-testimonial .show-more' ).each( function( elm ){
			elm.addEvent( 'click', function( event ) {
				_gaq.push( [ '_trackEvent', 'sidebar-testimonial', 'click', 'expand' ] );
				_gaq.push( [ 'glg._trackEvent', 'sidebar-testimonial', 'click', 'expand' ] );
				//console.log('sidebar-testimonial'+'click'+'expand');
			});
		});
	}

	// social media
	if ( $$( '.tracker-facebook' ).length ){
		$$( '.tracker-facebook' ).each( function( elm ){
			elm.addEvent( 'click', function( event ) {
				_gaq.push( [ '_trackEvent', 'offsite', 'click_link', 'facebook' ] );
				_gaq.push( [ 'glg._trackEvent', 'offsite', 'click_link', 'facebook' ] );
				//console.log('offsite-click_link-facebook');
			});
		});
	}

	if ( $$( '.tracker-twitter' ).length ){
		$$( '.tracker-twitter' ).each( function( elm ){
			elm.addEvent( 'click', function( event ){
				_gaq.push( [ '_trackEvent', 'offsite', 'click_link', 'twitter' ] );
				_gaq.push( [ 'glg._trackEvent', 'offsite', 'click_link', 'twitter' ] );
				//console.log( 'offsite-click_link-twitter' );
			});
		});
	}

});
