var currentPic = '01';
var currentUl = 'c1';
var timer = null;

$(document).ready(function()
{
	$('#work').cycle({ fx: 'fade', speed: 0, timeout: 10000, before: function() { $('ul#number li').fadeTo(0, 0.5); $('ul#number #' + this.id).fadeTo(0, 1); } });
	$('#agree').cycle({ fx: 'scrollLeft', speed: 300, timeout: 5000 });
	
	if(section)
	{
		$('#slideshow ul:first a:first img').fadeTo(0, 1);

		var img = new Image();
		$(img).load(function()
		{
			$(this).hide();
			$('#slideshow #main').append(this);
			$(this).fadeIn();
		}).attr('src', '../images/' + section + '/01_crop.jpg');

		timer = setTimeout('changePic()', 7500);
		
		$('#slideshow ul li a').hover(function()
		{			
			if ($(this).attr('name') != currentPic)
			{
				$('#slideshow ul li a').each(function()
				{
					if ($(this).attr('name') != currentPic)
						$(this).children("img").fadeTo(0, 0.5);	
				});
				
				$(this).children("img").fadeTo("normal", 1);
			}
		},
		function()
		{
			if($(this).attr('name') != currentPic)
				$(this).children("img").fadeTo("normal", 0.5);
		});
		
		$('#slideshow ul li a').click(function()
		{
			if (section == 'presentation')
				stopVideo();
				
			clearTimeout(timer);
			
			$('#slideshow ul li a img').fadeTo(0, 0.5);
			
			currentPic = $(this).attr('name');
			$(this).children("img").fadeTo(0, 1);
			
			$('#slideshow #main').children("img").remove();
			var img = new Image();
			
			$(img).load(function()
			{
				$(this).hide();
				$('#slideshow #main').append(this);
				$(this).fadeIn();
				timer = setTimeout('changePic()', 7500);
			}).attr('src', '../images/' + section + '/' + currentPic + '_crop.jpg');		
		});
	}
});

function changePic()
{
	$('#slideshow ul#' + currentUl + ' li a img').fadeTo("normal", 0.5);
	obj = $('#slideshow ul#' + currentUl + ' li a[name=' + currentPic + ']').parent().next().children('a');
	
	if (!obj.attr('name'))
	{
		ul = $('#slideshow ul#' + currentUl).next('ul');
	
		if (!ul.attr('id'))
		{
			if (currentUl != 'c1')
			{
				$('#slideshow ul#' + currentUl).hide();
				currentUl = 'c1';
				$('#slideshow ul#' + currentUl).show();			

				section = $('#slideshow ul#' + currentUl).attr('name');
				obj = $('#slideshow ul#' + currentUl + ' li a[name=01]');
			}
			else
				obj = $('#slideshow ul li a[name=01]');
		}
		else
		{
			$('#slideshow ul#' + currentUl).hide();
			currentUl = ul.attr('id');
			$('#slideshow ul#' + currentUl).show();
			
			section = $('#slideshow ul#' + currentUl).attr('name');
			obj = $('#slideshow ul#' + currentUl + ' li a[name=01]');
		}
	}
	
	currentPic = obj.attr('name');
	obj.children("img").fadeTo("normal", 1);
			
	$('#slideshow #main').children("img").remove();
	var img = new Image();
		
	$(img).load(function()
	{
		$(this).hide();
		$('#slideshow #main').append(this);
		$(this).fadeIn();
		timer = setTimeout('changePic()', 7500);
	}).attr('src', '../images/' + section + '/' + currentPic + '_crop.jpg');
}
