//
// Javascript library for the Albums module
// Copyright (c) 2010-2011, Yvan Rivard
//
// Started on : 2010-02-26
// Last update: 2011-11-29
//
// 2011-11-29 - Added jsDisplay for lightbox
//
// Requirements:
// 1. IDs must finish with a number with the count starting from one (ex: 'foobar1','foobar2',etc)
// 2. The displayed item must be number one when the page is loaded (ex: 'foobar1' must be visible)
// 3. The initial opacity of each element must be 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 or 100
//

var crossfade_id_name = new Array();
var crossfade_previous_id = new Array();
var crossfade_current_id = new Array();
var crossfade_previous_opacity = new Array();
var crossfade_current_opacity = new Array();
var crossfade_opacity_target = new Array();
var crossfade_transition_size = new Array();
var crossfade_timeout;
var crossfade_enable_timeout = false;
var crossfade_parent_container_final_height = 0;
var id_parent_container_final;

function jsDisplay( ref, value )
{
	if ( document.layers )
	{
		if ( value == 'block' )
			document.layers[ref].display = 'block';
		else if ( value == 'none' )
			document.layers[ref].display = 'none';
		else
		{
			current = (document.layers[ref].display == 'none') ? 'block' : 'none';
			document.layers[ref].display = current;
		}
	}
	else if ( document.all )
	{
		if ( value == 'block' )
			document.all[ref].style.display = 'block';
		else if ( value == 'none' )
			document.all[ref].style.display = 'none';
		else
		{
			current = (document.all[ref].style.display == 'none') ? 'block' : 'none';
			document.all[ref].style.display = current;
		}
	}
	else if ( document.getElementById )
	{
		if ( value == 'block' )
			document.getElementById(ref).style.display = 'block';
		else if ( value == 'none' )
			document.getElementById(ref).style.display = 'none';
		else
		{
			current = (document.getElementById(ref).style.display == 'none') ? 'block' : 'none';
			document.getElementById(ref).style.display = current;
		}
	}
}

function crossfade_id( id_parent_container, id_photo, id_text, id_name, id_fadetonumber_param )
{
	if ( !crossfade_enable_timeout )
	{
		crossfade_parent_container_final_height = 0;

		// --- Make parent container bigger if the new fade-in is bigger ---
		if ( document.getElementById( id_parent_container ).offsetHeight < 20 + document.getElementById( id_photo ).offsetHeight + document.getElementById( id_text ).offsetHeight )
		{
			document.getElementById( id_parent_container ).style.height = 20 + document.getElementById( id_photo ).offsetHeight + document.getElementById( id_text ).offsetHeight + 'px';
		}
		else
		{
			id_parent_container_final = id_parent_container;
			crossfade_parent_container_final_height = 20 + document.getElementById( id_photo ).offsetHeight + document.getElementById( id_text ).offsetHeight;
		}

		crossfade_id_name = id_name;
		for ( var i=0; i<crossfade_id_name.length; i++ )
		{
			if ( crossfade_previous_id[ crossfade_id_name[i] ] == null )
			{
				crossfade_previous_id[ crossfade_id_name[i] ] = 1;
			}
			switch ( id_fadetonumber_param )
			{
				case '-1':
					crossfade_previous_id[ crossfade_id_name[i] ] = crossfade_current_id[ crossfade_id_name[i] ];
					id_fadetonumber = crossfade_current_id[ crossfade_id_name[i] ] - 1;
				break;
				case '+1':
					crossfade_previous_id[ crossfade_id_name[i] ] = crossfade_current_id[ crossfade_id_name[i] ];
					id_fadetonumber = crossfade_current_id[ crossfade_id_name[i] ] + 1;
				break;
				default:
					id_fadetonumber = id_fadetonumber_param;
					crossfade_current_id[ crossfade_id_name[i] ] = id_fadetonumber;
					crossfade_previous_opacity[ crossfade_id_name[i] ] = document.getElementById( crossfade_id_name[i] + crossfade_previous_id[ crossfade_id_name[i] ] ).style.opacity * 100;
					crossfade_current_opacity[ crossfade_id_name[i] ] = document.getElementById( crossfade_id_name[i] + crossfade_current_id[ crossfade_id_name[i] ] ).style.opacity * 100;
 					crossfade_opacity_target[ crossfade_id_name[i] ] = crossfade_previous_opacity[ crossfade_id_name[i] ];
 					crossfade_transition_size[ crossfade_id_name[i] ] = ( crossfade_opacity_target[ crossfade_id_name[i] ] - crossfade_current_opacity[ crossfade_id_name[i] ] ) / 10;
				break;
			}

			// --- If we're not trying to cross-fade with the current ID and the element exists, start the cross-fade ---
			if ( ( id_fadetonumber != crossfade_previous_id[ crossfade_id_name[i] ] ) && ( document.getElementById( crossfade_id_name[i] + id_fadetonumber ) ) )
			{
				document.getElementById( crossfade_id_name[i] + id_fadetonumber ).style.visibility = 'visible';
				crossfade_current_id[ crossfade_id_name[i] ] = id_fadetonumber;
				crossfade_enable_timeout = true;
			}
		}

		if ( crossfade_enable_timeout )
		{
			crossfade_timeout = setTimeout( "crossfade_timer()", 10 );
		}

	}
}

