// JavaScript Document
;(function($){
$.fn.bgIframe = $.fn.bgiframe = function(s) {
	// This is only for IE6
	if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
		s = $.extend({
			top     : 'auto', // auto == .currentStyle.borderTopWidth
			left    : 'auto', // auto == .currentStyle.borderLeftWidth
			width   : 'auto', // auto == offsetWidth
			height  : 'auto', // auto == offsetHeight
			opacity : true,
			src     : 'javascript:false;'
		}, s || {});
		var prop = function(n){return n&&n.constructor==Number?n+'px':n;},
		    html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
		               'style="display:block;position:absolute;z-index:-1;'+
			               (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
					       'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
					       'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
					       'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
					       'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
					'"/>';
		return this.each(function() {
			if ( $('> iframe.bgiframe', this).length == 0 )
				this.insertBefore( document.createElement(html), this.firstChild );
		});
	}
	return this;
};

})(jQuery);

;(function($){
    $.dialog = function(options){
		options = options || {};
		options = $.extend({
		    "width"           : 400,
		    "height"          : 300,
			"title"           : "dialog",
			"ok"              : false,
			"cancel"          : false,
			"close"           : true,
			"closeHandle"     : function(){},
			"okHandle"        : function(){},
			"cancelHandle"    : function(){},
			"content"         : "",
			"href"            : "",
			"type"            : "ajax"
		},options);
		
		maskDiv();
		createDialog();
		$(window).resize(function(){			
		    wresize();						  
		});
		$("#DivDialog").bind("removemask",function(e,func){
		    removeMask(func);    
		});
		return $("#DivDialog");
		
		
		//add maskbg
		function maskDiv(){
			var wsize = maskSize();
			$("body").append("<div ID='MaskID'></div>");
            $("body").find("#MaskID").css({
				width      : wsize[0]+"px",
				height     : wsize[1]+"px",
			    position   : "absolute",
				top        : "0px",
				left       : "0px",
				background : "#ccc",
				filter     : "Alpha(opacity=50);",
				opacity    : "0.3",
				zIndex     : "10000"
			}).bgIframe();			
		}
		
		function wresize(){
			var wsize = maskSize();
			$("body").find("#MaskID").css({
			    width      : wsize[0]+"px",
				height     : wsize[1]+"px"
			});
			changeDialog();
		}
		
		function maskSize(){
			var wnd = $(window), doc = $(document);
			var wHeight = 0;
            if(wnd.height() > doc.height()){
                wHeight = wnd.height(); 
            }else{
                wHeight = doc.height();  
            }
			return Array(wnd.width(),wHeight);
		}
		
		//dialog position
		function position(MyDiv_w,MyDiv_h){
		    //var MyDiv_w          = parseInt(obj.width());
            //var MyDiv_h          = parseInt(obj.height());
     
            var width            = parseInt($(document).width());
            var height           = parseInt($(window).height());
            var left             = parseInt($(document).scrollLeft());
            var top              = parseInt($(document).scrollTop());

            var Div_topposition  = ((height / 2) - (MyDiv_h / 2) < 0) ? top : top + (height / 2) - (MyDiv_h / 2); 
            var Div_leftposition = left + (width / 2) - (MyDiv_w / 2); 
			return Array(Div_topposition,Div_leftposition);
		}
		
		function removeMask(func){
			$("#DivDialog").slideUp('normal',function(){
			    $(this).remove();
				$("#MaskID").remove();
				if(func) (func)();
			});
		}
		
		function createDialog(){			
			var $Dialog = $("<div id='DivDialog' class='DivDialog'><div class='title'><div class='outer'><div class='inner'><span>"+options.title+"</span></div></div></div><div class='outWarp'><div class='content'></div></div></div>");
			$("body").append($Dialog);
			if(options.close) $("#DivDialog .title").append("<div class='close'></div>");
			if(options.cancel && options.ok){
				$("#DivDialog .outWarp").append("<div class='bottom bottom1'><div class='ok'></div><div class='cancel'></div></div>");
				$("#DivDialog .outWarp").find(".ok").css({"cursor":"pointer"}).end().find(".cancel").css({"cursor":"pointer"});
				$("#DivDialog .outWarp").find(".bottom").css({"visibility":"hidden"});
			}
			else{
				if(options.ok){
					$("#DivDialog .outWarp").append("<div class='bottom bottom2'><div class='ok'></div></div>");
					$("#DivDialog .outWarp").find(".ok").css({"cursor":"pointer"});
					$("#DivDialog .outWarp").find(".bottom").css({"visibility":"hidden"});
				}
				if(options.cancel){
					$("#DivDialog .outWarp").append("<div class='bottom bottom2'><div class='cancel'></div></div>");
					$("#DivDialog .outWarp").find(".cancel").css({"cursor":"pointer"});
					$("#DivDialog .outWarp").find(".bottom").css({"visibility":"hidden"});
				}
			}
			$("#DivDialog").find(".close").click(function(){
			    removeMask(options.closeHandle);				
			});
			$("#DivDialog").find(".ok").click(function(){				
			    options.okHandle($("#DivDialog"));				
			});
			$("#DivDialog").find(".cancel").click(function(){
			    //removeMask(options.cancelHandle);
				options.cancelHandle($("#DivDialog"));
			});
			changeDialog().slideDown('slow',function(){
				var $content = $(this).find(".content");
				if(options.type == "html"){
					$content.html(options.content);
					wresize();
				}
				if(options.type == "ajax"){
					if(options.href){
						$.get(options.href,{"random":Math.floor(Math.random()*1000)},function(data){
			                $content.html(data);
			            },"html");
					}					
					$content.ajaxStart(function(){
					    $(this).addClass("loading");						
					});
					$content.ajaxSuccess(function(){
					    $(this).removeClass("loading");						
						wresize();						
					});
				}
				$("#DivDialog .outWarp").find(".bottom").css({"visibility":"visible"});
			});			
		}
		
		function changeDialog(){		    
			var $Dialog = $("body").find("#DivDialog");
			var width,height,height1,height2;
			if($Dialog.is(":visible")){
				width  = $Dialog.width();
				height2 = ($Dialog.find(".bottom").length > 0) ? $Dialog.find(".bottom").attr("offsetHeight") : 0;				
				height1 = $Dialog.find(".content").attr("offsetHeight") + height2 + 27;
				if(options.height >= height1){
					height = options.height;
				}else{
					height = height1;
				}				
			}else{
				width  = options.width;
				height = options.height;
			}
			var offset = position(width, height);			
			$Dialog.css({
		        "width"      : width+"px",
				"height"     : height+"px",				
				"left"       : offset[1]+"px",
				"top"        : offset[0]+"px"				
			});
			$("#DivDialog .outWarp").height(height - 26);					
			return $Dialog;
		}
	}
	
	$.alert = function(info,func,title,width,height){
		title  = (title) ? title : "Alert";
		width  = (width) ? width : 250;
		height = (height) ? height : 100; 
	    $.dialog({
		    "width"       : width,
			"height"      : height,
			"title"       : title,
			"ok"          : true,
			"type"        : "html",
			"content"     : info,
			"closeHandle" : func,
			"okHandle"    : function(obj){
				obj.trigger('removemask',[func]);
			}			
		}).find(".outWarp").addClass("warning").end().find(".content").css({"padding-left":"0px","margin-left":"45px"});	
	}
	
	$.windower = function(href,title,width,height,func){
		title  = (title) ? title : "window";
		width  = (width) ? width : 600;
		height = (height) ? height : 400; 
	    var w = $.dialog({
		    "width"       : width,
			"height"      : height,
			"title"       : title,			
			"type"        : "ajax",
			"href"        : href,
			"closeHandle" : func
		});	
		return w;
	}
})(jQuery);