/**
 * [PHPFOX_HEADER]
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author			Raymond Benc
 * @package 		Phpfox
 * @version 		$Id: editor.js 1299 2009-12-06 18:06:19Z Raymond_Benc $
 */

if (typeof(oEditor) == 'undefined')
{
	debug('oEditor not defined.');
}

var bAllowEditor = true;
if (oEditor['wysiwyg'] === false)
{
	bAllowEditor = false;	
}
/* ----- >>>>> PhpSNS WYSIWYG Editor */
        if ( getParam('bPhpsnsWysiwyg') == 'true' )
        {
            bAllowEditor = true;
        }
        /* <<<<< ----- PhpSNS WYSIWYG Editor */


if (bAllowEditor)
{
	if (getParam('bWysiwyg'))
	{
		switch(getParam('sEditor'))
		{
			case 'fckeditor':
				document.write('<script type="text/javascript" src="' + getParam('sJsStatic') + 'jscript/wysiwyg/fckeditor/phpfox.js?v=' + getParam('sVersion') + '"></script>');
				break;
			case 'tinymce':
				document.write('<script type="text/javascript" src="' + getParam('sJsStatic') + 'jscript/wysiwyg/tinymce/tiny_mce.js?v=' + getParam('sVersion') + '"></script>');
				document.write('<script type="text/javascript" src="' + getParam('sJsStatic') + 'jscript/wysiwyg/tinymce/init.php?v=' + getParam('sVersion') + '&load=' + oEditor['load'] + (oEditor['toggle'] ? '&amp;toggle=true' : '') + '"></script>');				
				document.write('<script type="text/javascript" src="' + getParam('sJsStatic') + 'jscript/wysiwyg/tinymce/phpfox.js?v=' + getParam('sVersion') + '"></script>');
				document.write('<script type="text/javascript" src="' + getParam('sJsStatic') + 'jscript/wysiwyg/default/phpfox.js?v=' + getParam('sVersion') + '"></script>');
				break;
				/* ----- >>>>> PhpSNS WYSIWYG Editor */
        case 'phpsns':
                if ( getParam('bPhpsnsWysiwyg') == 'true' ) { 
                    document.write('<link rel="stylesheet" type="text/css" href="' + getParam('sJsHome') + 'module/wysiwyg/include/component/view/style.css" />');
                    document.write('<script type="text/javascript" src="' + getParam('sJsHome') + 'module/wysiwyg/include/component/module/scripts/innovaeditor.js"></script>');
                    document.write('<script type="text/javascript" src="' + getParam('sJsHome') + 'module/wysiwyg/include/component/js/phpsns_wysiwyg.js"></script>');
                    break;
                }
        /* <<<<< ----- PhpSNS WYSIWYG Editor */

			default:
				document.write('<script type="text/javascript" src="' + getParam('sJsStatic') + 'jscript/wysiwyg/default/phpfox.js?v=' + getParam('sVersion') + '"></script>');
		}
	}
	else
	{
		document.write('<script type="text/javascript" src="' + getParam('sJsStatic') + 'jscript/wysiwyg/default/phpfox.js?v=' + getParam('sVersion') + '"></script>');
	}
}
else
{
	document.write('<script type="text/javascript" src="' + getParam('sJsStatic') + 'jscript/wysiwyg/default/phpfox.js?v=' + getParam('sVersion') + '"></script>');
}

