var lightbox = (function() {

    var active = false;
    var lightbox = null;
    var lightbox_width = "";
    var lightbox_height = "";
    
    var lbBgImgSrc 	 = "/web-altibox_red-theme/images/common/lightbox/bg_lightbox.png";
    var lbBgCloseSrc = "/web-altibox_red-theme/images/common/lightbox/lightbox_close.png";

    /* Show */
    function show(content, callback, width, height, closeback) {
        if (active) remove();
        
        active = true;
    
        var overlay = $('<div id="lbOverlay"></div>');
        lightbox = $('<div id="lbBg" style="display:none" class="roundcorner">\
                             <div class="lbCloseTxt">\
                                 <img src="'+lbBgCloseSrc+'">\
                             </div>\
                             <div id="lbCnt">'+content+'</div>\
                          </div>');
                         
        $('body').append(overlay);
        $('body').append(lightbox);
        
        // Determine content metrics
        var contentHeight = $("#lbCnt").height() + 100;
        
        // Style and position the lightbox content
        overlay.css('height', $(document).height())
               .css('z-index', 999);

        if (width)  { lightbox_width  = width;  lightbox.css('width',width);   }
        if (height) { lightbox_height = height; lightbox.css('height',height); }
        
        lightbox.css('z-index', 1000)
                .center()
                .css('top', 200);
                
	    // Handle user interaction
	    $(overlay).click(function(){
		    remove(closeback);
	    });
	    $(".lbCloseTxt").click(function(){
		    remove(closeback);
	    });
	    
	    overlay.css('display','block');
	    overlay.focus();
	    lightbox.css('display','inline');
        if ( typeof(callback) == 'function' ) callback(lightbox);
    }
    
    /* Replace the content without resize if size is set */
    function replaceContent(newContent, callback, hide) {
        if (hide) {
            $("#lbCnt").children().hide();
            $("#lbCnt").prepend(newContent);
        } else {
            $("#lbCnt").html(newContent);
        }
        if ( typeof(callback) == 'function' ) callback($("#lbCnt"));

    }
    
    function prependContent(newContent) {
        $("#lbCnt").prepend(newContent);
    }
    
    /* Hide closebutton */
    function hideCloseButton() {
        $(".lbCloseTxt").remove();
    }
    
    /* Hide overlay */
    
    /* Hide/Remove */
    function remove(callback) {
        active = false;
	    $("#lbBg").remove();
	    $("#lbOverlay").remove();
	    lightbox = null;
	    lightbox_height = "";
	    lightbox_width = "";
	    if ( typeof(callback) == 'function' ) callback();
    }
    
    /* Reposition */
    function reposition() {
        css({
            width : lightbox_width,
            height: lightbox_height,
            top   : 200
        });
    }
    
    /* Add some css */
    function css(values) {
        if (values.width) {
            lightbox.css('width', values.width);
        }
        if (values.height) {
            lightbox.css('height', values.height);
        }
        if (values.top) {
            lightbox.css('top', values.top);
        }
    }

    return {
        show:show,
        remove:remove,
        hide:remove,
        css:css,
        replaceContent:replaceContent,
        reposition:reposition,
        prependContent:prependContent,
        hideCloseButton:hideCloseButton
    }

})();

