/* *****************************************************************************
                          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','pogrubienie','[B]','[/B]');
myTags[myTags.length] = new Array('idubb_italic','italic','pochylenie','[I]','[/I]');
//myTags[myTags.length] = new Array('idubb_underline','underline','podkreślenie','[U]','[/U]');
//myTags[myTags.length] = new Array('idubb_del','strike','przekreślenie','[S]','[/S]');
//myTags[myTags.length] = new Array('idubb_h1','h1','nagłówek H1','[H1]','[/H1]');
//myTags[myTags.length] = new Array('idubb_h2','h2','nagłówek H2','[H2]','[/H2]');
//myTags[myTags.length] = new Array('idubb_h3','h3','nagłówek H3','[H3]','[/H3]');
//myTags[myTags.length] = new Array('idubb_h4','h4','nagłówek H4','[H4]','[/H4]');
//myTags[myTags.length] = new Array('idubb_h5','h5','nagłówek H5','[H5]','[/H5]');
//myTags[myTags.length] = new Array('idubb_h6','h6','nagłówek H6','[H6]','[/H6]');
//myTags[myTags.length] = new Array('idubb_sub','sub','indeks dolny','[SUB]','[/SUB]');
//myTags[myTags.length] = new Array('idubb_sup','sup','indeks górny','[SUP]','[/SUP]');
//myTags[myTags.length] = new Array('idubb_code','code','blok Kodu','[CODE]','[/CODE]');
//myTags[myTags.length] = new Array('idubb_pre','pre','tekst predefiniowany','[PRE]','[/PRE]');
//myTags[myTags.length] = new Array('idubb_quote','quote','cytat','[QUOTE]','[/QUOTE]');
//myTags[myTags.length] = new Array('idubb_left','left','worówanie do lewej','[LEFT]','[/LEFT]');
myTags[myTags.length] = new Array('idubb_center','center','worówanie do środka','[CENTER]','[/CENTER]');
//myTags[myTags.length] = new Array('idubb_right','right','worówanie do prawej','[RIGHT]','[/RIGHT]');
//myTags[myTags.length] = new Array('idubb_justify','justify','justowanie','[JUSTIFY]','[/JUSTIFY]');
//myTags[myTags.length] = new Array('idubb_list','list','lista punktowa','[LIST]','[/LIST]');
//myTags[myTags.length] = new Array('idubb_listnum','listnum','lista numerowana ','[LIST1]','[/LIST1]');
//myTags[myTags.length] = new Array('idubb_url','url','adres www','[URL]','[/URL]');
//myTags[myTags.length] = new Array('idubb_img','img','obrazek','[IMG]','[/IMG]');
//myTags[myTags.length] = new Array('idubb_color','color','kolor tekstu','[COLOR]','[/COLOR]');
//myTags[myTags.length] = new Array('idubb_email','email','adres e-mail','[EMAIL]','[/EMAIL]');
//myTags[myTags.length] = new Array('idubb_tab','tab','tabulator','[T]','[/T]');
//myTags[myTags.length] = new Array('idubb_br','br','nowa linia','[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\" target=\"_blank\">$1</a>");
	inputreg = inputreg.replace(/\[URL=([^\"]*)\]([^\[]*)\[\/URL\]/ig,"<a href=\"$1\" target=\"_blank\">$2</a>");
	inputreg = inputreg.replace(/\[EMAIL\]([^\[]*)\[\/EMAIL\]/ig,"<a href=\"mailto:$1\">$1</a>");
	inputreg = inputreg.replace(/\[EMAIL=([^\"]*)\](.*)\[\/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" id="' + myTags[i][0] + '" style="background: #FFFFFF url(./images_bbcode/' + myTags[i][1] + '.gif) no-repeat;width:20px;height:20px;padding:2px; margin:2px; border: 1px solid #777777; 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;
    
		do {
		  tmpval = prompt("Proszę wprowadzić kolejny element listy (aby przerwać kliknij anuluj lub wprowadź pste pole):","");
		  if ( tmpval != undefined && tmpval != '') {
		    Content += "[*]" + tmpval + "\n";
		  }
		} while (tmpval != null && tmpval != "" && tmpval != undefined);
		
		Content = Content + tagClose;
	/*
	if ( myButSwitch == 'list' || myButSwitch == 'listnum' ){
    Content = tagOpen;
    
		do {
		  tmpval = prompt("Wprowadź element listy (anuluj aby zakończyć):","");
		  if ( tmpval != undefined && tmpval != '') {
		    Content += "[*]" + tmpval + "\n";
		  }
		} while (tmpval != null && tmpval != "" && tmpval != undefined);
		
		Content = Content + tagClose;
		*/
  }
  // -- URL
  else if ( myButSwitch == 'url') {
    tagOpen = tagOpen.substring(0,tagOpen.length-1);
    Content  = tagOpen;
    tmphref  = prompt("Adres www:","http://");
    tmptext  = '';
    
    if ( mySelection == '' ) {
      tmptext  = prompt("Treść linku:","");
    }
    else {
      tmptext = mySelection;
    }
    
    if ( tmphref == null || tmphref == "" || tmphref == undefined ) { Content = Content + '=#'; }
    else                                                            { Content = Content + '=' + tmphref + ''; }
    
    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("Adres obrazka:","");
    tmptext  = '';
    if ( mySelection == '' ) {
      tmptext  = prompt("Tytuł obrazka (po najechaniu myszką):","");
    }
    else {
      tmptext = mySelection;
    }
    tmpalign = '';
    tmpalign = prompt("Wyrównanie obrazka (możliwe wartości - left, center, right, middle, top, bottom):","");
    
    
    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("Wprowadź kolor tekstu (np. [biały kolor]: #FFFFFF):","");
    tmptext  = '';
    if ( mySelection == '' ) {
      tmptext  = prompt("Wprowadź 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("Wprowadź adres e-mail:","");
    tmptext  = '';
    if ( mySelection == '' ) {
      tmptext  = prompt("Wprowadź tekst linku e-mail:","");
    }
    else {
      tmptext = mySelection;
    }

    if ( tmpemail == null || tmpemail == "" || tmpemail == undefined ) {
      Content = Content + '=#]';
    }
    else {
     Content = Content + '=' + 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);
}