function crossfade_timer()
{
	crossfade_enable_timeout = false;
	for ( var i=0; i<crossfade_id_name.length; i++ )
	{
		var id_style_previous = document.getElementById( crossfade_id_name[i] + crossfade_previous_id[ crossfade_id_name[i] ] ).style;
		var id_style_current = document.getElementById( crossfade_id_name[i] + crossfade_current_id[ crossfade_id_name[i] ] ).style;

		// Set opacity value for compliant browsers such as Firefox, Safari, Opera, Chrome, IE7 (0 to 0.99)
		// (0.99 maximum because of a Firefox/Mozilla bug which makes everything flickers if set to 1.00)
//		id_style_previous.opacity = ( crossfade_opacity_id[ crossfade_id_name[i] ] / 100 ) - 0.01;
//		id_style_current.opacity  = ( ( 100 - crossfade_opacity_id[ crossfade_id_name[i] ] ) / 100 ) -0.01;

		if ( crossfade_current_opacity[ crossfade_id_name[i] ] < crossfade_opacity_target[ crossfade_id_name[i] ] )
		{
			crossfade_previous_opacity[ crossfade_id_name[i] ] -= crossfade_transition_size[ crossfade_id_name[i] ];
			crossfade_current_opacity[ crossfade_id_name[i] ] += crossfade_transition_size[ crossfade_id_name[i] ];
			id_style_previous.opacity = ( crossfade_previous_opacity[ crossfade_id_name[i] ] / 100 );
			id_style_current.opacity  = ( crossfade_current_opacity[ crossfade_id_name[i] ] / 100 );

			// Set opacity value for older IE browsers (0 to 100)
			id_style_previous.filter = 'alpha(opacity=' + crossfade_previous_opacity[ crossfade_id_name[i] ] + ')';
			id_style_current.filter  = 'alpha(opacity=' + crossfade_current_opacity[ crossfade_id_name[i] ] + ')';

			crossfade_enable_timeout = true;
		}
		else
		{
			if ( crossfade_previous_opacity[ crossfade_id_name[i] ] == 0 )
			{
				id_style_previous.visibility = 'hidden';
			}
			crossfade_previous_id[ crossfade_id_name[i] ] = crossfade_current_id[ crossfade_id_name[i] ];

			if ( crossfade_parent_container_final_height > 0 )
			{
				document.getElementById( id_parent_container_final ).style.height = crossfade_parent_container_final_height + 'px';
			}

		}

	}

	if ( crossfade_enable_timeout )
	{
		crossfade_timeout = setTimeout( "crossfade_timer()", 50 );
	}
}

