$(document).ready(function() {
	
	d=document;
	if (typeof window.innerWidth!='undefined') {
	var winWidth = window.innerWidth;
	var winHeight = window.innerHeight;
	} else {
	if (d.documentElement && typeof d.documentElement.clientWidth!='undefined' && d.documentElement.clientWidth!=0) {
	var winWidth = d.documentElement.clientWidth
	var winHeight = d.documentElement.clientHeight
	} else {
	if (d.body && typeof d.body.clientWidth!='undefined') {
	var winWidth = d.body.clientWidth
	var winHeight = d.body.clientHeight
	}
	}
	}
	
	var open_next;
	var opis;
	
	$("body").prepend("<div class=\"overlay_bgr\"></div><div class=\"overlay\"  id=\"overlay\"><div id=\"img_cont\" style=\"position: relative;\"><div id=\"prev_img\"></div><div id=\"prev_img_btn\"><img id=\"prev_btn\" src=\"galeria/left.gif\" title=\"Wstecz\"/></div><img id=\"over_image\" src=\"\" /><div id=\"next_img\"></div><div id=\"next_img_btn\"><img id=\"next_btn\" title=\"Dalej\" src=\"galeria/right.gif\"/></div></div><div id=\"details\" class=\"details\"></div></div>");
	$("#next_btn").click(function(e){
		e.stopPropagation();
		var oil=$("#over_image").attr("src");
		var next = 0;
		var nxtitle="";
		var nx="";
        $('a.gallery').each(function(index){
        	if(next == 1){
        		nx = $(this).attr("href");
        		nxtitle = $(this).children("img").attr("title");
        		next = 0;
        	}
        	else 
        	if($(this).attr("href")==oil){
        		next = 1;
        	}
        	
        });
		
        /*$("a.close").click();*/
        if(nx!=""){
        $("#img_cont").css("display","none");
        $("#details").css("display","none");
        
        opis=nxtitle;
        $("#over_image").attr("src",nx);
        }
		
	});
	
	$("#prev_btn").click(function(e){
		e.stopPropagation();
		var oil=$("#over_image").attr("src");
		var next = 1;
		var nxtitle="";
		var nx="";
        $('a.gallery').each(function(index){
        	if($(this).attr("href")==oil){
        		next = 0;
        	}
        	if(next==1){
        		nx = $(this).attr("href");
        		nxtitle = $(this).children("img").attr("title");
        	}
        	
        });
		
        /*$("a.close").click();*/
        if(nx!=""){
        $("#img_cont").css("display","none");
        $("#details").css("display","none");
        
    	opis=nxtitle;
        $("#over_image").attr("src",nx);
        }
	});
	
	$("a[rel]").overlay({fixed: false,onBeforeLoad:function(){
		$("#over_image").unbind("load");
		$("#over_image").attr("src","");
		$(".overlay_bgr").css("display","block");
		$("#img_cont").css("display","none");
		$("#details").css("display","none");
	}, onLoad: function() {
		
		var link = this.getTrigger().attr("href");
		$(".overlay").click(function(e){
			$("a.close").click();
			$(".overlay_bgr").css("display","none");
			
		});
		
	
		opis = this.getTrigger().children("img").attr("title");
		$("#over_image").load(function(e){
			var imageObject = new Image();
			imageObject.src = $("#over_image").attr("src");
			var height = imageObject.height;
			var width = imageObject.width;
			var prop = height/width;
			if(height>=winHeight-100){
				  height = winHeight-100;
				  width = height/prop;
			}
			if(width>=winWidth-100){
				width = winWidth-100;
				height = width*prop;
			}
			$("#over_image").css("height",height);
			$("#over_image").css("width",width);
			var left = winWidth/2 - width/2;
			var top = winHeight/2 - height/2 + $(window).scrollTop()-30;
			
			
			$(".overlay").css("left",left);
			$(".overlay").css("top",top);
			
			
			
			if(opis != "" && opis!=null) {$("#details").html("<p>"+opis+"</p>");}
			else{$("#details").html("");}
			
		
			$("#img_cont").css("display","block");
			if($("#details").html()!=""){
			 $("#details").css("display","block");}
			
		});
		$("#over_image").attr("src",link);	
	}, onClose: function(){
		$(".overlay_bgr").css("display","none");
	}
		
	

});
	

}); 


