
// set up these variables so they're out of the functions (fixing scope)
// first one is the interval variable to hold the rotation (the setInterval )
var home_rotator=false
// these keep track of the current index of the image. 
// the panels may have different numbers of images in them.
//
var h1_index=0;
var h2_index=0;
var h3_index=0;

if ( homepage_timing > 0 ) {

	home_rotator = setInterval(
		function() {
			clicknext( 'h1' );
			clicknext( 'h2' );
			clicknext( 'h3' );
			}, homepage_timing * 1000 );
}

function clicknext( panel ) {
		if ( panel == 'h1' ) {
				index=h1_index;
		}
		else if ( panel == 'h2' ) {
				index=h2_index;
		}
		else if ( panel == 'h3' ) {
				index=h3_index;
		}
		else {
				return false;
		}
		index++;
		panelShow( panel, index );
		// get the current (index) of the panel
		// add one. If it doesn't exist, go to 0.
}

function panelShow( panel, showid ) {
		var images=false;
		if ( panel == 'h1' ) {
				images=$('#feature-showcase .trophy');
				dots= $('#fs_dots li a');
				link=$('#fs_link');
				index=h1_index;
		}
		else if ( panel == 'h2' ) {
				images=$('#first-impressions .trophy');
				dots= $('#fi_dots li a');
				link=$('#fi_link');
				index=h2_index;
		}
		else if ( panel == 'h3' ) {
				images=$('#hyperdesk-panel .trophy');
				dots= $('#hd_dots li a');
				link=$('#hd_link');
				index=h3_index;
		}
		if ( images == false ) return false;
		if ( images.length <= 1 ) return false;
		if (index == showid ) return false;
		// with one image, showid could be 0 for the one image, since it's the index.
		// 
		if ( (showid+1)  > images.length ) { showid=0; }

		var newImg = images[showid];
		var oldImg = images[index];
		$(newImg).fadeIn('slow', function() { $(oldImg).fadeOut( 'slow')});
		// don't forget to increment the dot to the new index.
		dots.removeClass('active');
		$( dots[showid] ).addClass('active');
		// and don't forget to make the link work for the new item.
		newlink = $(newImg).attr('href');
		$(link).attr({href:newlink});
		if ( newlink.length == 0 ) {
				$(link).addClass('inactive').bind('click', function( evt ){ evt.preventDefault;} );
		}
		else {
				$(link).removeClass('inactive').unbind('click');
		}

		
		// lastly, reset the index outside this scope
		if ( panel == 'h1' ) {
				h1_index=showid;
		}
		else if ( panel == 'h2' ) {
				h2_index=showid;
		}
		else if ( panel == 'h3' ) {
				h3_index=showid;
		}
		
		return true;
}


$(document).ready( function() {

	/*
	h2_slides=$('.fnord');
	if ( h1_slides.length > 1 ) {
		for ( var i=0;i<h1_slides.length;i++) {
			$('.fs-options .hometab-pages').append( $('<li><a href="" >&nbsp;</a></li>'));
		};
	}

	h3_slides=$('.hyperdesk');
	if ( h3_slides.length > 1 ) {
		for ( var i=0;i<h3_slides.length;i++) {
			$('#hd_dots').append( $('<li><a href="" >&nbsp;</a></li>'));
		};
	}
	*/

}); // herpderr, don't remove this.

