// JS QuickTags version 1.3.1
//
// Copyright (c) 2002-2008 Alex King
// http://alexking.org/projects/js-quicktags
//
// Thanks to Greg Heo <greg@node79.com> for his changes 
// to support multiple toolbars per page.
//
// Licensed under the LGPL license
// http://www.gnu.org/copyleft/lesser.html
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
// **********************************************************************
//
// This JavaScript will insert the tags below at the cursor position in IE and 
// Gecko-based browsers (Mozilla, Camino, Firefox, Netscape). For browsers that 
// do not support inserting at the cursor position (older versions of Safari, 
// OmniWeb) it appends the tags to the end of the content.
//
// Pass the ID of the <textarea> element to the edToolbar and function.
//
// Example:
//
//  <script type="text/javascript">edToolbar('canvas');</script>
//  <textarea id="canvas" rows="20" cols="50"></textarea>
//

var edButtons = new Array();
var extButtons = new Array();
var edOpenTags = new Array();

function edButton(id, display, tagStart, tagEnd, access, open) {
	this.id = id;				// used to name the toolbar button
	this.display = display;		// label on button
	this.tagStart = tagStart; 	// open tag
	this.tagEnd = tagEnd;		// close tag
	this.access = access;			// set to -1 if tag does not need to be closed
	this.open = open;			// set to -1 if tag does not need to be closed
}

edButtons.push(
	new edButton(
		'ed_bold'
		,'bold'
		,'<strong>'
		,'</strong>'
		,'b'
	)
);

edButtons.push(
	new edButton(
		'ed_italic'
		,'italic'
		,'<em>'
		,'</em>'
		,'i'
	)
);

edButtons.push(
	new edButton(
		'ed_u'
		,'underline'
		,'<u>'
		,'</u>'
	)
);

edButtons.push(
	new edButton(
		'ed_del'
		,'delete'
		,'<del>'
		,'</del>'
	)
);

edButtons.push(
	new edButton(
		'ed_block'
		,'quote'
		,'<blockquote>'
		,'</blockquote>'
		,'q'
	)
);

extButtons.push(
	new edButton(
		'ed_image'
		,'image'
		,''
		,''
	)
);

extButtons.push(
	new edButton(
		'ed_video'
		,'video'
		,''
		,''
	)
);

function jail_edShowButton(which, button, i) {
	if (button.access) {
		var accesskey = ' accesskey = "' + button.access + '"'
	}
	else {
		var accesskey = '';
	}
	return '<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="ed_button" onclick="edInsertTag(\'' + which + '\', ' + i + ');" value="' + button.display + '"  />';
}

function jail_extShowButton(which, button, i, entity_id, entity_type) {
	return '<input type="button" id="' + button.id + '_' + which + '" class="ed_button" onclick="edExtAction(\'' + which + '\',\'' + button.id + '\',\'' + entity_id + '\',\'' + entity_type + '\');" value="' + button.display + '"  />';
}

function in_array(needle, haystack, strict) {
    var found = false, key, strict = !!strict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}

function edExtAction(which, action, entity_id, entity_type) {
	//alert('which: ' + which + ', action: ' + action + ', id: ' + entity_id + ', type: ' + entity_type);
	if (action == 'ed_image') {
		uploadpopup(which, entity_id, action, entity_type);
	} else {
		videopopup(which, entity_id, action, entity_type);
	}
}

function edShowButton(which, button, i) {
	document.write(jail_edShowButton(which, button, i));
}

function edAddTag(which, button) {
	if (edButtons[button].tagEnd != '') {
		edOpenTags[which][edOpenTags[which].length] = button;
		document.getElementById(edButtons[button].id + '_' + which).value = '/' + document.getElementById(edButtons[button].id + '_' + which).value;
	}
}

function edRemoveTag(which, button) {
	for (i = 0; i < edOpenTags[which].length; i++) {
		if (edOpenTags[which][i] == button) {
			edOpenTags[which].splice(i, 1);
			document.getElementById(edButtons[button].id + '_' + which).value = document.getElementById(edButtons[button].id + '_' + which).value.replace('/', '');
		}
	}
}

