/*!
 * Toolbar to format text based on jQuery + jQuery UI + Color Picker
 *
 * Thanks to http://technology.hostei.com/?p=3
 *
 * Date: Monday 06/12/10
 */
$.fn.insertAtCaret = function(myValue){
	return this.each(function(){
		if (document.selection) {
			this.focus();
			sel = document.selection.createRange();
			sel.text = myValue;
			this.focus();
		}
		else if (this.selectionStart || this.selectionStart == '0') {
			var startPos = this.selectionStart;
			var endPos = this.selectionEnd;
			var scrollTop = this.scrollTop;
			this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
			this.focus();
			this.selectionStart = startPos + myValue.length;
			this.selectionEnd = startPos + myValue.length;
			this.scrollTop = scrollTop;
		} else {
			this.value += myValue;
			this.focus();
		}
	});
};
$.fn.insertRoundCaret = function (strStart, strEnd) {
	return this.each(function(){
		if (document.selection) {
			stringBefore = this.value;
			this.focus();
			sel = document.selection.createRange();
			insertstring = sel.text;
			fullinsertstring = strStart + sel.text + strEnd;
			sel.text = fullinsertstring;
			document.selection.empty();
			this.focus();
			stringAfter = this.value;
			i = stringAfter.lastIndexOf(fullinsertstring);
			range = this.createTextRange();
			numlines = stringBefore.substring(0,i).split("\n").length;
			i = i+3-numlines+tagName.length;
			j = insertstring.length;
			range.move("character",i);
			range.moveEnd("character",j);
			range.select();
		} else if (this.selectionStart || this.selectionStart == '0') {
			startPos = this.selectionStart;
			endPos = this.selectionEnd;
			scrollTop = this.scrollTop;
			this.value = this.value.substring(0, startPos) + strStart + this.value.substring(startPos,endPos) + strEnd + this.value.substring(endPos,this.value.length);
			this.focus();
			this.selectionStart = startPos + strStart.length ;
			this.selectionEnd = endPos + strStart.length;
			this.scrollTop = scrollTop;
		} else {
			this.value += strStart + strEnd;
			this.focus();
		}
	});
};
$.fn.isVisible = function(){
        if ($(this).css("visibility") == "hidden" || $(this).css("display") == "none") return false;
        else return true;
};
$(document).ready(function() {
	$("button#bold").click(function(){$('#textbody').insertRoundCaret('[b]', '[/b]')});
	$("button#italic").click(function(){$('#textbody').insertRoundCaret('[i]', '[/i]')});
	$("button#underline").click(function(){$('#textbody').insertRoundCaret('[u]', '[/u]')});
	$("button#info").click(function() { jAlert('Puoi usare questa toolbar per formattare il testo:<br><b>Bold</b>, <u>Underline</u>, <i>Italic</i><br>Puoi inoltre scegliere la dimensione del Font.<br>La sintassi dei link č: [a=Indirizzo]Nome Visualizzato[/a]<br>Nota: il codice HTML verrā elaboratco come semplice testo!', 'Info'); });
	$("button#link").click(function() { jPrompt('Indirizzo del link:<br>es. http:\\www.google.com', '', 'Inserisci i dati', function(l) { if(l) jPrompt('Nome visualizzato:<br>es. Google', '', 'Inserisci i dati', function(n) { $('#textbody').insertRoundCaret('[a='+l+']',n+'[/a]') }); }); });
	$("select#fontsize").change(function() { size = $('#fontsize option:selected').val(); if (size) { $('#textbody').insertRoundCaret('[s='+size+']', '[/s]'); $("select#fontsize").val("Font-Size") } });
	$("#ColorSelector").ColorPicker({
		onSubmit: function(hsb, hex, rgb, el) {	$(el).val(hex);	$(el).css("color","#"+hex); $('#textbody').insertRoundCaret('[c='+hex+']', '[/c]'); $(el).ColorPickerHide(); },
		onBeforeShow: function () { $(this).ColorPickerSetColor(this.value); }
	}).bind('keyup', function(){ $(this).ColorPickerSetColor(this.value); });
	$("#BackgroundSelector").ColorPicker({
		onSubmit: function(hsb, hex, rgb, el) {	$(el).val(hex);	$(el).css("background-color","#"+hex); $('#textbody').insertRoundCaret('[bc='+hex+']', '[/bc]'); $(el).ColorPickerHide(); },
		onBeforeShow: function () { $(this).ColorPickerSetColor(this.value); }
	}).bind('keyup', function(){ $(this).ColorPickerSetColor(this.value); });
	/*$("#format").buttonset();*/
	$("button#smiles").click(function() {
		if ($("div#smiles").isVisible() == true) $("div#smiles").hide("highlight");
		else $("div#smiles").show("highlight");
	});
	$("div#smiles").hide();
});
