var lightboxIndex;
var lightboxTotalImages = 0;
var dataSource = new Array();
var active = false;
var imageWidth;
var imageHeight;

$(document).ready(function()
{
	
	$('.lightbox').live("click", function() 
	{
		lightboxIndex = $('.lightbox').index($(this));
		$('#lightbox').fadeIn("fast");
		lightboxInit(false);
		getLightboxPhoto();
                setupButtons();
	})
	
	$("#lightbox .prev").live("click", function() 
	{
		lightboxIndex--;
		if (lightboxIndex < 0) index = lightboxTotalImages - 1;
		getLightboxPhoto();
		setupButtons();
	});

	$("#lightbox .next").live("click", function() 
	{
		lightboxIndex++;
		if (lightboxIndex == lightboxTotalImages) index = 0;
		getLightboxPhoto();
		setupButtons();
	});
	
	$("#lightbox .close").live("click", function() 
	{
		active = false;
		$("#lightbox").fadeOut('fast');
	});
});

function setupButtons() 
{
	lightboxTotalImages = $('.lightbox').length;
	
	if (lightboxIndex == 0) {
		$("#lightbox .buttonLeft").fadeOut("fast");
	} else {
		$("#lightbox .buttonLeft").fadeIn("fast");
	}
	if (lightboxIndex == lightboxTotalImages -1) {
		$("#lightbox .buttonRight").fadeOut("fast");
	} else {
		$("#lightbox .buttonRight").fadeIn("fast");
	}
}

function placeLightBoxElements() 
{
	StageUtils.fitAreaHeight($('#lightbox .holder .container img'), $(window).height() - 60);
	
	if($('#lightbox .holder .container img').css("height") != undefined && $('#lightbox .holder .container img').height() != 0)
	{
		imageWidth = parseFloat($('#lightbox .holder .container img').css("width"));
		imageHeight = parseFloat($('#lightbox .holder .container img').css("height"));
	}
	else
	{
		imageWidth = parseFloat($('#lightbox .holder .container img').get(0).width);
		imageHeight = parseFloat($('#lightbox .holder .container img').get(0).height);
	}

	$('#lightbox .holder .container').css("width", imageWidth);
	$('#lightbox .holder .container').css("height", imageHeight);
	$('#lightbox .holder .container').css("marginLeft", -imageWidth/2);
}

function lightboxInit(data)
{
	dataSource = [];
	if (data == false)
	{
		$('.lightbox').each(function() 
		{
			dataSource.push($(this).attr('rel'));
		});
	} else 
	{
		dataSource = data;
		lightboxIndex = 0;
		getLightboxPhoto();
                setupButtons();
	}
}

function lightboxShow(index)
{
	lightboxIndex = index;
	$('#lightbox').fadeIn("fast");
	getLightboxPhoto();
	setupButtons();
}

function getLightboxPhoto()
{
	active = false;
	
	if ($('#lightbox .holder .container').children().length == 0)
	{
		loadLightboxPhoto();
	} 
	else 
	{
		$("#lightbox .holder .container img").animate({ marginLeft: -imageWidth, easing: "easeInOutCubic" }, 500, function() 
		{
			loadLightboxPhoto();
		});
	}
	
}

function loadLightboxPhoto()
{
	var lightboxAddress = dataSource[lightboxIndex];
	var img = new Image();
	$(img).load(function () 
	{
		$('#lightbox .holder .container').html(this);
		placeLightBoxElements();
		$(this).css("marginLeft", imageWidth);
		active = true;
		$("#lightbox .holder .container img").animate({ marginLeft: 0, easing: "easeInOutCubic" }, 500);
	}).error(function () {
		alert('Failed to load resource.');
		$('#lightbox .holder .container').empty();
	}).attr('src', lightboxAddress);
}

$(window).resize(function() { if (active) placeLightBoxElements(); } );
