﻿/*
**************************************************************
Forum Module Special
**************************************************************
This Mod is designed to work with GuildPortal's forum editor
This mod includes:
 Special Characters
by Rob G (Mottie)
v1.0 completed 8/2009
v2.1 updated 1/2010 - Updated for newer version of Telerik's Editor, set to v2.1 to match the other modules.
v2.2 updated 4/2010 - Updated to work with editor with different IDs
v2.3 updated 9/2011 - Updated to work with the new GP mod system
v2.4 updated 9/2011 - Converted into a plugin
**************************************************************
License: http://sam.zoy.org/wtfpl/
*/
;(function($){
$.forumModuleSpecial = 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,
		customCode  : []
	},
	o = $.extend({}, defaults, options),
	tmp, modicon, editmode, charCode = [];

	charCode.push(["Symbols"], ["Copyright","&#169;"], ["Registered trademark","&#174;"], ["Trademark","&#153;"]);
	charCode.push(["Currency"], ["Cent","&#162;"], ["Currency","&#164;"], ["US","&#36;"], ["Pound","&pound;"], ["Yen","&yen;"], ["Euro","&euro;"]);
	charCode.push(["Arrows"], ["Left arrow","&#8592;"], ["Up arrow","&#8593;"], ["Right arrow","&#8594;"], ["Down arrow","&#8595;"], ["Left-right arrow","&#8596;"], ["Up-down arrow","&#8597;"], ["Solid left arrow","&#9668;"], ["Solid up arrow","&#9650;"], ["Solid right arrow","&#9658;"], ["Solid down arrow","&#9660;"]);
	charCode.push(["Punctuation"], ["Less than","&lt;"], ["Greater than","&gt;"], ["Ampersand","&amp;"], ["Dagger","&#134;"], ["Double Dagger","&#135;"], ["Bullet, small open circle","&#9702;"], ["Bullet, small open square","&#9643;"], ["Bullet, large open circle","&#9675;"], ["Bullet, large open square","&#9633;"], ["Bullet, small circle","&#183;"], ["Bullet, large circle","&#9679;"], ["Bullet, small square","&#9642;"], ["Bullet, large square","&#9632;"], ["Bullet, inverse","&#9688;"], ["Bullet, white circle","&#9689;"], ["Quote","&quot;"], ["Left single quote","&#145;"], ["Right single quote","&#146;"], ["Left double quote","&#147;"], ["Right double quote","&#148;"], ["Single low quote","&#8218;"], ["Double low quote","&#8222;"], ["Left single angled quote","&#8249;"], ["Right single angled quote","&#8250;"], ["Double left angled quote","&#171;"], ["Double right angled quote","&#187;"], ["Paragraph","&#182;"]);
	charCode.push(["Math"], ["Minus","&minus;"], ["Times","&times;"], ["Divide","&#247;"], ["Radical","&#8730;"], ["Infinity","&#8734;"], ["Asterisk","&#42;"], ["Degree","&#176;"], ["Plus or minus","&#177;"], ["Not sign","&#172;"], ["Less than","&lt;"], ["Greater than","&gt;"], ["Less than or equal to","&le;"], ["Greater than or equal to","&ge;"], ["Increment","&#8710;"], ["Integral","&int;"], ["Almost equal to","&#8776;"], ["Not equal to","&#8800;"], ["Identical to","&#8801;"], ["Intersection","&#8745;"], ["Empty set","&#216;"], ["Function","&#402;"], ["Superscript 1","&#185;"], ["Superscript 2","&#178;"], ["Superscript 3","&#179;"], ["One fourth","&#188;"], ["One half","&#189;"], ["Three quarters","&#190;"], ["Per mil","&#8240;"], ["Partical differential","&#8706;"], ["Product","&#8719;"], ["Sum","&#8721;"]);
	charCode.push(["Misc"], ["Spades","&#9824;"], ["Clubs","&#9827;"], ["Hearts","&#9829;"], ["Diamonds","&#9830;"], ["Eighth note","&#9834;"], ["Beamed note","&#9835;"], ["Cross (West Syriac)","&#9840;"], ["Cross (East Syriac)","&#9841;"], ["Smiley ","&#9786;"], ["Smiley (solid)","&#9787;"], ["Sun (white)","&#9788;"], ["Female","&#9792;"], ["Male","&#9794;"], ["Left ceiling","&#8968;"], ["Right ceiling","&#8969;"], ["Left floor","&#8970;"], ["Right floor","&#8971;"], ["Lozenge","&#9674;"], ["Diaeresis","&#168;"], ["Acute accent","&#180;"], ["Spacing cedilla","&#184;"], ["Inverted exclamation point","&iexcl;"], ["Inverted question mark","&#191;"], ["Broken vertical bar","&#166;"], ["Dash","&#151;"], ["Macron","&#175;"], ["Section","&#167;"]);
	charCode.push(["Greek"], ["Alpha","&#945;"], ["Alpha","&#913;"], ["Beta","&#946;"], ["Beta","&#914;"], ["Gamma","&#947;"], ["Gamma","&#915;"], ["Delta","&#948;"], ["Delta","&#916;"], ["Epsilon","&#949;"], ["Epsilon","&#917;"], ["Zeta","&#950;"], ["Zeta","&#918;"], ["Eta","&#951;"], ["Eta","&#919;"], ["Theta","&#952;"], ["Theta","&#920;"], ["Iota","&#953;"], ["Iota","&#921;"], ["Kappa","&#954;"], ["Kappa","&#922;"], ["Lambda","&#955;"], ["Lambda","&#923;"], ["Mu","&#956;"], ["Mu","&#924;"], ["Nu","&#957;"], ["Nu","&#925;"], ["Xi","&#958;"], ["Xi","&#926;"], ["Omicron","&#959;"], ["Omicron","&#927;"], ["Pi","&#960;"], ["Pi","&#928;"], ["Rho","&#961;"], ["Rho","&#929;"], ["Final sigma","&#962;"], ["Sigma","&#963;"], ["Sigma","&#931;"], ["Tau","&#964;"], ["Tau","&#932;"], ["Upsilon","&#965;"], ["Upsilon","&#933;"], ["Phi","&#966;"], ["Phi","&#934;"], ["Chi","&#967;"], ["Chi","&#935;"], ["Psi","&#968;"], ["Psi","&#936;"], ["Omega","&#969;"], ["Omega","&#937;"]);
	charCode.push(["Latin"], ["ae ligature","&#230;"], ["a grave","&#224;"], ["a acute","&#225;"], ["a circumflex","&#226;"], ["a tilde","&#227;"], ["a diaeresis","&#228;"], ["a ring","&#229;"], ["c acute","&#263;"], ["c cedilla","&#231;"], ["e grave","&#232;"], ["e acute","&#233;"], ["e circumflex","&#234;"], ["e diaeresis","&#235;"], ["florin","&#402;"], ["i grave","&#236;"], ["i acute","&#237;"], ["i circumflex","&#238;"], ["i diaeresis","&#239;"], ["eth","&#240;"], ["n tilde","&#241;"], ["oe ligature","&#339;"], ["o grave","&#242;"], ["o acute","&#243;"], ["o circumflex","&#244;"], ["o tilde","&#245;"], ["o diaeresis","&#246;"], ["o slash","&#248;"], ["s caron","&#353;"], ["u grave","&#249;"], ["u acute","&#250;"], ["u circumflex","&#251;"], ["u diaeresis","&#252;"], ["y acute","&#253;"], ["y diaeresis","&#255;"], ["thorn","&#254;"]);
	charCode.push(["Latin (caps)"], ["AE ligature","&#198;"], ["A grave","&#192;"], ["A acute","&#193;"], ["A bar","&#256;"], ["A caron","&#258;"], ["A cedilla","&#260;"], ["A circumflex","&#194;"], ["A tilde","&#195;"], ["A diaeresis","&#196;"], ["A ring","&#197;"], ["C acute","&#262;"], ["C cedilla","&#199;"], ["E grave","&#200;"], ["E acute","&#201;"], ["E circumflex","&#202;"], ["E diaeresis","&#203;"], ["I grave","&#204;"], ["I acute","&#205;"], ["I circumflex","&#206;"], ["I diaeresis","&#207;"], ["ETH","&#208;"], ["N tilde","&#209;"], ["OE ligature","&#338;"], ["O grave","&#210;"], ["O acute","&#211;"], ["O circumflex","&#212;"], ["O tilde","&#213;"], ["O diaeresis","&#214;"], ["O slash","&#216;"], ["S caron","&#352;"], ["U grave","&#217;"], ["U acute","&#218;"], ["U circumflex","&#219;"], ["U diaeresis","&#220;"], ["Y acute","&#221;"], ["Y diaeresis","&#376;"], ["THORN","&#222;"]);

	// Only add if posting/editing the forums
	if ($('#customModules').html() !== null) {
		editmode = ($('#customModules').hasClass('richEditor')) ? true : false;
		// Add Special characters
		// ********************
		tmp = '<a unselectable="on" title="Insert Special Characters" class="reTool tooltip addSpecial" href="#" ' +
			'style="text-decoration: none;"><span unselectable="on"><img width="18" style="border:0" ' +
			'src="http://guildportal.com/Cargo/Mods/72/special.png"/></span></a>';
		modicon = (editmode) ? '<li>' + tmp + '</li>' : tmp + '&nbsp;';
		$("#customModules")
			.append(modicon)
			.find('.addSpecial')
			.click(function(){
				fxnAddSpecial();
				return false;
			});
	}

	// Make Special character Popup
	// *********************
	function fxnAddSpecial() {
		var j, tmp, tmp2, modpop = '<div class="enterCharCode">' +
		'<img style="vertical-align:middle" width="25" src="http://guildportal.com/Cargo/Mods/72/special.png"> ' +
		'<span class="title">Special Characters</span><span class="errBox"/><br><hr><div id="eTabBox"><div><div>';
		for (j = 0; j < charCode.length; j++){
			if (typeof(charCode[j][1])==="undefined") { // if symbol not defined, it must be a new header
				modpop += '</div></div>&nbsp;<div style="float:left;padding:3px 10px;cursor:pointer;border:#888 1px solid;" class="esctab">';
				modpop += '<span><strong>' + charCode[j][0] + '</strong></span><div class="esccontent" style="display:none">';
				j++;
			}
			modpop += addSpecialModuleAddItem(charCode[j][0], charCode[j][1]);
		}
		modpop += '</div></div></div><div style="clear:both;"/><div style="margin-top:10px;height:70px" id="escwin"></div><div style="clear:both;"/>';
		modpop += '<br><br><input class="scbutton close" type="button" value="Done">';
		jQuery.facebox(modpop);
	}

	function setupSpecialMod(){
		var j, tmp, tmp2,
			fb = $('#facebox');
		if (!fb.find('.enterCharCode').length) { return; }

		// add in custom codes after the facebox window pops up, it's easier than messing with the array
		if (typeof(o.customCode) !== "undefined"){
			for (j=0; j < o.customCode.length; j++){
				tmp = $('.esctab:contains("' + o.customCode[j][0] + '")').find('.esccontent');
				if (tmp.length < 1) { // if tab doesn't exist, make it!
					tmp2 = '<div style="float:left;padding:3px 10px;cursor:pointer;border:#888 1px solid;" class="esctab"><span><strong>' +
					o.customCode[j][0] + '</strong></span><div class="esccontent" style="display:none">' + addSpecialModuleAddItem(o.customCode[j][1],o.customCode[j][2]) +
					'</div></div>';
					$('#eTabBox').append(tmp2);
				} else { // tab exists, add the new code to the end
					tmp.append(addSpecialModuleAddItem(o.customCode[j][1],o.customCode[j][2]));
				}
			}
		}
		addSpecialModuleMakeTabs( $('.esctab:eq(0)') ); // display first tab

		fb
			.addClass('forum-mod')
			.css('z-index', 999) // one less than the tooltip
			.find('.scbutton.close').bind('click', function(){
				$(document).trigger('close.facebox');
			}).end()
			.undelegate('.esctab', 'click')
			.delegate('.esctab', 'click', function(){
				addSpecialModuleMakeTabs(this);
			});
		fb
			.undelegate('.addthis', 'click')
			.delegate('.addthis', 'click', function(){
				addSpecialModulePostChar( $(this).attr('data-name'), $(this).attr('alt') );
			});

	}

	function addSpecialModuleAddItem(a, b){
		var tmp1, tmp2, okCharCode = true;
		if (!/(^&#\d{2,5};$|^&[a-zA-Z]+\d{0,3};$)/.test(b)) { okCharCode = false; }
		tmp1 = (okCharCode) ? '300' : '110';  // 300% if special code, 110% if text
		if (/^<img([^>]*)>$/.test(b)) {
			okCharCode = true;
		} else {
			b = b.replace(/(\s)/g,'&nbsp;');
		}
		tmp2 = '<div class="tooltip addthis" rel="' + o.tooltipSize + '" data-name="' + a + '" title="<div align=center>' + a + '<br><span style=font-size:' +
			tmp1 + '%>' + b + '<span></div>" alt="' + b + '" style="cursor:pointer;float:left;text-align:center;height:25px;white-space:nowrap;';
		tmp2 += (okCharCode) ? 'font-size:130%;width:25px;">' + b : 'width:auto;color:#999;">[<span style="color:#ddd">' + a + '</span>]&nbsp;';
		tmp2 += '</div> ';
		return tmp2;
	}

	function addSpecialModulePostChar (cname, ccode) {
		var j, editor, tmp = '<span title="' + cname + '">' + ccode + '</span>';
		if ($('#customModules').hasClass('richEditor')) {
			editor = $('[id$=txtEditor_internalEditor]').attr('id');
			$find( editor ).pasteHtml(tmp);
		} else {
			j = jQuery("[id$=txtEditor_internalTextBox]");
			j.val ( j.val() + tmp );
		}
	}

	function addSpecialModuleMakeTabs(tmp){
		$('#escwin').html( $(tmp).find('.esccontent').html() );
		$('.esctab').css({'background-color':'#333','color':'#999'});
		$(tmp).css({'background-color':'#000','color':'#ddd'});
	}

	// Special Styling for facebox
	$(document)
		.bind('reveal.facebox', function(){
			setupSpecialMod();
		})
		.bind('afterClose.facebox', function() {
			$('#facebox').removeClass('forum-mod');
		});

};

})( jQuery );

