var is_gecko = /gecko/i.test(navigator.userAgent);
var is_ie    = /MSIE/.test(navigator.userAgent);

function addBBCode(code_pre, code_post, elementId)
{
	editor = document.getElementById(elementId);
	theSelection = false;
	if (document.selection && editor.storedRange)
		theSelection = editor.storedRange.text;
		//theSelection = document.selection.createRange().text;

	if (!theSelection)
	{
		if (editor.selectionStart >= 0)
		{
			editor.value = editor.value.substring(0, editor.selectionStart) + code_pre + editor.value.substring(editor.selectionStart, editor.selectionEnd) + code_post + editor.value.substring(editor.selectionEnd);
			return;
		}
			
		if (document.selection && editor.storedRange)
		{			    
	    caretPos = getCaretPos(elementId);
			editor.value = editor.value.substring(0, caretPos) + code_pre + code_post + editor.value.substring(caretPos);
		}
		else
		{
			editor.value = editor.value + code_pre + code_post;
		}
	}
	else
	{
		if (editor.storedRange == null)
		{
			editor.value = editor.value + code_pre + code_post;
		}
		else
		{
			if (editor.storedRange.parentElement().id != elementId)
			//if (document.selection.createRange().parentElement().id != elementId)
				return;
	
			editor.storedRange.text = code_pre + theSelection + code_post;
			//document.selection.createRange().text = code_pre + theSelection + code_post;
		}
	}
} // addBBCode

function getCaretPos(elementId)
{
	var caretPos = -1;
	editor = document.getElementById(elementId);
	if (editor.selectionStart >= 0)
	{
		caretPos = editor.selectionStart;
	}
	else
	{
		if (document.selection && editor.storedRange)
		{			    
	    var bookmark = "~#";
	    var orig = editor.value;
	    var range = editor.storedRange;
	    range.text = bookmark;
	    caretPos = editor.value.search(bookmark);
	    editor.value = orig;
	  }
	}
	
	return caretPos;
} // getCaretPos

function setCaretPos(caretPos, elementId)
{
	editor = document.getElementById(elementId);
	editor.focus();
	
	if (is_gecko) {
		editor.setSelectionRange(caretPos, caretPos);
	} else {
		// assumed IE
		var range = editor.createTextRange();
		range.collapse(true);
		range.moveStart("character", caretPos);
		range.moveEnd("character", 0);
		range.select();
		editor.storedRange = document.selection.createRange();
	}
} // setCaretPos

function containsSelection(elementId)
{
	editor = document.getElementById(elementId);
	theSelection = false;
	if (document.selection)
		theSelection = document.selection.createRange().text;
		
	if (!theSelection)
	{
		return (editor.selectionStart < editor.selectionEnd);
	}
	else
	{
		return (document.selection.createRange().parentElement().id == elementId);
	}
} // containsSelection

function addBBLink(hyperlink, linktext, elementId)
{
	if (hyperlink == null || hyperlink == '')
		return;
	
	if (containsSelection(elementId))
		addBBCode('[url='+hyperlink+']', '[/url]', elementId);
	else
	{
		if (linktext != '')
			addBBCode('', '[url='+hyperlink+']'+linktext+'[/url]', elementId);
		else
			addBBCode('', '[url]'+hyperlink+'[/url]', elementId);
	}
} // addBBLink

function addBBImage(imageurl, align, elementId)
{
	if (imageurl == null || imageurl == '')
		return;
		
	addBBCode('', '[img' + align + ']'+imageurl+'[/img]', elementId);
} // addBBImage

function addBBSmiley(imageurl, elementId)
{
	if (imageurl == null || imageurl == '')
		return;
		
	addBBCode('', '[img]'+imageurl+'[/img]', elementId);
	document.getElementById(elementId+'SmileyWin').style.display = "none";
} // addBBSmiley

function addBBSmileyCode(code, elementId)
{
	if (code == null || code == '')
		return;
		
	addBBCode('', code, elementId);
	document.getElementById(elementId+'SmileyWin').style.display = "none";
} // addBBSmileyCode

function addBBColor(color, elementId)
{
	addBBCode('[color='+color+']', '[/color]', elementId);
	document.getElementById(elementId+'ColorWin').style.display = "none";
} // addBBColor

function addBBMedia(mediaurl, elementId)
{
	if (mediaurl == null || mediaurl == '')
		return;
		
	addBBCode('', '[media]'+mediaurl+'[/media]', elementId);
} // addBBMedia

function addBBYoutube(youtubeurl, elementId)
{
	if (youtubeurl == null || youtubeurl == '')
		return;
		
	addBBCode('', '[youtube]'+youtubeurl+'[/youtube]', elementId);
} // addBBYoutube

function showBBLinkWindow(elementId, path)
{
	var dialogheight = 130;
	var dialogwidth = 440;
	dialog = window.open(path+'/editor/link.php?elementId='+elementId+'&selection='+containsSelection(elementId), "linkDialog", "height="+ dialogheight +",width="+ dialogwidth+",resize=0,left="+ (screen.width-dialogwidth)/2 +",top="+ (screen.height-dialogheight)/2);
	dialog.focus();
} // showBBLinkWindow

