$(document).ready(function(){
	var image=false;
	var imagearray= new Array();
	$('.easylightbox').each(function() {
		var image=$(this).attr('href');
		imagearray.push(image);
		var image=true;
	});
	var closebutton="/public/css/web/images/close.gif";
	$('.easylightbox').click(function(event){
		$("#easylightbox").css("display", "inline");
		$("#easylightbox").html('<div id="easylightboxwrap"><font color="white">Afbeelding wordt geladen</font></div>');
		event.preventDefault();
		$("#easylightbox").css("display", "inline");
		$("#easylightbox").empty().html('afbeelding wordt geladen');
		$("#easylightbox").html('<div id="easylightboxwrap"><img class="loadingimage" src="'+$(this).attr('href')+'" style="display: none;"/></div>');
		$('.loadingimage').load(function () {
			$('.loadingimage').removeAttr('style');
			$('.loadingimage').css('opacity', '0');
			$('#easylightboxwrap').removeAttr('style');
			$('#easylightboxwrap').css('opacity', '0');
			var width = $(window).width(); 
			var height = $(window).height();		
			var maxWidth=width-500;
			var maxHeight=height-100;
			var img_width=$('.loadingimage').width();
			var img_height=$('.loadingimage').height();
			if(img_width>maxWidth){
				var tempsize = maxWidth/img_width;
				var tempsize = tempsize*100;
				var img_width=img_width/100;
				var img_width=img_width*tempsize;
				var img_height=img_height/100;
				var img_height=img_height*tempsize;
			}
			if(img_height>maxHeight){
				var tempsize = maxHeight/img_height;
				var tempsize = tempsize*100;
				var img_height=img_height/100;
				var img_height=img_height*tempsize;
				var img_width=img_width/100;
				var img_width=img_width*tempsize;
			}
			var margin_left=width-img_width;
			var margin_top=height-img_height;
			var margin_left=margin_left/2;
			var margin_top=margin_top/2;
			$('#easylightboxwrap').css('margin-left', margin_left);
			$('#easylightboxwrap').css('margin-top', margin_top);
			$('.loadingimage').css('border', '1px solid white');
			$('#easylightboxwrap').css('width', img_width+2);
			$('#easylightboxwrap').css('height', img_height+2);
			$('#easylightboxwrap').css('border', '1px solid white');
			$('.loadingimage').css('width', img_width);
			$('.loadingimage').css('height', img_height);
			$('.loadingimage').css('background', 'white');
			$('#easylightboxwrap').css('left', margin_left);
			$('#easylightboxwrap').css('top', margin_top);
			$('.loadingimage').animate({opacity: 1}, 500);
			$('.loadingimage').css('display', 'inline');
			$('#easylightboxwrap').animate({opacity: 1}, 500);
			$('#easylightboxwrap').css('display', 'block');
			if ($(".closebutton").length == 0){
				$('#easylightboxwrap').append('<a href="#" class="closebutton"><img style="z-index: 200; position: absolute;" src="'+closebutton+'" width="30" height="30" /></a>');
			}
			var buttonmargin=img_height/2+'px';
			$.each(imagearray, function(i, v) {
				var currentimage=$('.loadingimage').attr('src');
				var delimiter=i;
				var lastimage=imagearray.length-1;
				if(v==currentimage||i==currentimage){
					var previous=delimiter-1;
					var next=delimiter+1;
					if(previous=='-1'){
						var previous=lastimage;
					}
					if(next>lastimage){
						var next=0;
					}
					if ($(".previous").length > 0){
						$('.previous').attr('id', previous);
						$('.next').attr('id', next);
						$('.previous').css('margin-top', buttonmargin);
						$('.next').css('margin-top', buttonmargin);
					}else{
						$('#easylightboxwrap').prepend('<a style="width: 50px; height: 45px; text-align: left; position: absolute;font-size: 45px; line-height: 45px;  margin-top: '+buttonmargin+'; background: white;" class="previous" id="'+previous+'" href="#"><<</a>');
						$('#easylightboxwrap').append('<a style="margin-left: -50px; width: 50px; position: absolute; margin-top: '+buttonmargin+';font-size: 45px; line-height: 45px;   text-align: right; background: white;  height: 45px " class="next" id="'+next+'" href="#">>></a>');
					}
				}
			});
			$('.previous').click(function(event){
				event.preventDefault();
				var showimage=$(this).attr('id');
				$('.loadingimage').css('display', 'none');
				$('#easylightboxwrap').css('display', 'none');
				$('.loadingimage').attr('src', imagearray[showimage]);
				$('.loadingimage').attr('alt', showimage);
			});
			$('.next').click(function(event){
				event.preventDefault();
				var showimage=$(this).attr('id');
				$('.loadingimage').css('display', 'none');
				$('#easylightboxwrap').css('display', 'none');
				$('.loadingimage').attr('src', imagearray[showimage]);
				$('.loadingimage').attr('alt', showimage);
			});
			$(".closebutton").click(function(){
				$("#easylightbox").css("display", "none");
			});
		});
	});
});


