// insertcode is used for bold, italic, underline and quote and just
// wraps the tags around a selection or prompts the user for some
// text to apply the tag to
var BBCode = function(){
	window.undefined = window.undefined;
	this.initDone = false;
}
BBCode.prototype.init = function(t){
	if(this.initDone) return false;
	if(t == undefined) return false;
	this._target = t ? document.getElementById(t) : t;
	this.initDone = true;
	this._preview = getObject("preview");
	return true;
}
BBCode.prototype.noForm = function(){
	return this._target == undefined;
}
BBCode.prototype.initPreview = function(p){
	this._preview = p ? document.getElementById(p) : p;
	return true;
}
// insertcode is used for bold, italic, underline and quote and just
// wraps the tags around a selection or prompts the user for some
// text to apply the tag to
BBCode.prototype.insertCode = function (tag, desc, endtag) {
	if(this.noForm()) return false;
	var isDesc = (desc == undefined || desc == '') ? false : true;
	// our textfield
	var textarea = this._target;
	// our open tag
	var open = '['+tag+']';
	var close = '[/'+((endtag == undefined) ? tag : endtag)+']';
	if (!textarea.setSelectionRange) {
		var selected = document.selection.createRange().text;
		if (selected.length<=0) {
			// no text was selected so prompt the user for some text
			var src = prompt("Skriv in texten som du vill "+desc, "");
			if(src != '')
				textarea.value += open+((isDesc) ? src+close : '');
		} else {
			// put the code around the selected text
			document.selection.createRange().text = open+selected+((isDesc) ? close : '');
		}
	} else {
		// the text before the selection
		var pretext = textarea.value.substring(0, textarea.selectionStart);
		// the selected text with tags before and after
		var codetext = open+textarea.value.substring(textarea.selectionStart, textarea.selectionEnd)+((isDesc) ? close : '');
		// the text after the selection
		var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
		// check if there was a selection
		if (codetext == open+close) {
		//prompt the user
		var src = prompt("Skriv in texten som du vill "+desc, "");
		if(src != '' && src != null)
			codetext = open+((isDesc) ? src+close : '');
		}
		// update the text field
		if (codetext != open+close)
			textarea.value = pretext+codetext+posttext;
	}
	// set the focus on the text field
	textarea.focus();
}
// inserts an image by prompting the user for the url
BBCode.prototype.insertImage = function (html) {
	if(this.noForm()) return false;
	var src = prompt('Skriv in adressen till bilden', 'http://');
	if(src != '' && src != null)
		this.insertCode('img]'+src+'[/img');
}

// inserts a link by prompting the user for a url
BBCode.prototype.insertLink = function (html) {
	if(this.noForm()) return false;
	var src = prompt("Skriv in adressen", "http://");
	var sr = prompt("Skriv in texten som skall visas.", "");
	if(src != '' && src != null)
	{
		if(sr == '' || sr == null)
			sr = src;

		this.insertCode('url='+src+']'+sr+'[/url')
	}
}

// inserts a race by prompting the user for a id
BBCode.prototype.insertRace = function (html) {
	if(this.noForm()) return false;
	var sr = prompt("Skriv in lopp-id.", "");
	if(sr != '' && sr != null)
		this.insertCode('data_race]'+sr+'[/data_race')
}

// inserts a horse by prompting the user for a id
BBCode.prototype.insertHorse = function (html) {
	if(this.noForm()) return false;
	var sr = prompt("Skriv in häst-id.", "");
	if(sr != '' && sr != null)
		this.insertCode('data_horse]'+sr+'[/data_horse')
}

// inserts a horse full by prompting the user for a id
BBCode.prototype.insertHorseFull = function (html) {
	if(this.noForm()) return false;
	var sr = prompt("Skriv in häst-id.", "");
	if(sr != '' && sr != null)
		this.insertCode('data_horse_full]'+sr+'[/data_horse_full')
}
// inserts a driver by prompting the user for a id
BBCode.prototype.insertDriver = function (html) {
	if(this.noForm()) return false;
	var sr = prompt("Skriv in kusk-id.", "");
	if(sr != '' && sr != null)
		this.insertCode('data_driver]'+sr+'[/data_driver')
}

function insertQuote(target, source)
{
	target.value = target.value + '[quote]' +source.innerHTML + '[/quote]';
}
var link = '';


