(function($) {
    $.alerts = {
        win_ob:window.parent.document,
        divindex:Math.round(Math.random()*100000),                       //随机div层
        ismove:0,                                                        //是否能拖动
        myshow: function(message,title,callback,callbefore,swidth,sheight,type,force) {
            $.alerts.divindex++;
            
            if (!title)
                title='信息提示';
            var mainid='popup_main'+$.alerts.divindex;
            var buttonok = 'popup_ok'+$.alerts.divindex;
            var buttoncancel = 'popup_cancel'+$.alerts.divindex;
            var buttonclose = 'popup_close'+$.alerts.divindex;
            var force=(force==true) ? true : false;
            var winob = $(window.parent.document).find('body');
            var topmargin = 100;                                            //上偏移
            var pwidth = swidth>0 ? swidth : 260;                           //外框长宽
            var pheight= sheight>0 ? sheight :160;
            var bgwidth = (winob.width()-2)+'px';
            var bgheight = winob.height()>(window.screen.height-150) ? winob.height()+'px' : (window.screen.height-150)+'px';
	
            if($.browser.msie && parseInt($.browser.version) <= 6 ){
				var cheight = $.alerts.getscroll();
				ptop = cheight+topmargin+'px';
	
            }else{
                var ptop = (window.screen.height-pheight)/2-topmargin+'px';
				
            }
            var pleft = (window.screen.width-pwidth)/2+'px';

            if($.browser.msie && parseInt($.browser.version) <= 6 ){
                $('select').hide();
            }
            var button1 = '<input id="'+buttonok+'" class="popup_ok" type="button" value=" 确 认 "/>';
            var button2 = '';
            var close = (force==false) ? '<span class="popup_close" id="'+buttonclose+'">&nbsp;</span>' : '';
            var popup_body;
            var popup_foot;
            switch(type){
                case "alert":
                    break;
                case "jumb":
                    break;
                case "confirm":
                    button2 = '<input class="popup_cancel" id="'+buttoncancel+'" type="button" value=" 取 消 "/>';
                    break;
                case "box":
                    popup_body=message;
                    button1 ='';
                    break;
                case "iframe":
                    popup_body='<iframe id="popup_iframe" name="popup_iframe" frameborder="0" width="'+(pwidth-2)+'" height="'+(pheight-28)+'"></iframe>';		
                    button1 ='';
                    break;
                default:
                    break;

            }
            popup_body = popup_body ? popup_body : '<div class="popup_content">'
            +'<div class="popup_message">'+message+'</div>'
            +'</div>';

            popup_foot = '<div class="popup_panel">'
            +button1+button2
            +'</div>';
            winob.append('<div id="'+mainid+'" class="popup_main" style="width:'+bgwidth+';height:'+bgheight+';">'
                +'<div class="popup_bg"> </div>'
                +'<div class="popup_container" name="'+ptop+'" style="top:'+ptop+';left:'+pleft+';width:'+pwidth+'px;height:'+pheight+'px;">'
                +'<div class="popup_head"><h1 class="popup_title">'+title+'</h1>'
                +close+'</div>'
                +popup_body
                +popup_foot
                +'</div></div>'
                );

            if(type=='iframe'){
				$("#popup_iframe",$.alerts.win_ob).attr("src",message);
				//alert($.alerts.setheight);
			}
            if(callbefore) eval(callbefore);
            $("#"+buttoncancel+",#"+buttonclose,$.alerts.win_ob).click(function(){
                $.alerts.myhide(mainid);
            });

            $("#"+buttonok,$.alerts.win_ob).click(function(){
                $.alerts.myhide(mainid);
                if(callback) eval(callback);
            });

            $("#"+buttonok+",#"+buttoncancel+",#"+buttonclose,$.alerts.win_ob).keypress(                function(e){
                var kcode = (e.keyCode ? e.keyCode : e.which)
                if(kcode == 27 && force==false) $.alerts.myhide(mainid);              //esc退出
            //if(kcode == 13) $("#popup_ok").trigger('click');
                    
            });
            if($.alerts.ismove){
                try {
                    $(".popup_container",$.alerts.win_ob).draggable({
                        handle: $(".popup_title",$.alerts.win_ob)
                    });
                    $(".popup_title",$.alerts.win_ob).css({
                        cursor: 'move'
                    });
                } catch(e) { /* requires jQuery UI draggables */ }
            }
            
        },
        myhide: function(id){
            $("#"+id,$.alerts.win_ob).remove();
            if($.browser.msie && parseInt($.browser.version) <= 6 ){
                $("select").show();
            }
        },
        myjumb:function(url,target){
            if(url){
                if(target=='top'){
                    window.top.location.href = url;
                }else{
                    location.href = url;
                }
            }else{
                history.back();
            }
            var mainid='popup_main'+$.alerts.divindex;
            $.alerts.myhide(mainid);
        },
        getscroll:function() {

            var x, y;
            if(window.pageYOffset) {
                // all except IE	
                y = window.pageYOffset;
                x = window.pageXOffset;
            } else if(document.documentElement && document.documentElement.scrollTop) {									//第一个ie框
                // IE 6 Strict 
                y = document.documentElement.scrollTop;
                x = document.documentElement.scrollLeft;
            } else if(document.body) {																					//第二个ie框
                // all other IE
				var setheight=$(".popup_container",$.alerts.win_ob).attr("name");
				if(setheight){
					y = setheight.substring(0,setheight.length-2);
					y = parseInt(y);
				}else{
					y = document.body.scrollTop;
				}
                x = document.body.scrollLeft;
            }
            return y;
        }
    }
    my_alert = function(message,title,callback,force) {
        $.alerts.myshow(message,title,callback,'','','','alert',force);
        $("#popup_ok"+$.alerts.divindex,$.alerts.win_ob).focus();
    }
    my_jump = function(message,title,url,time,target) {
        if(url && time>0)
            $.alerts.myshow(message,title,"$.alerts.myjumb('"+url+"','"+target+"')",setTimeout("$.alerts.myjumb('"+url+"','"+target+"')",time*1000),'','','alert',true);
        else
            $.alerts.myshow(message,title,"$.alerts.myjumb('"+url+"','"+target+"')",'','','','alert',true);
        $("#popup_ok"+$.alerts.divindex,$.alerts.win_ob).focus();
    }
    my_confirm = function(message,title,callback) {
        $.alerts.myshow(message,title,callback,'','','','confirm',false);
        $("#popup_cancel"+$.alerts.divindex,$.alerts.win_ob).focus();
    }
    my_box = function(message,title,width,height,force) {
        $.alerts.myshow(message,title,'','',width,height,'box',force);
    }
	
	my_alert = function(message,title,callback,force) {
        $.alerts.myshow(message,title,callback,'','','','alert',force);
        $("#popup_ok"+$.alerts.divindex,$.alerts.win_ob).focus();
    }
	
	my_bigalert = function(message,title,width,height,url,time,target) {
        if(url && time>0)
            $.alerts.myshow(message,title,"$.alerts.myjumb('"+url+"','"+target+"')",setTimeout("$.alerts.myjumb('"+url+"','"+target+"')",time*1000),width,height,'alert',true);
        else
            $.alerts.myshow(message,title,"$.alerts.myjumb('"+url+"','"+target+"')",'',width,height,'alert',true);
        $("#popup_ok"+$.alerts.divindex,$.alerts.win_ob).focus();
    }

    my_iframe = function(src,title,width,height) {
        $.alerts.myshow(src,title,'','',width,height,'iframe',false);
    }
    my_close = function() {
        $(".popup_main",$.alerts.win_ob).remove();
    }

})(jQuery);
