﻿/*
**************************************************************
Forum Module Emotes
**************************************************************
This Mod is designed to work with GuildPortal's forum editor
This mod includes:
 Emoticons
by Rob G (Mottie)
v1.0 completed 4/2009
v2.0 updated 7/2009 - Updated to work with Telerik's Editor
v2.1 updated 1/2010 - Updated for newer version of Telerik's Editor
v2.2 updated 4/2010 - Updated to work with editor with different IDs
v2.3 updated 9/2011 - Updated to work with the new mod system
v2.4 updated 9/2011 - Converted into a plugin
**************************************************************
License: http://sam.zoy.org/wtfpl/
*/
;(function($){
$.forumModuleEmotes = function(options){

	// add module if editor window exists
	if ( !$('#bodyTransbackInterior').length ) { return; }

	// look for module box, add it if not there
	if (!$('#customModules').length) {
		// WYSIWYG Editor
		if ($('div.reToolbarWrapper').length) {
			$('div.reToolbarWrapper').append('<ul class="reToolbar Default"><li class="reGrip grip_first"></li>' +
				'<span id="customModules" class="richEditor"></span><li class="reGrip grip_last"></li>');
		} else {
			// Plain Text Editor
			$('div#editorContent table tr').prepend('<td id="customModules" class="txtEditor"></td>');
		}
	}

	var defaults = {
		tooltipSize : 75,
		emoteSize   : 20,
		emotes      : []
	},
	o = $.extend({}, defaults, options),
	tmp, modicon;

	// Only add if posting/editing the forums
	if ($('#customModules').html() !== null) {
		// Add Emoticons Icon
		// ********************
		tmp = '<a unselectable="on" title="Insert Custom Emotes" class="reTool tooltip addEmote" href="#" ' +
			'style="text-decoration: none;"><span unselectable="on"><img width="18" style="border:0" ' +
			'src="http://guildportal.com/Cargo/Mods/71/tongue.gif"/></span></a>';
		modicon = ($('#customModules').hasClass('richEditor')) ? '<li>' + tmp + '</li>' : tmp + '&nbsp;';
		$("#customModules")
			.append(modicon)
			.find('.addEmote')
			.click(function(){
				fxnModuleAddEmote();
				return false;
			});
	}

	// Make Emoticon Popup
	// *********************
	function fxnModuleAddEmote() {
		var j, ename, modpop = '<div class="enterEmote">' +
		'<img style="vertical-align:middle" width="25" src="http://guildportal.com/Cargo/Mods/71/tongue.gif"> ' +
		'<span class="title">Custom Emotes</span><span class="errBox"></span><br><hr>';
		for (j = 0; j < o.emotes.length; j++){
			ename = o.emotes[j][0].replace(/\'/g,"&rsquo;").replace(/\"/g,"&quot;");
			modpop += '<span class="tooltip modemote" title="<div align=center>' + ename + '<br><img src=';
			modpop += o.emotes[j][1] + '></div>" rel="' + o.tooltipSize + '"><img ';
			modpop += 'data-name="' + ename + '" style="padding:2px"';
			modpop += ' src="' + o.emotes[j][1] + '" style="border-width:0px;cursor:pointer;" ';
			modpop += 'width="' + o.emoteSize + '" height="' + o.emoteSize + '" /></span> ';
		}
		modpop += '<br><br><input class="scbutton close" type="button" value="Done""></div>';
		// open facebox
		$.facebox(modpop);
	}

	function setupEmoteMod(){
		var fb = $('#facebox');
		if (!fb.find('.enterEmote').length) { return; }
		fb
			.addClass('forum-mod')
			.css('z-index', 999) // one less than the tooltip
			.find('.modemote').click(function(){
				var j, editor,
					img = $(this).find('img'),
					nam = img.attr('data-name'),
					tmp = '<img alt="' + nam + '" class="tooltip" rel="100" title="' + nam + '" src="' + img.attr('src') + '">';
				if ($('#customModules').hasClass('richEditor')) {
					editor = $('[id$=txtEditor_internalEditor]').attr('id');
					$find( editor ).pasteHtml(tmp);
				} else {
					j = $("[id$=txtEditor_internalTextBox]");
					j.val ( j.val() + tmp );
				}
			}).end()
			.find('.scbutton.close').click(function(){
				$(document).trigger('close.facebox');
			});
	}

	// Special Styling for facebox
	$(document)
		.bind('reveal.facebox', function(){
			setupEmoteMod();
		})
		.bind('afterClose.facebox', function() {
			$('#facebox').removeClass('forum-mod');
		});

};

})( jQuery );
