

//*****************************
//*****************************
function bbCodeStoreSelectionOffsets(taElement)
{
	if ($.browser.msie)
	{
		taElement.focus();
		var range = document.selection.createRange();
		var stored_range = range.duplicate();
		stored_range.moveToElementText(taElement);
		stored_range.setEndPoint( 'EndToEnd', range );
		taElement.taSelectionStart = stored_range.text.length - range.text.length;
		taElement.taSelectionEnd = taElement.taSelectionStart + range.text.length;
	}
	else
	{
		taElement.taSelectionStart = taElement.selectionStart;
		taElement.taSelectionEnd = taElement.selectionEnd;
	}
}
//*****************************
//*****************************
function bbCodeInsertEmoticon(thisElement, imageurl)
{
	var taElement = $(thisElement.parentNode.parentNode).next()[0];
	var tagBegin = imageurl;
	var tagEnd = '';	
	bbCodeInsertTags(taElement, tagBegin, tagEnd);
}
//*****************************
//*****************************
function bbCodeSetColor(thisElement, color)
{	
	var taElement = $(thisElement.parentNode.parentNode).next()[0];
	var tagBegin = '[color=' + color + ']';
	var tagEnd = '[/color]';
	bbCodeInsertTags(taElement, tagBegin, tagEnd);
}
//*****************************
//*****************************
function bbCodeSetSize(thisElement, size)
{
	var taElement = $(thisElement.parentNode.parentNode).next()[0];
	var tagBegin = '[size=' + size + ']';
	var tagEnd = '[/size]';
	bbCodeInsertTags(taElement, tagBegin, tagEnd);
}
//*****************************
//*****************************
function bbCodeInsertImage(thisElement)
{
	var taElement = $(thisElement.parentNode).next()[0];
	var imageURL = prompt("Please enter the URL of your image:", "http://");
	if(imageURL == null)
		return;
	var tagBegin = "[img]" + imageURL + "[/img]";
	var tagEnd = '';
	bbCodeInsertTags(taElement, tagBegin, tagEnd);
}
//*****************************
//*****************************
function bbCodeInsertThumbnail(thisElement)
{
	var taElement = $(thisElement.parentNode).next()[0];
	var imageURL = prompt("Please enter the URL of your image:", "http://");
	if(imageURL == null)
		return;
	var tagBegin = "[thumb]" + imageURL + "[/thumb]";
	var tagEnd = '';
	bbCodeInsertTags(taElement, tagBegin, tagEnd);
}
//*****************************
//*****************************
function bbCodeInsertLink(thisElement)
{
	var taElement = $(thisElement.parentNode).next()[0];
	var linkURL = prompt("Please enter the URL of your link:", "http://");
	if(linkURL == null)
		return;
	if (taElement.taSelectionStart == taElement.taSelectionEnd)
	{
		var tagBegin = "[url=" + linkURL + "]" + linkURL + "[/url]";
		var tagEnd = '';
	}
	else
	{
		var tagBegin = "[url=" + linkURL + "]";
		var tagEnd = "[/url]";
	}
	bbCodeInsertTags(taElement, tagBegin, tagEnd);
}


//*****************************
//*****************************
function bbCodeInsertEmail(thisElement)
{
	var taElement = $(thisElement.parentNode).next()[0];
	var imageURL = prompt("Please enter an email address:", "");
	if(imageURL == null)
		return;
	var tagBegin = "[email]" + imageURL + "[/email]";
	var tagEnd = '';
	bbCodeInsertTags(taElement, tagBegin, tagEnd);
}





