window.addEvent('domready', function() {
	
	//create our Accordion instance - for the "news" section of the homepage
	var collapse = new Accordion($('div.contentColumns'), 'div.contentColumns h2', 'div.news', {
		show: 0,
		opacity: true,
		initialDisplayFx: false,
		onActive: function(toggler, element){
			toggler.getElements('span').setStyle('background-position', 'right -32px');
		},
		onBackground: function(toggler, element){
			toggler.getElements('span').setStyle('background-position', 'right -7px');
		}
	});
	
	//add the click event to all the nav elements
	$(document.body).getElements('#testimonialNav a').addEvents({
	    'click': gotoTestimonial
	});

	//remove the click for the arrows
	$(document.body).getElements('a.leftArrow').removeEvent(
	    'click', gotoTestimonial
	);
	$(document.body).getElements('a.rightArrow').removeEvent(
	    'click', gotoTestimonial
	);
	
	//add custom click function for the arrows
	$(document.body).getElements('a.leftArrow').addEvents({
	    'click': previousTestimonial
	});
	$(document.body).getElements('a.rightArrow').addEvents({
	    'click': nextTestimonial
	});
	
	//translates english numbers to numerial values (for bookmarkable testimonials)
	var translator = { "first" : 1, "second" : 2, "third" : 3, "fourth" : 4, "fifth" : 5 };
	
	function gotoTestimonial()
	{
		var address = $(this).get("href");
		address = address.split("#");
		address = address[ address.length - 1 ];
		
		var current = $(document.body).getElements( '#testimonials li.visible' );
		var oldNavNumber = current.get("id").toString();
		oldNavNumber = oldNavNumber.split("_")[0];

		var oldSize = current[0].getSize();
		current.set( "class", null );
		
		var newTestimonial = $( document.body ).getElements( '#testimonials #' + address );
		newTestimonial.set( "class", "visible" );
		
		var newSize = newTestimonial[0].getSize();
		newTestimonial.setStyle( "height", oldSize.y );
		newTestimonial.morph({ "height" : [ newSize.y ] });
		
		var navNumber = address.split( "_" )[ 0 ];
		
		var navList = $(document.body).getElements('#testimonialNav li');
		navList[ translator[ oldNavNumber ] ].erase("class");
		navList[ translator[ navNumber ] ].set("class", "current");
		
		return false;
	}
	
	function nextTestimonial()
	{
		var testimonialList = $(document.body).getElements('#testimonials li');
		for(var i = 0; i < testimonialList.length; i++ )
		{			
			var current = testimonialList[ i ].match( ".visible" ) ? testimonialList[ i ] : undefined;
			if( current )
			{
				if( testimonialList[ i + 1 ] )
				{
					var oldSize = current.getSize();
					current.erase( "class" );
					testimonialList[ i + 1 ].set( "class", "visible" );
					
					var newSize = testimonialList[ i + 1 ].getSize();
					testimonialList[ i + 1 ].setStyle( "height", oldSize.y );
					testimonialList[ i + 1 ].morph({ "height" : [ newSize.y ] });
					
					var navList = $(document.body).getElements('#testimonialNav li');
					
					navList[ i + 1 ].erase( "class" );
					navList[ i + 2 ].set( "class", "current" );
					
					break;
				}
			}
		}
		
		return false;
	}
	
	function previousTestimonial()
	{
		var testimonialList = $(document.body).getElements('#testimonials li');
		for( var i = testimonialList.length - 1; i > -1; i-- )
		{			
			var current = testimonialList[ i ].match( ".visible" ) ? testimonialList[ i ] : undefined;
			if( current )
			{
				if( testimonialList[ i - 1 ] )
				{
					var oldSize = current.getSize();
					current.erase( "class" );
					testimonialList[ i - 1 ].set( "class", "visible" );
					
					var newSize = testimonialList[ i - 1 ].getSize();
					testimonialList[ i - 1 ].setStyle( "height", oldSize.y );
					testimonialList[ i - 1 ].morph({ "height" : [ newSize.y ] });

					var navList = $(document.body).getElements('#testimonialNav li');
					
					navList[ i + 1 ].erase( "class" );
					navList[ i ].set( "class", "current" );
					
					break;
				}
			}
		}
		
		
		return false;
	}
});