//GLOBAL VARIABLES
var SMILEY_PATH = "/images/smiley/";
var mouseX, mouseY, winX, winY, scrLeft, scrTop;
var textCounterField = null;
var textArea = null;

//GLOBAL CHECKS
var OP = (navigator.userAgent.indexOf('Opera') != -1) ? true : false;
var IE = (navigator.userAgent.indexOf('MSIE') != -1 && !OP) ? true : false;
var GK = (navigator.userAgent.indexOf('Gecko') != -1) ? true : false;
var DM = (document.designMode && document.execCommand) ? true : false;




//Function for creating tags
function tag(name, tag){
	if(textArea == null)
		textArea = getObjectById("texteditor");
		
	if(tag.substring(0, 6) == '[color'){
		var tagadd   = tag;
		var tagclose = '[/color]';
	}else if(tag.substring(0, 5) == '[size'){
		var tagadd   = tag;
		var tagclose = '[/size]';
	}else{
		var tagadd   = '[' + tag + ']';
		var tagclose = '[/' + tag +']';
	}
	
	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 promptResult = prompt("Voer de tekst in die je " + name + " wilt:", "");
			if(promptResult==null || promptResult=="")
				return;
			textArea.value += tagadd+promptResult+tagclose; 
		}else{ 
			// put the code around the selected text 
			document.selection.createRange().text = tagadd+selected+tagclose; 
		} 
	}else{ 
		// the text before the selection 
		var pretext = textArea.value.substring(0, textArea.selectionStart); 
		// the selected text with tags before and after 
		var codetext = tagadd+textArea.value.substring(textArea.selectionStart,textArea.selectionEnd)+tagclose;
		// the text after the selection 
		var posttext = textArea.value.substring(textArea.selectionEnd, textArea.value.length); 
		// check if there was a selection 
		if (codetext == tagadd+tagclose) { 
		//prompt the user 
		var promptResult = prompt("Voer de tekst in die je " + name + " wilt:", "");
		if(promptResult==null || promptResult=="")
			return;
		codetext = tagadd+promptResult+tagclose; 
		} 
		// update the text field 
		textArea.value = pretext+codetext+posttext; 
	} 
	
	textArea.focus(); 
}

//Character count of the textarea
function validateLength() {
	if(textCounterField == null)
		textCounterField = getObjectById("counter");
		
	if(textArea == null)
		textArea = getObjectById("texteditor");
	
	if (textArea.value.length > 1000)
		textArea.value = textArea.value.substring(0, 1000);
	else
		textCounterField.innerHTML = 1000 - textArea.value.length;
}

//Refresh character count on focus
function refreshRestingChars(){
	if(textCounterField == null)
		textCounterField = getObjectById("counter");
		
	if(textArea == null)
		textArea = getObjectById("texteditor");

	textCounterField.innerHTML = 1000 - textArea.value.length;
}

//Function to add an URL to the textbox
function addUrl(){
	if(textArea == null)
		textArea = getObjectById("texteditor");
		
	var url = prompt("Geef het adres in van de webpagina die je wil toevoegen:", "http://");
	if(url == null || url == "" || url == "http://")
		return;
		
	var displayName = prompt("Geef de tekst in die in de plaats van de url moet worden weergegeven.\n"+
							 "Bij blanco wordt enkel het adres getoond:", "");
	
	if(displayName == null || displayName == "")
		textArea.value += '[url]'+url+'[/url]';
	else
		textArea.value += '[url='+url+']'+displayName+'[/url]'; 
}


//Function to add an IMG to the textbox
function addImg(){
	if(textArea == null)
		textArea = getObjectById("texteditor");

	var url = prompt("Geef het adres in van de foto die je wil toevoegen:", "http://");
	if(url == null || url == "" || url == "http://")
		return;

	textArea.value += '[img]'+url+'[/img]';
}


//FunctionS to show a list with the available smileys

  
function buildSmileyChart() {

  var s = new Array();

  var c = new Array();

  s[0] = new Array('icon_surprised.gif', 'icon_gniffle.gif', 'icon_inlove.gif', 'icon_hihi.gif', 'icon_cool.gif');
  s[1] = new Array('icon_embaraced.gif', 'icon_sad.gif', 'icon_soldier.gif', 'icon_autch.gif', 'icon_hello.gif');
  s[2] = new Array('icon_kiss.gif', 'icon_heart.gif', 'icon_mad.gif', 'icon_biggrin.gif', 'icon_tongue.gif');
  s[3] = new Array('icon_wink.gif', 'icon_smile.gif', 'icon_mmm.gif', 'icon_wow.gif', 'icon_rolleyes.gif');
  s[4] = new Array('icon_twisted.gif', 'icon_kitty.gif', '', '', '');

  c[0] = new Array(':-O', ':gniffle:', ':inlove:', ':hihi:', ':cool:');
  c[1] = new Array(':-$', ':-(', ':soldier:', ':autch:', ':hello:');
  c[2] = new Array('(K)', '(L)', ':-@', ':-D', ':-P');
  c[3] = new Array(';-)', ':-)', ':zonked:', ':wow:', ':rolleyes:');
  c[4] = new Array(':twisted:', ':kitty:', '', '', '');

  var html = '<table border="0" cellspacing="1" cellpadding="0" bgcolor="#808080">';

  var style, i, j;

  for(i = 0; i < s.length; i++) {
    html += '<tr>';

    for(j = 0; j < s[i].length; j++) {
      style = 'width: 25px; height: 25px; cursor:hand; background: #CCCCCC url(' + this.SITE_URL +
	       	  this.SMILEY_PATH + s[i][j] + ') no-repeat center center;';
			  
      html += '<td style="width: 25px; height: 25px;">' +
			  '<a href="javascript:insertSmiley(\'' + c[i][j] + '\')" ' +
              'title="' + c[i][j] + '"><div style="' + style + '"></div></a></td>';
    }
	
    html += '</tr>';
  }
  html += '</table>';
  
  return html;
}

