// IP Council

var image1;
var image2;
var image3;

var images = [];

var stepper = 1;
var cycleInterval;

var timer = 5000;

$( document ).ready( function()
{	
	image1 = new Image( 739, 205 );
	image2 = new Image( 739, 205 );
	image3 = new Image( 739, 205 );
	
	image1.src = 'images/homepage.jpg';
	image2.src = 'images/homepage-cycle-1.jpg';
	image3.src = 'images/homepage-cycle-2.jpg';
	
	images.push( image1, image2, image3 );
	
	startInterval();
	applyTabs();
	flowText( 0 );
	
	lineUpArticles();
});

function startInterval()
{
	cycleInterval = setInterval( 'cycleBG()', timer );
}

function setBGByIndex( index )
{
	$( 'div#homepage-img' ).css( { 'background' : 'url( "' + images[ index ].src + '" ) no-repeat' } );
}

function cycleBG()
{
	if( stepper == images.length ) { stepper = 0; }
	
	$( 'div#homepage-img' ).fadeOut( 0 );
	$( 'div#homepage-img' ).css( { 'background' : 'url( "' + images[ stepper ].src + '" ) no-repeat' } );
	selectTab( stepper );
	$( 'div#homepage-img' ).fadeIn( 'fast' );
	
	stepper++;
}

function applyTabs()
{
	$( 'div#homepage-tabs div.tab a' ).each
	(
		function( index )
		{			
			$( this ).bind( 'mouseover', function()
			{
				clearInterval( cycleInterval );
				stepper = index;
				setBGByIndex( index );
				selectTab( index );
			});
			
			$( this ).bind( 'mouseout', function()
			{
				stepper++;
				startInterval();
			});
		}
	
	);
}

function selectTab( index )
{
	var tabs = $( 'div div#homepage-tabs div.tab a' );
	
	tabs.removeClass( 'current' );
	tabs.removeClass( 'after' );
	
	$( tabs[ index ] ).addClass( 'current' );
	$( tabs[ index + 1 ] ).addClass( 'after' );
	
	flowText( index );
}

function flowText( index )
{
	$( 'div#homepage-quote' ).html( $( $( 'div#hidden-quotes div.quote' )[ index ] ).html() );
}

function lineUpArticles()
{
	var maxheight = 0;

	$( 'div.article' ).each
	(
		function( i )
		{
			// console.log( $( this ).height() );
			if( $( this ).height() > maxheight ) { maxheight = $( this ).height() }
		}
	);
	
	$( 'div.article' ).height( maxheight );
	$( 'div#left' ).height( maxheight + 310 );
}
