// JavaScript Document
var edButtons = new Array();

function edButton(id, tagStart, tagEnd) {
	this.id = id;				// used to name the toolbar button
	this.tagStart = tagStart; 	// open tag
	this.tagEnd = tagEnd;		// close tag
}

edButtons.push(
	new edButton(
		'B'
		,'<strong>'
		,'</strong>'
	)
)

edButtons.push(
	new edButton(		
		'I'
		,'<em>'
		,'</em>'
	)
)

edButtons.push(
	new edButton(
		'U'
		,'<u>'
		,'</u>'
	)
)

edButtons.push(
	new edButton(
		'link'
		,'<a>'
		,'</a>'
	)
)

edButtons.push(
	new edButton(
		'code'		
		,'<div class="sfondo-code"><code>'
		,'</code></div>'
	)
)

edButtons.push(
	new edButton(		
		'quote'
		,'<div class="sfondo-citazione"><blockquote>'
		,'</blockquote></div>'		
	)
)

edButtons.push(
	new edButton(		
		'img'
		,'<img>'
		,''
	)
)

function insTag(myField,tag)
{	
	switch (tag) 
	{
		case 'B':			
			getFieldSelection(myField,0);
			break;
		case 'I':			
			getFieldSelection(myField,1);
			break;
		case 'U':
			getFieldSelection(myField,2);
			break;
		case 'link':
			edInsertLink(myField, 3, '');
			break;
		case 'img':
			edInsertImage(myField,6);
			break;
		case 'quote':
			getFieldSelection(myField,5);
			break;
		case 'code':
			getFieldSelection(myField,4);
			break;
	}
}

function getFieldSelection(myField,id) {		
	var word = '';
	if (document.selection) {		
		document.getElementById(myField).focus();
		
	    var sel = document.selection.createRange();
		if (sel.text.length > 0) {
			sel.text = edButtons[id].tagStart + sel.text + edButtons[id].tagEnd;
		}
		document.getElementById(myField).focus();		
	}
	else if (document.getElementById(myField).selectionStart || document.getElementById(myField).selectionStart == '0') 
	{		
		var startPos = document.getElementById(myField).selectionStart;
		var endPos = document.getElementById(myField).selectionEnd;
		var cursorPos = endPos;
		var scrollTop = document.getElementById(myField).scrollTop;
		if (startPos != endPos) {			
			document.getElementById(myField).value = document.getElementById(myField).value.substring(0, startPos)
			              + edButtons[id].tagStart
			              + document.getElementById(myField).value.substring(startPos, endPos) 
			              + edButtons[id].tagEnd
			              + document.getElementById(myField).value.substring(endPos, document.getElementById(myField).value.length);
			cursorPos += edButtons[id].tagStart.length + edButtons[id].tagEnd.length;				
		}
		document.getElementById(myField).focus();
		document.getElementById(myField).selectionStart = cursorPos;
		document.getElementById(myField).selectionEnd = cursorPos;
		document.getElementById(myField).scrollTop = scrollTop;
	}
	
}

function edInsertLink(myField, id, defaultValue) {
	if (!defaultValue) {
		defaultValue = 'http://';
	}
	
	var URL = prompt('Inserire URL' ,defaultValue);
	if (URL) 
	{
		var http = URL.substring(0,7);
		if (http!='http://') URL='http://' + URL;
		edButtons[id].tagStart = '<a href="' + URL + '" target="_blank" class="discussione">';
		getFieldSelection(myField,id)
	}	
}

function edInsertContent(myField, myValue) {
	//IE support	
	if (document.selection) {
		document.getElementById(myField).focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (document.getElementById(myField).selectionStart || document.getElementById(myField).selectionStart == '0') {
		var startPos = document.getElementById(myField).selectionStart;
		var endPos = document.getElementById(myField).selectionEnd;
		var scrollTop = document.getElementById(myField).scrollTop;
		document.getElementById(myField).value = document.getElementById(myField).value.substring(0, startPos)
		              + myValue 
                      + document.getElementById(myField).value.substring(endPos, document.getElementById(myField).value.length);
		document.getElementById(myField).focus();
		document.getElementById(myField).selectionStart = startPos + myValue.length;
		document.getElementById(myField).selectionEnd = startPos + myValue.length;
		document.getElementById(myField).scrollTop = scrollTop;
	} else {
		document.getElementById(myField).value += myValue;
		document.getElementById(myField).focus();
	}
}

function edInsertImage(myField,id) {
	var myValue = prompt('Inserire URL dell"immagine', 'http://');
	if (myValue) {
		var http = myValue.substring(0,7);
		if (http!='http://') myValue='http://' + myValue;
		myValue = '<img src="' 
				+ myValue 
				+ '" alt="' + prompt('Descrizione immagine', '') 
				+ '" />';
		edButtons[id].tagStart = myValue;
		edInsertContent(myField, myValue);
	}
}