var fadeTimeout;

function _ft(title, currentArray, currentIndex, currentOpts) {
	return (title && title.length ? '<strong>' + title + '</strong>' : '' ) + '<span style="float:left">' + (currentIndex + 1) + ' of ' + currentArray.length + '</span><span style="float:right; cursor:pointer" onclick="$.fancybox.close();">[Close]</span>';
}
function gallery(bind)
{
	$(document).ready(function() {
		$('head').append('<link rel="stylesheet" href="fancybox/jquery.fancybox-1.3.1.css" type="text/css" />');
		$.getScript('fancybox/jquery.fancybox-1.3.1.pack.js', function(){
			$(bind).attr('rel', 'gallery').fancybox({
				'titlePosition': 'inside',
				'titleFormat': _ft,
				'showCloseButton': false,
				'transitionIn': 'elastic',
				'transitionOut': 'elastic'
			});
		});
		$.getScript('fancybox/jquery.easing-1.3.pack.js');
		$.getScript('fancybox/jquery.mousewheel-3.0.2.pack.js');

	});
}

function showInfo()
{
	clearTimeout(fadeTimeout);
	$('*').stop(true);
	$('#menu').slideUp(250, function() { $('#menu').html(''); } );
	$('#info').slideDown(250);
	$('#pop').fadeOut(250);
//	$('#info').animate({ height: '50%' }, 250 );
	$('#header, #footer').fadeTo(250, 1);
	var dt = new Date();
	var tz = dt.getTimezoneOffset();

	$.post('index.php', { 'info': page, 'file': file, 'tz': tz }, function (data)
	{
		$('#info').html(data);
	});

}

function showMenu()
{
	clearTimeout(fadeTimeout);
	$('*').stop(true);
	$('#info').slideUp(250, function() { $('#info').html(''); } );
	$('#menu').slideDown(250);
	$('#pop').fadeOut(250);
	$('#header, #footer').fadeTo(250, 1);

	$.post('index.php', { 'menu': page }, function (data)
	{
		$('#menu').html(data);
	});
}

function hideMenu()
{
	$('#menu, #info').slideUp(500, function() {
		$('#header, #footer').fadeTo(250, 0.2);
		$('#menu, #info').html('');
	});
}

// show contents
function v3pop(args) {
	clearTimeout(fadeTimeout);
	$('*').stop(true);
	$('#menu, #info').hide().html('');
	$('#header, #footer').stop().fadeTo(250, 1);
	if($('#pop').size() < 1)
	{
		$('body').append('<div id="pop" class="pop"></div>');
		$('#pop').bind('click', function (event)
		{
			if(event.target.id == 'pop' || event.target.nodeName.toUpperCase() == 'H2')
			{
				$('#pop').stop().fadeOut('1000', function() {
					$('#header, #footer').stop().fadeTo(250, 0.2);
				});
			}
		});
	}
	$('#pop').html('');
	$.post('index.php', { pop: args, page: page }, function(data) {
		$('#pop').html(data);
	});
	$('#pop').stop().fadeIn(500);
}

$().ready(function ()
{
// header
	// fade in top and bottom bars when mouse enters them
	$('#header, #footer').bind('mouseenter', function ()
	{
		clearTimeout(fadeTimeout);
		if($('#menu').is(':hidden'))
		{
			$('#header, #footer').fadeTo(250, 1);
//			$('#content').fadeTo(250, 0.2);
		}
	});
	// fade out top and bottom bars when mouse leaves them
	$('#header, #footer').bind('mouseleave', function ()
	{
		if(!$('#menu, #info, #pop').is(':visible'))
		{
			fadeTimeout = setTimeout("if(!$('#menu, #info, #pop').is(':visible')) {$('#header, #footer').fadeTo(1000, 0.2) }", 1000);
		}
	});
	// show menu when header clicked
	$('#header').bind('click', function (event)
	{
		if (event.target.nodeName.toUpperCase() != 'A')
		{
			showMenu();
		}
	});


	// show info when footer clicked
	$('#footer').bind('click', function ()
	{
		showInfo();
	});

	fadeTimeout = setTimeout("if(!$('#menu, #info, #pop').is(':visible')) {$('#header, #footer').fadeTo(1000, 0.2) }", 5000);

// menu
	// hide menu when clicked
	$('#menu').bind('click', function (event)
	{
		if(event.target.id == 'menu' || event.target.nodeName.toUpperCase() == 'H2')
		{
			hideMenu();
		}
	});
	$('#info').bind('click', function (event)
	{
		if(event.target.id == 'info')
		{
			hideMenu();
		}
	});
});