// JavaScript Document

//***************************************************************************************
// + a call from another file could be like this: onClick=javascript:tag_construct('bold','text');
// + possible values for 1. variable: bold, italic, underline, link, image
// + possible values for 2. variable: text, extended
//***************************************************************************************
//
// Version 1.0 - 03. June 2005:
// ============================
// + Introduction of the new bbcode insert system with prompts
//
// Version 1.1 - 05.June 2005:
// ===========================
// + New function insert_text(myField, myValue) for insering
//   the bbcode at the cursor position in textarea
// + New variable field_descriptor in function (tag_name,field_descriptor)
//   so tag_construct2(name) isn't needed anymore
//
// Version 1.2 - 08.June 2005:
// ===========================
// + If a mac based browser is used the selection of text will not recognized
// + Remove the prompt which wants the position of the text insering



//Variables for controlling opening and closing tags (function tag)

var tag = new Array();
//bold tag
tag["bold"] = new Array();
tag["bold"]["open"] = "[b]";
tag["bold"]["close"] = "[/b]";
tag["bold"]["text"] = "Text:";
tag["bold"]["helpline"] = "Удебелете избрания текст: [b]text[/b]";

//italic tag
tag["italic"] = new Array();
tag["italic"]["open"] = "[i]";
tag["italic"]["close"] = "[/i]";
tag["italic"]["text"] = "Text:";
tag["italic"]["helpline"] = "Сложете курсив на избрания текст: [i]text[/i]";

//underline tag
tag["underline"] = new Array();
tag["underline"]["open"] = "[u]";
tag["underline"]["close"] = "[/u]";
tag["underline"]["text"] = "Text:";
tag["underline"]["helpline"] = "Подчертайте избрания текст: [u]text[/u]";

//code tag
tag["code"] = new Array();
tag["code"]["open"] = "[code]";
tag["code"]["close"] = "[/code]";
tag["code"]["text"] = "Text:";
tag["code"]["helpline"] = "Обозначете като код избрания текст: [code]text[/code]";

//url tag
tag["link"] = new Array();
tag["link"]["open"] = "[url]";
tag["link"]["close"] = "[/url]";
tag["link"]["text"] = "Text:Link:";
//tag["link"]["text"] = "Text:,Link:";
tag["link"]["helpline"] = "Добавете линк към избрания текст: [url]text[/url]";

tag["link_ext"] = new Array();
tag["link_ext"]["open"] = "[url href=";
tag["link_ext"]["close"] = "[/url]";
tag["link_ext"]["text"] = "Text:Link:#";
//tag["link"]["text"] = "Text:,Link:";
tag["link_ext"]["helpline"] = "Добавете линк към избрания текст: [url href=http://url]text[/url]";

/*  Original tag
//image tag
tag["image"] = new Array();
tag["image"]["open"] = "[img]";
tag["image"]["close"] = "[/img]";
tag["image"]["text"] = "Link:";
*/

//image tag
tag["image"] = new Array();
tag["image"]["open"] = "[img]";
tag["image"]["close"] = "[/img]";
tag["image"]["text"] = "Link:,id";
tag["image"]["helpline"] = "Insert image: [img]http://image_url[/img]";

//image tag float=right
tag["image_r"] = new Array();
tag["image_r"]["open"] = "[img-right]";
tag["image_r"]["close"] = "[/img]";
tag["image_r"]["text"] = "Link:,id";
tag["image_r"]["helpline"] = "Insert image: [img]http://image_url[/img]";

//image tag float=left
tag["image_l"] = new Array();
tag["image_l"]["open"] = "[img-left]";
tag["image_l"]["close"] = "[/img]";
tag["image_l"]["text"] = "Link:,id";
tag["image_l"]["helpline"] = "Insert image: [img]http://image_url[/img]";

//center tag
tag["center"] = new Array();
tag["center"]["open"] = "[center]";
tag["center"]["close"] = "[/center]";
tag["center"]["text"] = "Text:";
tag["center"]["helpline"] = "Центрирайте избрания текст: [center]text[/center]";

//special tag
tag["special_left"] = new Array();
tag["special_left"]["open"] = "[special_left]";
tag["special_left"]["close"] = "[/special]";
tag["special_left"]["text"] = "Title@Content@Text";
tag["special_left"]["helpline"] = "Поставете избрания текст в каре с ширина 200px отляво";

