if( typeof at == "undefined" )
	at = {};
if( typeof at.treemotion == "undefined" )
	at.treemotion = {}
/**************************************************************/
/*              ADMIN                                         */
/**************************************************************/
at.treemotion.FFUtils = 
{
	instances: []
	,	selection: null
	,	launch: function( mode, id, options )
	{
		//mode = 'mceNews';
		tinyMCE.settings = at.treemotion.FFUtils.getSettings( mode );
		if( document.getElementsByTagName( 'base' ).length > 0 )
			tinyMCE.settings.document_base_url = document.getElementsByTagName( 'base' )[ 0 ].href;
		if( at.treemotion.CMS.backend && at.treemotion.CMSAdm.mceCSS != null ) 
			tinyMCE.settings.content_css = at.treemotion.CMSAdm.mceCSS;
		tinyMCE.execCommand("mceAddControl",false,id);
		var inst = tinyMCE.getInstanceById( id );
		//if( !inst ) return;
		this.instances.push( id );
		return;
		inst.controlManager.setDisabled( 'missRedakSave', false );
		inst.controlManager.setDisabled( 'missRedakSaveNewVersion', false );
		inst.controlManager.setDisabled( 'missRedakCancel', false );
		if (typeof options == "undefined") return;
		if( typeof options.nocontrol != "undefined" )
		{
			options.missRedakSave = false;
			options.missRedakSaveNewVersion = false;
			options.missRedakCancel = false;
		}
		if( typeof options.missRedakSave != "undefined" && !options.missRedakSave )
			inst.controlManager.setDisabled( 'missRedakSave', true );
		if( typeof options.missRedakSaveNewVersion != "undefined" && !options.missRedakSaveNewVersion )
			inst.controlManager.setDisabled( 'missRedakSaveNewVersion', true );
		if( typeof options.missRedakCancel != "undefined" && !options.missRedakCancel )
			inst.controlManager.setDisabled( 'missRedakCancel', true );
		
		if (typeof options.onLaunch != "undefined") {
			options.onLaunch(inst);
		}
	}
	,	clean: function( id )
	{
		
		tinyMCE.execCommand( "mceRemoveControl", false, id );
		for( var i = 0; i < this.instances.length; i++ )
		{
			if( this.instances[ i ] == id )
			{
				this.instances.splice( i, 1 );
			}
		}
	}
	,	settings: function( setting, value )
	{
		tinyMCE.settings[setting] = value;
	}
	,	cleans: function( id )
	{
		for( var i = 0; i < this.instances.length; i++ )
		{
			if( id == this.instances[ i ] || $( this.instances[ i ] ).descendantOf( $( id ) ) )
			{
				this.clean( this.instances[ i ] );
				i--;
			}
		}
	}
	,	commands: function(editor_id, elm, command, user_interface, value) 
	{
		var linkElm, imageElm, inst;
		switch (command) 
		{
			case "mceLink":
				inst = tinyMCE.getInstanceById(editor_id);
				linkElm = tinyMCE.getParentElement(inst.selection.getFocusElement(), "a");
				at.treemotion.CMSAdm.linkEditor( editor_id );
			return true;
			case "mceImage":
				inst = tinyMCE.getInstanceById(editor_id);
				imageElm = tinyMCE.getParentElement(inst.selection.getFocusElement(), "img");
				//console.log( filer );
			return true;
		}
		return false; // Pass to next handler in chain
	}
	,	setA: function( URL, target )
	{
		var inst = tinyMCE.activeEditor;
		var elm, elementArray, i;
		
		if( this.selection != null ) inst.selection.moveToBookmark( at.treemotion.FFUtils.selection );
		this.selection = null;
		
		elm = inst.selection.getNode();
		elm = inst.dom.getParent(elm, "A");
	
		// Remove element if there is no href
		if (!URL) {
			tinyMCE.execCommand("mceBeginUndoLevel");
			i = inst.selection.getBookmark();
			inst.dom.remove(elm, 1);
			inst.selection.moveToBookmark(i);
			tinyMCE.execCommand("mceEndUndoLevel");
			return;
		}
	
		tinyMCE.execCommand("mceBeginUndoLevel");
	
		// Create new anchor elements
		if (elm == null) {
			tinyMCE.execCommand("CreateLink", false, "#mce_temp_url#", {skip_undo : 1});
			elementArray = tinymce.grep(inst.dom.select("a"), function(n) {return inst.dom.getAttrib(n, 'href') == '#mce_temp_url#';});
			//console.log( elementArray );
			for (i=0; i<elementArray.length; i++)
			{
				inst.dom.setAttrib( elementArray[ i ], 'href', URL );
				inst.dom.setAttrib( elementArray[ i ], 'target', target );
			}
		} 
		else
		{
			inst.dom.setAttrib( elm, 'href', URL );
			inst.dom.setAttrib( elm, 'target', target );
		}
	
		// Don't move caret if selection was image
		if ( elm != null && ( elm.childNodes.length != 1 || elm.firstChild.nodeName != 'IMG' ) ) {
			inst.focus();
			inst.selection.select(elm);
			inst.selection.collapse(0);
		}
	
		tinyMCE.execCommand("mceEndUndoLevel");
	}
	,	insertImgFiler: function( f )
	{
		var ed = tinyMCE.activeEditor, el;
		if( this.selection != null ) ed.selection.moveToBookmark( at.treemotion.FFUtils.selection );
		this.selection = null;

		// Remove deprecated values
		args = {
			vspace : '',
			hspace : '',
			border : '',
			align : '',
			id: null
		};
		tinymce.extend(args, {
			src : 'media/' + f.name,
			width : '',/*f.widht,*/
			height : '' /*f.height*/
		});
		el = ed.selection.getNode();
		if (el && el.nodeName == 'IMG') {
			ed.dom.setAttribs(el, args);
		} else {
			ed.execCommand('mceInsertContent', false, '<img id="__mce_tmp" />', {skip_undo : 1});
			ed.dom.setAttribs('__mce_tmp', args);
			ed.undoManager.add();
		}
	}
	,	getSettings: function( mode )
	{
		switch( mode )
		{
			case 'mceMissRedak':
				return {
					mode : "none",
					theme: "advanced",
					plugins:"table,missRedak,paste,advlink",
					theme_advanced_toolbar_align : "left",
					theme_advanced_toolbar_location : "top",
					save_enablewhendirty : false,
					theme_advanced_buttons1 : "missRedakSave,missRedakSaveNewVersion,missRedakCancel,|," + 
		  				 "missRedakLink,missRedakUnlink,missRedakImage,|,missRedakPicasa,|,bold,italic,underline,separator" + 
		  				 ",bullist,numlist,|,charmap",
					theme_advanced_buttons2 : "pasteword,styleselect,formatselect,separator,cleanup,code",
					theme_advanced_buttons3 : "insertimage,insertpopup,tablecontrols,template,seperato",
					content_css: "",
					auto_resize: true
				};
			break;
			case 'mceNews':
				return {
					mode : "none",
					theme: "advanced",
					plugins:"table,missRedak,paste,advlink",
					theme_advanced_toolbar_align : "left",
					theme_advanced_toolbar_location : "top",
					save_enablewhendirty : false,
					theme_advanced_buttons1 : "missRedakLink,missRedakUnlink,missRedakImage,|,bold,italic,underline,separator" + 
		  				 ",bullist,numlist,|,charmap",
					theme_advanced_buttons2 : "pasteword,styleselect,formatselect,separator,cleanup,code",
					theme_advanced_buttons3 : "insertimage,insertpopup,tablecontrols,template,seperato",
					content_css: ""
				};
			break;
			case 'mceForum':
			default:
				return {
					mode : "none",
					theme : "simple",
					height: 150,
					extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|" + 
						"alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name]," + 
						"hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
					content_css: ""
				}
			break;
		}	
	}
}