$(function($){
    fx = {
        "initModal" : function(){
            if($(".modal-window").length==0){
                return $("<div>").hide().addClass("modal-window").appendTo("body");
            }else{
                return $(".modal-window");
            }
        },
        "boxin" : function(data, modal){
            $("<div>").hide().addClass("modal-overlay").click(function(event){
                //fx.boxout(event);
            }).appendTo("body");
            $("<div>").addClass("modal-wrapper").appendTo(modal);
            modal_wrapper = $("div.modal-wrapper");
            $("<div>").addClass("modal-content").appendTo(modal_wrapper);
            $("div.modal-content").append(data);
            $('.modal-window, .modal-overlay').fadeIn("slow");
        },
        "boxout" : function(event){
            if(event!=undefined){
                event.preventDefault();
            }
            $(".modal-window, .modal-overlay").fadeOut("slow", function(){$(this).remove();});
        },
        "showModal" : function(id,width, height){
            modal = $('#'+id);
            $("<a>").attr("href","#").addClass("modal-close-btn").html("Close&times;")
            .click(function(event){
                    if(event!=undefined){
                        event.preventDefault();
                    }
                    $('.modal-window, .modal-overlay').fadeOut("slow");
                    $('.modal-overlay').remove();
                    modal.removeClass('modal-overlay');
                    $(this).remove();
                }).appendTo(modal);
            $("<div>").addClass("modal-overlay").appendTo("body");
            modal.addClass('modal-window').fadeIn();
            $(".modal-window").css('width', width + 'px');
            $(".modal-window").css('height', height + 'px');
            $(".modal-window").css('margin-left', -1 * width / 2 + 'px');
        }
    };

//    $("a.xctrl").live("click", function(event){
//        event.preventDefault();
//        var data = $(this).attr("href").replace(/.+?\?(.*)$/,"$1");
//        modal = fx.initModal();
//        $("<a>").attr("href","#").addClass("modal-close-btn").html("Close&times;").click(function(event){
//            fx.boxout(event);
//        }).appendTo(modal);
//        $.ajax({
//            type: "GET",
//            url:data,
//            success: function(data){fx.boxin(data,modal);},
//            error: function(msg){modal.append(msg)}
//        });
//    });

});

function modal_show_html(html, width, height){
    if($(".modal-window").length==0){
        modal = fx.initModal();
        $("<a>").attr("href","#").addClass("modal-close-btn").html("Close&times;").click(function(event){fx.boxout(event);}).appendTo(modal);
        fx.boxin(html,modal);
    }else{
        $("div.modal-content").html(html);
    }
    $(".modal-window").css('width', width + 'px');
    $(".modal-window").css('height', height + 'px');
    $(".modal-window").css('margin-left', -1 * width / 2 + 'px');
}

function modal_close(){
    modal = fx.initModal();
    fx.boxout();
}
