﻿
// JScript


function GenerateIFrameHtml(dlgID, iframeURL, arg, width, height) 
{
    var html = "<iframe id='" +  dlgID + "_iframe' frameborder='0' scrolling='no' src='" + 
	    iframeURL + "' style='width:" + width + "px;height:100%;' ></iframe>";	
	alert(html);
	return html;
}

function Edentity_Jquery_Popup_Init(dlgID, generateHtml, width, height)
{
	if ( dlgID.indexOf("#") != 0)
	{
	    dlgID = "#" + dlgID;
	}
	
	jQuery.fn.extend({Show: function (arg) {
		if (jQuery.isFunction(arg)) { jQuery.event.add(this[0], 'Show', arg, null); }
		else { jQuery.event.trigger('Show', [arg]); }
	}});
	
	jQuery.fn.extend({Hide: function (arg) {
		if (jQuery.isFunction(arg)) { jQuery.event.add(this[0], 'Hide', arg, null); }
		else { jQuery.event.trigger('Hide'); }
	}});
	
	$(dlgID).Show(
		function (event, arg) {		
			$(dlgID).css('position', 'absolute');
			
			//alert(arg);
			
			if (arg.height) height = arg.height;
			if (arg.width) width = arg.width;

			if (arg.html)
			{
		        $(dlgID + '_html').html(arg.html);
			}
			else if (arg.iframeUrl)
			{
			    $(dlgID + '_html').html(GenerateIFrameHtml(dlgID, arg.iframeUrl, arg, width, height));
			}
			else
			{
		        $(dlgID + '_html').html('');
		    }
		    
		    $(window).bind('resize.Edentity_Jquery_Popup', function () { Edentity_Jquery_Popup_WindowResize(dlgID, arg); });
			
			//alert($(dlgID + '_body').html());
			
		    $(dlgID + '_body').css('height', height + 'px');
		    $(dlgID + '_body').css('width', width + 'px');
		    $(dlgID).css('width', width + 'px');
		    $(dlgID + '_top').css('width', width + 'px');
		    $(dlgID + '_bottom').css('width', width + 'px');
						
		    $(dlgID).css('top', Math.max($(window).height()/2-height/2, 20) + 'px');
		    $(dlgID).css('left', $(window).width()/2-width/2 + 'px');
			
			Edentity_Jquery_Popup_BlockUI();
			$(dlgID).css('display', '');
		}
	);		
		
	$(dlgID).Hide(
		function () {
			$(dlgID).css('display', 'none');
			Edentity_Jquery_Popup_UnBlockUI();
			$(dlgID + '_html').html('');
			$(window).unbind('resize.Edentity_Jquery_Popup');
		}
	);
}


function Edentity_Jquery_Popup_BlockUI() {
	var f = ($.browser.msie) ? $('<iframe class="blockUI" style="z-index:1000;border:none;margin:0;padding:0;position:absolute;width:100%;height:100%;top:0;left:0" src="javascript:false;"></iframe>')
		: $('<div class="blockUI" style="display:none"></div>');
	var w = $('<div class="blockUI" style="z-index:1001;cursor:wait;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0;position:fixed;"></div>');
	var noalpha = this.op8 || $.browser.mozilla && /Linux/.test(navigator.platform);
	if (!noalpha) w.css({ backgroundColor: '#000', opacity: '0.5' });
	if ($.browser.msie) f.css('opacity','0.0');
	if ($.browser.msie && /MSIE 6.0/.test(navigator.userAgent)) {
		$('html,body').css('height','100%');
		$.each([f,w], function(i,o) {
			var s = o[0].style; s.position = 'absolute';
			s.setExpression('height','document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + "px"');
			s.setExpression('width','jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"')
		});
	}
	$([f[0],w[0]]).appendTo('body');
}
function Edentity_Jquery_Popup_UnBlockUI() {
	var els = $('body').children().filter('.blockUI');
	els.remove();
}
function Edentity_Jquery_Popup_WindowResize(dlgID, args) {
    var height = args.height;
    var width = args.width;
    $(dlgID).css('top', Math.max($(window).height()/2-height/2, 20) + 'px');
    $(dlgID).css('left', $(window).width()/2-width/2 + 'px');
}
