// +---------------------------------------------------------------------------+
// | TinyMCE Plugin for Geeklog - The Ultimate Weblog                          |
// +---------------------------------------------------------------------------+
// | public_html/tinymce/js/tinymce_loader.js.php                              |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2010 mystral-kk - geeklog AT mystral-kk DOT net             |
// +---------------------------------------------------------------------------+
// |                                                                           |
// | This program is free software; you can redistribute it and/or             |
// | modify it under the terms of the GNU General Public License               |
// | as published by the Free Software Foundation; either version 2            |
// | of the License, or (at your option) any later version.                    |
// |                                                                           |
// | This program is distributed in the hope that it will be useful,           |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
// | GNU General Public License for more details.                              |
// |                                                                           |
// | You should have received a copy of the GNU General Public License         |
// | along with this program; if not, write to the Free Software Foundation,   |
// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
// |                                                                           |
// +---------------------------------------------------------------------------+

var gl_tinymce = {};

// Properties
gl_tinymce.baseUrl = "http://gltmatrix.com";

gl_tinymce.mode = "auto";

gl_tinymce.targetClassName = "tinymce_enabled";

gl_tinymce.target_ids = ["introtext", "bodytext", "html_content"];

gl_tinymce.style_formats = [
	{title : 'Bold text', inline : 'b'},
	{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
	{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
	{title : 'Table styles'},
	{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}	// Don't put a comma in the last line
];


// Methods
/**
* Returns if the target HTML element has a given class
*/
gl_tinymce.hasClass = function(target, className) {
	var pattern = new RegExp("(^| )" + className + "( |$)");
	
	return pattern.test(target.className);
};

/**
* Adds a given class to the target HTML element
*/
gl_tinymce.addClass = function(targetElement, className) {
	if (!this.hasClass(targetElement, className)) {
		if (targetElement.className == "") {
			targetElement.className = className;
		} else {
			targetElement.className += " " + className;
		}
	}
};

/**
* Returns an HTML element with a given name
*/
gl_tinymce.getElementByName = function(tagType, name) {
	var elms,
		elm,
		i;
	
	if (document.getElementsByTagName) {
		elms = document.getElementsByTagName(tagType.toUpperCase());
	} else {
		elms = document.all;
	}
	
	for (i = 0; i < elms.length; i ++) {
		elm = elms[i];
		
		if (elm.name === name) {
			return elm;
		}
	}
	
	return null;
};

/**
* Attaches an event listener to an object
*/
gl_tinymce.addEventListener = function(target, type, listener) {
	if (target.addEventListener) {
		target.addEventListener(type, listener, false);
	} else if (target.attachEvent) {
		target.attachEvent("on" + type, function() { listener.call(target, window.event); });
	}
};

/**
* Lets TinyMCE show all its instances
*/
gl_tinymce.showEditors = function() {
	var that = gl_tinymce;
	
	if (tinymce.editors.length === 0) {
		// Still no instance of TinyMCE 
		that.loadTinyMCE();
	} else {
		// Already has TinyMCE instance(s)
		for (i = 0; i< tinymce.editors.length; i ++) {
			tinymce.editors[i].show();
		}
	}
};

/**
* Lets TinyMCE hide all its instances
*/
gl_tinymce.hideEditors = function() {
	for (i = 0; i< tinymce.editors.length; i ++) {
		tinymce.editors[i].hide();
	}
};



/**
* Handler for the event in which "postmode" dropdown list in story/comment/calendar
* editor has been changed.
*/
gl_tinymce.onEditorChangeCommon = function(e) {
	var that = gl_tinymce,
		value;
	
	if (e.target) {
		value = e.target.value;
	} else {
		value = e.srcElement.value;
	}
	
	if (value === "html") {
		that.showEditors();
	} else {
		that.hideEditors();
	}
};

/**
* Handler for the event in which "PHP" dropdown list in static page editor has
* been changed.
*/
gl_tinymce.onBlockEditorChange = function(e) {
	var that = gl_tinymce,
		value;
	
	if (e.target) {
		value = e.target.value;
	} else {
		value = e.srcElement.value;
	}
	
	if (value === "normal") {
		that.showEditors();
	} else {
		that.hideEditors();
	}
};

/**
* Attaches an event listener when block editor is loaded
*/
gl_tinymce.attachListenerToBlockEditor = function() {
	var selector,
		target;
	
	selector = this.getElementByName("SELECT", "type");
	target = this.getElementByName("TEXTAREA", "content");
	
	if (selector && target) {
		this.addClass(target, this.targetClassName);
		this.addEventListener(selector, "change", this.onBlockEditorChange);
		if (selector.value === "normal") {
			this.loadTinyMCE();
		}
	}
};

/**
* Handler for the event in which "html" checkbox in mail editor has been
* changed.
*/
gl_tinymce.onMailEditorChange = function(e) {
	var that = gl_tinymce,
		value;
	
	if (e.target) {
		value = e.target.checked;
	} else {
		value = e.srcElement.checked;
	}
	
	if (value === true) {
		that.showEditors();
	} else {
		that.hideEditors();
	}
};

/**
* Attaches an event listener when mail editor is loaded
*/
gl_tinymce.attachListenerToMailEditor = function() {
	var selector,
		area,
		tx;
	
	selector = this.getElementByName("input", "html");
	area = this.getElementByName("TEXTAREA", "message");
	
	if (selector && area) {
		this.addClass(area, this.targetClassName);
		this.addEventListener(selector, "change", this.onMailEditorChange);
		if (document.all) {
			// Workaround for IE, since it doesn't fire "onchange" event as
			// soon as the state of a checkbox is changed
			this.addEventListener(selector, "click", function(e) {e.srcElement.blur();e.srcElement.focus();});
		}
		
		if (selector.check === true) {
			this.loadTinyMCE();
		}
	}
};

/**
* Attaches an event listener when calendar editor is loaded
*/
gl_tinymce.attachListenerToCalendarEditor = function() {
	var selector,
		area,
		tx;
	
	selector = this.getElementByName("SELECT", "postmode");
	area = this.getElementByName("TEXTAREA", "description");
	
	if (selector && area) {
		this.addClass(area, this.targetClassName);
		this.addEventListener(selector, "change", this.onEditorChangeCommon);
		
		if (selector.value === "html") {
			this.loadTinyMCE();
		}
	}
};

/**
* Attaches an event listener when comment editor is loaded
*/
gl_tinymce.attachListenerToCommentEditor = function() {
	var selector,
		area,
		tx;
	
	selector = this.getElementByName("SELECT", "postmode");
	area = this.getElementByName("TEXTAREA", "comment");
	
	if (selector && area) {
		this.addClass(area, this.targetClassName);
		this.addEventListener(selector, "change", this.onEditorChangeCommon);
		
		if (selector.value === "html") {
			this.loadTinyMCE();
		}
	}
};

/**
* Attaches an event listener when story editor is loaded
*/
gl_tinymce.attachListenerToStoryEditor = function() {
	var selector,
		area1,
		area2;
	
	selector = this.getElementByName("SELECT", "postmode");
	area1 = this.getElementByName("TEXTAREA", "introtext");
	area2 = this.getElementByName("TEXTAREA", "bodytext");
	
	if (selector && area1 && area2) {
		this.addClass(area1, this.targetClassName);
		this.addClass(area2, this.targetClassName);
		this.addEventListener(selector, "change", this.onEditorChangeCommon);
		
		if (selector.value === "html") {
			this.loadTinyMCE();
		}
	}
};

/**
* Handler for the event in which "PHP" dropdown list in static page editor has
* been changed.
*/
gl_tinymce.onSPEditorChange = function(e) {
	var that = gl_tinymce,
		value;
	
	if (e.target) {
		value = e.target.value;
	} else {
		value = e.srcElement.value;
	}
	
	if (parseInt(value) === 0) {
		// PHP will not be executed
		that.showEditors();
	} else {
		// PHP will be executed
		that.hideEditors();
	}
};

/**
* Attaches an event listener when static page editor is loaded
*/
gl_tinymce.attachListenerToSP = function() {
	var selector,
		target;
	
	selector = this.getElementByName("SELECT", "sp_php");
	target = this.getElementByName("TEXTAREA", "sp_content");
	
	if (target) {
		this.addClass(target, this.targetClassName);
		
		if (selector) {
			this.addEventListener(selector, "change", this.onSPEditorChange);
			
			if (parseInt(selector.value) === 0) {
				this.loadTinyMCE();
			}
		} else {
			this.loadTinyMCE();
		}
	}
};

/**
*  Returns default configuration
*/
gl_tinymce.getDefaultConfig = function() {
	var d = {};
	
	// General options
	d.mode = "specific_textareas";
	
	if (d.mode === "specific_textareas") {
		d.editor_selector = "tinymce_enabled";
	}

	d.document_base_url = this.baseUrl + "/";
	d.relative_urls =  false;
	d.element_format = "html";
	
	if (0 === 1) {
		d.force_br_newlines = true;
		d.forced_root_block = "";
	} else {
		d.force_p_newlines = true;
	}
	
	d.theme ="advanced";
	d.plugins = "advlink,advlist,autoresize,autosave,iespell,paste,searchreplace,style,syntaxhl,visualchars,contextmenu,inlinepopups,xhtmlxtras";

	// Theme options
	d.theme_advanced_buttons1 = "bold,italic,|,formatselect";
	d.theme_advanced_buttons2 = "cut,copy,paste,|,bullist,numlist,|,blockquote,|,undo,redo,|,link,unlink,cleanup,code,|";
	d.theme_advanced_buttons3 = "|,hr,visualaid,|,|";
	d.theme_advanced_buttons4 = "styleprops,|,visualchars,restoredraft,syntaxhl";
	d.theme_advanced_toolbar_location = "top";
	d.theme_advanced_toolbar_align =  "left";
	d.theme_advanced_statusbar_location = "bottom";
	d.theme_advanced_resizing = true;
	d.file_browser_callback = "tinyBrowser";
	d.height = "360";
	d.width = "90%";

	// Locale
	d.language = "ja";
	d.directionality = "ltr";

	// Your site CSS
	d.content_css = this.baseUrl + "/tinymce/js/editor.css";

	// Drop lists for link/image/media/template dialogs
//	d.template_external_list_url = "lists/template_list.js";
	d.template_templates = [{title: "Sample", src: "http://gltmatrix.com/tinymce/templates/sample.html", description: "Sample event form"}];
//	d.external_link_list_url = "lists/link_list.js";
//	d.external_image_list_url = "lists/image_list.js";
//	d.media_external_list_url = "lists/media_list.js";

	// Style formats
	d.style_formats = this.style_formats;

	// Replace values for the template plugin
	// See http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/template
//	d.template_cdate_classes = "cdate creationdate";
//	d.template_mdate_classes = "mdate modifieddate";
//	d.template_selected_content_classes = "selcontent";
//	d.template_cdate_format = "%m/%d/%Y : %H:%M:%S";
//	d.template_mdate_format = "%m/%d/%Y : %H:%M:%S";
//	d.template_replace_values = {
//			username : "Some User",
//			staffid : "991234"
//	};
	
//? d.template_replace_values = {
//????????? username : "Some User",
//????????? staffid : "991234"
//? };
d.extended_valid_elements = "textarea[cols|rows|disabled|name|readonly|class]";
return d;
};

/**
* Loads TinyMCE
*/
gl_tinymce.loadTinyMCE = function(configData) {
	configData = configData || this.getDefaultConfig();
	tinyMCE.init(configData);
}

/**
* Initializes TinyMCE
*/
gl_tinymce.init = function() {
	var that = gl_tinymce,
		loc = window.location.href,
		elemId,
		i,
		conf;
	
	if (loc.indexOf("admin/plugins/tinymce/index.php") >= 0) {
		conf = that.getDefaultConfig();
		conf.mode = "css_id";
		that.loadTinyMCE(conf);
		tinyMCE.execCommand("mceAddControl", true, "content");
	} else if (that.mode === "auto") {
		if (loc.indexOf("admin/story.php") >= 0) {
			that.attachListenerToStoryEditor();
		} else if (loc.indexOf("admin/plugins/staticpages") >= 0) {
			that.attachListenerToSP();
		} else if (loc.indexOf("comment.php") >= 0) {
			that.attachListenerToCommentEditor();
		} else if (loc.indexOf("admin/plugins/calendar") >= 0) {
			that.attachListenerToCalendarEditor();
		} else if (loc.indexOf("admin/mail.php") >= 0) {
			that.attachListenerToMailEditor();
		} else if (loc.indexOf("admin/block.php") >= 0) {
			that.attachListenerToBlockEditor();
		} else {
			that.loadTinyMCE();
		}
	} else if (that.mode === "css_id") {
		// Converts textareas with given CSS ids
		for (i = 0; i < that.target_ids.length; i ++) {
			elemId = that.target_ids[i];
			
			if (document.getElementById(elemId)) {
				that.loadTinyMCE();
				tinyMCE.execCommand("mceAddControl", true, elemId);
			}
		}
	} else {
		that.loadTinyMCE();
	}
};

// Sets window onload event handler
gl_tinymce.addEventListener(window, "load", gl_tinymce.init);