BBCode.prototype.updatePreview = function(){
	var preview = this._preview;

	var source = this._target;
	var text = source.value;
	text = text.replace(/<*>/g, "");
	text = text.replace(/\[quote\](.*?)\[\/quote\]/g, "<div class=forum_quote style=border: 1px solid #928d86; width=100% class=text><img src='.$link.'img/quote.gif><br />$1</div>");
	text = text.replace(/\[b\](.*?)\[\/b\]/g, "<b>$1</b>");
	text = text.replace(/\[i\](.*?)\[\/i\]/g, "<i>$1</i>");
	text = text.replace(/\[u\](.*?)\[\/u\]/g, "<u>$1</u>");
	text = text.replace(/\[s\](.*?)\[\/s\]/g, "<strike>$1</strike>");
	text = text.replace(/\[url=(\W?)(.*?)(\W?)\](.*?)\[\/url\]/g, "<a href=\"$2\" class=\"link\" target=\"_blank\">$4</a>");
	text = text.replace(/\[img\](.*?)\[\/img\]/g, "<img style=\"max-width:100%;\" src=\"$1\">");
	text = text.replace(/\\\"/g, "&quot;");
	text = text.replace(/\\\'/g, "&acute;");
	text = text.replace(/\[color=(\W?)(.*?)(\W?)\](.*?)\[\/color\]/g, "<span class=\"$2\">$4</span>");
	text = text.replace(/\[youtube\]http:\/\/www\.youtube\.com\/watch\?v\=(\w+)\[\/youtube\]/g, "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/$1&hl=en&fs=1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"http://www.youtube.com/v/$1&hl=en&fs=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object>");

	text = insertSmilies(text);
	text = text.replace( new RegExp( "\\n", "g" ), "<br />" );
	preview.innerHTML = text;

	return true;
}

function SetLink(text)
{
	link = text;
}

function insertSmilies(text)
{
	text = text.replace(/\:D/g, "<img src=\"" + link + "img/smiley/veryhappy.gif\">");
	text = text.replace(/\:\-D/g, "<img src=\"" + link + "img/smiley/veryhappy.gif\">");
	text = text.replace(/\:\-\)/g, "<img src=\"" + link + "img/smiley/happy.gif\">");
	text = text.replace(/\:\)/g, "<img src=\"" + link + "img/smiley/happy.gif\">");
	text = text.replace(/\;D/g, "<img src=\"" + link + "img/smiley/winking.gif\">");
	text = text.replace(/\;\)/g, "<img src=\"" + link + "img/smiley/winking.gif\">");
	text = text.replace(/\;D/g, "<img src=\"" + link + "img/smiley/winking.gif\">");
	text = text.replace(/\:\-S/g, "<img src=\"" + link + "img/smiley/confused.gif\">");
	text = text.replace(/\:S/g, "<img src=\"" + link + "img/smiley/confused.gif\">");
	text = text.replace(/\:\:o/g, "<img src=\"" + link + "img/smiley/party.gif\">");
	text = text.replace(/\:\@/g, "<img src=\"" + link + "img/smiley/mad.gif\">");
	text = text.replace(/\:\-\@/g, "<img src=\"" + link + "img/smiley/mad.gif\">");
	text = text.replace(/\:\(/g, "<img src=\"" + link + "img/smiley/sad.gif\">");
	text = text.replace(/\:\-\(/g, "<img src=\"" + link + "img/smiley/sad.gif\">");
	text = text.replace(/\(L\)/g, "<img src=\"" + link + "img/smiley/love.gif\">");
	text = text.replace(/\(l\)/g, "<img src=\"" + link + "img/smiley/love.gif\">");
	text = text.replace(/o\(/g, "<img src=\"" + link + "img/smiley/sick.gif\">");
	text = text.replace(/\(H\)/g, "<img src=\"" + link + "img/smiley/cool.gif\">");
	return text;
}

BBCode.prototype.insertSmiley = function (smiley)
{
	if(this.noForm()) return false;

	// our textfield
	var textarea = this._target;
	// our open tag
	var pos;
	if (!textarea.setSelectionRange) {
		var selected = document.selection.createRange().text;
		if (selected.length<=0) {
			// insert smiley last
			textarea.value += smiley;
			pos = textarea.value.length;
		}
	} else {
		pos = textarea.selectionStart + smiley.length;
		// the text before the selection
		var pretext = textarea.value.substring(0, textarea.selectionStart);
		// the selected text with tags before and after
		var codetext = smiley;
		// the text after the selection
		var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
		// check if there was a selection

		// update the text field
		textarea.value = pretext+codetext+posttext;
	}

	// set the focus on the text field
	doSetCaretPosition(textarea, pos);

}

function doSetCaretPosition (oField, iCaretPos) {

     // IE Support
     if (document.selection) {

       // Set focus on the element
       oField.focus ();

       // Create empty selection range
       var oSel = document.selection.createRange ();

       // Move selection start and end to 0 position
       oSel.moveStart ('character', -oField.value.length);

       // Move selection start and end to desired position
       oSel.moveStart ('character', iCaretPos);
       oSel.moveEnd ('character', 0);
       oSel.select ();
     }

     // Firefox support
     else if (oField.selectionStart || oField.selectionStart == '0') {
       oField.selectionStart = iCaretPos;
       oField.selectionEnd = iCaretPos;
       oField.focus ();
     }
}


//Popup window
function hidePopup()
{
	var popup = getObject('popup');
	popup.innerHTML = '';
}

function getObject( obj ) {


  if ( document.getElementById ) {
    obj = document.getElementById( obj );


  } else if ( document.all ) {
    obj = document.all.item( obj );


  } else {
    obj = null;
  }


  return obj;
}

function showPopup( obj, text, e ) {


  var popobj = getObject('popup');


  if (popobj==null) return;

  popobj.innerHTML = text;

  moveObject(popobj, e);

}

function moveObject(obj, e )
{
	var tempX = 0;
  	var tempY = 0;
 	var offset = 5;

	if (document.all) {
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {
		tempX = e.pageX;
		tempY = e.pageY;
	}


	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}



	if (tempX > 470) tempX = 470;

	obj.style.top  = (tempY + offset + 5) + 'px';
	obj.style.left = (tempX + offset) + 'px';


	displayObject( obj, true );
}

function displayObject( obj, show ) {


  obj = getObject( obj );
  if (obj==null) return;


  obj.style.display = show ? 'block' : 'none';
  obj.style.visibility = show ? 'visible' : 'hidden';
}

function SetNoValue(o)
{
	var v = getObject(o);
	if(v != null)
	{
		v.value = '';
	}

	return true;
}