function showBBImageWindow(elementId, path)
{
	var dialogheight = 200;
	var dialogwidth = 420;
	dialog = window.open(path+'/editor/image.php?elementId='+elementId, "imageDialog", "height="+ dialogheight +",width="+ dialogwidth+",resize=0,left="+ (screen.width-dialogwidth)/2 +",top="+ (screen.height-dialogheight)/2);
	dialog.focus();
} // showBBImageWindow

function showBBMediaWindow(elementId, path)
{
	var dialogheight = 100;
	var dialogwidth = 420;
	dialog = window.open(path+'/editor/media.php?elementId='+elementId, "mediaDialog", "height="+ dialogheight +",width="+ dialogwidth+",resize=0,left="+ (screen.width-dialogwidth)/2 +",top="+ (screen.height-dialogheight)/2);
	dialog.focus();
} // showBBMediaWindow

function showBBYoutubeWindow(elementId, path)
{
	var dialogheight = 140;
	var dialogwidth = 420;
	dialog = window.open(path+'/editor/youtube.php?elementId='+elementId, "youtubeDialog", "height="+ dialogheight +",width="+ dialogwidth+",resize=0,left="+ (screen.width-dialogwidth)/2 +",top="+ (screen.height-dialogheight)/2);
	dialog.focus();
} // showBBYoutubeWindow

function addBBLinkPrompt(elementId)
{
	hyperlink = prompt("Enter an URL:", "http://");
	if (hyperlink == null)
		return;
	
	if (containsSelection(elementId))
		addBBCode('[url='+hyperlink+']', '[/url]', elementId);
	else
		addBBCode('', '[url]'+hyperlink+'[/url]', elementId);
} // addBBLink

function addBBImagePrompt(elementId)
{
	imagelink = prompt("Enter an URL:", "http://");
	if (imagelink == null)
		return;
	
	addBBCode('', '[img]'+imagelink+'[/img]', elementId);
} // addBBImage

function showPreview(elementId, color)
{
	editor = document.getElementById(elementId);
	//previewForm = document.getElementById(elementId+'PreviewForm');
	previewFormObject = document.getElementById('previewForm');
	previewFormObject.bbcode.value = editor.value;
	previewFormObject.color.value = color;
	
	var dialogheight = 600;
	var dialogwidth = 680;
	dialog = window.open('', "previewWindow", "scrollbars=yes,height="+ dialogheight +",width="+ dialogwidth+",resize=0,left="+ (screen.width-dialogwidth)/2 +",top="+ (screen.height-dialogheight)/2);
	
	previewFormObject.submit();
} // showPreview

function showPopWindow(e, winType, elementId)
{
	var editorBtn = e.target || e.srcElement;
    var colorWin  = $(elementId + 'ColorWin');
    var smileyWin = $(elementId + 'SmileyWin');
    
	if (winType == 'color')
	{
	  //colorWin.style.left = (getPosX(editorBtn)+10) + "px";
	  //colorWin.style.top =  (getPosY(editorBtn)+30) + "px";
	  colorWin.style.display  = (colorWin.style.display=="block") ? "none" : "block";
	  smileyWin.style.display = "none";
	}
	else if (winType == 'smiley')
	{
	  //smileyWin.style.left = (getPosX(editorBtn)+10) + "px";
	  //smileyWin.style.top  = (getPosY(smileyWin)+30) + "px";
	  smileyWin.style.display = (smileyWin.style.display=="block") ? "none" : "block";
	  colorWin.style.display = "none";
	}
} // showPopWindow

function getPosX(obj) 
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
} // getPosX

function getPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
} // getPosY

var _imageId = 0;
function checkResize(elem, maxWidth)
{
	if(elem.width > maxWidth) 
	{
		elem.width = maxWidth;
		_imageId++;
		elem.id = '_image' + _imageId;
		setTimeout('_timedResize(\''+elem.id+'\', '+maxWidth+')', 2000);
	}
} // checkResize

function _timedResize(elemId, maxWidth)
{
	var elem = document.getElementById(elemId);
	if(elem.width > maxWidth)
		elem.width = maxWidth;
} // _timedResize

function open_window(comment_url, title, width, height)
{
	var $val = document.getElementById('comment').value;
//	var newstr = $val.replace("/\n", "<br />");
	var new_url = comment_url + '?comment=' + escape($val);
	
	if (document.getElementById('hexColor'))
	{
		new_url += '&bgcolor=' + escape(document.getElementById('hexColor').value);
	}
	var top = (screen.availHeight - height)/2;
	var left = (screen.availWidth - width)/2;
	var features = "top=" + top + ",left=" + left + ",toolbar=no,location=no,menubar=no,resizable=yes,status=no,scrollbars=yes,width=" + width + ",height=" + height + "";
	var wnd = window.open(new_url,title,features);
	if (wnd == undefined)
	{
		alert('Popup blocker actief.');
	}
	else
	{
		wnd.focus();
	}
}

/*document.write(
	'<form method="post" name="previewForm" id="previewForm" action="editor/preview.php" target="previewWindow">'+
	'<input type="hidden" name="bbcode" value="" />'+
	'<input type="hidden" name="color" value="#ffffff" />'+
	'</form>'
	);*/