(function($){
    $.modelDlg = function(options){
        var settings = {
            'width':"400px",
            'height':"200px"
        };
        var bgdiv,dlgdiv,titlediv,contentdiv,headdiv;
        function setCss(){
        	//bgdiv
            var pageSize = getPageSize();
            $(bgdiv).attr("id","modeldlg_bgdiv");
            $(bgdiv).css({
                "filter":"Alpha(opacity=40)",
                "top":"0px",
                "left":"0px",
                "background-color":"#FFFFFF",
                "opacity":"0.10",
                "position":"absolute",
                "z-index":"10000",
                "width":window.screen.availWidth+"px",
                "text-align":"left",
                "height":pageSize[1]+"px"                
            });
            
            //dlgdiv
            $(dlgdiv).attr("id","modeldlg_dlgdiv");
            $(dlgdiv).attr("tabindex","9999");
            $(dlgdiv).css({
            	"margin":"10px 10px",
            	"padding-bottom":"10px",
                "background-color":"#FFFFDD",
                "border":"3px solid gray",
                "position":"absolute",
                "display":"block",
                "z-index":"10001",
                "width":settings.width,
                "text-align":"left",
                //"height":settings.height,
                "height":"auto",
                "top":"0px",
                "left":"0px",
                "overflow":"visible"
            });
            var windowSize = getWindowSize();
            var scrollSize = getScrollXY();
            var height = (windowSize.height-parseInt(settings.height))/2 + scrollSize.y;
            $(dlgdiv).css("top",height+"px");
            var width = (windowSize.width-parseInt(settings.width))/2 + scrollSize.x;
            $(dlgdiv).css("left",width+"px");            

            //headdiv
            $(headdiv).css({
            	"background-color":"white"
            });
            //titlediv
            $(titlediv).css({
            	"width":"80%",
            	"padding":"5px 0px 0px 5px",
            	"font-weight":"bold",
            	"text-align":"left",
            	"font-size":"12px"
            });
            
            //contentdiv
            $(contentdiv).css({
                "padding":"10px 20px 10px 20px"
            });           
            
            //input button
            $("#jq_dlg_yes,#jq_dlg_no").css({
            	"background":"none repeat scroll 0 0 #E5E5E5",
            	"border":"1px solid #A6A6A6",
            	"color":"#404040",
            	"cursor":"pointer",
            	"font":"1em Arial,helvetica,sans-serif",
            	"margin":"0px 20px",
            	"padding":"2px 3px",
            	"display":"block",
            	"width":"80px",
            	"float":"right"
            });
            $("#jq_dlg_yes,#jq_dlg_no").hover(
                    function(){$(this).css({'background-color':'#FD9905',"color":"#FFFFFF"});},
                    function(){$(this).css({'background-color':'#E5E5E5',"color":"#404040"});}
                    );

        };
        function setBind(){
        	$("#modeldlg_cancel").bind('click',function(){
                cancel();return false;
            });
        	$("#jq_dlg_yes").bind('click',function(){
                cancel();
            });
        	$("#jq_dlg_no").bind('click',function(){
                cancel();
            });
            $("#modeldlg_bgdiv").bind('click',function(){
                //$("#modeldlg_cancel").focus();//ff下必须是input才触发focus
            	$(dlgdiv).focus();
            });
        }
        function cancel(){
            $("div#modeldlg_bgdiv").remove();
            $("div#modeldlg_dlgdiv").remove();
            //$("body").css("overflow-y","visible");
        };
        this.show = function(){
        	$("body").append(bgdiv);
            $("body").append(dlgdiv); 
        	setCss();
        	setBind();        	    
            
            //cancle button
            $("#modeldlg_cancel").css({
                "display":"block",
                "float":"right"
            });
            $("#modeldlg_cancel").hover(
                    function(){$("#modeldlg_cancel img").attr("src",'http://www.eefocus.com/images/myspace/buttonclose-float.gif');},
                    function(){$("#modeldlg_cancel img").attr("src",'http://www.eefocus.com/images/myspace/buttonclose.gif');
            });
        };
        this.alert = function(msg, reload){
        	this.init();
        	this.title('提示信息');
        	this.show();        	
        	this.content(msg);        	
        	$("#jq_dlg_no").css({"display":"none"});
        	if(reload==true){
        		$("#jq_dlg_yes").unbind('click');
            	$("#jq_dlg_yes").bind('click',function(){
            		cancel();
            		window.location.reload();
                });        		
        	}
        };
        this.cancel = function(){
            cancel();
        };
        this.success = function(callback){   
        	$("#jq_dlg_yes").unbind('click');
        	$("#jq_dlg_yes").bind('click',function(){
        		cancel();
        		callback(getdata());                
            });
        };
        this.content = function(html){
            $(contentdiv).append('<form>'+html+'</form>');
        };
        this.title = function(title){
        	$(titlediv).append(title);
        };
        function getdata(){
        	return $(contentdiv).children('form').serialize();
        };
        this.init = function(){
        	$(titlediv).empty();
        	$(contentdiv).empty();        	
        };
        //init
        if(options){
            $.extend(settings,options);     
            bgdiv = $("<div />");
            dlgdiv = $("<div />");
            headdiv = $("<div />");
            titlediv = $("<div />");
            contentdiv = $("<div />");
                        
            $(headdiv).append('<a id="modeldlg_cancel" tabindex="0"><img border="0" alt="关闭" style="cursor: pointer;" src="http://www.eefocus.com/images/myspace/buttonclose.gif" /></a>');
            $(headdiv).append(titlediv);
            $(dlgdiv).append(headdiv);
            $(dlgdiv).append(contentdiv);
            $(dlgdiv).append("<div><br /><input id='jq_dlg_no' type='button' value='取消' /><input id='jq_dlg_yes' type='button' value='确定' /></div>");
        };
        
        function getWindowSize() {
            var myWidth = 0, myHeight = 0;
            if( typeof( window.innerWidth ) == 'number' ) {
                //Non-IE
                myWidth = window.innerWidth;
                myHeight = window.innerHeight;
            } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                //IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
            } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                //IE 4 compatible
                myWidth = document.body.clientWidth;
                myHeight = document.body.clientHeight;
            }
            var size = new Object();
            size.width = myWidth;
            size.height = myHeight;
            return size;
        };

        // get the scroll offset of the browser
        function getScrollXY() {
            var scrOfX = 0, scrOfY = 0;
            if( typeof( window.pageYOffset ) == 'number' ) {
                //Netscape compliant
                scrOfY = window.pageYOffset;
                scrOfX = window.pageXOffset;
            } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
                //DOM compliant
                scrOfY = document.body.scrollTop;
                scrOfX = document.body.scrollLeft;
            } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
                //IE6 standards compliant mode
                scrOfY = document.documentElement.scrollTop;
                scrOfX = document.documentElement.scrollLeft;
            }
            var offset = new Object();
            offset.x = scrOfX;
            offset.y = scrOfY;
            return offset;
        };
        
        function getPageSize() {
            var xScroll, yScroll;  
            if (window.innerHeight && window.scrollMaxY) {  
                xScroll = document.body.scrollWidth;
                yScroll = window.innerHeight + window.scrollMaxY;
            } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
                xScroll = document.body.scrollWidth;
                yScroll = document.body.scrollHeight;
            } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
                xScroll = document.body.offsetWidth;
                yScroll = document.body.offsetHeight;
            }

            var windowWidth, windowHeight;
            if (self.innerHeight) {  // all except Explorer
                windowWidth = self.innerWidth;
                windowHeight = self.innerHeight;
            } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
            } else if (document.body) { // other Explorers
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
            }  

            // for small pages with total height less then height of the viewport
            if(yScroll < windowHeight){
                pageHeight = windowHeight;
            } else {
                pageHeight = yScroll;
            }

            if(xScroll < windowWidth){  
                pageWidth = windowWidth;
            } else {
                pageWidth = xScroll;
            }

            arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
            return arrayPageSize;
        };
    };
})(jQuery);


