$(document).ready
(
	function() 
	{
		$('#header').init
		(
			function()
			{
				$('#header').hide();
				$('#header').fadeIn('slow');
			}
		);

		$('#header').init();
		
		$('.extlink').click
		(
			function()
			{
				window.open($(this).attr('href'));
				return false;
			}
		);
		
		$('#login').click
		(
			function()
			{
				$('#auth > .error').remove();
				$('#login').hide();
				var form = $('form[name=login]');
				if (form.hasClass('hidden'))
				{
					form.removeClass('hidden');
					$('#username').focus();
				} else
				{
					form.addClass('hidden');
				}
				return false;
			}
		);	
		
		$('#menu > ul > li > a:not([class=active])' ).hover
		(	
			function() // over
			{
				$(this).animate({opacity: 0.1},0);
				$(this).animate({opacity: 1},500);
			}, 
			function() // out
			{
			}
		);
		
		$('input:text').blur
		(
			function()
			{
				ItemUtil.validate($(this));
			}
		);
		

		$('a.thumb,.contactThumb > a' ).hover
		(	
			function() // over
			{
				$(this).animate({opacity: 0.5},0);
				$(this).animate({opacity: 1},500);
			}, 
			function() // out
			{
			}
		);	

		$('.mainThumb > .switch').click
		(
			function()
			{
				stopThumbTimer($(this));
				reloadThumb($(this));
				startThumbTimer($(this));
				return false;
			}
		);
		
		$('#popupThumb > a#thumb').click
		(
			function()
			{
				closeThumb();
				return false;
			}
		);
		
		$('a.thumb').click
		(
			function(event)
			{
				popupThumb($(this));
				return false;
			}
		);
		
		startThumbTimer($('.mainThumb > .switch'));
	}
);

function closeThumb()
{
	$('#popupThumb').fadeOut('fast');
	$('#popupThumb').find('img').fadeOut('slow');
	popupBackgroundOff();	
}

function navThumbClick(parentId, e)
{
	var e = $('#' + parentId + ' > a.thumb[href=' + e.href + ']');
	if (e.size() == 1)
	{
		e.click();
	}
}

function popupThumb(e)
{
	var href = e.attr('href');
	var parent = e.parent();
	var img = e.find('img');
	var thumb = $('#popupThumb').find('img');
	//
	var preload = new Image();
	preload.onload = function()
	{
		thumb.hide();
		thumb.attr('src', href);
		thumb.attr('class', img.attr('class'));
		var navigation = '<li>' + parent.attr('title') + '&nbsp;&nbsp;</li>'; 
		var i = 1;
		//
		var thumbs = parent.find('a.thumb');
		thumbs.each
		(
			function()
			{
				var cls = '';
				if ($(this).attr('href') == href)
				{
					cls = ' class="active"';
				}
				var li = '<li' + cls + '><a href="' + $(this).attr('href') + '" onclick="navThumbClick(\'' + parent.attr('id') + '\', this); return false;">' + (i++) + '</a></li>';
				navigation += li;
			}
		)
		$('#popupThumb').find('.navigation').html(navigation + '<li class="right"><a href="#" onclick="closeThumb(); return false;">&times;</a></li>');
		// show thumb if neccessary
		if (!$('#popupThumb').is(':visible'))
		{
			popupBackgroundOn();
			$('#popupThumb').center();
			$('#popupThumb').show();
		}
		thumb.fadeIn('fast');	
	}
	preload.src = href;	
}

function popupBackgroundOn()
{
	var background = $('#popupBackground');
	background.show();
	background.fadeTo("fast", .4);
	background.height($(window).height() + $(window).scrollTop());
	background.width($(window).width() + $(window).scrollLeft());
}

function popupBackgroundOff()
{
	var background = $('#popupBackground');
	background.fadeTo("fast", 0);
	background.hide();
}

function startThumbTimer(e)
{
	e.everyTime(6000, 'thumbSwitch', 
			function() 
			{
				reloadThumb(e);
			}
	);	
}

function stopThumbTimer(e)
{
	e.stopTime('thumbSwitch');
}

function reloadThumb(e)
{
	e.fadeTo('normal', .1, 
			function()
			{
				var thumb = e.find('img');
				var images = e.parent().find('ul[class=hidden] > li > img');
				var currentSrc = thumb.attr('src');
				var next = e.parent().find('ul[class=hidden] > li > img:first');
				var found = false;
				images.each
				(
						function()
						{
							if (found)
							{
								next = $(this);
								found = false;
							}
							if (currentSrc == $(this).attr('src'))
							{
								found = true;
							}
						}
				);
				// preload
				var preload = new Image();
				preload.onload = function()
				{
					thumb.attr('src', next.attr('src'));
					thumb.attr('alt', next.attr('alt'));
					e.fadeTo('normal', 1);
				}
				preload.src = next.attr('src');
			}
		);
}
