﻿/*
 * jQuery HG.Dialog v1.0 - http://jquery.holygrace.cn
 *
 * author : 蒋与杨(young.jiang)
 * email  : young18.j@gmail.com
 * Copyright (c) 2007 young.jiang
 *
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 *
 * Parameter
 * @animate     : 显示动画，其中"bounceout","elasout","backin"等特效需要easing library,默认为无效果
 * @maskColor   : 遮罩层的颜色,默认为#000000
 * @maskOpacity : 遮罩层透明度,默认为5
 * @left @top   : 对话框显示时的位置,默认为正中间
 * @draggable   : 是否可以拖拽
 * @dragHandle  : 拖拽助手,详情见HG.Drag,当draggable为true是有效
 * @dragRange   : 拖拽范围,详情见HG.Drag,当draggable为true是有效
 * @close       : 触发关闭事件的对象，jquery选择器
 *
 * Event
 * CloseDialog()  : 出发关闭对话框事件
 *
 *  Example:
 *  $("#dialog").Dialog();
 *       
 *  $("#dialog").Dialog({
 *       animate     : "bounceout",
 *       draggable   : true,
 *       dragHandle  : "#dialogtitle",
 *       close       : "#close"
 *  });
 *
 * $("#dialog").CloseDialog();
 */


jQuery.fn.extend({
    Dialog : function(options){
        options = $.extend({
            animate     : "",   //推荐： "bounceout","elasout","backin"
            maskColor   : "#000000",            
            maskOpacity : "5",
            left        : "",
            top         : "",
            draggable   : false,
            dragHandle  : "",
            dragRange   : "",
            close       : ""
         },options);	
        
        return this.each(function(){
			var dialog=$(this);
			$("body").append("<div id='dialogmask'><div>");
			$('#dialogmask').css({
                position   : "absolute",
                top:"0px",
                left:"0px",
                width:$(window).width()+"px",
                height:$("body").height()+"px",
                zIndex      : "999",
                background:options.maskColor
			});
			$('#dialogmask').Opacity(options.maskOpacity);
			 
			//show
			var toLeft=(options.left=="")?(($(window).width()-dialog.width())/2):options.left;
			var toTop=(options.top=="")?(($(window).height()-dialog.height())/3):options.top;
			 
			if(options.animate=="")
			{
			    dialog.css({
			        position   : "absolute",
			        top        : (toTop+document.documentElement.scrollTop)+"px",
			        zIndex      : "1000",
			        left       : toLeft
			    });
			    dialog.show();
			}
			else
			{
                dialog.css({
			        position   : "absolute",
			        top        : "0px",
			        zIndex      : "1000",
			        left       : toLeft+"px"
			    });
			    dialog.slideDown(200);
			    dialog.animate(
			        {top: (toTop+document.documentElement.scrollTop)+"px"},
			        { easing : options.animate ,
			          duration : 800
			        }
			     );
			}
            
            //drag
            if(options.draggable)
            {
                dialog.Drag({
                    handle : options.dragHandle,
                    range  : options.dragRange
                })
            }
			
			//close
			if(options.close!="")
			{
			    $(options.close).click(function(){
			        dialog.CloseDialog();
			        $(this).unbind("click");
			    })
			    
			}
			
			//scroll
			$(window).scroll(function(){			    
			    dialog.css({
			        top:(toTop+ $(this).scrollTop())+"px"
			    });
			})
        })
    },
    CloseDialog:function(){
        return this.each(function(){
            $(this).slideUp(100,function(){
                 $('#dialogmask').remove();
				 $('.dialogbackgroup').remove();
				 //$('.dialogmsg').remove();
            })
        });
    },
    DialogRange:function(){
        return this.each(function(){ 
            $(this).width($(window).width()-3);
            $(this).height($(window).height()-8);
        });
    }
})
