var win = {
	
	m_sType : "dialog",
	m_iWidth : 600,
	m_iHeight : 600,
	m_iTop : 0,
	m_iLeft : 0,
	
	m_oData : null,
	
	m_fnOnLoad : function(oData){},
	m_fnOnClose : function(oData){},

	
	loadURL : function(sTitle, sURL, oOption)
	{
		this.show(sTitle, '', oOption);
				
		jQuery('#win-content').addClass('winloading');
		jQuery('#win-content').html('<div class="ajax-wait">&nbsp;</div>');
		
		jQuery('#win-wait').show();
		jQuery('#win-content input').attr('disabled', true);
		jQuery('#win-content textarea').attr('disabled', true);
		jQuery('#win-xontent button').attr('disabled', true);
		
		
		var _self = this;
		jQuery.ajax({
			type: "GET",
			url: sURL,
			success: function(msg){
				jQuery('#win-content').removeClass('winloading');
				jQuery('#win-wait').hide();
				jQuery('#win-content input').attr('disabled', false);
				jQuery('#win-content textarea').attr('disabled', false);
				jQuery('#win-content button').attr('disabled', false);
				
				jQuery('#win-content').html(msg);

				eval(_self.m_fnOnLoad(_self.m_oData));
			}
		});
	},

		
	openURL : function(sTitle, sURL, oOption)
	{
		this.setOpt(oOption);
		
		var iBrowserWidth = 0;
		var iBrowserHeight = 0;
		if (jQuery.browser.msie)
		{  
			iBrowserWidth = document.compatMode == "CSS1Compat"?document.documentElement.clientWidth:document.body.clientWidth;
			iBrowserHeight = document.compatMode == "CSS1Compat"?document.documentElement.clientHeight:document.body.clientHeight;  
		} else {  
			iBrowserWidth = document.documentElement.clientWidth;
			iBrowserHeight = document.documentElement.clientHeight;
		}

		var iLeft = (iBrowserWidth-this.m_iWidth)/2;
		var iTop = (iBrowserHeight-this.m_iHeight)/2;

		window.open(url, "eventDialog", "width="+m_iWidt+", left="+iLeft+", top="+iTop+", toolbar=no, menubar=no, location=no, status=no, scrollbars=yes");
	},
	
	show : function(sTitle, sContent, oOption)
	{
		this.setOpt(oOption);
	
		var e = document.getElementById('win');
		if(!e){
			e = document.createElement('div');

			var sHtml  = '';
			sHtml += '<div id="win" class="' + this.m_sType + '" style="top: 0px; display: none;">';
			sHtml += '	<!-- top section -->';
			sHtml += '	<div id="win-tl"></div>';
			sHtml += '	<div id="win-tm"></div>';
			sHtml += '	<div id="win-tr"></div>';
			sHtml += '	<div style="clear: both;"></div>';
			sHtml += '	<!-- middle section -->';
			sHtml += '	<div id="win-ml"></div>';
			sHtml += '	<div id="win-content-outer">';
			sHtml += '		<div id="win-content-top">';
			sHtml += '			<a href="javascript:void(0);" onclick="win.hide();" id="win-close-btn">Close</a>';
			sHtml += '			<div id="win-logo">'+sTitle+'</div>';
			sHtml += '		</div>';
			sHtml += '		<div id="win-content">'+sContent+'</div>';
			sHtml += '	</div>';
			sHtml += '	<div id="win-mr"></div>';
			sHtml += '	<div style="clear: both;"></div>';
			sHtml += '	<!-- bottom section -->';
			sHtml += '	<div id="win-bl"></div>';
			sHtml += '	<div id="win-bm"></div>';
			sHtml += '	<div id="win-br"></div>';
			sHtml += '	<div style="clear: both;"></div>';
			sHtml += '</div>';
			
			e.innerHTML = sHtml;
			document.body.appendChild(e);
		} else {
			jQuery('#win-logo').html(sTitle);
			jQuery('#win-content').html(sContent);
		}
		
		// Set Window Width + 40
		jQuery('#win').width(this.m_iWidth + 40);
		jQuery('#win-content-outer, #win-content-top, #win-content').width(this.m_iWidth);
		jQuery('#win-bm, #win-tm').width(this.m_iWidth);
		
		
		var myWidth = 0, myHeight = 0;
		myWidth  = jQuery(window).width();
		myHeight = jQuery(window).height();
		


		var yPos;
		if(this.m_iTop>0)
		{
			yPos = this.m_iTop;
		}
		else
		{
			if( jQuery.browser.opera && jQuery.browser.version > "9.5" && jQuery.fn.jquery <= "1.2.6" )
			{
				yPos = document.documentElement['clientHeight'] - 30;
			}
			else
			{
				yPos = jQuery(window).height() - 30;
			}
		}

		var leftPos;
		if(this.m_iLeft>0)
		{
			leftPos = this.m_iLeft;
		}
		else
		{
			leftPos = (myWidth - this.m_iWidth)/2;
		}

		
		var allElems = document.getElementsByTagName?document.getElementsByTagName("*"):document.all; // or test for that too
		var maxZIndex = 0;
	
		for(var i=0;i<allElems.length;i++) {
			var elem = allElems[i];
			var style = null;
			if (elem.currentStyle) 
				style = elem.currentStyle;
			else if (document.defaultView && document.defaultView.getComputedStyle) {
				style = document.defaultView.getComputedStyle(elem,"");
			}
	
			var sNum;
			if (style) {
				sNum = Number(style.zIndex);
			} else {
				sNum = Number(elem.style.zIndex);
			}
			if (!isNaN(sNum)) {
				maxZIndex = Math.max(maxZIndex,sNum);
			}
		}
		
		jQuery('#win').css('zindex', maxZIndex + 1);

		// Set editor position, center it in screen regardless of the scroll position
		jQuery("#win").css('marginTop', (jQuery(document).scrollTop() + 10 +(yPos - this.m_iHeight)/2) + (20)+'px');
		
		
		// Set height and width for transparent window
		jQuery('#win').css('height', this.m_iHeight).css('left', leftPos);
		jQuery('#win-content').css('height', (this.m_iHeight - 50)) // - 30px title and 20px border
								 .css('width', (this.m_iWidth - 20));  // -20px border
		jQuery('#win-content-outer').css('height', this.m_iHeight);
		jQuery('#win-tm, #win-bm').css('width', this.m_iWidth);
		jQuery('#win-ml, #win-mr').css('height', this.m_iHeight);
		
		// Rebuild alpha PNG for IE6
		if (jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7)
		{
			jQuery('#win-tm, #win-bm, #win-ml, #win-mr').each(function()
			{
				jQuery(this)[0].filters(0).sizingMethod="crop";
			})
		}
		
		jQuery('#win').fadeIn();
	},


	
	hide : function()
	{
		if(jQuery('#win-action').get().length > 0)
			jQuery('#win-action').animate({ bottom: "-40px"}, 200 , '', function(){
				jQuery('#win').fadeOut('fast', function(){ jQuery(this).remove();});
			});
		else
			jQuery('#win').fadeOut('fast', function(){ jQuery(this).remove();});
			
		eval(this.m_fnOnClose(this.m_oData));
	},
	
	
	actions : function(action)
	{
		var html = '<div id="win-action">'; 
		html += '<table><tr>';
		html += '<td align="left"><div id="win-wait">&nbsp;</div></td><td  align="right" valign="middle">';
		html += action;
		html += '</td></tr></table></div>';
		
		jQuery('#win-action').remove();
		jQuery('#win-content-outer').append(html);
		_height = jQuery('#win-content').height();
		_height = _height - 34;
		jQuery('#win-content').height( _height );
		
		jQuery('#win-action').animate({bottom: "0px"}, 200 );
	},
	
	resize : function()
	{
		var iCurrentHeight = jQuery('#win-mr').height();
		var iReduceHeight = (iCurrentHeight - this.m_iHeight) /2; 
		

		jQuery('#win').animate({marginTop: "+="+ iReduceHeight +"px"}, 200 );
		//jQuery('#win-tl,#win-tm,#win-tr').animate({marginTop: "+="+ reduceHeight +"px"}, 200 );
		
		// Reduce height
		jQuery('#win-mr,#win-ml,#win-content-outer').animate({height: this.m_iHeight +"px"}, 200 );
		jQuery('#win-content').animate({height: this.m_iHeight - 50 +"px"}, 200 );
	},
	
	
	setOpt : function(oOption)
	{
		if(typeof oOption=="object")
		{
			if(oOption.type) this.m_sType = oOption.type;
			if(oOption.width) this.m_iWidth = oOption.width;
			if(oOption.height) this.m_iHeight = oOption.height;
			if(oOption.top) this.m_iTop = oOption.top;
			if(oOption.left) this.m_iLeft = oOption.left;
			if(oOption.onLoad) this.m_fnOnLoad = oOption.onLoad;
			if(oOption.onClose) this.m_fnOnClose = oOption.onClose;
		}
	}
	
}

