(function($) {

$.fn.mDialog = function(options) {
		
	function initDialog() {
	
		wOpen.title = $(".title h3", wOpen) || false;		
		wOpen.content = $(".content", wOpen) || false;	
		wOpen.result = $(".result", wOpen) || false;	
		wOpen.loadInfo = $(".loadInfo", wOpen) || false;
		
		if(bVer==6 && msie)
			$(wOpen).css("position", "absolute");
		else if(opt.autoScroll)
			$(wOpen).css("position", "fixed");
		else
			$(wOpen).css("position", "absolute");
		
		$(wOpen).remove();
		$("body").prepend($(wOpen));
		
		InitButtons();
		
		$(window).bind("resize", function(event){if(isOpen) rePosition();});
		$(window).bind("scroll", function(event){if(isOpen) rePosition();});
		
		if(opt.ShowButton){		
			$(opt.ShowButton).bind("click", function(event){DialogShow(); return false;});			
		}
	}	
	
	function DialogShow()
	{
		if(!modalDiv)
			modalDiv = $(opt.BGLayer);
	
		if($(modalDiv).attr("tagName") != "DIV") {
		
			modalDiv = $('<div />');
			
			if(msie)
				modalDiv.css({"filter":"progid:DXImageTransform.Microsoft.Alpha(opacity=30)"});
			else
				modalDiv.css({"opacity":"0.3"});
			
			$(modalDiv).attr("class", "ModalDialog");
				
			$(modalDiv).hide();
			
			$("body").prepend($(modalDiv));
		}
		
		$(modalDiv).unbind("click");
		
		$(modalDiv).bind("click", function(){DialogHide(); return false;});
	
		$(modalDiv).height(hModalDiv); 
		
		rePosition();
		
		if((msie && bVer>6) || !msie)
		{
			$(modalDiv).show();
		}
		
		$(wOpen).show();
		isOpen = true;
	}
	
	function rePosition()
	{	
		var LeftPos = 0;
		var TopPos = 0;
		
		if(opt.autoCenter) {
			
			if(!opt.PosDialog.left || !opt.PosDialog.right)			
				LeftPos = $(window).width()/2-$(wOpen).width()/2;
				
			if(!opt.PosDialog.top || !opt.PosDialog.down)
				TopPos = $(window).height()/2-$(wOpen).height()/2;
		}
		
		if(parseInt(opt.PosDialog.top)>0)
			TopPos += parseInt(opt.PosDialog.top);
			
		if(parseInt(opt.PosDialog.down)>0)
			TopPos += parseInt($("body").height()) + parseInt(opt.PosDialog.down);
			
		if(bVer==6 && msie && opt.autoScroll){
		
			if(parseInt($(document).scrollTop())>0)
				TopPos += parseInt($(document).scrollTop());
				
			if(parseInt($(document).scrollLeft())>0)
				LeftPos += parseInt($(document).scrollLeft());
		}
		
		if(opt.PosDialog.left && parseInt(opt.PosDialog.left)>0)
			LeftPos = opt.PosDialog.left;
			
		if(opt.PosDialog.right && parseInt(opt.PosDialog.right)>0)
			LeftPos -= parseInt(opt.PosDialog.right)
			
		$(wOpen).css({"left":LeftPos+"px", "top":TopPos+"px"});
	
	}	
	
	function InitButtons(){
	
		if(opt.CloseButton)
			$("["+opt.CloseButton+"]", wOpen).each(function(){
				
				$(this).css({"cursor":"pointer"});
				
				$(this).click(function(){DialogHide();});				
			});
			
		if(opt.Post){
			
			if($(opt.SendButton, wOpen) && $(opt.Post.sForm, wOpen).attr("tagName") == "FORM")
			{
				opt.formElements = $(wOpen).defInputValue({editColor:"#333333"});
				
				$("input[type=hidden]", wOpen).each(function(){opt.formElements.push({inp:this});});
				
				$("["+opt.SendButton+"]", wOpen).click(function(){SendForm(opt); return false;});
			}
		}
	}
	
	function DialogHide()
	{
		$(modalDiv).hide();
		
		$(wOpen).hide();
		
		$(opt.formElements).each(function(){if(this.defV){this.inp.SetDefValue();}});
		
		$(wOpen.result).text("").hide();
		
		$(wOpen.content).show();
		
		isOpen = false;
	}
	
	function SendForm(opt)
	{
		var Data = new String();
		
		var vCheck = true;

		$(opt.formElements).each(function()
		{			
			var Name = $(this.inp).attr("name");
			var Value = $(this.inp).val();
			
			if(Value == this.defV || Value == "")
				vCheck = false;

			if(Name.length>0 && Value.length>0 && vCheck)
			{			
				if(Data.length>0)
					Data +="&";
				
				Data += Name+"="+Value;			
			}
		});
		
		if(Data.length>0 && vCheck) {
		
			$.ajax({
				converters: {
					"* text": window.String,
					"text html": true,
					"text json": jQuery.parseJSON,
					"xml": true
				},
				type: "POST",			
				url: opt.Post.url,
				data: Data,
				dataType: "xml",
				cache: "false",
				success: function(result)
				{
					if($(result).find("result").text() == "Y"){

						if(wOpen.content && wOpen.result)
							$(wOpen.content).css("opacity",1).hide();
						
						if(wOpen.result){
							$(wOpen.result).removeClass("error").addClass("ok");
							$(wOpen.result).text($(result).find("message").text()).show();
						}
							
					} else if($(result).find("result").text() == "N") {
						
						if(wOpen.content)
							$(wOpen.content).hide();
						
						if(wOpen.result){
							$(wOpen.result).removeClass("ok").addClass("error");
							$(wOpen.result).text($(result).find("message").text()).show();
						}
					}						
				},				
				beforeSend:function(){if(wOpen.loadInfo){$("["+opt.SendButton+"]", wOpen).hide(); $(wOpen.loadInfo).show();}},
				complete:function(){if(wOpen.loadInfo){$("["+opt.SendButton+"]", wOpen).show(); $(wOpen.loadInfo).hide();}}
			});
		} else {
			if(wOpen.result){
				$(wOpen.result).removeClass("ok").addClass("error");
				
				if(wOpen.content)
					$(wOpen.content).hide();
				
				$(wOpen.result).text("Вы заполнили не все поля").show();				
				setTimeout(function(){$(wOpen.result).text("").hide(); if(wOpen.content) {$(wOpen.content).show();}}, 2000);
			}
		}
	}
	
	if(!options)
		options = {};
		
	var defaults = {
		PosDialog:false,
		autoScroll:true,
		autoCenter:true,
		ShowButton:"#LogIn",
		CloseButton:'rel="Close"',
		SendButton:'rel="Send"',
		BGLayer:".ModalDialog"
	};
		
	var opt = $.extend(defaults, options);
		
	var wOpen = this;
	var msie = $.browser.msie == true;
	var bVer = parseInt($.browser.version.substr(0,3));
	var modalDiv = false;
	var hModalDiv = $(document).height();
	var isOpen = false;
	
	initDialog();
}
})(jQuery);
