var rst = "";

var pics = new Array();
var activePic = "";

$(document).ready(function() {
	var documentwidth = $(document).width();
	var documentheight = $(document).height();
	$('#blind').width(documentwidth);
	$('#blind').height(documentheight);
	$(window).resize(function(){
		clearTimeout(rst);
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		$('#bg').width(browserwidth);
		$('#bg').height(browserheight);
		rst=setTimeout("resizeBG()",100);
	});
	$('div#gallery div.pic img.thumbon').click(function(){
		iniOff = $(this).offset();
		$('#preview').css('left', iniOff.left+'px');
		$('#preview').css('top', iniOff.top+'px');
		$('#blind').fadeIn();
		var theimg = $(this).attr('id');
		loadImg(theimg);
	});
	$('#close').click(function(){
		$('#blind').hide();
		$("#nav").hide();
		activePic = "";
		resetPreview();
	});
	resetPreview();

	i = 0;
	$('div#gallery div.pic img.thumbon').each(function(){
		pics[i] = $(this).attr('id');
		i++;
	});

	$('#next').click(function(){
		thispic = activePic.replace('img_', '')*1;
		var nextpic = 1;
		if(thispic < pics.length)
			nextpic = thispic+1;
		theimg = 'img_'+nextpic;
		$('#nav').hide();
		loadImg(theimg);
	});
	$('#prev').click(function(){
		thispic = activePic.replace('img_', '')*1;
		var nextpic = pics.length;
		if(thispic > 1)
			nextpic = thispic-1;
		theimg = 'img_'+nextpic;
		$('#nav').hide();
		loadImg(theimg);
	});
	

	var thumbs = new Array();
	var thumbsTO = new Array();
/*
	$(".thumb").each(function(){
		checkIfThumbIsLoaded($(this).attr('id'));
	});


$(".thumb").load(function(){
	$(this).pixastic("desaturate");
});
*/
$(window).load(function(){
	$(".thumb").each(function(){
		$(this).pixastic("desaturate");
	});
});



$('.thumbon').hover(function(){
		$(this).animate({
			opacity: 1
		}, 100);
	}, function(){
		$(this).animate({
			opacity: 0
		}, 100);
	});
});

function checkIfThumbIsLoaded(theid){
	var jImg = $("#"+theid);
	if(jImg[ 0 ].complete)
		jImg.pixastic("desaturate");
	else
		thumbsTO[theid]=setTimeout("checkIfThumbIsLoaded('"+theid+"')",500);
}

var imgloadTO = "";
var imageLoaded = false;
var previewTop = 0;
var previewLeft = 0;
var previewBordersH = 48;
var previewBordersV = 40;
var activeImg = '';

function resetPreview(){
	if($('#gallery').length > 0){
		var prevOffset = $('#gallery').offset();
		$('#previewimg').width(47);
		$('#previewimg').height(55);
		$('#preview').width(93);
		$('#preview').height(93);
		var initLeft = prevOffset.left;
		var initTop = prevOffset.top;
		previewTop = Math.ceil(prevOffset.top + ($('#gallery').height()/2));
		previewLeft = Math.ceil(prevOffset.left + ($('#gallery').width()/2));
		$('#preview').css('top', initTop+'px');
		$('#preview').css('left', initLeft+'px');
	}
}

function loadImg(theimg){
	var imgsrc = $('#'+theimg).attr('imag');
	activePic = $('#'+theimg).attr('id');
	imageLoaded = false;
	$('#previewimg').html('<img src="'+imgsrc+'" alt="" border="0" />');
	checkIfImgIsLoaded();
	urlLabel = '/getLabel/id/'+$('#'+theimg).attr('imgid')+'/';
	$.getScript(urlLabel);
}

function checkIfImgIsLoaded(){
	var jImg = $("#previewimg img");
	if(jImg[ 0 ].complete)
		setImgSize();
	else
		t=setTimeout("checkIfImgIsLoaded()",500);
}

function setImgSize(){
	var imgwidth = $('#previewimg img').width();
	var imgheight = $('#previewimg img').height();
	var newpositionLeft = previewLeft - Math.ceil((imgwidth + previewBordersH)/2);
	var newpositionTop = previewTop - Math.ceil((imgheight + previewBordersV)/2);
	$('#preview').animate({
			top: newpositionTop,
			left: newpositionLeft,
			width: (imgwidth+previewBordersH)+'px',
			height: (previewBordersV+imgheight)+'px'
		}, 500, 'swing', function(){
		$('#previewimg').animate({
			width: imgwidth+'px',
			height: imgheight+'px'
		}, 500, 'swing', function(){
			$("#previewimg img").fadeIn();
			$("#nav").fadeIn();
		});
	});
}

function checkIfBGLoaded(){
	var jImg = $( "#background" );
	if(jImg[ 0 ].complete)
		resizeBG();
	else
		t=setTimeout("checkIfBGLoaded()",1000);
}
function resizeBG(){
	$('#bg').show();
	var bgwidth = $('#background').width();
	var bgheight = $('#background').height();
	var browserwidth = $('#bg').width();
	var browserheight = $('#bg').height();
	var ratioW = 1;
	var ratioH = 1;
	ratioW = Math.ceil(browserwidth * 100 / bgwidth);
	ratioH = Math.ceil(browserheight * 100 / bgheight);
	newration = ((ratioW > ratioH)?ratioW:ratioH);
	newwidth = Math.ceil(bgwidth * (newration / 100));
	newheight = Math.ceil(bgheight * (newration / 100));
	$('#background').width(newwidth);
	$('#background').height(newheight);
	if(newwidth > browserwidth){
		newpos = Math.ceil((newwidth - browserwidth) / 2);
		$('#background').css('left', '-'+newpos+"px");
	}
	if(newheight > browserheight){
		newpos = Math.ceil((newheight - browserheight) / 2);
		$('#background').css('top', '-'+newpos+"px");
	}
	$('#logohome').css('top', (browserheight-125)+"px");
	$('#logohome').css('left', (browserwidth-270)+"px");
}