function jwinex (config)
{
	var jwin = 
	{	
		config: {
			title: "Заголовок",
			content: "Тестируем окно",			
			z: 500,
			width: 450,
			height: 300,
			modal:true,
			left: 0,
			top:0,
			bgcolor:"#000",
			bgopacity:0.7,
			draggable:true},
		
		overlay: null,
		win: null,
		
		create: function (config)
		{			
			this.config = config;
		},
	
		show: function (config)
		{			
			this.create (config);			
			var self = this;
			
			if (self.config.modal)
			{				
				self.overlay = self.createOverlay ();
				self.overlay.fadeIn (300, function ()
				{					
					var html = '<div class="jw-wrap" style="display:none;"><div class="jw-head" id="jwhead"><div class="jw-closer">&nbsp;</div><div class="jw-title"></div></div><div class="jw-body"><div class="jw-content"></div></div></div>';
					self.win = $(html).appendTo ('body');
					self.calculatePosition ();
					self.win.fadeIn (500);
					self.win.find(".jw-title").html (self.config.title);
					self.win.find(".jw-content").html (self.config.content);
					self.win.find(".jw-closer").click (function ()
					{
						self.close();
					});
					
					if (self.config.draggable)
						self.win.draggable({opacity:0.8});
				});										
			}
			else
			{
				var html = '<div class="jw-wrap" style="display:none; position:absolute; cursor:move;"><div class="jw-head" id="jwhead"><div class="jw-closer">&nbsp;</div><div class="jw-title"></div></div><div class="jw-body"><div class="jw-content"></div></div></div>';
				self.win = $(html).appendTo ('body');			
				self.calculatePosition ();				
				self.win.fadeIn (500);
				self.win.find(".jw-title").html (self.config.title);
				self.win.find(".jw-content").html (self.config.content);
				self.win.find(".jw-closer").click (function ()
				{					
					self.close();
				});
				self.win.click (function ()
				{
					$("div.jw-wrap").css ({"z-index" : self.config.z});
					self.win.css ({"z-index" : self.config.z + 10});
				});						
				
				if (self.config.draggable)
					self.win.draggable({opacity:0.8});
			}						
		},
	
		createOverlay: function ()
		{		
			var html = "<div class=\"jw-overlay\"></div>";      	
      		var h = $(document).height(); 
			var w = $(document).width();
    	  	var o = $(html).appendTo('body');
      		o.css ({position:'absolute', top:0, left:0, opacity:this.config.bgopacity, backgroundColor:this.config.bgcolor, display:'none', 'z-index':this.config.z-1, height:h, width:w});
			$(window).bind ('resize', function ()
			{
            	var h = $(document).height();
                var w = $(document).width();
                o.css({height:h, width:w});
            });
        	return o;
		},  
	
		calculatePosition: function()
		{
    		this.win.css({"width" : this.config.width, "z-index" : this.config.z});
       		var dh = this.win.find('.jw-body').height();
	       	//if(dh>this.config.height){
    	        this.win.find('.jw-content').css({height:this.config.height,overflow:'auto'}); // auto | hidden | scroll | visible
       		//}

	       	if (this.config.left && this.config.top)
			{				
				this.win.css({top: this.config.top, left: this.config.left });
			}	
			else
			{			
				var wnd = $(window);
    		   	var doc = $(document);
       			var pTop = doc.scrollTop();
	       		var pLeft = doc.scrollLeft();
				var minTop = pTop;
        
				pTop += (wnd.height() - this.win.height()) / 2;
	    	    pTop = Math.max(pTop, minTop);
    	    	pLeft += (wnd.width() - this.win.width()) / 2;
	        	this.win.css({top: pTop, left: pLeft});
			}
	    }, 
	
		close: function ()
		{
			var self = this;
			this.win.fadeOut (500, function ()
			{
				if (self.config.modal)
					self.overlay.fadeOut (300);
			});
		}
	};
	
	jwin.show (config);
	return jwin;
}


/////////////////////////////////////

function genWindow (content)
{
	jwinex ({title : "", content : content, modal : true, width : 800, height : 600, z:5000});
}

function regWindow()
{	
	jwinex ({title : "Регистрация", content : $("#regwindow").html(), modal : true, width : 450, height : 550, z:500, bgcolor:"#000", bgopacity:0.5, draggable:true});
}

function msgWindow(title, msg)
{
	jwinex ({title : title, content : "<center>"+msg+"</center>", modal : false, width : 200, height : 100, z:600});
}

function basketMsgWindow(title, m1, m2, m3)
{	
	content = "<div style='font-size:16px; font-weight:bold;'><img src='" + root + "/img/info_32.png' style='float:left; margin: 0 10px 10px 0;'>&nbsp;" + m1 + "</div><br>" +
			  "<div style='text-align:center;font-size:16px;font-weight:bold;'>" + "<a onclick='$(\".jw-closer\").click();'>" + m2 + "</a>&nbsp;&nbsp;&nbsp;<a href='" + root + "/" + langname + "/" + specpage.orderpage + "/'>" + m3 + "</a></div>";

	w = jwinex ({title : config.name, content : content, modal : true, width : 400, height : 100, z:9999, bgcolor:"#000", bgopacity:0.5, draggable:true});
}

