var bkg = false;
var lbkg= false;
var path = 'images/about/tmb/';

var processing_crossfade = false;
var opacity = 0;
var opacity_st = 0;
var interval = 0;

var randomPerson_timer = 0;
var randPerson = '';

var last_idx = 0;

var ie = false
if (document.all) ie = true;

$(document).ready( function() {
	$('.people a').eq(last_idx).css('color', '#f9b344');
	randomPerson_timer = setTimeout( "doFadeOut()", 3000 );

	$('.people a').mouseover( function() {
		clearTimeout( randomPerson_timer );
		idx = $('.people a').index(this);
		crossFadeTo(idx);
	});
	$('.people a').mouseout( function() {
		clearTimeout( randomPerson_timer );
		idx = $('.people a').index(this);
		$('.people a').eq(idx).css('color', '#fff');
		randomPerson_timer = setTimeout( 'doFadeOut()', 3000 );
	});
	/*
	$('.people a').focus( function() {
		clearTimeout( randomPerson_timer );
		idx = $('.people a').index(this);
		crossFadeTo(idx);
	});
	$('.people a').blur( function() {
		idx = $('.people a').index(this);
		crossFadeTo(idx);
		$('.people a').eq(idx).css('color', '#fff');
		timer = setTimeout( 'randomPerson', 3000 );
	});
	*/
});


function setOpacity( jObj, opacity )
{
	if (ie)
		$(jObj).css('filter','alpha(opacity='+Math.round(opacity*100)+')');
	else
		$(jObj).css('opacity',opacity);
}

function getOpacity( jObj )
{
	if (ie)
	{
		opacity = $(jObj).css('filter');
		if (opacity == 'undefined' || opacity == null)
			opacity = 100;
		else
		{
			opacity = opacity.match( / *alpha\( *opacity *= *([0-9]+) *\) */ );
			if (opacity!=null)
				opacity = parseInt( opacity[1] );
			else
				opacity = 100;
		}
		opacity /= 100;
	}
	else
		opacity = $(jObj).css('opacity');

	return opacity;
}
function doCrossFade(jObj)
{
	max_time = 1000;
	if (ie)
	{
		// jQery internet exploDer filter.alpha animate solution
		interval_st  = 25;
		interval_end = max_time;
		interval_imp = interval_end/interval_st;

		opacity_start = opacity;
		opacity_end   = (bkg ? 0.00 : 1.00);
		opacity_delta = opacity_end - opacity_start;
		opacity_st    = opacity_delta / interval_imp;

		interval = 0;
		interval_timer = setInterval
		( 
			function () {
				$(jObj).css("filter", "alpha(opacity="+Math.round(opacity*100)+")");
				opacity += opacity_st;
				opacity = Math.max(Math.min(opacity,1),0);
				if (interval++ > interval_imp) {
					clearInterval(interval_timer);
					processing_crossfade = false;
				}
			}, interval_st 
		);
	}
	else
	{
		// simple jQuery: just animate the opacity 
		$(jObj).animate({
			"opacity":(bkg?0.00:1.00)
		}, max_time, function() {
			processing_crossfade = false;
		});
	}
}
function doFadeOut(jObj)
{
	crossFadeTo(image_list.length-1);
	randomPerson_timer = setTimeout( 'randomPerson()', 3000 );
}


function crossFadeTo(idx)
{
	img_fore = $('#photo').attr('src')
	img_back = $('#photo_bkg').css('backgroundImage');
	if (img_back!=null)
		img_back = img_back.match( /^url\( *([^\) ]*) *\)$/ )[1];
	else
		img_back = img_fore;

	img_new  = path + image_list[idx];
	if (last_idx!=idx)
	{
		$('.people a').eq(last_idx).css('color', '#fff');
		$('.people a').eq(idx).css('color', '#f9b344');
		last_idx = idx;
	}
	opacity = getOpacity( '#photo' );
//	alert( img_fore+"\n"+img_back );
	if (opacity == 1.00) {
		bkg = true;	lbkg = true;
	} else if (opacity == 0.00)	{
		bkg = false; lbkg = false;
	} else
		bkg = lbkg;

	if (bkg)
		$('#photo_bkg').css('backgroundImage', 'url('+img_new+')');
	else
	{
		$('#photo').attr('src', img_new);
		if (opacity>0.00 && opacity<1.00)
			setOpacity("#photo",opacity);
	}
	if (!processing_crossfade && (img_fore != img_back))
	{
		processing_crossfade = true;
		doCrossFade('#photo');
	}
}
function randomPerson()
{
	clearTimeout( randomPerson_timer );
	do {
		tmp = Math.round(Math.random()*(image_list.length-2));
	} while (tmp==randPerson);
	randPerson = tmp;
	crossFadeTo(randPerson);
	randomPerson_timer = setTimeout( 'randomPerson()', 3000 );
}
