/*

	@Class:	InputClearAndReplace
	@Author:	Brandon Gray for O3 World 2008
	@Brief:	Loops through all inputs/textareas with a class name of 'clear_replace' and clears the default text when focused
			when focus is lost it checks to see if the field is blank (no new data entered), if value is blank the default text is placed back in
	@Example: <input type="text" name="firstname" id="firstname" value="" class="clear_replace" />
			
*/

var InputClearAndReplace = new Class ({
	
	options: {
		
		InputElement: '.clear_replace'
		
	},
	
	initialize: function()  {
		
		var inputElementList = $$( this.options.InputElement );
		
		var originalValue = new Array();
		
		inputElementList.each( function( element, i ) {
			
			originalValue.push( inputElementList[ i ].value );
			
			element.onfocus = function() {
				
				if( this.value == originalValue[ i ] ) this.value = '';
				
			},
			
			element.onblur = function() {
				
				if( this.value == '' ) this.value = originalValue[ i ];
				
			}
			
		});
		
	}

});

/*

	@Class:		FeaturedProgram
	@Author:		Brandon Gray
	@Brief:		Controls the display of content in the "featured" section on the home page.
	@Requirements:	Mootools 1.2
			
*/
var FeaturedProgram = new Class ({
	
	initialize: function()  {		
		this.menuLinks 	= $$( '#feat_tabs ul a' );
		this.contentDivs	= $$( '#featured_content_wrap div.featured_content' );
		this.pos			= 0;
		this.total		= this.menuLinks.length;
		this.isPaused 		= false;
		this.timer		= this.updateCount.bind( this ).periodical( 5000 );
		
		// Grab the images and preload them in the background
		var images = $$( '#feat_tabs li a' ).get( 'rel' );
		var myImages = Asset.images( images );
		
		// Assign a width to the UL element so it centers. Each A is 9px wide plus 10px horizontal margin (19px total)
		var listWidth = this.menuLinks.length * 19;
		$( 'feat_tabs' ).getElement( 'ul' ).setStyle( 'width', listWidth );
		
		// Position the tabs
		$( 'feat_tabs' ).setStyles({
			'width': 		listWidth,
			'left': 		( 960 - listWidth ) / 2,
			'visibility': 	'visible'
		});
		
		for ( var i = 0; i < this.menuLinks.length; i++ ) this.menuLinks[ i ].addEvent( 'click', this.setMenu.bindWithEvent( this, [ i ] ) );
		
		$( 'feat_prev' ).addEvent( 'click', function( event ) {
		
			this.updateCount( 'prev' );
			event.stop();

		}.bind( this ) );
		
		$( 'feat_next' ).addEvent( 'click', function( event ) {
		
			this.updateCount( 'next' );
			event.stop();
			
		}.bind( this ) );
				
	},
	
	updateCount: function( direction ) {
		
		if ( direction ) {
			
			$clear( this.timer );
			this.timer = null;
			this.isPaused = true;
			
			if ( direction == 'prev' ) {
			
				// If we're currently on the first image then set pos to the last image in the array, otherwise set it to the previous image in the array
				( this.pos == 0 ) ?	this.pos = this.total - 1 : this.pos--;
				
			} else {
				
				// If we're currently on the last image then set pos to the first image in the array, otherwise set it to the next image in the array
				( this.pos == ( this.total - 1 ) ) ? this.pos = 0 : this.pos++;
				
			};
			
		} else {
			
			( this.pos != ( this.total - 1 ) ) ? this.pos++ : this.pos = 0;
			
		};
		
		this.setMenu( null, this.pos );
		this.setContent( this.pos );
		
	},
	
	setMenu: function( event, i ) {
		
		if( event ) {
			
			event.stop();
			$clear( this.timer );
			this.timer = null;
			this.isPaused = true;
			
		}
		
		this.pos = i;
		
		for ( var j = 0; j < this.menuLinks.length; j++ ) {
			
			this.menuLinks[ j ].removeClass( 'active' );
			
		}
		
		this.menuLinks[ i ].addClass( 'active' );
		this.setContent( i );
		
	},
	
	setContent: function( i ) {
		
		var image = this.menuLinks[ i ].get( 'rel' );
		$( 'featured_image' ).getElement( 'img' ).set({
			
			'styles' : {
				'opacity' : '0'
			},
			'src' : image
		
		}).fade( '1' );
		
		for ( var k = 0; k < this.contentDivs.length; k++ ) {
			
			this.contentDivs[ k ].setStyle( 'display', 'none' );
			this.contentDivs[ k ].setStyle( 'opacity', '0' );
			
		};
		
		this.contentDivs[ i ].setStyle( 'display', 'block' );
		this.contentDivs[ i ].fade( '1' );
		$( 'featured_image' ).set( 'href', this.contentDivs[ i ].getElement( 'p' ).getNext( 'a' ).get( 'href' ) );
		
	}
	
});


window.addEvent( 'load', function() {
	
	var InputList = new InputClearAndReplace();
	if ( $( 'featured' ) ) var featured = new FeaturedProgram();
	
});

