var lastImageIndex;
var currentImageIndex = 0;
var imagesAmount = 0;
var images = new Array();
var vars;

var height_margin = 180;

var viewportwidth;
var viewportheight;

function getViewportDimensions()
{
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	} else if (typeof document.documentElement != 'undefined'
		&& typeof document.documentElement.clientWidth !=
		'undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	} else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
}

function loadImage(src, caption)
{
	if(document.getElementById('imageBrowser'))
	{
		document.getElementById('imageBrowser').loadImage(src, caption);
	}
}

function setThumbnails()
{
	$('#thumbnail_' + lastImageIndex + ' a img').animate({paddingBottom: "0px"}, 400);
	$('#thumbnail_' + lastImageIndex + ' a').animate({opacity: 0.3}, 400).removeClass('active');
	$('#thumbnail_' + currentImageIndex + ' a img').animate({paddingBottom: "5px"}, 400);
	$('#thumbnail_' + currentImageIndex + ' a').animate({opacity: 1}, 400).addClass('active');
}

function showImage(i)
{
	if(i != currentImageIndex && i >= 0 && i < imagesAmount)
	{
		lastImageIndex = currentImageIndex;
		currentImageIndex = i;
		setThumbnails();
		loadImage('/gallery/' + images[currentImageIndex][1], images[currentImageIndex][0]);
	}
}

function previousImage()
{
	if((currentImageIndex - 1) >= 0)
	{
		lastImageIndex = currentImageIndex;
		currentImageIndex--;
		setThumbnails();
		loadImage('/gallery/' + images[currentImageIndex][1], images[currentImageIndex][0]);
	}
}

function nextImage()
{
	if((currentImageIndex + 1) < imagesAmount)
	{
		lastImageIndex = currentImageIndex;
		currentImageIndex++;
		setThumbnails();
		loadImage('/gallery/' + images[currentImageIndex][1], images[currentImageIndex][0]);
	}
}

var resizeTimer;
$(window).bind('resize', function() {
	if (resizeTimer) clearTimeout(resizeTimer);
	resizeTimer = setTimeout(onWindowResize, 100);
});

function onWindowResize()
{
	getViewportDimensions();
	$('#flash').height(viewportheight - height_margin);
	$('#imageBrowser').height(viewportheight - height_margin);
}

$(window).load(function(){
	
	getViewportDimensions();
	
	//onWindowResize();
	
	if($('#thumbnails')) {
		$('#thumbnails a').hover(function() {
			if(!$(this).hasClass('active')) {
				$(this).stop(true);
				$(this).animate({opacity:1}, 400);
			}
		}, function () {
			if(!$(this).hasClass('active')) {
				$(this).animate({opacity:0.3}, 400);
			}
		});
	}
	
	if($('#imageBrowser'))
	{
		var params = {scale: "noscale", salign: "tl", bgcolor: "#F9F9F9"};
		swfobject.embedSWF("/assets/swf/ImageBrowser.swf", "imageBrowser", "100%", viewportheight - 180, "9.0.0", "/assets/expressInstall.swf", vars, params);
		setThumbnails();
	}
});
