/*
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * Dialog windows operations.
 */

var FCKDialog = 
{
	BaseZIndex : 10000
} ;

// This method opens a dialog window using the standard dialog template.
FCKDialog.OpenDialog = function( dialogName, dialogTitle, dialogPage, width, height, customValue, parentWindow, resizable )
{
	// Setup the dialog info.
	var oDialogInfo = new Object() ;
	oDialogInfo.Title = dialogTitle ;
	oDialogInfo.Page = dialogPage ;
	oDialogInfo.Editor = window ;
	oDialogInfo.CustomValue = customValue ;		// Optional

	var sUrl = FCKConfig.BasePath + 'fckdialog.html' ;
	this.Show( oDialogInfo, dialogName, sUrl, width, height, parentWindow, resizable ) ;
}

FCKDialog._LoadPNG = function( element, url )
{
	if ( FCKBrowserInfo.IsIE )
		element.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + url + '",sizingMethod="scale")' ;
	else
	{
		element.style.backgroundImage = 'url(' + url + ')' ;
		element.style.backgroundRepeat = 'repeat' ;
	}
}

FCKDialog._SetOpacity = function( element, opacity )
{
	if ( FCKBrowserInfo.IsIE )
		element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + parseInt( opacity * 100 ) + ')' ;
	else
		element.style.opacity = opacity ;
}

FCKDialog.Show = function( dialogInfo, dialogName, pageUrl, dialogWidth, dialogHeight, parentWindow, resizable )
{
	var ownerWindow = FCK.Window.parent ;
	var ownerDocument = ownerWindow.document ;
	var innerWidth = FCKBrowserInfo.IsIE ? ownerDocument.documentElement.offsetWidth : ownerWindow.innerWidth;
	var innerHeight = FCKBrowserInfo.IsIE ? ownerDocument.documentElement.offsetHeight : ownerWindow.innerHeight;
	var iTop  = (innerHeight - dialogHeight) / 2 ;
	var iLeft = (innerWidth - dialogWidth)  / 2	;
	if ( ! ownerWindow.FCKDialogArguments )
		ownerWindow.FCKDialogArguments = {} ;
	var dialogId = parseInt( Math.random() * 0x10000000, 10 ) ;
	ownerWindow.FCKDialogArguments[dialogId] = dialogInfo ;
	
	// The container will contain the following things:
	// 1. The dialog iframe
	// 2. The dialog shadow
	var container = ownerDocument.createElement( 'div' ) ;
	container.dialogId = dialogId ;
	container.ownerWindow = ownerWindow ;
	dialogInfo.Dialog = container ;
	FCKDomTools.SetElementStyles( container, 
			{
				'position' : 'absolute',
				'top' : iTop + 'px',
				'left' : iLeft + 'px',
				'overflow' : 'visible',
				'zIndex' : this.BaseZIndex += 100
			} ) ;

	var frame = ownerDocument.createElement( 'iframe' ) ;
	frame.id = dialogId ;
	frame.frameBorder = 0 ;
	FCKDomTools.SetElementStyles( frame, 
			{ 
				'border' : 'none', 
				'position' : 'absolute',
				'top' : '6px',
				'left' : '6px',
				'zIndex' : '100'
			} ) ;
	ownerDocument.body.appendChild( container ) ;
	container.appendChild( frame ) ;
	this.ResizeDialog( container, dialogWidth, dialogHeight ) ;

	// Assigning frame.src directly does not work in Opera 9.5, need to use a timeout trick
	var loadFrame = function()
	{
		frame.src = pageUrl + '?dialogId=' + dialogId ;
	}
	setTimeout( loadFrame, 0 ) ;

	FCKFocusManager.Lock() ;

	var oWindow = frame.contentWindow ;
	oWindow.focus() ;
	this.Window = oWindow ;
}



// The following member functions should be called with a dialog container element as subject.
FCKDialog.ResizeDialog = function( container, width, height )
{
	FCKDomTools.SetElementStyles( container,
		{
			'width' : ( width + 20 ) + 'px',
			'height' : ( height + 20 ) + 'px'
		} ) ;
	this.RemoveShadow( container ) ;
	this.GenerateShadow( container ) ;
	var frame = container.getElementsByTagName( 'iframe' )[0] ;
	FCKDomTools.SetElementStyles( frame,
		{
			'width' : width + 'px',
			'height' : height + 'px'
		} ) ;
}

FCKDialog.RemoveShadow = function( container )
{
	for( var i = 0 ; i < container.childNodes.length ; i++ )
	{
		var candidate = container.childNodes[i] ;
		if ( candidate.nodeName.IEquals( 'div' ) )
			candidate.parentNode.removeChild( candidate ) ;
	}
}

FCKDialog.GenerateShadow = function( container )
{
	var doc = container.ownerDocument ;
	var shadowBlock = doc.createElement( 'div' ) ;
	FCKDomTools.SetElementStyles( shadowBlock,
		{
			'width' : container.offsetWidth + 'px',
			'height' : container.offsetHeight + 'px',
			'position' : 'absolute',
			'top' : '0px',
			'left' : '0px',
			'zIndex' : '0'
		} ) ;

	// 0 1 2
	// 3 4 5
	// 6 7 8
	var partUrl = 
	[
		'dialog.shadow.top.left.png',
		'dialog.shadow.top.png',
		'dialog.shadow.top.right.png',
		'dialog.shadow.left.png',
		'dialog.shadow.core.png',
		'dialog.shadow.right.png',
		'dialog.shadow.bottom.left.png',
		'dialog.shadow.bottom.png',
		'dialog.shadow.bottom.right.png'
	];
	for ( var i = 0 ; i < 9 ; i++ )
	{
		var d = doc.createElement( 'div' ) ;
		var modulo = i % 3 ;
		var quotient = parseInt( i / 3 ) ;
		if ( modulo % 2 == 0 )
			d.style.width = '20px' ;
		else
			d.style.width = ( container.offsetWidth - 40 ) + 'px' ;

		if ( quotient % 2 == 0 )
			d.style.height = '20px' ;
		else
			d.style.height = ( container.offsetHeight - 40 ) + 'px' ;
		this._LoadPNG( d, FCKConfig.SkinPath + 'images/' + partUrl[i] ) ;
		var x = [ 0, 20, container.offsetWidth - 20 ] ;
		var y = [ 0, 20, container.offsetHeight - 20 ] ;
		FCKDomTools.SetElementStyles( d,
			{
				'left' : x[modulo] + 'px',
				'top' : y[quotient] + 'px',
				'position' : 'absolute'
			} ) ;
		shadowBlock.appendChild( d ) ;
	}

	this._SetOpacity( shadowBlock, 0.65 ) ;
	container.appendChild( shadowBlock ) ;
}

FCKDialog.CloseDialog = function( container )
{
	var ownerWindow = container.ownerWindow ;
	var dialogId = container.dialogId ;
	delete ownerWindow.FCKDialogArguments[dialogId] ;
	container.parentNode.removeChild( container ) ;
	this.BaseZIndex -= 100 ;
}
