$(document).ready(function() {
	setBg();
//	$(window).resize(setBg); old one
	setup();
});

/* old one 
function setBg() {
	h = $(document).height();
	w = $(document).width();
	if(w > h)
		r = Math.floor(h/1.5);
	else
		r = Math.floor(w/1.5);
	$('html').css('background-image', '-webkit-gradient(radial, ' + Math.floor((w/2)) + ' ' + Math.floor((h/2)) + ', ' + r + ', ' + Math.floor((w/2)) + ' ' + Math.floor((h/2)) + ', 0, from(#000000), to(#ffffff))');
}
*/
function setBg() {
	h = $(window).height();
	d = $(document).height();
	w = $(document).width();
	
	$('html').css('background-image', '-webkit-gradient(radial, ' + Math.floor((w/2)) + ' ' + Math.floor((h/2)) + ', 600, ' + Math.floor((w/2)) + ' ' + Math.floor((h/2)) + ', 0, from(#000000), to(#ffffff))');
	
	//$('html').css('background-image', '-moz-radial-gradient(50% ' + (d-h)/d + '% 90deg, circle closest-corner, #FFFFFF, #191919)');
}

function setup() {
    imgWidths = 0;
	tallest = 0;
    $('.ngg-gallery-thumbnail-box').each(function() {
        imgWidths += $(this).width();
		$(this).find('img').each(function() {
			height = $(this).height();
			if(tallest < height)
				tallest = height;
		});
    });
	$('.ngg-galleryoverview').width(imgWidths);
	$('.ngg-galleryoverview').height(tallest);
	clickStop = false;
	$('#gallery-wrap').mouseenter(function() {
		if(!clickStop) {
			play = false;
			$('#icon-pause').fadeIn();
		} else
			$('#icon-play').fadeIn();
	}).mouseleave(function() {
		if(!clickStop) {
			play = true;
			$('#icon-pause').fadeOut();
		} else
			$('#icon-play').fadeOut();
	});
	$('#gallery-wrap').click(function(e) {
		e.preventDefault();
		if(!clickStop) {
			play = false;
			clickStop = true;
			$('#icon-play').fadeIn();
			$('#icon-pause').fadeOut();
		} else {
			play = true;
			clickStop = false;
			$('#icon-play').fadeOut();
		}
	});
	$('#icon-prev').click(prev);
	$('#icon-next').click(next);
	
	imgs = $('.ngg-gallery-thumbnail-box');
	last = imgs.length - 1;
	$(imgs[0]).css('left', '0');
	
	play = true;
	setTimeout('slideshow()',3000);
}
function slideshow() {
	if(play) {
		move();
		shift();
	}
	setTimeout('slideshow()',3000);
}
function next() {
	play = false;
	clickStop = true;
	move();
	shift();
}
function prev() {
	play = false;
	clickStop = true;
	shiftBack();
	moveBack();
}
function move() {
	$(imgs[0]).animate({
		left: '-1100px'
	}, 600, 'linear', function() { $(this).css('left', '1100px'); });
	$(imgs[1]).animate({
		left: '0px'
	}, 600, 'linear');
}
function moveBack() {
	$(imgs[0]).css('left', '-1100px');
	$(imgs[0]).animate({
		left: '0'
	}, 1000, 'linear');
	$(imgs[1]).animate({
		left: '1100px'
	}, 1000, 'linear');
}
function shift() {
	first = $(imgs[0]).detach();
	$(first).insertAfter(imgs[last]);
	imgs = $('.ngg-gallery-thumbnail-box');
}
function shiftBack() {
	end = $(imgs[last]).detach();
	$(end).insertBefore(imgs[0]);
	imgs = $('.ngg-gallery-thumbnail-box');
}
