
/**
*
*  Copyright 2003 Componence DDS. All rights reserved.
*  Last change date: 23.03.2004
*
*  Browser compatibility :
*  PC :
*     IE 5.0
*     NS 6.0
*     Mozilla 1.3
*     Opera 7.0
*  Apple :
*     IE ??? 5.2.3
*     NS 7.1
*     Mozilla 1.6
*     Opera ???
*     Safari 1.2
*  Linux :
*     NS        ???
*     Mozilla   1.2
*     Opera     7.11
*     Konqueror n/a
*
* Description :
*     This file creates a new instance of JSDialog class
*/
/**
* JSDialog class creates a new cross-browser Modal and Modless Dialogs Elements<br />
* A JSDialog object encapsulates public methods:
*   <ul>
*     <li>showPopup()</li>
*     <li>showDialog()</li>
*   </ul>
* Depends on:
*   <ul>
*     <li>JSBrowser (componence-lib-browser.js)</li>
*   </ul>
*
*  Defines global variables:
*   <ul>
*     <li>Dialog [Object] - it is an instanse of JSDialog class</li>
*   </ul>
* 
* @ version  {version}
* @since    1.0
* @constructor JSDialog
*/
//ComponentConfig.includeCommons();

function /*:JSClass:*/ JSDialog() {
	this.version = "{version}";
  
	if ( window.Global ) {
		if ( !Global.required( "JSBrowser" ) ) {
    	this.showPopup  = function(){};
    	this.showDialog = function(){};
  	  return null;
		}
  }
		
	/* @public methods */
	this.showPopup  = showPopup;
	this.showDialog = showDialog;
	
	/* @public properties */
	
	/** 
	* Public Property<br />
	* Contains reference to opened window
	* @type Window
	*/
	this.opened = null;
	
	/** 
	* Public Property<br />
	* Contains path to stylesheets folder
	* @type String
	*/
	this.cssPath = ( window.ComponentConfig ) ? window.ComponentConfig.cssPath : "";
	
	/**
	* @private
	*/
	this.nbsp = "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;";
	
	/**
	* Public method.
	* Displays a new Pop-up Element.
	* @param  sCaption [String] String that specifies the Window Title of the document that be displayed
	* @param  sHTML [String]    String that specifies the HTML Code that be pasted into new window and displayed
	* @param  sParams [String]  Variant that specifies the arguments to use when displaying the document
	* @return Window Element [Window] 
	* @type   Window
	* @see    JSDialog
	*/
	function /*:Window:*/ showPopup( /*:String:*/ sCaption, /*:String:*/ sHTML, /*:String:*/ sParams ) {
		var parsed = this.parseParams( sParams );
		
		if ( this.opened && !( this.opened.closed ) ) {
			this.opened.close();
		}
		var pos = ( parsed.center ) ? this.getCenter( parsed.height, parsed.width ) : "";
		
		this.opened = window.open( "", "", this.getSize( parsed.height, parsed.width ) + ", " + parsed.sParams + ", " + pos );
		with ( this.opened.document ) {
			open();
			write( this.setTitle( sCaption ) );
			write( sHTML );
			close();
		}
		this.opened.focus();
		this.setWindowEvent( parsed.modal );
		return this.opened;
	}
	
	/**
	* Public method.
	* Displays a new Dialog Element.
	* @param  sURL     [String]   String that specifies the URL of the document to load and display 
	* @param  sParams  [String]   Variant that specifies the arguments to use when displaying the document
	* @return Window Element [Window] 
	* @type   Window 
	* @see    JSDialog
	*/
	function /*:Window:*/ showDialog( /*:String:*/  sURL, /*:String:*/ sParams ) {
		var parsed = this.parseParams( sParams );
		
		if ( this.opened && !( this.opened.closed ) ) {
			this.opened.close();
		}
		var pos = ( parsed.center ) ? this.getCenter( parsed.height, parsed.width ) : "";
		this.opened = window.open( sURL, "", this.getSize( parsed.height, parsed.width ) + ", " + parsed.sParams + ", " + pos );
		this.opened.focus();
		this.setWindowEvent( parsed.modal );
		return this.opened;
	}
	
	/* @private methods */
	
	/**
	* Private method.
	* @private
	* Parse sParams.
	* @param  sParams [string]  Variant that specifies the arguments to use when displaying the document
	* @return Params  [object] 
	* @type   void 
	* @see    JSDialog
	*/
	this.parseParams = function /*:Object:*/ ( /*:String:*/ sParams ) {
	
		var Params = new Object();
		Params.sParams = sParams;
		sParams = sParams.replace( /\s/g, "" );
		sParams = sParams.replace( /:/g, "=" );
		sParams = sParams.replace( /yes/g, true );
		sParams = sParams.replace( /no/g, false );
		var arr = sParams.split( "," );
		
		for ( var i = 0; i < arr.length; i++ ) {
			var values = arr[i].split( "=" );
			eval( "Params." + values[0] + "=" + values[1] );
		}
		
		if ( window.showModalDialog && Params.modal && 1 == 2 ) {
			Params.sParams = Params.sParams.replace( "width", "dialogWidth" );
			Params.sParams = Params.sParams.replace( "height", "dialogHeight" );
			Params.sParams = Params.sParams.replace( "scrollbars", "scroll" );
			Params.sParams = Params.sParams.replace( /=/g, ":" );
			Params.sParams = Params.sParams.replace( /,/g, ";" );
			Params.sParams = Params.sParams.replace( /true/g, "1" );
			Params.sParams = Params.sParams.replace( /false/g, "0" );
			Params.sParams = Params.sParams.replace( /(\d\d[^;])/g, "$1px" );
			Params.sParams+= ";help:0; edge:raised;";
		}
		return Params;
	};
	
	/**
	* Private method.
	* @private
	* Calculate top and left choordinates of new window for displayng by center of screen.
	* @param  height  [int]
	* @param  width   [int]
	* @return String  [string] 
	* @type   void 
	* @see    JSDialog
	*/
	this.getCenter = function /*:String:*/ ( /*:Int:*/ height, /*:Int:*/ width ) {
		return "top=" + ( screen.height / 2 - height / 2 ) + ", left= " + ( screen.width / 2 - width / 2 );
	};
	
	/**
	* Private method.
	* @private
	* Calculate width and height of new window.
	* @param  height  [int]
	* @param  width   [int]
	* @return String  [string] 
	* @type   void 
	* @see    JSDialog
	*/
	this.getSize = function /*:String:*/ ( /*:Int:*/ height, /*:Int:*/ width ) {
		var size = "";
		if ( width && height ) {
			size = "width=" + width + ", height=" + height;
		} else if ( width ) {
			size = "width=" + width;
		} else if ( height ) {
			size = "height=" + height;
		}
		return size;
	};
	
	/**
	* Private method.
	* @private
	* Sets a title of new window and includes css file.
	* @param  text  [string]
	* @return void
	* @type   void 
	* @see    JSDialog
	*/
	this.setTitle = function /*:Void:*/ ( /*:String:*/ text ) {
		return "<html><head><title>" + text + " " + this.nbsp + "</title>" +
		"<link rel='stylesheet' href='" + this.cssPath + "componence-dialog.css' />" +
		"</head><body class='" + ( Browser.ie5 ? "bodyIE" : "bodyNS" )+"'>";
	};
	
	/**
	* Private method.
	* @private
	* Sets onfocus event for parent window.
	* @param  isModal  [boolean]
	* @return void
	* @type   void 
	* @see    JSDialog
	*/
	this.setWindowEvent = function /*:Void:*/ ( /*:Boolean:*/ isModal ) {
		if ( isModal ) {
			this.setFrameEvents( window );
			this.setFramesEvents();
		}
		window.onunload = function() {
			if ( Dialog.opened && !( Dialog.opened.closed ) ) {
				Dialog.opened.close();
			}
		};
	};
	
	this.setFramesEvents = function() {
		top.Dialog = Dialog;
		top.onfocus = function() {
			if ( top.Dialog && top.Dialog.opened && !( top.Dialog.opened.closed ) ) {
				top.Dialog.opened.focus();
				//top.blur();
				//if ( top.event ) {
				//	top.event.returnValue = false;
				//}
				//return false;
			}
		};
		for ( var i = 0; i < top.frames.length; i++ ) {
			this.setFrameEvents( top.frames[i].window );
		}
	};
	
	this.setFrameEvents = function( frameWin ) {
		if ( frameWin && frameWin.document ) {
			var doc = frameWin.document;
			if ( doc.attachEvent ) {
				doc.attachEvent( "onmousedown", top.onfocus );
				doc.attachEvent( "ondblclick", top.onfocus );
				doc.attachEvent( "onselectstart", top.onfocus );
				doc.attachEvent( "onkeydown", top.onfocus );
				doc.attachEvent( "onactivate", top.onfocus );
				doc.attachEvent( "onbeforeactivate", top.onfocus );
				doc.attachEvent( "onbeforeeditfocus", top.onfocus );
				doc.attachEvent( "onclick", top.onfocus );
				doc.attachEvent( "oncontextmenu", top.onfocus );
				//doc.attachEvent( "oncontextmenu", new Function( "window.onfocus();" ) );
				doc.attachEvent( "oncontrolselect", top.onfocus );
				//doc.attachEvent( "onscroll", top.onfocus );
				doc.attachEvent( "ondrag", top.onfocus );
				doc.attachEvent( "ondragenter", top.onfocus );
				doc.attachEvent( "ondragstart", top.onfocus );
				doc.attachEvent( "ondrop", top.onfocus );
				doc.attachEvent( "onfocusin", top.onfocus );
				doc.attachEvent( "onmouseenter", top.onfocus );
				doc.attachEvent( "onmouseup", top.onfocus );
				doc.attachEvent( "onselect", top.onfocus );
				
				frameWin.attachEvent( "onresizestart", top.onfocus );
				frameWin.attachEvent( "onresize", top.onfocus );
				frameWin.attachEvent( "onfocus", top.onfocus );
				frameWin.attachEvent( "onmove", top.onfocus );
				//frameWin.attachEvent( "onscroll", top.onfocus );
			} else if( doc.addEventListener ) {
				doc.addEventListener( "mousedown", top.onfocus, false );
			}
			if ( frameWin.frames && frameWin.frames.length > 0 ) {
				for ( var i = 0; i < frameWin.frames.length; i++ ) {
					try {
						if( frameWin.frames[i].length ) {
							top.Dialog.setFrameEvents( frameWin.frames[i] );
						}
					} catch ( err ) {
						alert( err.message+"\r\nframes.length = "+frameWin.frames.length+"\r\nframe.name="+frameWin.frames[i].name );
					}
				}
			}
		}
	};
}

var /*:Object:*/ Dialog = new JSDialog();