/* *****************************************************************************
                          A'CODE BBCODE EDITOR V 1.0
              Copyright 2006 (C) Ansugo ( http://ansugo.zaxaz.com )

    Feel free ( free in free beer, free in freedom or freedom projects on iraq,
    doesn't matter for me what you understand from free ) to use on any type of 
    projects. You can change codes, to whatever you want. Just remember who 
    writes this code first. Rest is yours.
    
              Special Thanks To :
              Peter-Paul Koch (Some Code)     http://www.quirksmode.org/
              Alex King       (A routine)     http://www.alexking.org/
              Mark James      (Silk buttons)  http://www.famfamfam.com/
              Türker Yıldırım (Tests)         http://www.turker.biz/
              -----------------------------------------------------
                          A'CODE BBCODE EDITOR V 1.0
            Telif Hakkı 2006 (C) Ansugo ( http://ansugo.zaxaz.com )

    İstediğiniz yerde özgürce (özgür'ü ifade etmek türkçe de ne kolay!) kullana-
    bilirsiniz. Kodları istediğiniz gibi değiştirebilirsiniz. Sadece ilk kimin
    yazdığını hatırlayın. Gerisi size kalmış.

              Teşekkürler :
              Peter-Paul Koch (Kod parçaları)  http://www.quirksmode.org/
              Alex King       (Bir rutin)      http://www.alexking.org/
              Mark James      (Düğmeler)       http://www.famfamfam.com/
              Türker Yıldırım (Denemeler)      http://www.turker.biz/
   ***************************************************************************** */


/* *****************************************************************************
   myTags array is an array which holds the bbcodes. The format is like this :
   A unique button id which starts with idubb_
   A unique button name
   A button title (to show when user hover on )
   A start tag
   An end tag
   ----------------------------
   myTags dizisi bb kodlarını tutan bir dizidir. Dizi düzeni şöyledir :
   idubb_ ile başlayan benzersiz bir button tanımlama numarası
   Benzersiz bir buton adı
   Bir buton başlığı ( kullanıcı buton üzerine gelince gösterilecek )
   Bir başlangıc tagı
   Bir bitiş tagı
   ***************************************************************************** */


var myTags = new Array();
myTags[myTags.length] = new Array('idubb_strong','strong','Fed tekst','[B]','[/B]');
myTags[myTags.length] = new Array('idubb_italic','italic','Kursiv tekst','[I]','[/I]');
myTags[myTags.length] = new Array('idubb_underline','underline','Understreget tekst','[U]','[/U]');
myTags[myTags.length] = new Array('idubb_del','strike','Gennemstreget tekst','[S]','[/S]');
myTags[myTags.length] = new Array('idubb_h1','h1','Overskrift 1','[H1]','[/H1]');
myTags[myTags.length] = new Array('idubb_h2','h2','Overskrift 2','[H2]','[/H2]');
myTags[myTags.length] = new Array('idubb_h3','h3','Overskrift 3','[H3]','[/H3]');
/*myTags[myTags.length] = new Array('idubb_h4','h4','Overskrift 4','[H4]','[/H4]');
myTags[myTags.length] = new Array('idubb_h5','h5','Overskrift 5','[H5]','[/H5]');
myTags[myTags.length] = new Array('idubb_h6','h6','Overskrift 6','[H6]','[/H6]');*/
myTags[myTags.length] = new Array('idubb_sub','sub','Sænket skrift','[SUB]','[/SUB]');
myTags[myTags.length] = new Array('idubb_sup','sup','Hævet skrift','[SUP]','[/SUP]');
myTags[myTags.length] = new Array('idubb_code','code','Kode','[CODE]','[/CODE]');
myTags[myTags.length] = new Array('idubb_pre','pre','Predefineret tekst','[PRE]','[/PRE]');
myTags[myTags.length] = new Array('idubb_quote','quote','Citat','[QUOTE]','[/QUOTE]');
myTags[myTags.length] = new Array('idubb_left','left','Placer venstre','[LEFT]','[/LEFT]');
myTags[myTags.length] = new Array('idubb_center','center','Placer i midten','[CENTER]','[/CENTER]');
myTags[myTags.length] = new Array('idubb_right','right','Placer højre','[RIGHT]','[/RIGHT]');
myTags[myTags.length] = new Array('idubb_justify','justify','Udlignet tekst','[JUSTIFY]','[/JUSTIFY]');
myTags[myTags.length] = new Array('idubb_list','list','Normal liste','[LIST]','[/LIST]');
myTags[myTags.length] = new Array('idubb_listnum','listnum','Nummereret liste','[LIST1]','[/LIST1]');
myTags[myTags.length] = new Array('idubb_url','url','URL','[URL]','[/URL]');
myTags[myTags.length] = new Array('idubb_img','img','Billede','[IMG]','[/IMG]');
myTags[myTags.length] = new Array('idubb_color','color','Tekstfarve','[COLOR]','[/COLOR]');
myTags[myTags.length] = new Array('idubb_email','email','Email adresse','[EMAIL]','[/EMAIL]');
/*myTags[myTags.length] = new Array('idubb_tab','tab','Indsæt "tab"','[T]','[/T]');*/
myTags[myTags.length] = new Array('idubb_br','br','Indsæt linjeskift','[BR]','[/BR]');


