var b_tag = 0;
var i_tag = 0;
var u_tag = 0;
var s_tag = 0;
var sup_tag = 0;
var sub_tag = 0;
var quote_tag = 0;
var font_tag = 0;
var size_tag = 0;
var color_tag = 0;
var xtext_height = 0;
var align_tag = 0;
var typeurl = "Enter the URL for your link location:";
var typeemail = "Enter your email address:";
var typeimg = "Enter the location of your image:";
var typeline = "Enter a % for your <hr> tag (25 to 100):";
var typeprogress = "Enter a % for your progress bar (1 to 100):";
var youtube = 0;

function xtext_chars(maxx)
{
	var xtext = get_by_id('xtext');
	var maxxx = get_by_id('xtext_max');
	maxxx.innerHTML = maxx - xtext.value.length;
}

// optimize these ;o





function enlarge(text_element, start_height)
{
	var obj = get_by_id(text_element);
	xtext_height++;
	do_height(obj, start_height);
}
function shorten(text_element, start_height)
{
	var obj = get_by_id(text_element);
	xtext_height--;
	do_height(obj, start_height);
}
function do_height(obj, start_height)
{
	if (xtext_height == 0)
		var change = start_height;
	else if (xtext_height < -1)
		var change = start_height - 75;
	else
		var change = start_height + xtext_height * 75;
	obj.style.height = change + "px";	
}





function ytube(obj)
{
	var id;
	var ypos;
	id = prompt("Enter the entire Link or the video-id below.");


	if (id.length > 11) {
	ypos = id.indexOf("=");
	ypos++;
	xpos = ypos+11;
	id = id.substring(ypos,xpos);
	} else if (id.length < 11) {
	id = "INVALID"; 
	}


	topen = "[youtube=" + id + "]";
	complex_insert(topen, "0", "0");

}



function tag(obj, tag)
{
	topen = "[" + tag + "]";
	tclose = "[/" + tag + "]";
	tstatus = eval(tag + "_tag");
	rstatus = complex_insert(topen, tclose, tstatus);
	if (rstatus != null)
		eval(tag + "_tag = " + rstatus);

	if (rstatus == 0)
	{
		obj.value = tag;
		eval("obj.className = 'bbcode'");
	}
	else if (rstatus == 1)
	{
		obj.value = '/' + tag;
		eval("obj.className = 'bbcode_open'");
	}
}

function font(obj, tag)
{
	topen =  "[" + tag + "=" + obj.value + "]";
	tclose = "[/" + tag + "]";
	tstatus = eval(tag + "_tag");
	rstatus = complex_insert(topen, tclose, tstatus);
	
	if (rstatus != null)
		eval(tag + "_tag = " + rstatus);
	
	obj_open = get_by_id('bbcode_' + tag);
	obj_close = get_by_id('bbcode_' + tag + '_close');
	obj_open.selectedIndex = 0;
	if (rstatus == 0)
	{
		hide(obj_close);
		show(obj_open);
	}
	else if (rstatus == 1)
	{
		hide(obj_open);
		show(obj_close);
	}
}

function vari(obj, tag)
{

	topen = "";
	tstatus = null;
	namelink = null;
	
	var address = prompt(eval("type" + tag), "");
	if (address != null && address != "")
	{
		if (tag == "url" || tag == "email")
		{
			namelink = prompt("Enter a title for your link (not required):", "");
		}
		
		var tclose = "[" + tag + "=" + address + "]";
		
		if (namelink != null && namelink != "")
		{
			tclose += namelink + "[/" + tag + "]";
		}
		
		complex_insert(topen, tclose, tstatus);
	}
	else
	{
		var obj = get_by_id('xtext');
		obj.focus();
	}
}

function special(obj)
{
	if(obj.value == 'collapse')
		topen = '[collapse=Collapse Box]';
	else if (obj.value == 'table')
		topen = '[table=width:100%|class:outline|cell-padding:4]\n[tr]\n[td=width:50%][/td]\n[td=width:50%][/td]\n[/tr][tr]\n[td=width:50%][/td]\n[td=width:50%][/td]\n[/tr]';
	else
		topen = '[' + obj.value + ']';
	tclose = '[/' + obj.value + ']';
	obj.selectedIndex = 0;
	tstatus = null;
	complex_insert(topen, tclose, tstatus, 1);
}

