// JavaScript Document
(function($) {
 
	$.fn.extend({
	  "insertAtCaret": function(myValue){
		  var obj;
		  if ( typeof this[0].name !='undefined' ) 
			obj = this[0];
		  else 
			obj = this;
		  if ($.browser.msie) {
			obj.focus();
			sel = document.selection.createRange();
			if (document.selection.type == "None")
				sel.text = myValue;
			else if (myValue.substring(0,1) != "<")
				sel.text = sel.text + myValue;
			else { 
				sel.text = myValue.substring(0,3) + sel.text + myValue.substring(3);
			}
			obj.focus();
		  } else if ($.browser.mozilla || $.browser.webkit || $.browser.opera) {
			var startPos = obj.selectionStart;
			var endPos = obj.selectionEnd;
			var scrollTop = obj.scrollTop;
			if (startPos == endPos) {
				obj.value = obj.value.substring(0, startPos)+myValue+obj.value.substring(endPos,obj.value.length);
				obj.focus();
				obj.selectionStart = startPos + myValue.length;
				obj.selectionEnd = startPos + myValue.length;
			} else if (myValue.substring(0,1) != "<") {
				obj.value = obj.value.substring(0, startPos) + obj.value.substring(startPos,endPos) + myValue + obj.value.substring(endPos,obj.value.length);
				obj.focus();
				obj.selectionStart = startPos + myValue.length + (endPos - startPos);
				obj.selectionEnd = startPos + myValue.length + (endPos - startPos);
			} else {
				obj.value = obj.value.substring(0, startPos)+myValue.substring(0,3) + obj.value.substring(startPos,endPos) + myValue.substring(3)+obj.value.substring(endPos,obj.value.length);
				obj.focus();
				obj.selectionStart = startPos + myValue.length + (endPos - startPos);
				obj.selectionEnd = startPos + myValue.length + (endPos - startPos);
			}
			obj.scrollTop = scrollTop;
		  } else {
			obj.value += myValue;
			obj.focus();
		  }
	  }
	});

    var plugin = Echo.createPlugin({
        "name": "InsertCharacters",
        "applications": ["Submit"],
        "init": function(plugin, application) {
            plugin.extendTemplate("Submit", plugin.insertTemplate, "insertBefore", "echo-submit-content");
            plugin.extendRenderer("Submit", "text", plugin.textRenderer);
        }
    });
 
	var icons = [
				 	["Bold","http://c0029117.cdn1.cloudfiles.rackspacecloud.com/bold.gif","&lt;b&gt;&lt;/b&gt;"],
				 	["Italic","http://c0029117.cdn1.cloudfiles.rackspacecloud.com/italic.gif","&lt;i&gt;&lt;/i&gt;"],
				 	["Underline","http://c0029117.cdn1.cloudfiles.rackspacecloud.com/underline.gif","&lt;u&gt;&lt;/u&gt;"],
					["Smile","http://c0.echoenabled.com/images/smileys/emoticon_smile.png"," :-) "],
					["Wink","http://c0.echoenabled.com/images/smileys/emoticon_wink.png"," ;-) "],
					["Surprised","http://c0.echoenabled.com/images/smileys/emoticon_surprised.png"," =-O "],
					["Evil grin","http://c0.echoenabled.com/images/smileys/emoticon_evilgrin.png"," B-) "],
					["Laughing","http://c0.echoenabled.com/images/smileys/emoticon_grin.png"," :-D "],
					["Happy","http://c0.echoenabled.com/images/smileys/emoticon_happy.png"," =) "],
					["Tongue out","http://c0.echoenabled.com/images/smileys/emoticon_tongue.png"," :-P "],
					["Frown","http://c0.echoenabled.com/images/smileys/emoticon_unhappy.png"," :-( "]
				];

	// HTML which will be inserted to the template to display the icons
    plugin.insertTemplate = function() {
		var iconStr = '<div class="echo-submit-iconContainer">';
		for (var i = 0; i < icons.length; i++) {
			iconStr += "<img src=\"" + icons[i][1] + "\" title=\"" + icons[i][0] + "\" alt=\"" + icons[i][2] + "\"> ";
		}
		iconStr += '</div>';
		return iconStr; 
	};
 
    // to extend existing text renderer of the Submit application
    plugin.textRenderer = function(element, dom) {
        var application = this;
        // we should call existing renderer so as not to break things
        application.parentRenderer("text", arguments);
		$(element)
			.parents(".echo-submit-container")
			.find(".echo-submit-iconContainer")
			.children("img")
			.click(function(event) {
				var eTarget = $(event.target)
				.parents(".echo-submit-container")
				.find(".echo-submit-text");
				$(eTarget).focus()
				.insertAtCaret($(this).attr("alt"));
				$(eTarget).trigger("keyup");
		});
    };
 
})(jQuery);

