(function($){

    $.fn.fullscreener =  function()
    {
        var fs = $('<div id="fullscreener">'+
                        '<div id="fullscreener_middle">'+
                            '<a href="#" id="fullscreener_inner"></a>'+
                        '</div>'+
                        '<a href="#" id="fullscreener_close">x</a>'+
                   '</div>');

        

        var clear = function()
        {
            fs.find("#fullscreener_inner").html('');
            $("#fullscreener").remove();
            return false;
        }
        
        var img = $("<img/>");
        var append = function(code)
        {
            fs.find("#fullscreener_close, #fullscreener_inner").one("click", null, clear);
            $("body").append(code);
        }

        var load_handler = function()
        {
             var w = this.width;
             var h = this.height;

             var sw = $(window).width();
             var sh = $(window).height();

             var r_max = sw/sh;
             var r_im = w/h;

             var text = this.getAttribute("alt");

             if(w>sw && h>sh)
             {
                if(r_max>r_im)
                {
                    h=sh;
                    w=Math.round(h*r_im);
                }
                else
                {
                    w=sw;
                    h=Math.round(w/r_im);
                }
             }
             else if(w>sw)
             {
                w=sw;
                h=Math.round(w/r_im);
             }
             else if(h>sh)
             {
                h=sh;
                w=Math.round(h*r_im);
             }

             
             
             fs.find("#fullscreener_inner").
                css({
                    width: w-1,
                    height: h-1
                }).
                append(img.attr("src", this.src).css(
                    {
                        width: "100%",
                        height: "100%"
                    }).
                    after($("<div>").attr('id', 'fullscreener_desc').text(text))
                );
                    
              fs.find("#fullscreener_middle").removeClass("loading").
                 css("opacity", 0).animate({opacity: 1}, 1000);

        }

        $(window).keydown(function(e){
            if(e.keyCode==27 || e.keyCode==8)
                clear();
        });

        this.each(function()
        {
            
            var el = $(this);
            el.click(function()
            {
                clear();
                
                fs.find("#fullscreener_middle").addClass("loading");

                var text = $(this).attr('title');

                var il = new Image();
                il.src = $(this).attr('href');
                il.setAttribute('alt',  text ? text : "");

                if(il.complete)
                {
                    load_handler.call(il);
                }
                else
                {
                    il.onload = load_handler;
                }
                
                il.onerror = function(){alert('No file ' + this.src)}
                
                append(fs);
                return false;
            });
        });
        
    }
})(jQuery);