function edCheckOpenTags(which, button) {
	var tag = 0;
	for (i = 0; i < edOpenTags[which].length; i++) {
		if (edOpenTags[which][i] == button) {
			tag++;
		}
	}
	if (tag > 0) {
		return true; // tag found
	}
	else {
		return false; // tag not found
	}
}	

function jail_edToolbar(id, edit) {
	var result;
	result = '<div id="ed_toolbar_' + id + '"><span>';
	for (i = 0; i < edButtons.length; i++) {
		result += jail_edShowButton(edit, edButtons[i], i);
	}
	result += '</span>';
	result += '</div>';
	return result;
}

function jail_extToolbar(id, edit, entity_id, entity_type) {
	var result;
	result = '<div id="ed_toolbar_' + id + '"><span>';
	for (i = 0; i < edButtons.length; i++) {
		result += jail_edShowButton(edit, edButtons[i], i);
	}
	for (i = 0; i < extButtons.length; i++) {
		result += jail_extShowButton(edit, extButtons[i], i, entity_id, entity_type);
	}
	result += '</span>';
	result += '</div>';
	return result;
}

function insert_edToolbar(where, id, edit) {
	div = document.getElementById(where);
	div.innerHTML = jail_edToolbar(id, edit);
	edOpenTags[edit] = new Array();
}

function insert_extToolbar(where, id, edit, entity_id, entity_type) {
	div = document.getElementById(where);
	div.innerHTML = jail_extToolbar(id, edit, entity_id, entity_type);
	edOpenTags[edit] = new Array();
}

function edToolbar(id, edit) {
	document.write(jail_edToolbar(id, edit));
	edOpenTags[edit] = new Array();
}

function extToolbar(id, edit, entity_id, entity_type) {
	document.write(jail_extToolbar(id, edit, entity_id, entity_type));
	edOpenTags[edit] = new Array();
}

// insertion code

function edInsertTag(which, i) {
    myField = document.getElementById(which);
	//IE support
	if (document.selection) {
		myField.focus();
	    sel = document.selection.createRange();
		if (sel.text.length > 0) {
			sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd;
		}
		else {
			if (!edCheckOpenTags(which, i) || edButtons[i].tagEnd == '') {
				sel.text = edButtons[i].tagStart;
				edAddTag(which, i);
			}
			else {
				sel.text = edButtons[i].tagEnd;
				edRemoveTag(which, i);
			}
		}
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var cursorPos = endPos;
		var scrollTop = myField.scrollTop;
		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos)
			              + edButtons[i].tagStart
			              + myField.value.substring(startPos, endPos) 
			              + edButtons[i].tagEnd
			              + myField.value.substring(endPos, myField.value.length);
			cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
		}
		else {
			if (!edCheckOpenTags(which, i) || edButtons[i].tagEnd == '') {
				myField.value = myField.value.substring(0, startPos) 
				              + edButtons[i].tagStart
				              + myField.value.substring(endPos, myField.value.length);
				edAddTag(which, i);
				cursorPos = startPos + edButtons[i].tagStart.length;
			}
			else {
				myField.value = myField.value.substring(0, startPos) 
				              + edButtons[i].tagEnd
				              + myField.value.substring(endPos, myField.value.length);
				edRemoveTag(which, i);
				cursorPos = startPos + edButtons[i].tagEnd.length;
			}
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
		myField.scrollTop = scrollTop;
	}
	else {
		if (!edCheckOpenTags(which, i) || edButtons[i].tagEnd == '') {
			myField.value += edButtons[i].tagStart;
			edAddTag(which, i);
		}
		else {
			myField.value += edButtons[i].tagEnd;
			edRemoveTag(which, i);
		}
		myField.focus();
	}
}

function edInsertContent(which, myValue) {
    myField = document.getElementById(which);
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
	} else {
		myField.value += myValue;
		myField.focus();
	}
}

function edAddExtTag(which, file_tag, type)
{
	edInsertContent(which, '<' + type + '=' + file_tag + ' />');
}



