/*
 * ImageBox
 * Based on jQuery, the JavaScript library (http://jquery.com)
 * Autor: Hristo Chakarov (ickata@ickata.net)
*/

function initImageBox(){
	$("body").append('<div id="imageholder"><div id="overlay"></div><div id="ib-spacer"></div><div id="ibpic"><img src="" alt="" title="" /><div style="padding-top: 5px"><a href="#" id="arrow-left"></a><span></span><a href="#" id="arrow-right"></a></div></div></div><div id="ibloading"><img src="images/loadingAnimation.gif" /></div>');
	
	var bpadding = 10;
	var animeSpeed = 400;
	var pheightPrev = 0;

	$('a.imagebox').click(function(){
								 
		$("#arrow-left").css({display: 'block'});
		$("#arrow-right").css({display: 'block'});
		$("#ibpic div").hide(0);
		
		$("#ibloading").fadeIn(300);	
		$("body").addClass("noscroll");
		$("html").addClass("noscroll");
		$("#overlay").show();
		$("#imageholder").show();
		//$("#box").css({ display: "block" });
		var pagesize = getPageSize();
		var title = ($(this).attr('title') != null) ? $(this).attr('title') : '';
		
		preload_image = new Image();
		preload_image.src = $(this).attr('href');
		
		preload_image.onload = function() {
			
			$("#ibpic img").attr({ src: preload_image.src });
			
			var pwidth = preload_image.width;
			var pheight = preload_image.height;
			var pwidth2 = 0;
			var pheight2 = 0;
			
			function fixdimensions(){
				if(pwidth > pagesize[0]){
					pwidth2 = pagesize[0]-200;
					pheight2 = (pheight*pwidth2)/pwidth;
					pwidth = pwidth2;
					pheight = pheight2;
				}else if(pheight > pagesize[1]){
					pheight2 = pagesize[1]-200;
					pwidth2 = (pwidth*pheight2)/pheight;
					pwidth = pwidth2;
					pheight = pheight2;
				}
				$("#ibpic img").attr({ width: pwidth, height: pheight });
			}
			
			fixdimensions();
			
			var marginleft = (pwidth+2*bpadding)/2;
			//var margintop = (pheight+2*bpadding)/2;
			var margintop = (pagesize[1]-pheight)/2;
			
			function showImageDescription() {
				setTimeout (function() { $("#ibpic div").animate({ height: 'show' }, animeSpeed) }, animeSpeed*2);
			}

			$("#ibloading").fadeOut(100, function(){
				//alert(margintop);
				$("#ib-spacer").css({ height: margintop+'px' });
				$("#ibpic").css({ padding: bpadding+"px", /*marginTop: margintop+"px",*/ opacity: 0, width: '5%' });
				$("#ibpic img").css({ width: '100%', height: pheight+'px' });
				//$("#ibpic").fadeIn(300);
				$("#ibpic").animate( { height: 'show', opacity: 0.5 }, animeSpeed )
					.animate( { width: pwidth+'px' }, animeSpeed)
					.animate( { opacity: 1 }, { duration: animeSpeed, callback: showImageDescription() } );
				$("#ibpic span").empty();
				$("#ibpic span").append(document.createTextNode(title));
				
				pheightPrev = pheight;
				
			});
		}
		
		$("#arrow-left").attr('href', $(this).parent().prev().children("a").attr('href'));
		$("#arrow-left").attr('title', $(this).parent().prev().children("a").attr('title'));
		$("#arrow-right").attr('href', $(this).parent().next().children("a").attr('href'));
		$("#arrow-right").attr('title', $(this).parent().next().children("a").attr('title'));
		
		if ($(this).attr('href') == $("#gallery ul a.imagebox:last").attr('href')) $("#arrow-right").css({display: 'none'});
		if ($(this).attr('href') == $("#gallery ul a.imagebox:first").attr('href')) $("#arrow-left").css({display: 'none'});
		
		return false;
		
	});
	
	$("#ibpic a").click(function() {
								 
		$("#arrow-left").css({display: 'block'});
		$("#arrow-right").css({display: 'block'});
		
		var current = $(this);
		
		//$("#ibpic img").css({ visibility: 'hidden' });
		$("#ibpic img").fadeTo(0.7,0.0001,function() { //);
			//$("#ibpic").css({ width: 'auto' });
			$("#ibloading").fadeIn(300);
			//
			var pagesize = getPageSize();
			var title = (current.attr('title') != null) ? current.attr('title') : '';
			
			preload_image = new Image();
			preload_image.src = current.attr('href');
			
			preload_image.onload = function(){
				$("#ibpic img").attr({ src: preload_image.src });
				
				var pwidth = preload_image.width;
				var pheight = preload_image.height;
				var pwidth2 = 0;
				var pheight2 = 0;
				
				function fixdimensions(){
					if(pwidth > pagesize[0]){
						pwidth2 = pagesize[0]-200;
						pheight2 = (pheight*pwidth2)/pwidth;
						pwidth = pwidth2;
						pheight = pheight2;
					}else if(pheight > pagesize[1]){
						pheight2 = pagesize[1]-200;
						pwidth2 = (pwidth*pheight2)/pheight;
						pwidth = pwidth2;
						pheight = pheight2;
					}
					//$("#ibpic img").attr({ width: pwidth, height: pheight });
				}
				
				fixdimensions();
				
				var marginleft = (pwidth+2*bpadding)/2;
				//var margintop = (pheight+2*bpadding)/2;
				var margintop = (pagesize[1]-pheight)/2;
				
				function ibpicImgAnimate() {
					//$("#ibpic img").css({ visibility: 'visible' });
					//$("#ibpic img").animate( { opacity: 1 }, animeSpeed );
					//$("#ibpic img").fadeTo(animeSpeed,1);
					setTimeout ( function() { $("#ibpic img").animate( { opacity: 1 }, { duration: animeSpeed } ) },animeSpeed);
				}
				
				function ibpicAnimate() {
					if (pheight == pheightPrev) $("#ibpic").animate( { width: pwidth+'px' }, { duration: animeSpeed, callback: ibpicImgAnimate() } );
					else setTimeout ( function() { $("#ibpic").animate( { width: pwidth+'px' }, { duration: animeSpeed, callback: ibpicImgAnimate() } ) },animeSpeed);
				}
	
				$("#ibloading").fadeOut(100, function(){
					
					//$("#ibpic").css({ padding: bpadding+"px", /*marginLeft: -marginleft+"px", marginTop: margintop+"px",*/ opacity: 0, width: '5%' });
					//$("#ib-spacer").css({ height: margintop+'px' });
					$("#ibpic img").css({ width: '100%' });
					//$("#ibpic img").fadeOut(1);
					//$("#ibpic").fadeIn(300);
					$("#ib-spacer").animate( { height: margintop+'px' }, animeSpeed );
					if (pheight == pheightPrev) { ibpicAnimate();  }
					else $("#ibpic img").animate( { height: pheight+'px' }, { duration: animeSpeed, callback: ibpicAnimate() } );
					$("#ibpic span").empty();
					$("#ibpic span").append(document.createTextNode(title));
			
					pheightPrev = pheight;
					
				});
				
			}
			
			if (current.attr('href') == $("#gallery ul a.imagebox:last").attr('href')) $("#arrow-right").css({display: 'none'});
			if (current.attr('href') == $("#gallery ul a.imagebox:first").attr('href')) $("#arrow-left").css({display: 'none'});
			
			prevLink = $("#gallery a[href="+current.attr('href')+"]").parent().prev().children("a").attr('href');
			nextLink = $("#gallery a[href="+current.attr('href')+"]").parent().next().children("a").attr('href');
			
			$("#arrow-left").attr('title', ( ($("#gallery a[href="+current.attr('href')+"]").parent().prev().children("a").attr('title') != null) ? $("#gallery a[href="+current.attr('href')+"]").parent().prev().children("a").attr('title') : '' ));
			$("#arrow-right").attr('title', ( ($("#gallery a[href="+current.attr('href')+"]").parent().next().children("a").attr('title') != null) ? $("#gallery a[href="+current.attr('href')+"]").parent().next().children("a").attr('title') : '' ));
			$("#arrow-right").attr('href', nextLink);
			$("#arrow-left").attr('href', prevLink);
			
		});
				
		return false;
		
	});
	
	function keyPressLeft() {
			
		var current = $("#arrow-left");
		
		if (current.attr('href') != $("#gallery ul a.imagebox:first").attr('href')) {
		//
			$("#arrow-left").css({display: 'block'});
			$("#arrow-right").css({display: 'block'});
			
			//$("#ibpic img").css({ visibility: 'hidden' });
			$("#ibpic img").fadeTo(0.7,0.0001,function() { //);
				//$("#ibpic").css({ width: 'auto' });
				$("#ibloading").fadeIn(300);
				//
				var pagesize = getPageSize();
				var title = (current.attr('title') != null) ? current.attr('title') : '';
				
				preload_image = new Image();
				preload_image.src = current.attr('href');
				
				preload_image.onload = function(){
					$("#ibpic img").attr({ src: preload_image.src });
					
					var pwidth = preload_image.width;
					var pheight = preload_image.height;
					var pwidth2 = 0;
					var pheight2 = 0;
					
					function fixdimensions(){
						if(pwidth > pagesize[0]){
							pwidth2 = pagesize[0]-200;
							pheight2 = (pheight*pwidth2)/pwidth;
							pwidth = pwidth2;
							pheight = pheight2;
						}else if(pheight > pagesize[1]){
							pheight2 = pagesize[1]-200;
							pwidth2 = (pwidth*pheight2)/pheight;
							pwidth = pwidth2;
							pheight = pheight2;
						}
						//$("#ibpic img").attr({ width: pwidth, height: pheight });
					}
					
					fixdimensions();
					
					var marginleft = (pwidth+2*bpadding)/2;
					//var margintop = (pheight+2*bpadding)/2;
					var margintop = (pagesize[1]-pheight)/2;
					
					function ibpicImgAnimate() {
						//$("#ibpic img").css({ visibility: 'visible' });
						//$("#ibpic img").animate( { opacity: 1 }, animeSpeed );
						//$("#ibpic img").fadeTo(animeSpeed,1);
						setTimeout ( function() { $("#ibpic img").animate( { opacity: 1 }, { duration: animeSpeed } ) },animeSpeed);
					}
					
					function ibpicAnimate() {
						if (pheight == pheightPrev) $("#ibpic").animate( { width: pwidth+'px' }, { duration: animeSpeed, callback: ibpicImgAnimate() } );
						else setTimeout ( function() { $("#ibpic").animate( { width: pwidth+'px' }, { duration: animeSpeed, callback: ibpicImgAnimate() } ) },animeSpeed);
					}
		
					$("#ibloading").fadeOut(100, function(){
						
						//$("#ibpic").css({ padding: bpadding+"px", /*marginLeft: -marginleft+"px", marginTop: margintop+"px",*/ opacity: 0, width: '5%' });
						//$("#ib-spacer").css({ height: margintop+'px' });
						$("#ibpic img").css({ width: '100%' });
						//$("#ibpic img").fadeOut(1);
						//$("#ibpic").fadeIn(300);
						$("#ib-spacer").animate( { height: margintop+'px' }, animeSpeed );
						if (pheight == pheightPrev) { ibpicAnimate();  }
						else $("#ibpic img").animate( { height: pheight+'px' }, { duration: animeSpeed, callback: ibpicAnimate() } );
						$("#ibpic span").empty();
						$("#ibpic span").append(document.createTextNode(title));
				
						pheightPrev = pheight;
						
					});
					
				}
				
				if (current.attr('href') == $("#gallery ul a.imagebox:last").attr('href')) $("#arrow-right").css({display: 'none'});
				if (current.attr('href') == $("#gallery ul a.imagebox:first").attr('href')) $("#arrow-left").css({display: 'none'});
				
				prevLink = $("#gallery a[href="+current.attr('href')+"]").parent().prev().children("a").attr('href');
				nextLink = $("#gallery a[href="+current.attr('href')+"]").parent().next().children("a").attr('href');
				
				$("#arrow-left").attr('title', ( ($("#gallery a[href="+current.attr('href')+"]").parent().prev().children("a").attr('title') != null) ? $("#gallery a[href="+current.attr('href')+"]").parent().prev().children("a").attr('title') : '' ));
				$("#arrow-right").attr('title', ( ($("#gallery a[href="+current.attr('href')+"]").parent().next().children("a").attr('title') != null) ? $("#gallery a[href="+current.attr('href')+"]").parent().next().children("a").attr('title') : '' ));
				$("#arrow-right").attr('href', nextLink);
				$("#arrow-left").attr('href', prevLink);
									
			});
			//
		}
	}
	
	function keyPressRight() {
		var current = $("#arrow-right");
		
		if ($("#gallery a[href="+current.attr('href')+"]").parent().prev().children("a").attr('href') != $("#gallery ul a.imagebox:last").attr('href')) {
			//
			$("#arrow-left").css({display: 'block'});
			$("#arrow-right").css({display: 'block'});
			
			//$("#ibpic img").css({ visibility: 'hidden' });
			$("#ibpic img").fadeTo(0.7,0.0001,function() { //);
				//$("#ibpic").css({ width: 'auto' });
				$("#ibloading").fadeIn(300);
				//
				var pagesize = getPageSize();
				var title = (current.attr('title') != null) ? current.attr('title') : '';
				
				preload_image = new Image();
				preload_image.src = current.attr('href');
				
				preload_image.onload = function(){
					$("#ibpic img").attr({ src: preload_image.src });
					
					var pwidth = preload_image.width;
					var pheight = preload_image.height;
					var pwidth2 = 0;
					var pheight2 = 0;
					
					function fixdimensions(){
						if(pwidth > pagesize[0]){
							pwidth2 = pagesize[0]-200;
							pheight2 = (pheight*pwidth2)/pwidth;
							pwidth = pwidth2;
							pheight = pheight2;
						}else if(pheight > pagesize[1]){
							pheight2 = pagesize[1]-200;
							pwidth2 = (pwidth*pheight2)/pheight;
							pwidth = pwidth2;
							pheight = pheight2;
						}
						//$("#ibpic img").attr({ width: pwidth, height: pheight });
					}
					
					fixdimensions();
					
					var marginleft = (pwidth+2*bpadding)/2;
					//var margintop = (pheight+2*bpadding)/2;
					var margintop = (pagesize[1]-pheight)/2;
					
					function ibpicImgAnimate() {
						//$("#ibpic img").css({ visibility: 'visible' });
						//$("#ibpic img").animate( { opacity: 1 }, animeSpeed );
						//$("#ibpic img").fadeTo(animeSpeed,1);
						setTimeout ( function() { $("#ibpic img").animate( { opacity: 1 }, { duration: animeSpeed } ) },animeSpeed);
					}
					
					function ibpicAnimate() {
						if (pheight == pheightPrev) $("#ibpic").animate( { width: pwidth+'px' }, { duration: animeSpeed, callback: ibpicImgAnimate() } );
						else setTimeout ( function() { $("#ibpic").animate( { width: pwidth+'px' }, { duration: animeSpeed, callback: ibpicImgAnimate() } ) },animeSpeed);
					}
		
					$("#ibloading").fadeOut(100, function(){
						
						//$("#ibpic").css({ padding: bpadding+"px", /*marginLeft: -marginleft+"px", marginTop: margintop+"px",*/ opacity: 0, width: '5%' });
						//$("#ib-spacer").css({ height: margintop+'px' });
						$("#ibpic img").css({ width: '100%' });
						//$("#ibpic img").fadeOut(1);
						//$("#ibpic").fadeIn(300);
						$("#ib-spacer").animate( { height: margintop+'px' }, animeSpeed );
						if (pheight == pheightPrev) { ibpicAnimate();  }
						else $("#ibpic img").animate( { height: pheight+'px' }, { duration: animeSpeed, callback: ibpicAnimate() } );
						$("#ibpic span").empty();
						$("#ibpic span").append(document.createTextNode(title));
				
						pheightPrev = pheight;
						
					});
					
				}
				
				if (current.attr('href') == $("#gallery ul a.imagebox:last").attr('href')) $("#arrow-right").css({display: 'none'});
				if (current.attr('href') == $("#gallery ul a.imagebox:first").attr('href')) $("#arrow-left").css({display: 'none'});
				
				prevLink = $("#gallery a[href="+current.attr('href')+"]").parent().prev().children("a").attr('href');
				nextLink = $("#gallery a[href="+current.attr('href')+"]").parent().next().children("a").attr('href');
				
				$("#arrow-left").attr('title', ( ($("#gallery a[href="+current.attr('href')+"]").parent().prev().children("a").attr('title') != null) ? $("#gallery a[href="+current.attr('href')+"]").parent().prev().children("a").attr('title') : '' ));
				$("#arrow-right").attr('title', ( ($("#gallery a[href="+current.attr('href')+"]").parent().next().children("a").attr('title') != null) ? $("#gallery a[href="+current.attr('href')+"]").parent().next().children("a").attr('title') : '' ));
				$("#arrow-right").attr('href', nextLink);
				$("#arrow-left").attr('href', prevLink);
				
			});
		}
	}
	
	$(document).keydown(function(event) {
		
		if ($("body").hasClass("noscroll")) {
		
			switch (event.keyCode) {
				
				case (37):
					keyPressLeft();
					break;
				
				case (80):
					keyPressLeft();
					break;
				
				case (39):
					keyPressRight();
					break;
					
				case (78):
					keyPressRight();
					break;
					
				case (27):
					ibclose();
					break;
				
			}
		}
		
	});
	
	$("#footer a").click(function() {
		$("body").addClass("noscroll");
		$("html").addClass("noscroll");
		$("#overlay").show();
		$("#imageholder").show();
		$("#thanks").fadeIn(200);
		return false;
	});
	
	$("#overlay").click(function(){ ibclose(); });
	//$("#imageholder").click(function(){ ibclose(); });
	//$("#ibpic").click(function() { ibclose(false) });
	$("#ibpic img").click(function(){ ibclose(); });
	
}

function ibclose(){
	$("#ibloading").hide();
	$("#ibpic").fadeOut(200);
	$("#overlay").hide();
	$("#imageholder").fadeOut(200);
	$("#thanks").hide();
	$("body").removeClass("noscroll");
	$("html").removeClass("noscroll");
}

function getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h) 
	return arrayPageSize;
}

$(document).ready(function() {
	initImageBox();
});
