

var Filemanager = function( basePath, width, height, selectFunction , type)
{
	// The URL path for the installation folder of Filemanager (default = "/Filemanager/").
	this.BasePath = basePath || Filemanager.DEFAULT_BASEPATH ;

	// The Filemanager width (ex: 600, '80%') (default = "100%").
	this.Width	= width || '100%' ;

	// The Filemanager height (ex: 500, '100%') (default = 400).
	this.Height	= height || 400 ;

	// An optional function to be called when the user selects a file in Filemanager.
	this.SelectFunction = selectFunction || null ;

	// The name of the CSS class rule assigned to the Filemanager frame (default = "FilemanagerStyle").
	this.ClassName = null || 'FilemanagerFrame' ;
	
	// The server language of the connector
	this.ConnectorLanguage = 'php' ;

	
	switch (type) {
	case "image":
		this.FileType = 'Type=' + 'Image';
		break;
	case "media":
		this.FileType = 'Type=' + 'Flash';
		break;
	case "file":
		this.FileType = 'Type=' + 'File';
		break;
	default:
		this.FileType = "";
	}
}

Filemanager.DEFAULT_BASEPATH = '/Filemanager/' ;

Filemanager.prototype = {

	// Renders Filemanager in the current document.
	Create : function()
	{
		document.write( this.CreateHtml() ) ;
	},

	// Gets the HTML needed to create a Filemanager instance.
	CreateHtml : function()
	{
		var className = this.ClassName ;
		if ( className && className.length > 0 )
			className = ' class="' + className + '"' ;

		return '<iframe src="' + this._BuildUrl() + '" width="' + this.Width + '" ' +
			'height="' + this.Height + '"' + className + ' frameborder="0" scrolling="no"></iframe>' ;
	},

	// Opens Filemanager in a popup. The "width" and "height" parameters accept
	// numbers (pixels) or percent (of screen size) values.
	Popup : function( width, height )
	{
		width = width || '80%' ;
		height = height || '70%' ;

		if ( typeof width == 'string' && width.length > 1 && width.substr( width.length - 1, 1 ) == '%' )
			width = parseInt( window.screen.width * parseInt( width ) / 100 ) ;

		if ( typeof height == 'string' && height.length > 1 && height.substr( height.length - 1, 1 ) == '%' )
			height = parseInt( window.screen.height * parseInt( height ) / 100 ) ;

		if ( width < 200 )
			width = 200 ;

		if ( height < 200 )
			height = 200 ;

		var top = parseInt( ( window.screen.height - height ) / 2 ) ;
		var left = parseInt( ( window.screen.width  - width ) / 2 ) ;

		var options = 'location=no,menubar=no,toolbar=no,dependent=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable=yes' +
			',width='  + width +
			',height=' + height +
			',top='  + top +
			',left=' + left ;

		var popupWindow = window.open( '', 'FilemanagerPopup', options, true ) ;

		// Blocked by a popup blocker.
		if ( !popupWindow )
			return false ;

		try
		{
			popupWindow.moveTo( left, top ) ;
			popupWindow.resizeTo( width, height ) ;
			popupWindow.focus() ;
			popupWindow.location.href = this._BuildUrl() ;
		}
		catch (e)
		{
			popupWindow = window.open( this._BuildUrl(), 'FilemanagerPopup', options, true ) ;
		}

		return true ;
	},

	_BuildUrl : function()
	{
		var url = this.BasePath ;

		if ( !url || url.length == 0 )
			url = Filemanager.DEFAULT_BASEPATH ;

		if ( url.substr( url.length - 1, 1 ) != '/' )
			url = url + '/' ;

		url += 'filemanager/browser.html?Connector=connectors/php/connector.php' ;

		if ( this.SelectFunction )
		{
			var functionName = this.SelectFunction ;
			if ( typeof functionName == 'function' )
				functionName = functionName.toString().match( /function ([^(]+)/ )[1] ;

			url += '?'+ this.FileType;
		}

		return url ;
	}

} ;

// Static "Create".
Filemanager.Create = function( basePath, width, height, selectFunction , type)
{
	var Fmng = new Filemanager( basePath, width, height, selectFunction, type) ;
	Fmng.Create() ;
}

// Static "Popup".
Filemanager.Popup = function( basePath, width, height, selectFunction, type)
{
	var Fmng = new Filemanager( basePath, null, null, selectFunction, type) ;
	Fmng.Popup( width, height ) ;
}