//special tag
tag["special_right"] = new Array();
tag["special_right"]["open"] = "[special_right]";
tag["special_right"]["close"] = "[/special]";
tag["special_right"]["text"] = "Title@Content@Text";
tag["special_right"]["helpline"] = "Поставете избрания текст в каре с ширина 200px отдясно";

//hr tag
tag["hr"] = new Array();
tag["hr"]["open"] = "[hr]";
tag["hr"]["close"] = "";
tag["hr"]["text"] = "Text:";
tag["hr"]["helpline"] = "Вмъкнете хоризонтален разделител";

//ul tag
tag["ul"] = new Array();
tag["ul"]["open"] = "[ul]";
tag["ul"]["close"] = "[/ul]";
tag["ul"]["text"] = "Text:";
tag["ul"]["helpline"] = "Преобразувайте избрания текст в списък";


//color tag
tag["color"] = new Array();
tag["color"]["open"] = "[color=";
tag["color"]["close"] = "[/color]";
tag["color"]["text"] = "Text^Text";
tag["color"]["helpline"] = "Добавете цвят към избрания текст";

//font size tag
tag["fontsize"] = new Array();
tag["fontsize"]["open"] = "[fontsize=";
tag["fontsize"]["close"] = "[/fontsize]";
tag["fontsize"]["text"] = "Text$Text";
tag["fontsize"]["helpline"] = "Укажете размер на шрифта";

//pagebreak
tag["pagebreak"] = new Array();
tag["pagebreak"]["open"] = "[pagebreak]";
tag["pagebreak"]["close"] = "";
tag["pagebreak"]["text"] = "Text:";
tag["pagebreak"]["helpline"] = "Вмъкнете разделител за страница";

// Function:
// =========
// + Creating non-font tags in textareas
//
// insert values:
// ==============
// + tag_name: Name of the tag, eg. bold, italic, underline, link, image.
// + field_descriptor: Name of the textarea for the inserting of the text.
//
// return values:
// ==============
// + -

function tag_construct(tag_name, field_descriptor, id) {
     var selected_text = '';
     var usertext = '';
     var text = window.document.getElementById(field_descriptor).value;

     //Search for selected text in the textarea for bbcode
     selected_text = get_selected_text(window.document.getElementById(field_descriptor));
     
		 //Search if the tags are already in the selected text
     if ((search_for_tag(tag_name,selected_text) == 1) && (selected_text != '')) {
       window.alert("Този BBCode вече съществува в избрания текст!");
       return;
     }

     if (tag[tag_name]["text"].indexOf(",") == -1 && tag[tag_name]["text"].indexOf("#") == -1 && tag[tag_name]["text"].indexOf("@") == -1 && tag[tag_name]["text"].indexOf("^") == -1 && tag[tag_name]["text"].indexOf("$") == -1) {
	      usertext = selected_text;
         insert_text(window.document.getElementById(field_descriptor),tag[tag_name]["open"] + usertext + tag[tag_name]["close"]);
     }
 	  else if (tag[tag_name]["text"].indexOf("#") != -1) {
		   url = window.prompt("Моля въведете URL на линка:", "http://");
		   if (url != null && url != "" && url != "http://"){
			   var whole_text = tag[tag_name]["open"] + url + "]" + selected_text + tag[tag_name]["close"];
			   insert_text(window.document.getElementById(field_descriptor), whole_text);
		   }
     }
     else if (tag[tag_name]["text"].indexOf("$") != -1) {
		   	
		   var fontsize = window.prompt("Моля въведете големината на шрифта в пиксели:", "");
			var whole_text = tag[tag_name]["open"] + fontsize + "]" + selected_text + tag[tag_name]["close"];
			insert_text(window.document.getElementById(field_descriptor), whole_text);

     }
     else if (tag[tag_name]["text"].indexOf("^") != -1) {
		   	
		   var color = document.getElementById('cp_'+field_descriptor).value;
			var whole_text = tag[tag_name]["open"] + color + "]" + selected_text + tag[tag_name]["close"];
			insert_text(window.document.getElementById(field_descriptor), whole_text);

     }
   	 else if (tag[tag_name]["text"].indexOf("@") != -1) {
		   _title = window.prompt("Моля въведете заглавието на карето:", "");
		   _bottom_txt = window.prompt("Моля въведете текста под карето:", "");
		   
  		 	var whole_text = 
  		 				tag[tag_name]["open"] +  
  		 				"[title]" + _title + "[/title]" +
  		 				"[content]" + selected_text + "[/content]" +
  		 				"[b_text]" + _bottom_txt + "[/b_text]" +
  		 				tag[tag_name]["close"];
			 insert_text(window.document.getElementById(field_descriptor), whole_text);
     }
	  else {
			var el = document.getElementById(id);
			usertext = el.innerHTML;
			
			insert_text(window.document.getElementById(field_descriptor),tag[tag_name]["open"] + usertext + tag[tag_name]["close"]);	
		}
	 
	 	document.getElementById('bold').focus();
}



