// ***********************************************************************
//   Script for controlling first page feature item
// ***********************************************************************
// this array consists of the id attributes of the divs we wish to alternate between
var divs_to_fade = new Array( 'feature_item_0', 'feature_item_1', 'feature_item_2', 'feature_item_3', 'feature_item_4' );

// the starting index in the above array.  It should be set to the value of the div which doesn't have the CSS Display property set to "none"
var feature_index = 0;

// the number of milliseconds between swaps.  Default is five seconds.
var feature_wait = 5000;

// the interval, so we can access it later
var feature_interval = null;

// only keep on fading when this is not true
var is_paused = false;

// the function that performs the fade
function swapFade() {
	// Silly IE hack because it is stupid, oh so stupid
	if( feature_index == -1 ) feature_index = 0;

	if( !is_paused )
	{
		Effect.Fade(divs_to_fade[feature_index], { duration:1, from:1.0, to:0.0 });
		document.getElementById( 'feature_controls_number' + (feature_index+1) ).className = "feature_controls_inactive";
		feature_index++;
		if (feature_index == 5) feature_index = 0;
		document.getElementById( 'feature_controls_number' + (feature_index+1) ).className = "feature_controls_active";
		Effect.Appear(divs_to_fade[feature_index], { duration:1, from:0.0, to:1.0 });
	}
}

// the function that handles clicks
function swapFadeToItem( item ) {
	// Silly IE hack because it is stupid, oh so stupid
	if( feature_index == -1 ) feature_index = 0;
	
	clearInterval( feature_interval );	
	if( item != feature_index )
	{
		document.getElementById( 'feature_controls_number' + (feature_index+1) ).className = "feature_controls_inactive";
		document.getElementById( 'feature_controls_number' + (item+1) ).className = "feature_controls_active";
		Effect.Fade(divs_to_fade[feature_index], { duration:1, from:1.0, to:0.0 });
		Effect.Appear(divs_to_fade[item], { duration:1, from:0.0, to:1.0 });
	}
	feature_index = Number( item );
}

function swapFadeUp() {
	// Silly IE hack because it is stupid, oh so stupid
	if( feature_index == -1 ) feature_index = 0;
	
	if( feature_index == 0 )
		j = 4;
	else
		j = feature_index - 1;
	swapFadeToItem( j );
}
function swapFadeDown() {
	// Silly IE hack because it is stupid, oh so stupid
	if( feature_index == -1 ) feature_index = 0;
	
	if( feature_index == 4 )
		j = 0;
	else 
		j = feature_index + 1;
	swapFadeToItem( j );
}
function pauseFade() {
	is_paused = true;
}
function resumeFade() {
	is_paused = false;
}

// the onload event handler that starts the fading.
function startPage() {
	setTimeout( "feature_interval = setInterval('swapFade()',feature_wait)", feature_wait );
}

// ***********************************************************************
// ***********************************************************************
// ***********************************************************************


// ***********************************************************************
//   Script for controlling first page contest stage
// ***********************************************************************
function hideContest( row_id, contest_id, user_id )
{
	// Vars
	var row = $( "con_row_" + row_id );
	var table = $( "con_table" );
	var stage = $( "con_stage" );
	
	// Save pref
	new Ajax.Request( "/tavlingar/ajax_hide_contest.php", {
		method: 'get',
		parameters: {
			"contest": contest_id,
			"user": user_id
		},
		onSuccess: function(transport){
			// Remove row
			row.parentNode.removeChild( row );
			
			// Remove stage if no rows left
			if( table.rows.length == 0 )
			{
				stage.parentNode.removeChild( stage );
				return true;
			}
			
			// Recolor lines
			for( var i=0; i<table.rows.length; i++ )
			{
				if( i%2==0 )
					table.rows[i].className = "con_row";
				else if( i%2==1 )
					table.rows[i].className = "con_row con_borders";
			}
		}
	});
}
// ***********************************************************************
// ***********************************************************************
// ***********************************************************************