var Modal = function(){	
	var skin = {
				'default': 	'<div class="modal-overlay"></div>'+																												
							'<div class="modal-window" style="width:{width}px;" >'+	
							'<iframe class="iframeie" style="width:{width}px;" frameborder="0" scrolling="no" src=""></iframe>'+
							'		<div class="header" style="width:{width}px;">'+							
							'			<div class="caption">{title}</div>'+
							'			<div onClick="javascript:Modal.close();" class="close"></div>'+
							'		</div>'+
							' 	<div class="content">'+
							'			<iframe class="modal" frameborder="0" style="width:{width}px;position:absolute;" scrolling="no" src="{src}"></iframe>'+
							' 	</div>'+	
							'</div>'
				};
	
	return {
		close:function()
		{			
			$(".modal-window").remove(); $(".modal-overlay").remove();
		},

		open:function(src, title, width)
		{
			$("body").append(this.fetch(skin['default'], {'title':title, 'width':width, 'src':src}));	
			this.setView();
			$(".header").dragdrop(".modal-window");
			$(window).resize(this.setView);
		},	
		
		setView: function(){
		
			var align = function(iHeight){
				
				var win = $(window);
				
				iLeft = (win.width()-$(".modal-window").width())/2;
				//iTop = (win.height()-$(".modal-window").height())/2 + win.scrollTop();
				iTop = (win.height()-$(".modal").height())/2 + win.scrollTop();
				$(".modal-window").css({left:(iLeft>0)?iLeft:2, top:(iTop>0)?iTop:2});
				
				var doc = $(document), width = doc.width(), height = doc.height();	
				if(jQuery.browser.msie && parseInt(jQuery.browser.version) != 7){
					width -= 21;
					height -= 1;
				}
				if(jQuery.browser.mozilla){width -= 9;}
				if(jQuery.browser.safari){width -= 9; height += 3;}	
				$(".modal-overlay").css({'width':width , 'height':height});
			};
			align();
			$("iframe[class=modal]").each(function(){
				var src = this.src; this.src = ''; this.src = src;
			});
			$("iframe[class=modal]").load(function(){
				$(this).css({'height':$(this.contentWindow.document.body).height()+25});
				align();
			});
		},

		fetch: function(sTemplate, aParam){
			sTemplate = sTemplate.replace(/\s/g, ' ');
			var req = /{([^}]+)\}/g,
				aMatches = sTemplate.match(req),key;
			if(aMatches)
			{
				for(var i = 0; i < aMatches.length; i++) {
					key = aMatches[i].replace(req, "$1");
					if(typeof aParam[key] != 'undefined')
					{
						sTemplate = sTemplate.replace(aMatches[i], aParam[key]);
					}
				}
			}
			return sTemplate;
		}
	};
}();