/* *****************************************************************************
   ShowAfterRegex function is a gateway to tell regex engine to format the text,
   and writes the formatted text to layer defined.
   ----------------------------
   ShowAfterRegex fonksiyonu düzenli ifade motoruna girilen yazıyı düzenlemesini
   söyler ve düzenlenmiş çıktıyı belirtilmiş olan katmana yazar.
   ***************************************************************************** */


function ShowAfterRegex (myText,myDiv) {
  myText = document.getElementById(myText);
  myDiv  = document.getElementById(myDiv);
  //alert(myDiv.scroll);
  myDiv.innerHTML = myText.value;
  myDiv.innerHTML = RegExpian(myText);
  
  if ( document.getElementById(myText.name + 'html' ) ) {
    document.getElementById(myText.name + 'html').value = myDiv.innerHTML;
  }
  
  myDiv.scrollTop = 20000;
}


/* *****************************************************************************
   RegExpian function is a function to format the bbcoded text to html format.
   ----------------------------
   RegExpian fonksiyonu bb kodları ile verilen yazıyı html biçimine çevirir.
   ***************************************************************************** */


function RegExpian (input) {
  inputreg = input.value;

  inputreg = inputreg.replace(/&/ig,"&amp;");
  inputreg = inputreg.replace(/>/ig,"&gt;");
  inputreg = inputreg.replace(/</ig,"&lt;");
  //inputreg = inputreg.replace(/\"/ig,"&quot;");
  //inputreg = inputreg.replace(/\[/ig,"&#91;");
  //inputreg = inputreg.replace(/\]/ig,"&#93;");

  inputreg = inputreg.replace(/\r\n/ig,"\n");
  inputreg = inputreg.replace(/\r/ig,"\n");
  
  inputreg = inputreg.replace(/\[B\]/ig,"<strong>");
  inputreg = inputreg.replace(/\[\/B\]/ig,"</strong>");
  inputreg = inputreg.replace(/\[I\]/ig,"<em>");
  inputreg = inputreg.replace(/\[\/I\]/ig,"</em>");
  inputreg = inputreg.replace(/\[U\]/ig,"<u>");
  inputreg = inputreg.replace(/\[\/U\]/ig,"</u>");
  inputreg = inputreg.replace(/\[S\]/ig,"<strike>");
  inputreg = inputreg.replace(/\[\/S\]/ig,"</strike>");
  inputreg = inputreg.replace(/\[H1\]/ig,"<h1>");
  inputreg = inputreg.replace(/\[\/H1\]/ig,"</h1>");
  inputreg = inputreg.replace(/\[H2\]/ig,"<h2>");
  inputreg = inputreg.replace(/\[\/H2\]/ig,"</h2>");
  inputreg = inputreg.replace(/\[H3\]/ig,"<h3>");
  inputreg = inputreg.replace(/\[\/H3\]/ig,"</h3>");
  inputreg = inputreg.replace(/\[H4\]/ig,"<h4>");
  inputreg = inputreg.replace(/\[\/H4\]/ig,"</h4>");
  inputreg = inputreg.replace(/\[H5\]/ig,"<h5>");
  inputreg = inputreg.replace(/\[\/H5\]/ig,"</h5>");
  inputreg = inputreg.replace(/\[H6\]/ig,"<h6>");
  inputreg = inputreg.replace(/\[\/H6\]/ig,"</h6>");
  inputreg = inputreg.replace(/\[SUB\]/ig,"<sub>");
  inputreg = inputreg.replace(/\[\/SUB\]/ig,"</sub>");
  inputreg = inputreg.replace(/\[SUP\]/ig,"<sup>");
  inputreg = inputreg.replace(/\[\/SUP\]/ig,"</sup>");
  inputreg = inputreg.replace(/\[CODE\]/ig,"<code>");
  inputreg = inputreg.replace(/\[\/CODE\]/ig,"</code>");
  inputreg = inputreg.replace(/\[PRE\]/ig,"<pre>");
  inputreg = inputreg.replace(/\[\/PRE\]/ig,"</pre>");
  inputreg = inputreg.replace(/\[QUOTE\]/ig,"<blockquote>");
  inputreg = inputreg.replace(/\[\/QUOTE\]/ig,"</blockquote>");
  inputreg = inputreg.replace(/\[LEFT\]/ig,"<div align=\"left\">");
  inputreg = inputreg.replace(/\[\/LEFT\]/ig,"</div>");
  inputreg = inputreg.replace(/\[CENTER\]/ig,"<div align=\"center\">");
  inputreg = inputreg.replace(/\[\/CENTER\]/ig,"</div>");
  inputreg = inputreg.replace(/\[RIGHT\]/ig,"<div align=\"right\">");
  inputreg = inputreg.replace(/\[\/RIGHT\]/ig,"</div>");
  inputreg = inputreg.replace(/\[JUSTIFY\]/ig,"<div align=\"justify\">");
  inputreg = inputreg.replace(/\[\/JUSTIFY\]/ig,"</div>");
  inputreg = inputreg.replace(/\[LIST\]/ig,"<ul>");
  inputreg = inputreg.replace(/\[\/LIST\]/ig,"</ul>");
  inputreg = inputreg.replace(/\[LIST1\]/ig,"<ol>");
  inputreg = inputreg.replace(/\[\/LIST1\]/ig,"</ol>");
  inputreg = inputreg.replace(/\[\*\](.*)(\n|\r|\r\n)/ig,"<li>$1</li>"); // A fix for list lines - Liste satırları için düzeltme
  inputreg = inputreg.replace(/\[\*\](.*)/ig,"<li>$1</li>"); // A fix for list lines - Liste satırları için düzeltme
  
  inputreg = inputreg.replace(/\[URL\]([^\[]*)\[\/URL\]/ig,"<a href=\"$1\" title=\"$1\">$1</a>");
  inputreg = inputreg.replace(/\[URL HREF=\"([^\"]*)\"]([^\[]*)\[\/URL\]/ig,"<a href=\"$1\">$2</a>");
  inputreg = inputreg.replace(/\[URL=\"([^\"]*)\"]([^\[]*)\[\/URL\]/ig,"<a href=\"$1\">$2</a>");
  inputreg = inputreg.replace(/\[EMAIL\]([^\[]*)\[\/EMAIL\]/ig,"<a href=\"mailto:$1\" title=\"$1\">$1</a>");
  inputreg = inputreg.replace(/\[EMAIL HREF=\"([^\"]*)\"](.*)\[\/EMAIL\]/ig,"<a href=\"mailto:$1\">$2</a>");
  inputreg = inputreg.replace(/\[IMG\]([^\[]*)\[\/IMG\]/ig,"<img src=\"$1\" />");
  inputreg = inputreg.replace(/\[IMG SRC=\"([^\"]*)\" ALIGN=\"([^\"]*)\"]([^\[]*)\[\/IMG\]/ig,"<img src=\"$1\" alt=\"$3\" align=\"$2\" />");
  inputreg = inputreg.replace(/\[IMG SRC=\"([^\"]*)\"]([^\[]*)\[\/IMG\]/ig,"<img src=\"$1\" alt=\"$2\" />");
  inputreg = inputreg.replace(/\[COLOR NUM=\"([^\"]*)\"]([^\[]*)\[\/COLOR\]/ig,"<font color=\"$1\">$2</font>");
  inputreg = inputreg.replace(/\[BR\]/ig,"<br />");

  inputreg = inputreg.replace(/\n/ig,"<br />");
  inputreg = inputreg.replace(/\t/ig,"&nbsp;&nbsp;&nbsp;&nbsp;");
  return inputreg;
}


/* *****************************************************************************
   WriteTagButtons function is function which writes the buttons to area where
   it called from.
   ----------------------------
   WriteTagButtons fonksiyonu fonksiyonun çağrıldığı yere düğmeleri yazan bir 
   fonksiyondur.
   ***************************************************************************** */


function WriteTagButtons (textareatemp,layertemp) {
  for ( i = 0; i < myTags.length; i++ ) {
    document.write('<input type="button" class="bbcodebutton" id="' + myTags[i][0] + '" style="background:url(/fundanemt/modules/fundaForum/frontend/bbcodeeditor/images/' + myTags[i][1] + '.gif) no-repeat;width:22px;height:22px;padding:2px;border:1px solid #cfcfcf; cursor:pointer;" name="' + myTags[i][1] + '" onClick="WriteOption(\'' + myTags[i][0] + '\',\''+textareatemp+'\',\''+ layertemp + '\',\'' + myTags[i][3] + '\',\'' + myTags[i][4] + '\');" title="' + myTags[i][2] + '" />');
  }
}


/* *****************************************************************************
    WriteOption is a function which gets button id,textarea, write layer, open
    tag and close tag from buttons' onClick event and writes the proper text to 
    textarea, after that writes the formatted output to layer.
    ----------------------------
    WriteOption fonksiyonu düğmelerden birine basıldığında (onClick) düğme tanım 
    numarasını, yazı alanını, yazılakcak katmanı, açılış tagını ve kapanış tagını 
    alır, yazılması gereken yazıyı yazı alanına yazar ve bundan sonra düzenlenmiş 
    html çıktıyı ilgili katmana yazar.
   ***************************************************************************** */

function WriteOption (myButId,myText,myLayer,tagOpen,tagClose) {
  myText = document.getElementById(myText);
  myLayer= document.getElementById(myLayer);


  mySelection = '';
  
  if ( window.getSelection || document.getSelection ) {
    mySelection = myText.value.substring(myText.selectionStart,myText.selectionEnd);
  }
  else {
    mySelection = iestr = document.selection.createRange().text;
  }

    
  Content = '';


  myButSwitch = myButId.substring(6,myButId.length);

  // -- LIST
  if ( myButSwitch == 'list' || myButSwitch == 'listnum' ) {
    Content = tagOpen + "\n";
    
		do {
		  tmpval = prompt("Please enter an item:","");
		  if ( tmpval != undefined && tmpval != '') {
		    Content += "[*]" + tmpval + "\n";
		  }
		} while (tmpval != null && tmpval != "" && tmpval != undefined);
		
		Content = Content + tagClose + "\n";
  }
  // -- URL
  else if ( myButSwitch == 'url') {
    tagOpen = tagOpen.substring(0,tagOpen.length-1);
    Content  = tagOpen;
    tmphref  = prompt("Adresse:","http://");
   // tmptitle = prompt("Titel for adresse:","");
    tmptext  = '';
    
    if ( mySelection == '' ) {
      tmptext  = prompt("Tekst for link:","");
    }
    else {
      tmptext = mySelection;
    }
    
    if ( tmphref == null || tmphref == "" || tmphref == undefined ) { Content = Content + ' ' + 'HREF="#"'; }
    else                                                            { Content = Content + ' ' + 'HREF="' + tmphref + '"'; }
    
 //   if ( tmptitle == null || tmptitle == "" || tmptitle == undefined ) { Content = Content + ' ' + 'TITLE="' + '"'; }
  //  else                                                               { Content = Content + ' ' + 'TITLE="' + tmptitle + '"'; }
    
    if ( tmptext == null || tmptext == "" || tmptext == undefined ) {
      if ( tmphref != undefined && tmphref != '' ) {
        Content = Content + ']' + tmphref;
      }
      else {
        Content = Content + ']';
      }
    }
    else {
     Content = Content + ']' + '' + tmptext;
    }
    
    Content = Content + tagClose;
  }
  // -- IMG
  else if ( myButSwitch == 'img') {
    tagOpen = tagOpen.substring(0,tagOpen.length-1);
    Content  = tagOpen;
    tmphref  = prompt("Adresse til billede:","");
    tmptext  = '';
    if ( mySelection == '' ) {
      tmptext  = prompt("Beskrivelse:","");
    }
    else {
      tmptext = mySelection;
    }
    tmpalign = '';
    tmpalign = prompt("Justering (left, right, center)? :","");
    
    
    if ( tmphref == null || tmphref == "" || tmphref == undefined ) {
      Content = Content + ' ' + 'SRC="#"';
    }
    else {
     Content = Content + ' ' + 'SRC="' + tmphref + '"';
    }
    
    if ( tmpalign == null || tmpalign == "" || tmpalign == undefined ) {
      Content = Content + ' ALIGN="CENTER"';
    }
    else {
      Content = Content + ' ALIGN="' + tmpalign + '"';
    }

    if ( tmptext == null || tmptext == "" || tmptext == undefined ) {
      if ( tmphref != undefined && tmphref != '' ) {
        Content = Content + ']' + tmphref;
      }
      else {
        Content = Content + ']';
      }
    }
    else {
     Content = Content + ']' + '' + tmptext;
    }
    
    Content = Content + tagClose;
  }
  // -- COLOR
  else if ( myButSwitch == 'color') {
    tagOpen = tagOpen.substring(0,tagOpen.length-1);

    Content  = tagOpen;
    tmpcolor = prompt("Skriv farvens hexværdi som f.eks #FFFFFF:","");
    tmptext  = '';
    if ( mySelection == '' ) {
      tmptext  = prompt("Skriv Tekst","");
    }
    else {
      tmptext = mySelection;
    }
    
    if ( tmpcolor == null || tmpcolor == "" || tmpcolor == undefined ) {
      Content = Content + ' ' + 'NUM="#"]';
    }
    else {
     Content = Content + ' ' + 'NUM="' + tmpcolor + '"]';
    }

    if ( tmptext == null || tmptext == "" || tmptext == undefined ) {
    }
    else {
     Content = Content + tmptext;
    }
    
    Content = Content + tagClose;
  }
  // -- EMAIL
  else if ( myButSwitch == 'email') {
    tagOpen = tagOpen.substring(0,tagOpen.length-1);

    Content  = tagOpen;
    tmpemail = prompt("Skriv Email Adresse","");
    tmptext  = '';
    if ( mySelection == '' ) {
      tmptext  = prompt("Skriv Email Link Tekst","");
    }
    else {
      tmptext = mySelection;
    }

    if ( tmpemail == null || tmpemail == "" || tmpemail == undefined ) {
      Content = Content + ' ' + 'HREF="#"]';
    }
    else {
     Content = Content + ' ' + 'HREF="' + tmpemail + '"]';
    }

    if ( tmptext == null || tmptext == "" || tmptext == undefined ) {
      Content = Content + tmpemail;
    }
    else {
     Content = Content + tmptext;
    }
    
    Content = Content + tagClose;
  }
  else if ( myButSwitch == "br" ) {
    Content = "[BR]";
  }
  else if ( myButSwitch == 'newline' ) {
    Content = '\n';
  }
  else if ( myButSwitch == 'tab' ) {
    Content = '\t';
  }
  
		var startPos = myText.selectionStart;
		var endPos = myText.selectionEnd;
		var cursorPos = endPos;
		var scrollTop = myText.scrollTop;
  
  if ( window.getSelection || document.getSelection ) {
    // Almost Mozilla or Opera
    if ( myText.selectionStart != myText.selectionEnd ) {
      if ( Content != '' ) {
        myText.value = 
          myText.value.substring(0,myText.selectionStart)
        + Content
        + myText.value.substring(myText.selectionEnd,myText.value.length);
        
        cursorPos += Content.length - mySelection.length;
      }
      else {
       myText.value = 
        myText.value.substring(0,myText.selectionStart)
        + tagOpen
        + myText.value.substring(myText.selectionStart,myText.selectionEnd)
        + tagClose
        + myText.value.substring(myText.selectionEnd,myText.value.length);
        
        cursorPos += tagOpen.length + tagClose.length;
        
      }
    }
    else {
      if ( Content != '' ) {
        myText.value = 
          myText.value.substring(0,myText.selectionStart)
        + Content
        + myText.value.substring(myText.selectionEnd,myText.value.length);
        
        cursorPos += Content.length;
      }
      else {
      myText.value = 
        myText.value.substring(0,myText.selectionStart)
      + tagOpen
      + myText.value.substring(myText.selectionStart,myText.selectionEnd)
      + tagClose
      + myText.value.substring(myText.selectionEnd,myText.value.length);
      
      cursorPos += tagOpen.length;
      
      }
    }
    
		myText.focus();
		myText.selectionStart = cursorPos;
		myText.selectionEnd = cursorPos;
		myText.scrollTop = scrollTop;
  }
  else if (document.selection) {
    // Almost suckzors
    myText.focus();
    iestr = document.selection.createRange();
    
    if ( iestr.text.length > 0 ) {
      if ( Content != '' ) {
        iestr.text = Content;
      }
      else {
        iestr.text = tagOpen + iestr.text + tagClose;
      }
    }
    else {
      if ( Content != '' ) {
        iestr.text = Content;
      }
      else {
        iestr.text = tagOpen + tagClose;
      }
    }
    myText.focus();
  }
  ShowAfterRegex(myText.id,myLayer.id);
}

function togglePreview(item){
   if(item.style.display=='block'){
      item.style.display='none';
      item.style.visibility='hidden';
   }else{
      item.style.display='block';
      item.style.visibility='visible';
   }
}
