function checkButtons()
{
	var previous = $('#slide div.active').prev();
	var next = $('#slide div.active').next();
	
	if(previous.length < 1) 
		$('#left').fadeOut();
	else	
		$('#left').fadeIn();
			
	if(next.length < 1) 
		$('#right').fadeOut();
	else	
		$('#right').fadeIn();
}
function checkHistory()
{	
	// Check for a # in the url and activate it:
	var url = window.location.href;
	if(url.lastIndexOf('#'))
	{
		var activeFrame = '#'+$('#slide div.active').attr('id');
		var target = url.substring(url.lastIndexOf('#'));
		
		if(target != activeFrame && !window.sliding)
		{
			activate(target);
		}
	}
	
}
function preload()
{
	_preload($('#slide div.active'));
}
function _preload(target)
{
	if($('div.content',target).length == 0)
	{
		// check for the ajax pseudoclass that has the html filename 
		var page = target.attr('id');
		$('#'+page).load('/piece/'+page+'/ div.content',function()
		{		
			if(!$('#'+page).hasClass('active'))
				$('#'+page).fadeTo('fast',window.dim);
			$('#'+page+' div.content').fadeIn();
			positionThumbs();
			
			if(target.next())
				_preload(target.next());
				
			if(target.prev())
				_preload(target.prev());
			
			
			$('#slide div.active .content div.image a').lightBox({fixedNavigation:true});

		});
	}
}
function activateImage(target)
{
	if(target == '' || $(target).hasClass('current'))
		return false;
	
	var theid = target.attr('id');
	theid = theid.replace('thumb-','');
	target = theid;
	// image:
	$('#slide div.active div.image.current').removeClass('current').fadeOut();
	$('div.image#image-'+target).hide().addClass('current').fadeIn();
	
	// thumb:
	$('#slide div.active li.current').removeClass('current');
	$('#slide div.active li#thumb-'+target).addClass('current');	
	
}
function activate(target)
{
	if(target == '')
		return false;
			
	var target = $(target);
	
	if(target.length != 0 && !target.hasClass('active'))
	{
		var ease = 'easeOutCirc';
	
		// get it's id for the # string:
		var name = target.attr('id');
		window.sliding = true;
		
		$('#slide div.active')
			.removeClass('active')
			.addClass('inactive')
			.animate({opacity:dim});
			
		target
			.addClass('active')
			.animate({opacity:active});
			
		checkButtons();
					
		// Load in the content
		if($('#'+name+' .content').length == 0 && $('#'+name).hasClass('ajax'))
		{
			// check for the ajax pseudoclass that has the html filename 
			var page = name=='home' ? 'index' : name.replace('image-',''); 
			$('#'+name).load('/piece/'+page+'/ div.content',function()
			{
				//$('#slide div.active').css('background-image','url(img/bg_item.png)');
				$('#slide div.active .content').fadeIn('fast');
			});
		}
		positionThumbs();
		$('#wrap').scrollTo(target,400,{
			offset:-$(window).width()/2,
			over:0.5,
			onAfter:function()
			{
				//window.location = '#'+name;
				window.sliding  = false;

			}
		});
	}
}
function positionThumbs()
{
	$('ul.thumbs').each(function()
	{
		var ul = $(this);
		var ml = '-'+(3+($('li',ul).length * $('li',ul).outerWidth())/2)+'px';
				
		ul.css({marginLeft:ml});
		
		$('div.image a',$(this).parent()).lightBox({fixedNavigation:true});		
	});
}
function fixwrap()
{

	$('#wrap').width($(window).width());
	$('#wrap').css({paddingTop:(($(window).height()/2)-$('#wrap').height()/2)-100});
	
	var arrowpos =$(window).height()/2+10;
	if (arrowpos  < 360 ) arrowpos = 360; 
	
	$('#left').css({top:arrowpos});
	$('#right').css({top:arrowpos});
}
$(document).ready(function()
{	 
	window.screenPause = 5000;
	dim = 0.4;
	window.dim = dim;
	active = 1.0;
	window.sliding  = false;
	$(window).resize(fixwrap);
	fixwrap();
	$('.inactive').css({opacity:dim});
	//$('.item').width($(window).width()-160);
	
	$('#right').click(function()
	{
		var target = $('#slide div.active').next();
		if(!window.sliding) activate(target);
		return(false);
	});
	/*$("a[href^='#']").click(function()
	{
		var theid = $(this).attr('href');
		if(!window.sliding) activate(theid);
		return(false);
	});*/
	$('#left').click(function()
	{
		var target = $('#slide div.active').prev();
		if(!window.sliding) activate(target);
		return(false);
	}); 
	
	$('div.active li.thumb').live('click',function()
	{
		activateImage($(this));	
	});
	$('div.zoom').live('click',function(){$('div.image.current a',$(this).parent()).click();});

	preload();
	//checkHistory();
	//setInterval(checkHistory, 200);
	setTimeout(checkButtons,300);

}); 