// Function:
// =========
// + Insert the text at the cursor position. If no cursor position can excluded
//   a prompt to insert the position where the text should be insert will be shown.
// + Code from: http://www.alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript/
//              http://aktuell.de.selfhtml.org/tippstricks/javascript/bbcode/
//
// insert values:
// ==============
// + myField: Name of the textarea for the inserting of the text.
// + myValue: Text which should be insert into the textarea.
//
// return values:
// ==============
// + -

function insert_text(myField, myValue) {
     //IE support
     if (document.selection) {
      myField.focus();
      sel = document.selection.createRange();
      sel.text = myValue;
     }
     //MOZILLA/NETSCAPE support
     else if (myField.selectionStart || myField.selectionStart == '0') {
      var startPos = myField.selectionStart;
      var endPos = myField.selectionEnd;

      myField.value = myField.value.substring(0, startPos)
      + myValue
      + myField.value.substring(endPos, myField.value.length);
     }
     //NON IE/MOZILLA/NETSCAPE browser support
     else {
      myField.value += myValue;
     }
}



// Function:
// =========
// + Get the selected text from the textarea of the site if text was selected.
//
// insert values:
// ==============
// + myField: Name of the textarea where the text should be shown.
//
// return values:
// ==============
// + selected_text: The selected text which was found.
//                  If nothing was selected "" is returned.

function get_selected_text(myField) {
    var selected_text = '';

    if(navigator.platform.toLowerCase() != "macppc") {
     if (window.getSelection) {
      var startPos = myField.selectionStart;
      var endPos = myField.selectionEnd;
      selected_text = myField.value.substring(startPos,endPos);
     } else if (document.getSelection) {
      var startPos = myField.selectionStart;
      var endPos = myField.selectionEnd;
      selected_text = myField.value.substring(startPos,endPos);
     } else if (document.selection) {
      selected_text = document.selection.createRange().text;
     }
    }

    return selected_text;
}



// Function:
// =========
// + Check if the tag the user choose is already in the selected text.
//
// insert values:
// ==============
// + tag_name: Name of the tag which should be searched.
// + text_for_search: Text where should be looked for the tag.
//
// return values:
// ==============
// + found: If the value is "0" the tag could not be found in the text.
//          If the value is "1" the tag was found in the text.

function search_for_tag(tag_name,text_for_search) {

    if (text_for_search.indexOf(tag[tag_name]["open"]) == -1) {
     if (text_for_search.indexOf(tag[tag_name]["close"]) == -1) {
      return 0;
     }
    }

    return 1;
}



// Function:
// =========
// + helpline
//
// insert values:
// ==============
// + tag_name: Name of the tag which should be searched.
//
// return values:
// ==============
// + -

function helpline(tag_name,obj){
	var hlp_line = document.getElementById('hlp_line');
	hlp_line.innerHTML = tag[tag_name]["helpline"];
	
	obj.setAttribute("class", "bb_icon_over");
	obj.setAttribute("className", "bb_icon_over");
	
	return;
}

function helpline_def(obj){
	var hlp_line = document.getElementById('hlp_line');
	var hlp_line_def = document.getElementById('hlp_line_def');
	hlp_line.innerHTML = hlp_line_def.innerHTML;
	
	obj.setAttribute("class", "bb_icon");
	obj.setAttribute("className", "bb_icon");
	return;
}

function open_colorpicker(_var){
	var target_url = "js/color_picker.html?" + _var;
	window.open(target_url,"","width=200,height=330");	
}
