var gIndex=4;
var xIndex=1;
var ieOffset=10;
var thumbnails;

$(document).ready(function(){
	$('#i5').hide();
	if (jQuery.browser.msie) ieOffset = 5;
	$('#i5').css('left',$('#i1').position().left);
	$('#i5').css('top',$('#i1').position().top+ieOffset);
	
	getAllThumbs();
});

function getAllThumbs() {
	
	$.getJSON("/gallery/images.php?action=thumbs", function(json){
		thumbnails = json;
		$('#i1').attr('src',thumbnails.thumbnails[0]);
		$('#i2').attr('src',thumbnails.thumbnails[1]);
		$('#i3').attr('src',thumbnails.thumbnails[2]);
		$('#i4').attr('src',thumbnails.thumbnails[3]);
		$('#i5').attr('src',thumbnails.thumbnails[4]).fadeIn(3000, function () { galleryFade() });
	});
}

function galleryFade() {
    $('#i'+xIndex).attr('src',thumbnails.thumbnails[gIndex]);
	xIndex = (xIndex%4)+1;
	gIndex = (gIndex+1)%(thumbnails.size);
	
	$('#i5').hide();
	$('#i5').css('left',$('#i'+xIndex).position().left);
	$('#i5').css('top',$('#i'+xIndex).position().top+ieOffset);
	$('#i5').height($('#i'+xIndex).height());
	$('#i5').width($('#i'+xIndex).width());
	$('#i5').attr('src',thumbnails.thumbnails[gIndex]).fadeIn(3000, function () { galleryFade() });
}

function getSingle() {
	$.getJSON("/gallery/getSingle.php", function(json){
		checkPictures(json.filename);
	});
}

function checkPictures(image) {
	var result = false;
	$('#pictures img').each(function () {
		/*alert((($(this).attr("src")) == image) + ' [' + $(this).attr("src") + '] [' + image + ']');*/
		if (($(this).attr("src")) == image) {
			result = true;
		}
	});
	if (result) {
		//getSingle();
	} else {
		$('#pictures img:eq('+index+')').attr("src",image);
		index = (index+1)%4;
		timer.reset(1500);
	}
}
