Seraphim.modalDialog = Class.create({
	initialize: function(){
		this.sourceUrl 				= false;	// Default url is blank
		this.htmlContent 			= '';	// Default message is blank
		this.cssFileName 			= 'SISModalDialog.css'; // Default CSS file
		this.height 					= 0;	// Default height of modal message
		this.width 						= 0;	// Default width of modal message
		this.cssClassOfMessageBox = false;	// Default alternative css class for the message box
		this.topPadding 			= 50;
		this.bottomPadding 		= 70;
		
		this.waitMessage 			= false;
		this.stausBarVisible 	= false;
		this.reqOptions 			= {};
		this.isDialogOpen 		= false;
		this.closeBox					= {};
		
		try{
			if(!standardObjectsCreated) new Seraphim();
		}catch(e){
			alert('You need to include the Seraphim.js file');
		}
		Seraphim.commonObj.loadCSS(this.cssFileName);
	},
	setSourceUrl: function(sourceUrl){
		this.sourceUrl = sourceUrl;
	},
	getSourceUrl: function(){
		return this.sourceUrl;
	},
	setHtmlContent:function(htmlContent){
		this.htmlContent = htmlContent;
	},
	getHtmlContent:function(){
		return this.htmlContent;
	},
	setCssFileName: function(cssFileName){
		this.cssFileName = cssFileName;
	},
	getCssFileName: function(){
		return this.cssFileName;
	},
	setHeight: function(height){
		this.height = height;
	},
	getHeight: function(){
		return this.height;
	},
	setWidth: function(width){
		this.width = width;
	},
	getWidth: function(){
		return this.width;
	},
	setReqOptions: function(reqOptions){
		this.reqOptions = reqOptions;
	},
	getReqOptions: function(){
		return this.reqOptions;
	},
	setCssClassOfMessageBox:function(cssClassOfMessageBox){
		this.cssClassOfMessageBox = cssClassOfMessageBox;
	},
	getCssClassOfMessageBox:function(){
		return this.cssClassOfMessageBox;
	},
	setTopPadding: function(topPadding){
		this.topPadding = topPadding;
	},
	getTopPadding: function(){
		return this.topPadding;
	},
	setBottomPadding: function(bottomPadding){
		this.bottomPadding = bottomPadding;
	},
	getBottomPadding: function(){
		return this.bottomPadding;
	},
	setWaitMessage:function(waitMessage){
		this.waitMessage = waitMessage;
	},
	getWaitMessage:function(){
		return this.waitMessage;
	},
	setStatusBarVisible: function(stausBarVisible){
		this.stausBarVisible = stausBarVisible;
	},
	isStatusBarVisible: function(){
		return this.stausBarVisible;
	},
	setStausBarVisible: function(stausBarVisible){
		// Misspelling maintained for backward compatibility
		this.stausBarVisible = stausBarVisible;
	},
	isStausBarVisible: function(){
		// Misspelling maintained for backward compatibility
		return this.stausBarVisible;
	},
	__getResizeEvent: function(){
		var el = ((function(){
			this.__resizeAndPositionDivElements();
			this.__resizeTransparentDiv();
		}).bind(this)).bindAsEventListener(el);
		this.__getResizeEvent = function(){
			return el;
		};
		return el;
	},
	display:function(){
		var that = this;
		if(!$('Seraphim_modalBox_transparentDiv')){
			this.__createDivElements();
		}else{
			this.__resizeTransparentDiv();
			this.__resizeAndPositionDivElements();
		}
		this.__addHTMLContent();
		Event.observe(window,'resize',this.__getResizeEvent());
		Event.observe(window,'scroll',this.__getResizeEvent());
	},
	cancelDialog: function(){
		this.__hideDialog();
		if (this.getReqOptions().onCancel){
			(this.getReqOptions().onCancel || Prototype.emptyFunction)();
		}
	},
	closeDialog: function(){
		this.__hideDialog();
		if (this.getReqOptions().onClose){
			(this.getReqOptions().onClose || Prototype.emptyFunction)();
		}
	},
	__hideDialog: function(){
		Event.stopObserving(window,'resize',this.__getResizeEvent());
		Event.stopObserving(window,'scroll',this.__getResizeEvent());

		Effect.Fade($('Seraphim_modalBox_transparentDiv'),{scope: 'dialogxscope', duration: 0.5});
		if (this.isStausBarVisible()){
			$('Seraphim_statusBarDiv').setStyle({display : 'none'});
		}
		$('Seraphim_modalBox_contentDiv').setStyle({display : 'none'});
		$('Seraphim_messageAreaDiv').setStyle({display : 'none'});
		$('Seraphim_messageAreaDiv').innerHTML = '';
		// for IE bug, we are creating an IFRAME
		if($('SISModalDialog_iframeElement')){
			$('SISModalDialog_iframeElement').setStyle({display : 'none'});
		}
		$(document.documentElement).setStyle({overflowX: 'auto'});
		this.isDialogOpen = false;
	},
	resetDialog: function(){
		this.setSourceUrl(false);
		this.setHtmlContent('');
		this.setCssFileName('SISModalDialog.css');
		this.setHeight(0);
		this.setWidth(0);
		this.setReqOptions({});
		this.setCssClassOfMessageBox(false);
		this.setWaitMessage(false);
	},
	__createDivElements:function(){
		var that = this;
		// create the transparent DIV
		var transparentDiv = document.createElement('DIV');
		$(transparentDiv).addClassName('SISModalDialog_transparentDivs');
		$(transparentDiv).id = 'Seraphim_modalBox_transparentDiv';
		document.body.insertBefore(transparentDiv, document.body.childNodes[0]);
		
		// create the content DIV
		var contentDiv = document.createElement('DIV');
		if(this.getCssClassOfMessageBox()){
			$(contentDiv).addClassName(this.getCssClassOfMessageBox());
		}else{
			$(contentDiv).addClassName('SISModalDialog_contentDiv');
		}
		$(contentDiv).id = 'Seraphim_modalBox_contentDiv';
		document.body.insertBefore(contentDiv, document.body.childNodes[0]);

		var messageDiv = document.createElement('DIV');
		$(messageDiv).addClassName('SISModalDialog_messageAreaDiv');
		$(messageDiv).id = 'Seraphim_messageAreaDiv';

		var statusBarDiv = document.createElement('DIV');
		$(statusBarDiv).addClassName('SISModalDialog_statusBarDiv');
		$(statusBarDiv).id = 'Seraphim_statusBarDiv';
		if (!this.isStausBarVisible()){
			$(statusBarDiv).setStyle({display : 'none'});
		}
		
		var img = document.createElement('IMG');
		img.src = Seraphim.configObj.getImagePath() + 'close.gif';
		img.style.styleFloat = 'right';
		img.style.cursor = 'hand';
		img.style.cursor = 'pointer';
		$(statusBarDiv).appendChild(img);
		
		Event.observe(img, 'click', this.cancelDialog.bindAsEventListener(this));
	
		$(contentDiv).appendChild(statusBarDiv);
		$(contentDiv).appendChild(messageDiv);

		// for IE bug, we are creating an IFRAME
		if(Seraphim.clientInfoObj.isMSIE()){
			var iframeElement = document.createElement('<iframe frameborder=0 src="about:blank" scrolling="no">');
			iframeElement.id = 'SISModalDialog_iframeElement';
			$(iframeElement).setStyle({
				filter : 'alpha(opacity=0)',
				cssText : 'filter:alpha(opacity=0)',
				position : 'absolute',
				zIndex : 100001,
				display : 'none',
				left : '0px',
				top : '0px'
			});
			document.body.insertBefore(iframeElement, document.body.childNodes[0]);
		}

		// add all element events
		this.__resizeTransparentDiv();
		this.__resizeAndPositionDivElements();
	},
	__resizeAndPositionDivElements: function(){
		if(!$('Seraphim_modalBox_contentDiv')){
			return;
		}

		$('Seraphim_modalBox_contentDiv').style.width		= (this.getWidth() > 0) 	? this.getWidth() + 'px' 	: '';
		$('Seraphim_modalBox_contentDiv').style.height	= (this.getHeight() > 0) 	? this.getHeight() + 'px' : '';
		
		if (!this.isDialogOpen){
			$('Seraphim_modalBox_transparentDiv').setStyle({display : 'block'});
			$('Seraphim_modalBox_contentDiv').setStyle({display : 'block'});
			if (this.isStausBarVisible()){
				$('Seraphim_statusBarDiv').setStyle({display : 'block'});
			}
			$('Seraphim_messageAreaDiv').setStyle({display : 'block'});
			if(Seraphim.clientInfoObj.isMSIE()){
				$('SISModalDialog_iframeElement').setStyle({display : 'block'});
			}
			this.isDialogOpen = true;
		}

		$(document.documentElement).setStyle({overflowX: 'hidden'});
		var bodyWidth		= Seraphim.clientInfoObj.getBrowserWidth();
		var bodyHeight	= Seraphim.clientInfoObj.getBrowserHeight();

		var tmpWidth		= $('Seraphim_modalBox_contentDiv').offsetWidth;
		var tmpHeight		= $('Seraphim_modalBox_contentDiv').offsetHeight;
		var tmpLeft			= Math.ceil((bodyWidth - tmpWidth) / 2);

    $('Seraphim_modalBox_contentDiv').setStyle({
      left: ((tmpLeft < 0) 	? 0 : tmpLeft) + 'px'
    });
		var tmpTop = this.__getDialogNewTop();
		if (tmpTop){
	    $('Seraphim_modalBox_contentDiv').setStyle({
	      top: tmpTop + 'px'
	    });
		}
	},
	__getDialogNewTop: function(){
		var _top 				= Seraphim.clientInfoObj.getTopPos($('Seraphim_modalBox_contentDiv'));
		var _height 		= this.getHeight();
		var _bottom 		= _top+_height;
		var _topScroll	= Seraphim.clientInfoObj.getClientScrollTop();
		var _viewPort		= Seraphim.clientInfoObj.getBrowserHeight();
		
		var ttl = _top>_topScroll+this.getTopPadding();
		var bth = _bottom<_topScroll+_viewPort-this.getBottomPadding();
		var tth = _top<_topScroll+this.getTopPadding();
		var btl = _bottom>_topScroll+_viewPort-this.getBottomPadding();
		var sm 	= _height<=_viewPort-this.getTopPadding()-this.getBottomPadding();
		
		var _newTop =null;
		if(sm&&!(tth||btl)){
		}else{
			if(ttl||bth&&sm){
				if(ttl&&sm){
					_newTop=_topScroll+_viewPort-this.getBottomPadding()-_height;
				}else{
					_newTop=_topScroll+this.getTopPadding();
				}
			}else{
				if(bth){
					_newTop=_topScroll+_viewPort-this.getBottomPadding()-_height;
				}
			}
		}
		if(_newTop!==null){
			return _newTop;
		}else{
			return false;
		}
	},
	__resizeTransparentDiv: function(){
		if(!$('Seraphim_modalBox_transparentDiv')){
			return;
		}

		var pageHeight 	= Seraphim.clientInfoObj.getScrollHeight();
		var viewPort 		= Seraphim.clientInfoObj.getBrowserHeight();
		var divHeight	=	(Seraphim.clientInfoObj.getBrowserHeight()>Seraphim.clientInfoObj.getScrollHeight()) ? (Seraphim.clientInfoObj.getBrowserHeight() + Seraphim.clientInfoObj.getClientScrollTop()) - ((Seraphim.clientInfoObj.isMSIE()) ? 4 : 0) : Seraphim.clientInfoObj.getScrollHeight();
		var divWidth	= Seraphim.clientInfoObj.getBrowserWidth();
	
		$('Seraphim_modalBox_transparentDiv').style.height 	= divHeight + 'px';
		$('Seraphim_modalBox_transparentDiv').style.width 	= divWidth + 'px';
		if(Seraphim.clientInfoObj.isMSIE()){
			$('SISModalDialog_iframeElement').style.height 		= divHeight + 'px';
			$('SISModalDialog_iframeElement').style.width 		= divWidth + 'px';
		}
	},
	__addHTMLContent:function(){
		$('Seraphim_messageAreaDiv').innerHTML = '';
		if (this.getSourceUrl()){
			try{
				if (this.getWaitMessage()){
					this.getReqOptions().onLoading = (function(transport){
						$('Seraphim_messageAreaDiv').innerHTML = this.getWaitMessage();
					}).bind(this);
				}
				this.getReqOptions().onSuccess = (function(transport){
					$('Seraphim_messageAreaDiv').innerHTML = transport.responseText;
				}).bind(this);
				var ajax = new Ajax.Updater($('Seraphim_messageAreaDiv'),this.getSourceUrl(),this.getReqOptions());
			}catch(e){
				alert('You need to include the prototype.js file');
			}
		}else{
			if (typeof(this.getHtmlContent()) == 'object'){
				$('Seraphim_messageAreaDiv').appendChild(this.getHtmlContent());
			}else{
				$('Seraphim_messageAreaDiv').innerHTML = this.getHtmlContent();
			}
			if (this.getReqOptions().onComplete){
				var annonFunction = (function(){
					(this.getReqOptions().onComplete || Prototype.emptyFunction)();
				}).bind(this);
				setTimeout(annonFunction,100);
			}
		}
	}
});