var Modal = function(){	
	var skin = {'default': 	'<div class="modal-overlay"></div>'+																												
							'<div class="modal-window ui-dialog ui-widget  ui-corner-all  ui-draggable ui-resizable" style="width:{width}px;" >'+	
							'<iframe class="iframeie" style="width:{width}px;" frameborder="1" scrolling="no" src="javascript:false"></iframe>'+
							'		<div class="modal-header ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix" style="width:{width}px;">'+	
							'			<div class="modal-caption" style="width:{width}px;">{title}&nbsp;&nbsp;&nbsp;</div>'+
							'			<div onClick="javascript:Modal.close();" class="modal-close"></div>'+
							'		</div>'+
							' 	<div class="modal-content ui-dialog ui-widget ui-widget-content ui-corner-all  ui-draggable ui-resizable" style="width:{width}px;">'+
							'			<iframe class="modal" popup="iframe" frameborder="0" style="width:{width}px;position:absolute;" scrolling="no" src="javascript:false"></iframe>'+
							' 	</div>'+	
							'</div>',	
				'popup': 	'<div class="modal-window ui-dialog ui-widget  ui-corner-all  ui-draggable ui-resizable" style="width:{width}px;" >'+	
							'<iframe class="iframeie" style="width:{width}px;" frameborder="1" scrolling="no" src="javascript:false"></iframe>'+
							'		<div class="modal-header ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix modal-fix-border" style="width:{width}px;">'+	
							'			<div class="modal-caption" style="width:{width}px;">{title}&nbsp;&nbsp;&nbsp;</div>'+
							'			<div onClick="javascript:Modal.close();" class="modal-close"></div>'+
							'		</div>'+
							' 	<div class="modal-content ui-dialog ui-widget ui-widget-content ui-corner-all  ui-draggable ui-resizable" style="width:{width}px;">'+
							'			<iframe class="modal" popup="iframe" frameborder="0" style="width:{width}px;position:absolute;" scrolling="no" src="javascript:false"></iframe>'+
							' 	</div>'+	
							'</div>'				
				};
				
	return {	
		fn: new Function(),
		
		close:function()
		{			
			this.fn.call();
			this.fn = new Function();
			$(".modal-window").remove(); $(".modal-overlay").remove();
			this.option();
			this.reset_parent();
		},
		
		reset_parent: function(){
			if(parent && parent.Modal){
				parent.Modal.setHeight();				
			}
		},
		
		open:function(src, title, width, flag, fun)
		{	
			src = this.makeRnd(src);
			if(typeof fun == "function"){this.fn = fun;}
			$("body").append(this.fetch(skin['default'], {'width':width,'title':title}));	
			$("iframe[class=modal]").each(function(){this.src = src;});				
			this.setView(flag);
			$(".modal-header").dragdrop(".modal-window");				
			$(window).resize(this.resize);
			this.option(true);			
		},
		
		popup:function(src, title, width, flag, fun)
		{	
			src = this.makeRnd(src);
			if(typeof fun == "function"){this.fn = fun;}
			$("body").append(this.fetch(skin['popup'], {'width':width,'title':title}));				
			$("iframe[class=modal]").each(function(){this.src = src;});				
			this.setView(flag);
			$(".modal-header").dragdrop(".modal-window");				
			$(window).resize(this.resize);
			this.option(true);			
		},
		
		align: function(iHeight){
			var win = $(window);
			iLeft = (win.width()-$(".modal-window").width())/2;					
			iTop = win.scrollTop() + 10;
			$(".modal-window").css({left:(iLeft>0)?iLeft:2, top:(iTop>0)?iTop:2});	
			$(".modal-overlay").css({width: 0,height: 0}).css({width: Modal.width(),height: Modal.height()});
		},	
		
		resize: function(){			
			$(".modal-overlay").css({width: 0,height: 0}).css({width: Modal.width(),height: Modal.height()});
		},
		
		setView: function(flag){			
			Modal.align();
			$("iframe[class=modal]").load(function(){					
				var me = this;
				$(me).css({'height':$(me.contentWindow.document.body).height()+25});

				$("img", me.contentWindow.document.body).load(function(){
					Modal.setHeight();
				});
				
				setTimeout(function(){
					Modal.setHeight();					
				},1000);
				
				if(parent.Modal){
					parent.Modal.setHeight($(me).height());					
				}
				
				Modal.align($(me).height());	
				
				$(".modal-window > .modal-content").filter(function(){
					var bFlag = false;
					$(this).find("iframe").each(function(){
						if(this == me){
							bFlag = true;
						}
					});
					return bFlag;										  
				}).height($(me).height()+5);
			});	
		},
		
		setHeight: function(iHeight){
			$("iframe[class=modal]").each(function(){					
				var me = this;
				var iLocalHeight = $(me.contentWindow.document.body).height()+25;
				
				iLocalHeight = (iHeight && iLocalHeight<iHeight)?(iHeight+50):iLocalHeight;
				
				$(me).css({'height':iLocalHeight});	
				Modal.align($(me).height());
				
				$(".modal-window > .modal-content").filter(function(){
					var bFlag = false;
					$(this).find("iframe").each(function(){
						if(this == me){
							bFlag = true;
						}
					});
					return bFlag;										  
				}).height($(me).height()+5);
			});				
		},
		
		option: function(bFlag){
			if(bFlag){
				$('select,APPLET').hide();	
			}else{
				$('select,APPLET').show();	
			}
			
		},
		
		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;
		},
		
		height: function() {
			// handle IE 6
			if ($.browser.msie && $.browser.version != 7) {
				var scrollHeight = Math.max(
					document.documentElement.scrollHeight,
					document.body.scrollHeight
				);
				var offsetHeight = Math.max(
					document.documentElement.offsetHeight,
					document.body.offsetHeight
				);
	
				if (scrollHeight < offsetHeight) {
					return $(window).height() + 'px';
				} else {
					return scrollHeight + 'px';
				}
			// handle "good" browsers
			} else {
				return $(document).height() + 'px';
			}
		},
	
		width: function() {
			// handle IE 6
			if ($.browser.msie && $.browser.version != 7) {
				var scrollWidth = Math.max(
					document.documentElement.scrollWidth,
					document.body.scrollWidth
				);
				var offsetWidth = Math.max(
					document.documentElement.offsetWidth,
					document.body.offsetWidth
				);
	
				if (scrollWidth < offsetWidth) {
					return $(window).width() + 'px';
				} else {
					return scrollWidth + 'px';
				}
			// handle "good" browsers
			} else {
				return $(document).width() + 'px';
			}
		},
		
		makeRnd: function(sUrl){
			if(sUrl.search(/rnd/i) != -1){
				return sUrl.replace(/rnd=[^&]+/g,'rnd='+Math.random());
			}else{
				if(sUrl.search(/\?/i) != -1){
					return  sUrl+"&"+'rnd='+Math.random();
				}else{
					return  sUrl+"?"+'rnd='+Math.random();
				}
			}
		}
	};
}();