//*****************************
//*****************************
function bbCodeSetBold(thisElement)
{
	var taElement = $(thisElement.parentNode).next()[0];
	var tagBegin = '[b]';
	var tagEnd = '[/b]';
	bbCodeInsertTags(taElement, tagBegin, tagEnd);
}
//*****************************
//*****************************
function bbCodeSetItalic(thisElement)
{
	var taElement = $(thisElement.parentNode).next()[0];
	var tagBegin = '[i]';
	var tagEnd = '[/i]';
	bbCodeInsertTags(taElement, tagBegin, tagEnd);
}
//*****************************
//*****************************
function bbCodeSetUnderline(thisElement)
{
	var taElement = $(thisElement.parentNode).next()[0];
	var tagBegin = '[u]';
	var tagEnd = '[/u]';
	bbCodeInsertTags(taElement, tagBegin, tagEnd);
}
//*****************************
//*****************************
function bbCodeInsertTags(taElement, tagBegin, tagEnd)
{
	var selectedText = taElement.value;
	var newTextMiddle = selectedText.substring(taElement.taSelectionStart, taElement.taSelectionEnd);
	var newTextBegin = selectedText.substring(0, taElement.taSelectionStart);
	var newTextEnd = selectedText.substring(taElement.taSelectionEnd);
	taElement.value = (newTextBegin + tagBegin + newTextMiddle + tagEnd + newTextEnd);
	taElement.taSelectionStart += tagBegin.length;
	taElement.taSelectionEnd += tagBegin.length;
	if (taElement.createTextRange)
	{
		taElement.focus();
		var range = taElement.createTextRange();
		var rangeLength = taElement.taSelectionEnd - range.text.length;
		range.moveStart("character", taElement.taSelectionStart);
		range.moveEnd("character", rangeLength);
		range.select();
	}
	else if (taElement.setSelectionRange)
	{
		taElement.setSelectionRange(taElement.taSelectionStart, taElement.taSelectionEnd);
	}
	taElement.focus();
}
//*****************************
//*****************************
function bbCodeQuote(id, idContent, idUsername)
{
	var taElement		= document.getElementById(id);
	var eContent		= document.getElementById(idContent);
	var eUsername		= document.getElementById(idUsername);	
	var temp = eUsername.innerHTML;
	temp = temp.replace(/\s/g, "");	
	$(taElement).append('[quote=' + temp + "]" + eContent.innerHTML + "[/quote]");

}
//*****************************
//*****************************
function bbcodeShowDropdown(thisElement, dropDownToShow)
{
	var contElement = thisElement.parentNode;
	//GET FIRST THREE CHILDREN
	var cNodes = contElement.childNodes;
	var nodeCount = 0;
	for (i = 0; i < cNodes.length; ++i)
	{
		if(cNodes[i].nodeType != 1)
			continue;
		if (dropDownToShow == nodeCount)
		{
			if (cNodes[i].style.display == 'block')
				cNodes[i].style.display = 'none';
			else
				cNodes[i].style.display = 'block';
		}
		else
		{
			cNodes[i].style.display = 'none';
		}
		nodeCount = nodeCount +1;
		if(nodeCount >= 3)
			break;
	}
	return;
}
//*****************************
//*****************************
function bbcodeHideDropdown(thisElement)
{
	parentNode = thisElement.parentNode;
	parentNode.style.display = 'none';
}
//*****************************
//*****************************
function showForm(formElement, focusElement, offset)
{
	$('#' + formElement).show();
	var totalOffset = $('#' + focusElement).offset().top + offset;
	$('html,body').animate({scrollTop:totalOffset}, 1000);
	$('#' + focusElement).focus();
}


$(document).ready(function()
{
	$('.textarea_bbcode').each(
	
		function()
		{			
			$(this).attr('taSelectionStart', '');
			$(this).attr('taSelectionEnd', '');					
			$(this).click(
				function()
				{
					bbCodeStoreSelectionOffsets(this);
				}
			);
			$(this).select(
				function()
				{
					bbCodeStoreSelectionOffsets(this);
				}
			);
			bbcodeInsertHTML(this);					
		}
	)
});