var Editor = 
{
	sVersion: '1.0',
	
	sEditorId: 'text',
	
	sImagePath: getParam('sJsStaticImage') + 'editor/',
	
	sEditor: getParam('sEditor'),
	
	aEditors: new Array(),
	
	setId: function(sId)
	{
		this.sEditorId = sId;	
		
		this.aEditors[sId] = true;
		
		return this;
	},
	
	getId: function()
	{
		return this.sEditorId;
	},
	
	getEditors: function()
	{
		for (sEditor in this.aEditors)
		{
			this.sEditorId = sEditor;
			this.getEditor();
		}
	},
	
	fullScreen: function(sEditorId)
	{
		tb_show(oTranslations['core.full_screen_editor'], '#?TB_inline=true&type=textarea&parent_id=' + sEditorId);		
		
		return false;
	},
	
	getEditor: function(bReturn)
	{
		var sHtml;		
		
		if (this.sEditor == 'tinymce' && typeof(tinyMCE) == 'object' && tinyMCE.getInstanceById(this.sEditorId) == null)
		{
			this.sEditor = 'default';
		}
		
		if (!bAllowEditor)
		{
			this.sEditor = 'default';
		}

		sHtml = '';	

		sHtml += '<div style="float:right;"><a href="#" onclick="return Editor.fullScreen(\'' + this.sEditorId + '\');" class="editor_button"><img class="editor_button" src="' + this.sImagePath + 'fullscreen.png" alt="" title="' + oTranslations['core.toggle_fullscreen'] + '" /></a></div>';		
		sHtml += this.getBBCode('text_bold.png', 'b', getPhrase('core.bold'));
		sHtml += this.getBBCode('text_italic.png', 'i', getPhrase('core.italic'));
		sHtml += this.getBBCode('text_underline.png', 'u', getPhrase('core.underline'));
		
		if (isModule('emoticon'))
		{
			sHtml += this.getSeparator();		
			sHtml += '<a href="#" class="editor_button" onclick="tb_show(\'' + getPhrase('emoticon.emoticons').replace("'", "\\'") + '\', $.ajaxBox(\'emoticon.preview\', \'height=300&width=300&editor_id=' + this.getId() + '\')); return false;"><img class="editor_button" src="' + this.sImagePath + 'emoticon.png" alt="' + getPhrase('emoticon.emoticons') + '" title="' + getPhrase('emoticon.emoticons') + '" /></a>';
		}

		sHtml += this.getSeparator();
		sHtml += '<a href="#" class="editor_button" onclick="return Editor.promptUrl(\'' + this.sEditorId + '\');">' + this.getImage('link_add.png', oTranslations['core.add_a_link']) + '</a>';
		
		sHtml += this.getSeparator();
		sHtml += '<a href="#" class="editor_button" onclick="return Editor.promptImg(\'' + this.sEditorId + '\');">' + this.getImage('photo.png', oTranslations['core.add_a_photo']) + '</a>';		
		
		if (isModule('attachment') && typeof(Attachment) != 'undefined')
		{
			sHtml += '<a href="#" class="editor_button" onclick="return Editor.getAttachments(\'' + this.sEditorId + '\');"><img class="editor_button" src="' + this.sImagePath + 'attach.png" alt="' + getPhrase('attachment.attach_files') + '" title="' + getPhrase('attachment.attach_files') + '" /></a>';			
		}
		
		if (isModule('video'))
		{
			sHtml += '<a href="#" class="editor_button" onclick="tb_show(\'' + oTranslations['core.add_a_video'].replace("'", "\\'") + '\', $.ajaxBox(\'video.add\', \'height=300&width=600&editor_id=' + this.getId() + '\')); return false;"><img class="editor_button" src="' + this.sImagePath + 'video.png" alt="' + oTranslations['core.add_a_video'] + '" title="' + oTranslations['core.add_a_video'] + '" /></a>';						
		}
		
		if ((bAllowEditor || oEditor['toggle']) && getParam('bWysiwyg'))
		{
			sHtml += this.getSeparator();			
			sHtml += '<a href="#" class="editor_button" onclick="return Editor.toggle(\'' + this.sEditorId + '\');"><img class="editor_button" src="' + this.sImagePath + 'toggle.gif" alt="' + getPhrase('core.toggle') + '" title="' + getPhrase('core.toggle') + '" id="editor_toggle" /></a>';
		}
		
		sHtml += '<div class="clear"></div>';
		
		if (bReturn)
		{
			return sHtml;
		}

		$('#js_editor_menu_' + this.getId()).html(sHtml);
		$('#js_editor_menu_' + this.getId()).show();
		$('#editor_toggle').blur();

		return true;
	},
	
	getAttachments: function(sEditorId)
	{
		tb_show('' + getPhrase('attachment.attach_files') + '', $.ajaxBox('attachment.add', 'height=500&width=600&category_id=' + Attachment['sCategory'] + '&attachments=' + $('#js_attachment').val() + '&item_id=' + Attachment['iItemId'] + '&editor_id=' + sEditorId));
		
		return false;
	},
	
	promptUrl: function(sEditorId)
	{
		tb_show('', $.ajaxBox('core.prompt', 'height=200&width=300&type=url&editor=' + sEditorId));
		
		return false;
	},
	
	promptImg: function(sEditorId)
	{
		tb_show('', $.ajaxBox('core.prompt', 'height=200&width=300&type=img&editor=' + sEditorId));

		return false;
	},	
	
	toggle: function(sEditorId) 
	{
		if (tinyMCE.getInstanceById(sEditorId) == null)
		{
			this.sEditor = 'tinymce';
			if (oEditor['toggle'])
			{
				customTinyMCE_init(sEditorId);
			}
			tinyMCE.execCommand('mceAddControl', false, sEditorId);
			$('#js_editor_menu_' + sEditorId).hide();
			debug('Enabled WYSIWYG text editor');
			deleteCookie('editor_wysiwyg');
		}
		else 
		{			
			// sEditorId = 'default';
			tinyMCE.execCommand('mceRemoveControl', false, sEditorId);
			if (oEditor['toggle'])
			{
				$('#layer_text').html('<div id="layer_text"><textarea name="val[text]" rows="12" cols="50" class="w_95" id="text">' + tinyMCE.activeEditor.getContent() + '</textarea></div>');				
			}
			debug('Disabled WYSIWYG text editor');			
			setCookie('editor_wysiwyg', true, 360);
			if ($('#js_editor_menu_' + sEditorId).html() != '')
			{
				$('#js_editor_menu_' + sEditorId).show();
				$('#editor_toggle').blur();
				return false;
			}

			this.getEditor();
		}
		
		return false;
	},
	
	getSeparator: function()
	{
		return '<div class="editor_separator"></div>';
	},
	
	getBBCode: function(sName, sCode, sTitle)
	{
		return '<a href="#" class="editor_button" onclick="return Editor.createBBtag(\'[' + sCode + ']\', \'[/' + sCode + ']\', \'' + this.sEditorId + '\');">' + this.getImage(sName, sTitle) + '</a>';
	},
	
	getImage: function(sName, sTitle)
	{
		return '<img class="editor_button" src="' + this.sImagePath + sName + '" alt="' + sTitle + '" title="' + sTitle + '" />';
	},
	
	getContent: function()
	{		
		eval('var sContent = ' + this.sEditor + '_wysiwyg_getContent();');
	
		return sContent;
	},
	
	insert: function(mValue)
	{	
		eval(this.sEditor + '_wysiwyg_insert(mValue);');
		
		return true;
	},
	
	remove: function(mValue)
	{	
		eval(this.sEditor + '_wysiwyg_remove(mValue);');
		
		return true;
	},
	
	createBBtag: function( openerTag , closerTag , areaId, sEmptyValue ) 
	{
		if(bIsIE && bIsWin) 
		{
			this.createBBtag_IE( openerTag , closerTag , areaId, sEmptyValue );
		}
		else 
		{
			this.createBBtag_nav( openerTag , closerTag , areaId, sEmptyValue );
		}
		
		return false;
	},
	
	createBBtag_IE: function( openerTag , closerTag , areaId, sEmptyValue ) 
	{
		var txtArea = document.getElementById( areaId );
		var aSelection = document.selection.createRange().text;
		var range = txtArea.createTextRange();
	
		if(aSelection) 
		{
			document.selection.createRange().text = openerTag + aSelection + closerTag;
			txtArea.focus();
			range.move('textedit');
			range.select();
		}
		else 
		{
			if (!empty(sEmptyValue))
			{
				openerTag = openerTag + sEmptyValue;
			}			
			
			var oldStringLength = range.text.length + openerTag.length;
			txtArea.value += openerTag + closerTag;
			txtArea.focus();
			range.move('character',oldStringLength);
			range.collapse(false);
			range.select();
		}
		return;
	},
	
	createBBtag_nav: function( openerTag , closerTag , areaId, sEmptyValue ) 
	{
		var txtArea = document.getElementById( areaId );
		if (txtArea.selectionEnd && (txtArea.selectionEnd - txtArea.selectionStart > 0) ) 
		{
			var preString = (txtArea.value).substring(0,txtArea.selectionStart);
			var newString = openerTag + (txtArea.value).substring(txtArea.selectionStart,txtArea.selectionEnd) + closerTag;
			var postString = (txtArea.value).substring(txtArea.selectionEnd);
			txtArea.value = preString + newString + postString;
			txtArea.focus();
		}
		else 
		{
			if (!empty(sEmptyValue))
			{
				openerTag = openerTag + sEmptyValue;
			}
			
			var offset = txtArea.selectionStart;
			var preString = (txtArea.value).substring(0,offset);
			var newString = openerTag + closerTag;
			var postString = (txtArea.value).substring(offset);
			txtArea.value = preString + newString + postString;
			txtArea.selectionStart = offset + openerTag.length;
			txtArea.selectionEnd = offset + openerTag.length;
			txtArea.focus();
		}
		return;
	}	
};

if (!bAllowEditor)
{
	var bForceDefaultEditor = true;
}