function insertSmiley(smiley) {
	if(textArea == null)
		textArea = getObjectById("texteditor");

	textArea.value += smiley;
	textArea.focus();
	viewSmileyDialog();
	return;
}

function viewSmileyDialog() {
	
	var obj = getObjectById('dlgSmiley');
	
	if(textArea == null)
		textArea = getObjectById("texteditor");
	
    if(obj) {
      if(IE) {
        textArea.focus();
        var curSelection = document.selection.createRange();
      }
      if(obj.style.visibility == 'visible') 
	  	obj.style.visibility = 'hidden';
      else {
        var wdth = hght = 0;
        var top = mouseY;
        var left = mouseX;

        if(IE){
          wdth = obj.style.pixelWidth;
          hght = obj.style.pixelHeight;
        }else{
          wdth = obj.offsetWidth;
          hght = obj.offsetHeight;
        }

        rtoGetWinXY();
        if(left + wdth - scrLeft > winX) left = winX + scrLeft - wdth;
        if(top + hght - scrTop > winY) top = winY + scrTop - hght - 20;

        obj.style.left = (left - 50) + 'px';
        obj.style.top = top + 'px';
        obj.style.visibility = 'visible';
      }
    }
}

function GetMousePosition(e) {
  if(e && e.pageX != null) {
    mouseX = e.pageX;
    mouseY = e.pageY;
  }
  else if(event && event.clientX != null) {
    mouseX = event.clientX + rtoGetScrollLeft();
    mouseY = event.clientY + rtoGetScrollTop();
  }
}

//Document actions:
document.onmousedown = GetMousePosition;

document.writeln('<div id="dlgSmiley" style="' + 
				 'position: absolute; ' +
                 'padding: 4px; ' +
                 'z-index: 69; ' +
                 'background-color: #E0E0E0; ' +
                 'border: 2px groove; ' +
                 'text-align: center; ' +
                 'visibility: hidden; ' +
				 '"><center>' +
               	 '<form name="smileys" class="cssForm">' +
               	 '<input type=hidden name="id" value="">' +
               	 buildSmileyChart() +
               	 '<input type=button value="Sluiten" class="HTMLForm_submit" onClick="javascript:viewSmileyDialog();">' +
                 '</form>' +
                 '</center></div>');
			   
//###################################################


//Function to get elements by ID

function getObjectById(id) {
  var obj = false;
  if(document.getElementById) 
  	obj = document.getElementById(id);
  else if(document.all) 
  	obj = document.all[id];
  return obj;
}


//Function to get the screensize

function rtoGetWinXY() {
  if(window.innerWidth) {
    winX = window.innerWidth;
    winY = window.innerHeight;
  }
  else if(document.documentElement && document.documentElement.clientWidth) {
    winX = document.documentElement.clientWidth;
    winY = document.documentElement.clientHeight;
  }
  else if(document.body && document.body.clientWidth) {
    winX = document.body.clientWidth;
    winY = document.body.clientHeight;
  }
  else {
    winX = screen.width;
    winY = screen.height;
  }
  scrLeft = rtoGetScrollLeft();
  scrTop = rtoGetScrollTop();
}

function rtoGetScrollLeft() {
  var scrLeft = 0;
  if(window.pageXOffset) scrLeft = window.pageXOffset;
  else if(document.documentElement && document.documentElement.scrollLeft)
    scrLeft = document.documentElement.scrollLeft;
  else if(document.body && document.body.scrollLeft)
    scrLeft = document.body.scrollLeft;
  return scrLeft;
}

function rtoGetScrollTop() {
  var scrTop = 0;
  if(window.pageYOffset) scrTop = window.pageYOffset;
  else if(document.documentElement && document.documentElement.scrollTop)
    scrTop = document.documentElement.scrollTop;
  else if(document.body && document.body.scrollTop)
    scrTop = document.body.scrollTop;
  return scrTop;
}

