
(function($) {
    $.fn.popUpWindowClose = function (){
        if( $(this).length>0){
            if (  $(this).find("#popup_window_body").length>0){
                if ( typeof   $(this).data("close") =='function'){
                    $(this).data("close") ();
                }
                $(this).find("#popup_window_body").empty();
            }
        }
        $(this).removeData("close");
        $(this).hide();
    }
    $.fn.popUpWindowResize = function (){
        resizePopupWindow(this);
    }
    $.fn.popUpWindow = function ( data) {
        if ($(this).length>0){
            $(this).find("#popup_window_title").remove();
            $(this).find("#popup_window_body").remove();
            if (  $(this).find("#popup_window_title").length<1){
                $(this).append('<div id="popup_window_title" style="width:auto;text-align: left;background-color:  fuchsia;margin: 5px;padding: 5px"><span id="title_text" style="width:45%;color:black">Windown Title</span><span id="title_close" style="width:45%;float: right;text-align: right"><img alt="close" width="24" src="../themes/custom/app/images/button-close-icon.png"></span></div>');
            }
            if (  $(this).find("#popup_window_body").length<1){
                $(this).append('<div style="margin:auto;text-align:center"><div id="popup_window_body" style="width:auto ;overflow:auto;padding: 5px; ;z-index:6000;background-color:white;margin: 5px;"></div></div>');
                var loading= '<div> <img alt="Loading" width="100" src="../themes/custom/app/images/ajax-loader.gif"> </div>'+'<div>正在处理，请等待</div></div>';
                $(this).find("#popup_window_body").append(loading);
            }
            $(this).attr("style","position:absolute;display:none;z-index:5000;vertical-align:middle;margin:auto;border:1px solid  gray;background-color:blue;");
            $(this).draggable();
            $(this).find("#popup_window_body").mousedown( function(event){
                event.stopPropagation()
            });
         
            var target =this;
            $(this).find("img").click(function(){
                if ( typeof   $(target).data("close") !='undefined'){
                    $(target).data("close") ();
                }
                $(target).find("#popup_window_body").empty();
                $(target).hide();
            });

            $(this).find("div#popup_window_title img").hover(function(){
                $(this).attr("src","../themes/custom/app/images/button-close-icon_high.png");
            },function(){
                $(this).attr("src","../themes/custom/app/images/button-close-icon.png");
            });
           
            $(this).resizable( {
                resize: function(event, ui) {
                    var height=  $("#popup_window").height();
                    var head_height= $("#popup_window").find("#popup_window_title").outerHeight(true);
                    var body_height= $("#popup_window").find("#popup_window_body").height()
                    var body_outerheight= $("#popup_window").find("#popup_window_body").outerHeight(true)
                    $("#popup_window_body").height(height-head_height-body_outerheight+body_height);
                }
            });
 
            if (typeof data.title !=='undefined' && data.title != null){
                $(this).find("#title_text").html(data.title);
            }
            if (typeof data.close !=='undefined' && data.close != null){
                $(this).data("close",data.close);
            }else {
                $(this).removeData("close");
            }
            if (typeof data.content !=='undefined' && data.content != null){
                $(this).find("#popup_window_body").html(data.content);
                window.setTimeout(function(){
                    resizePopupWindow(target);
                }, 200);
            }
            if (typeof data.url !=='undefined' && data.url != null){
                if (typeof data.data =='undefined' || data.data == null){

                    $(this).find("#popup_window_body").load(data.url,function(){
                        resizePopupWindow(target);
                    });
                } else {

                    $(this).find("#popup_window_body").load(data.url,data.data,function(){
                        resizePopupWindow(target);
                    });
                }
            }
            $(this).find("#popup_window_body").dblclick(function(){
                resizePopupWindow(target);
            });
            $(this).show();
            window.setTimeout(function(){
                resizePopupWindow(this);
            }, 100);
        }   
    }
    function resizePopupWindow(target){      
        var height=$(window).height();
        var width=$(window).width();
        var head_height= $(target).find("#popup_window_title").outerHeight(true);
        var body_height= $(target).find("#popup_window_body").height()
        var body_outerheight= $("#popup_window").find("#popup_window_body").outerHeight(true)
        var scrollHeight =$("#popup_window").find("#popup_window_body")[0].scrollHeight;
        // alert("scrollHeight :"+scrollHeight);
        //   if (  body_outerheight<scrollHeight){
        //       body_outerheight=scrollHeight;
        //   }
        if (scrollHeight<body_outerheight){
            scrollHeight=body_outerheight;
        }
        var outwidth=  $(target).outerWidth(true);
        var outheight=  head_height+scrollHeight;

        var div_height=outheight;
        var div_width=outwidth;

        if (div_height>height){
            div_height=height;
        }
        if (div_width<width/2){
            div_width=width/2;
        }else if (div_width>width){
            div_width=width;
        }
        var div_left=(width-div_width)/2;
        var div_top=(height-div_height)/2;

        $(target).css("height",div_height);
        $(target).css("width",div_width);
        $(target).find("#popup_window_body").height(div_height-head_height-body_outerheight+body_height);
        $(target).css("left",div_left);
        var top= $(window).scrollTop();
        $(target).css("top",top+div_top);

        return false;
    }
})(jQuery);