function trigger()
{
    var enterTRIGGER = prompt("Enter your trigger description (optional):", "");

    if (enterTRIGGER) {
        enterTRIGGER = "[DESCRIPTION]"+enterTRIGGER+"[/DESCRIPTION]\n";
    }

	var playervalue = "init";
	var theplayer = "";

	while ( (playervalue != "") && (playervalue != null) )
	{
		playervalue = prompt("Players: (enter one at a time -- cancel to end input):", "");
		if ( (playervalue != "") && (playervalue != null) )
		{
			theplayer = theplayer+"[*]"+playervalue+"\n";
		}
	}

    if (theplayer) {
        theplayer = "[PLAYERS]\n"+theplayer+"[/PLAYERS]\n";
    }

	var condvalue = "init";
	var thecond = "";

	while ( (condvalue != "") && (condvalue != null) )
	{
		condvalue = prompt("Trigger conditions:  (enter one at a time -- cancel to end input)", "");
		if ( (condvalue != "") && (condvalue != null) )
		{
			thecond = thecond+"[*]"+condvalue+"\n";
		}
	}

	var actionvalue = "init";
	var theaction = "";

	while ( (actionvalue != "") && (actionvalue != null) )
	{
		actionvalue = prompt("Trigger actions: (enter one at a time -- cancel to end input)", "");
		if ( (actionvalue != "") && (actionvalue != null) )
		{
			theaction = theaction+"[*]"+actionvalue+"\n";
		}
	}

	if ( ( theaction != "" ) && ( thecond != "" ) )
	{
		var tstatus = null;
		complex_insert("", "[TRIGGER]\n" + enterTRIGGER + "" + theplayer + "[CONDITIONS]\n" + thecond + "[/CONDITIONS]\n[ACTIONS]\n" + theaction + "[/ACTIONS]\n[/TRIGGER]\n", tstatus);
	}
}

function tag_list()
{
	var item = 1;
	var list = "";

	while ((item != "") && (item != null))
	{
		item = prompt("Enter a list item or cancel to finish.", "");
		if ( (item != "") && (item != null) )
		{
			list += "[*]" + item + "\n";
		}
	}
	
	if (list != "")
	{
		var topen = "";
		var tclose = "[LIST]\n" + list + "[/LIST]\n";
		var tstatus = null;
		complex_insert(topen, tclose, tstatus);
	}
}

function smilie(display)
{
	complex_insert("", " " + display + " ", null);
}
/* fixxy?  Ain't fucking with this now */
function complex_insert(topen, tclose, tstatus, tdoboth, tobj)
{
	if (tobj)
		var obj = tobj;
	else
		var obj = get_by_id('xtext');
	var xinput = "";
	var rvalue = false;
	var rstatus = null;
	if (obj.selectionStart == null)
	{
		obj.focus();
		var stext = document.selection;
		var range = stext.createRange();
		range.colapse;
		if(range.text.length > 0)
			xinput = topen + range.text + tclose;
		else
		{
			rvalue = true;

			if (tstatus == 1) xinput = tclose;
			else if (tstatus == 0) xinput = topen;
			else xinput = tclose;
		}
		range.text = xinput;
		range.select();
	}
	else if (obj.selectionStart != null)
	{
		scrollsave = obj.scrollTop;tlength = obj.textLength;selstart = obj.selectionStart;selend = obj.selectionEnd;
		tstart = '';tmiddle = '';tend = '';
		if (selstart == selend)
		{
			rvalue = true;
			if (tdoboth) tmiddle = topen + tclose;
			else if (tstatus == 1) tmiddle = tclose;
			else if (tstatus == 0) tmiddle = topen;
			else tmiddle = tclose;
		}
		else
		tmiddle = topen + (obj.value).substring(selstart, selend) + tclose;
		tstart = (obj.value).substring(0, selstart);
		tend = (obj.value).substring(selend, tlength);
		obj.value = tstart + tmiddle + tend;
		var position = selstart + (tmiddle.length);
		obj.selectionStart = position;
		obj.selectionEnd = position;
		obj.scrollTop = scrollsave;
	}	
	if (tstatus != null && rvalue == true)
	{
		if (tstatus == 1)
			rstatus = 0;
		else
			rstatus = 1;
	}
	obj.focus();
	return rstatus;
}
	