//*****************************
//*****************************
function bbcodeInsertHTML(e)
{
	//URL_IMAGE
	var html = '\n\
		<div style="height:30px; width:383px; position:relative; color:white;">\n\
			<div style="position:absolute; top:29px; left:0px; width:109px; padding:0px 0px 5px 5px; display:none; border:1px solid #555555; background-color:black;">\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \'8)\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/cool.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \':\\\'(\');"	class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/crying.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \':D\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/ecstatic.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \':[\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/embarrassed.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \':!\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/foot_mouth.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \':(\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/frowning.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \'O:)\');"	class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/innocent.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \':*\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/kissing.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \':X\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/lips_sealed.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \':$\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/money_mouth.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \':)\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/smiley.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \'=O\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/surprised.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \':P\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/tongue.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \':T\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/undecided.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \';)\');"		class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/winking.png" alt="" />\n\
				<img onclick="javascript:bbcodeHideDropdown(this); bbCodeInsertEmoticon(this, \'>:O\');"	class="bbcode_emoticon"	src="http://image.www.gametracker.com/emoticons/yelling.png" alt="" />\n\
			</div>\n\
			<div style="position:absolute; top:29px; left:29px; width:184px; padding:0px 0px 5px 5px; display:none; border:1px solid #555555; background-color:black;">\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'aqua\');"			class="bbcode_color" style="background-color:aqua;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'black\');"		class="bbcode_color" style="background-color:black;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'blue\');"			class="bbcode_color" style="background-color:blue;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'fuchsia\');"		class="bbcode_color" style="background-color:fuchsia;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'gray\');"			class="bbcode_color" style="background-color:gray;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'green\');"		class="bbcode_color" style="background-color:green;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'lime\');"			class="bbcode_color" style="background-color:lime;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'maroon\');"		class="bbcode_color" style="background-color:maroon;">&nbsp;</div>\n\
				<div class="item_float_clear">\n\
				</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'navy\');"			class="bbcode_color" style="background-color:navy;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'olive\');"		class="bbcode_color" style="background-color:olive;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'purple\');"		class="bbcode_color" style="background-color:purple;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'red\');"			class="bbcode_color" style="background-color:red;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'silver\');"		class="bbcode_color" style="background-color:silver;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'teal\');"			class="bbcode_color" style="background-color:teal;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'white\');"		class="bbcode_color" style="background-color:white;">&nbsp;</div>\n\
				<div onclick="javascript:bbcodeHideDropdown(this); bbCodeSetColor(this, \'yellow\');"		class="bbcode_color" style="background-color:yellow;">&nbsp;</div>\n\
				<div class="item_float_clear">\n\
				</div>\n\
			</div>\n\
			<div style="position:absolute; top:29px; left:53px; width:92px; padding:0px 0px 5px 5px; display:none; border:1px solid #555555; background-color:black;">\n\
				<span onclick="javascript:bbcodeHideDropdown(this); bbCodeSetSize(this, 8);"				style="font-size:8pt;">8</span>\n\
				<span onclick="javascript:bbcodeHideDropdown(this); bbCodeSetSize(this, 12);"				style="font-size:12pt;">12</span>\n\
				<span onclick="javascript:bbcodeHideDropdown(this); bbCodeSetSize(this, 16);"				style="font-size:16pt;">16</span>\n\
				<span onclick="javascript:bbcodeHideDropdown(this); bbCodeSetSize(this, 20);"				style="font-size:20pt;">20</span>\n\
				<br/>\n\
			</div>\n\
			<img onclick="javascript:bbcodeShowDropdown(this,0);"											style="display:block; position:absolute; cursor:pointer; top:0px; left:5px;"	src="http://image.www.gametracker.com/emoticons/smiley.png" alt="" />\n\
			<img onclick="javascript:bbcodeShowDropdown(this,1);"											style="display:block; position:absolute; cursor:pointer; top:5px; left:29px;"	src="/images/global/bbcode/btn_color.gif" alt="" />\n\
			<img onclick="javascript:bbcodeShowDropdown(this,2);"											style="display:block; position:absolute; cursor:pointer; top:5px; left:53px;"	src="/images/global/bbcode/btn_fontsize.gif" alt="" />\n\
			<img onclick="javascript:bbcodeShowDropdown(this,-1);	javascript:bbCodeSetBold(this);"		style="display:block; position:absolute; cursor:pointer; top:5px; left:77px;"	src="/images/global/bbcode/btn_bold.gif" alt="" />\n\
			<img onclick="javascript:bbcodeShowDropdown(this,-1); javascript:bbCodeSetItalic(this);"		style="display:block; position:absolute; cursor:pointer; top:5px; left:101px;"	src="/images/global/bbcode/btn_italic.gif" alt="" />\n\
			<img onclick="javascript:bbcodeShowDropdown(this,-1); javascript:bbCodeSetUnderline(this);"		style="display:block; position:absolute; cursor:pointer; top:5px; left:125px;"	src="/images/global/bbcode/btn_underline.gif" alt="" />\n\
			<img onclick="javascript:bbcodeShowDropdown(this,-1); javascript:bbCodeInsertImage(this);"		style="display:block; position:absolute; cursor:pointer; top:5px; left:149px;"	src="/images/global/bbcode/btn_image.gif" alt="" />\n\
			<img onclick="javascript:bbcodeShowDropdown(this,-1); javascript:bbCodeInsertThumbnail(this);"	style="display:block; position:absolute; cursor:pointer; top:5px; left:203px;"	src="/images/global/bbcode/btn_thumbnail.gif" alt="" />\n\
			<img onclick="javascript:bbcodeShowDropdown(this,-1); javascript:bbCodeInsertLink(this);"		style="display:block; position:absolute; cursor:pointer; top:5px; left:289px;"	src="/images/global/bbcode/btn_link.gif" alt="" />\n\
			<img onclick="javascript:bbcodeShowDropdown(this,-1); javascript:bbCodeInsertEmail(this);"		style="display:block; position:absolute; cursor:pointer; top:5px; left:332px;"	src="/images/global/bbcode/btn_email.gif" alt="" />\n';
	$(e).before(html);
}