﻿var gal;
var src;
var img;
var cur_index;
var gallery_on = false;

$(document).ready(function(){
	$('ul.album').pngFix();
	$(function(){
		$('a[rel*=external]').click( 
			function() {
				window.open(this.href);
				return false;
			}
		);
		$("#shadow").click(
			function(){			
				hide();
			}
		);
		$(".album li").mouseover(
			function(){
				$(".album li").css("z-index","9");
				this.style.zIndex = 998;
			}
		);
		$("a.photo").click(
			function(){
				gallery_on = true;
				this.blur();
				src = this.href.substr(0,this.href.length-4)+'.jpg';
				gal = eval('photos'+this.className.substr(6,this.className.length-6));
				cur_index = this.id.substr(8,this.id.length-8);
				cur_index--;
				preloadphoto();
				return false;
			}
		);
	});
});
function preloadphoto(){
	$("#viewer").css({display:'none'});
	src = gal[cur_index];
	img = new Image();
	img.src = src;
	img.onload = show;

	$("#shadow").css({height:$(document).height()+'px'});
	$("#viewer").html('<p style="text-align:center;"><img src="../img/loader.gif" alt="" title="" style="border:0;" /></p>');	
	center(450,80,"viewer");
				
	$("#shadow").removeClass("hidden");
}
function show(){
	var nav = '';
	nav = (cur_index>0) ? nav+'<div class="prv" style="top:'+Math.round(img.height/2)+'px" onclick="cur_index--;preloadphoto();" alt="&#8592; Ctrl" title="&#8592; Ctrl">' : nav;
	nav = (cur_index<gal.length-1) ? nav+'</div><div class="next" style="top:'+Math.round(img.height/2)+'px" onclick="cur_index++;preloadphoto();" alt="Ctrl &#8594;" title="Ctrl &#8594;"></div>' : nav;
	center(img.width,img.height,"viewer");
	$("#viewer").html('<img src="'+src+'" alt="" title="" onclick="hide();" />'+nav);
	$("#viewer").fadeTo('',1);
	
}
function hide(){
	$("#viewer").fadeOut('fast');
	$("#shadow").addClass("hidden");
	gallery_on = false;
}
function center(viewerW,viewerH,id){
	var clientH, offset, top; 		
	clientH = this.document.body.clientHeight;	
	offset = (viewerH>clientH)? 0 : Math.floor((clientH-viewerH)/2);
	top = scrollTop()+offset;
	$("#"+id).css({top: top+'px',width: viewerW+'px', height: viewerH+'px', marginLeft: '-'+Math.floor(viewerW/2)+'px'});
}
function scrollTop(){
	var top = 0;
	if( typeof(window.pageYOffset) == 'number'){
		top = window.pageYOffset;
	}else if(document.body&&(document.body.scrollLeft||document.body.scrollTop)){
		top = document.body.scrollTop;
	}else if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop)){
		top = document.documentElement.scrollTop;
	}
	return top;
}

document.onkeydown = navigation;

function navigation(event){
	if (!document.getElementById) return;
	if (window.event) event = window.event;
	if (event.ctrlKey){
		var link = null;
		var href = null;
		switch(event.keyCode ? event.keyCode : event.which ? event.which : null){
			case 0x25:
				if(gallery_on){
					if(cur_index>0){
						cur_index--;
						preloadphoto();
					}
				}
				else{
					link=document.getElementById('prv');
				}
				break;
			case 0x27:
				if(gallery_on){
					if(cur_index<gal.length-1){
						cur_index++;
						preloadphoto();
					}
				}
				else{
					link=document.getElementById('next');
				}
				break;
		}
		if(gallery_on){
		}
		else{
			if(link && link.href) document.location=link.href;
		}
	}
}
