// ...

var _btn = new CButton();
function OnMouseOver( e )	{_btn.OnMouseOver( e );}
function OnMouseOut( e )	{_btn.OnMouseOut( e );}
function OnMouseDown( e )	{_btn.OnMouseDown( e );}
function OnMouseClick( e)	{_btn.OnMouseClick( e );}

var _wnd = null;
function OpenSubWnd( sWndName, sPath, iWidth, iHeight, sQuery ) {
	if (_wnd) {
		if (!_wnd.closed) {
			_wnd.Close();
		}
	}
	_wnd = new CSubWindow( sWndName, sPath, iWidth, iHeight );
	_wnd.Open( sQuery );
}
function CloseSubwindow() {
	if (_wnd) {
		_wnd.Close();
		_wnd = null;
	}
}


function SetBodyColor( sColor, iWait ) {
	if (typeof( iWait ) != 'number') {
		iWait = 0;
	}
	setTimeout( 
		function() {
			var o = document.getElementsByTagName( 'body' );
			if (o) {
				o[ 0 ].style.backgroundColor = sColor;
			}
		},
		iWait
	);
}


function CSubWindow( sWndName, sPath, iWidth, iHeight ) {
	this._OPTIONS	= 'toolbar=no, location=no, resizable=no, directories=no, status=no, menubar=no, scrollbars=no, width=#WIDTH, height=#HEIGHT';
	this.m_name		= sWndName;
	this.m_path		= sPath;
	this.m_width	= iWidth;
	this.m_height	= iHeight;
	this.m_wnd		= null;

	this.Open = function( sQuery ) {
		var sOption = this._OPTIONS;
		var sAgent = (new CBrowser()).GetAgent( false );

		if (typeof( sQuery ) != 'string' ) {
			sQuery = '';
		}
		sOption = sOption.replace( '#WIDTH',  this.m_width.toString() );
		sOption = sOption.replace( '#HEIGHT', this.m_height.toString() );
		this.m_wnd = window.open( this.m_path + sQuery, this.m_name, sOption );
	}

	this.Close = function() {
		if (this.m_wnd) {
			if (!this.m_wnd.closed) {
				this.m_wnd.blur();
				this.m_wnd.close();
				window.focus();
			}
			this.m_wnd = null;
		}
	}

	this.Focus = function() {
		if (! this.m_wnd.closed) {
			if ((new CBrowser()).GetAgent( false ) == "Safari") {
				window.blur();
			}
			this.m_wnd.focus();
		}
	}

	this.GetWnd = function() {
		return this.m_wnd;
	}
}


function CBrowser() {
	this.GetAgent = function( bAddVer ) {
		//	alert( navigator.userAgent );
		var r;
		if      (r = this.CheckName( 'Chrome',  bAddVer )) {return r;}
		else if (r = this.CheckName( 'Firefox', bAddVer )) {return r;}
		else if (r = this.CheckName( 'Safari',  bAddVer )) {return r;}
		else if (r = this.CheckName( 'Opera',   bAddVer )) {return r;}
		else if (r = this.CheckName( 'MSIE',    bAddVer )) {return r;}
		else if (r = this.CheckName( 'Mozilla', bAddVer )) {return r;}
		return 'unknown';
	}

	this.CheckName = function( sName, bAddVer ) {
		var r;
		if (r = navigator.userAgent.match( sName + '[/ ]+' + '([0-9.]+)' )) {
			if (bAddVer) {return sName + ' ' + RegExp.$1;}
			else         {return sName;}
		}
		return null;
	}
}


function CButton() {
	this.SUFFIX = new Object();
	this.SUFFIX[ 'off'  ] = '';
	this.SUFFIX[ 'on'   ] = '_on';
	this.SUFFIX[ 'over' ] = '_on';

	this.OnMouseOver = function( e ) {
		this.Redraw( e, 'over' );
	}

	this.OnMouseOut = function( e ) {
		this.Redraw( e, 'off' );
	}

	this.OnMouseDown = function( e ) {
		this.Redraw( e, 'on' );
	}

	this.OnMouseClick = function( e ) {
		this.Redraw( e, 'off' );
	}

	this.Redraw = function( elem, state ) {
		if (typeof( elem ) == 'string') {
			var elem = document.getElementById( elem );
		}
		if (elem) {
			var path   = elem.src;
			var suffix = this.SUFFIX[ 'over' ] + '|' + this.SUFFIX[ 'on' ];

			var s = '(#S)?\.(gif|jpg|png)$'.replace( '#S', suffix );
			var m = path.match( RegExp( s, 'i' ) );
			if (m) {
				var ext = '.' + RegExp.$2;
				path = path.replace( m[ 0 ], '' ) + this.SUFFIX[ state ] + ext;
				elem.src = path;
			}
		}
	}
}


function CCheckOpener( sMyName ) {
	this.DEFAULT_INTERVAL = 523;
	this.m_my_name = sMyName;
	this.m_timer   = null;

	this.Start = function( iInterval ) {
		if (!this.m_timer) {
			if (typeof( iInterval ) != 'number') {
				iInterval = this.DEFAULT_INTERVAL;
			}
			this.m_timer = setInterval( this.m_my_name + '.Check();', iInterval );
		}
	}

	this.Check = function() {
		if (window.opener) {
			if (!window.opener.closed) {
				return;
			}
		}
		if (this.m_timer) {
			clearInterval( this.m_timer );
			this.m_timer = null;
		}
		window.close();
	}
}
