var photoID = 'photo';
var BGID = 'photoBg';
var linkClass='photoLink';
var BGColor ='000';
var borderColor='fff';
var borderSize=10;
var ie6 = false;

function showPhoto(url){

 	$('<div id="'+photoID+'"></div>').appendTo(document.body);
 	$('<div id="'+BGID+'"></div>').appendTo(document.body);
 	$('<div id="progressBar"><img src="/static/img/progress.gif" alt=""/></div>').appendTo(document.body);
 	$('<div id="close"><img src="/static/img/close.png" alt=""/></div>').appendTo(document.body);
 	$("#"+photoID).css("z-index","42");
 	$("#"+BGID).css("z-index","40");
 	$("#progressBar").css("z-index","41");
 	$("#close").css("z-index","43");

 	if(!ie6){

	 	$("#"+photoID).css({	 		position:'fixed',
			top:'50%',
			left:'50%',
			border: borderSize+'px solid #'+borderColor,
			overflow:'hidden',
			opacity:'0'
	    });

	 	$("#"+BGID).css({
		    position:'fixed',
			width:'100%',
			height:'100%',
			background:'#'+BGColor,
			opacity:'0',
			top:'0',
			left:'0'
		});

		$("#progressBar").css({			position:'fixed',
			top:'50%',
			left:'50%',
			background:'url(/static/img/progressBG.png)',
			width:'32px',
			height:'32px',
			padding:'4px',
			margin:' -20px 0 0 -20px'
		});

		$("#close").css({
			position:'fixed',
			top:'50%',
			left:'50%',
			cursor:'pointer',
			display:'none',
			opacity:'0.5'
		});

    }else{
    	$("#"+photoID).css({    		position:'absolute',
    	    top:'0',
			left:'0',
			border: borderSize+'px solid #'+borderColor,
			filter:'alpha(opacity=0)',
			overflow:'hidden'
    	});

    	$("#"+BGID).css({
    		position:'absolute',
			width:'100%',
			background:'#'+BGColor,
			top:'0',
			left:'0',
			filter:'alpha(opacity=0)'
        });

        $("#progressBar").css({
			position:'absolute',
			left:'50%',
			background:'url(/static/img/progressBG.png)',
			width:'32px',
			height:'32px',
			padding:'4px',
			margin:'0px 0 0 -20px'
		});

		$("#close").css({
			position:'absolute',
			left:'50%',
			cursor:'pointer',
			display:'none',
			filter:'alpha(opacity=50)'
		});

    }

    if(ie6){
    	$("#"+BGID).css("height",$(document).height()+"px");

    }

	$("#"+BGID).animate({ opacity: "0.5" }, 300, function(){

 		$("#"+photoID).html('<img src="'+url+'" alt="" />');

   		var img=$("#"+photoID).find("img");

     	img.load(function(){

			imgWidth = img.width();
			imgHeight = img.height();

   			if(imgWidth>$(window).width() || imgHeight>$(window).height()){

	      		if(($(window).width()/imgWidth)>=($(window).height()/imgHeight)){
	            	imgHeight=$(window).height()-(borderSize*4);
	            	imgWidth = imgWidth/(img.height()/imgHeight);
	            	img.css("height",imgHeight+"px");

	            }else{
	            	imgWidth=$(window).width()-(borderSize*4);
	                imgHeight = imgHeight/(img.width()/imgWidth);
	                img.css("width",imgWidth+"px");

	            }

			}

			var  left= (imgWidth/2)+borderSize;
			var  top= (imgHeight/2)+borderSize;

            $("#"+photoID).css("margin-left","-"+left+"px");
            $("#close").css("margin-left",(left-(borderSize*2)-30)+"px");

			if(!ie6){
				$("#"+photoID).css("margin-top","-"+top+"px");
                $("#close").css("margin-top","-"+(top-(borderSize*2))+"px");

			}else{				$("#"+photoID).css("top",(d.documentElement.scrollTop+borderSize)+"px");
				$("#"+photoID).css("left","50%");
				$("#progressBar").css("top",(d.documentElement.scrollTop+(borderSize*3))+"px");
				$("#close").css("top",(d.documentElement.scrollTop+(borderSize*3))+"px");

			}

            $("#"+photoID).css("height","0").animate({opacity: "1",height: imgHeight}, 500, "swing",function(){
            	$("#close").css("display","");
            });

		});

	});

	$("#"+BGID).click(function(){
    	hidePhoto();

    });

    $("#close").click(function(){

    	hidePhoto();

    });

    $("#close").hover(function(){

    	$("#close").animate({opacity: "1"},100);

    },function(){

    	$("#close").animate({opacity: "0.5"},100);

    });

}

function hidePhoto(){

	$("#progressBar").remove();

	$("#close").remove();

	$("#"+photoID).animate({opacity: "0",height: "0"}, 500,"swing",function(){

    	$("#"+photoID).remove();

 		$("#"+BGID).animate({ opacity: "0" }, 300,function(){

			$("#"+BGID).remove();

		});

    });

}

$(document).ready(function(){

    if ($.browser.msie && $.browser.version == 6) {

   		ie6=true;
	}

    $("."+linkClass).click(function(){

    	showPhoto($(this).attr("href"));
        return false;

    });

});
