/*
 * Plugin jQuery.BBCode
 * Version 0.2 
 *
 * Based on jQuery.BBCode plugin (http://www.kamaikinproject.ru)
 */
(function($){
  $.fn.bbcode = function(options){
		// default settings
    var options = $.extend({
      tag_bold: true,
      tag_italic: true,
      tag_underline: true,
      tag_quote: true,
      tag_link: false,
      tag_image: false,
      button_image: true,
      image_url: 'http://enklawanetwork.pl/layout/images/comment/'
    },options||{});
    //  panel 
    var text = '<div id="comment-bbcontrols">'
    if(options.tag_bold){
      text = text + '<a href="#" id="b" title="Pogrubienie">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bold.jpg" />';
      }else{
        text = text + 'Pogrubienie';
      }
      text = text + '</a>';
    }
    if(options.tag_italic){
      text = text + '<a href="#" id="i" title="Kursywa">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'italic.jpg" />';
      }else{
        text = text + 'Kursywa';
      }
      text = text + '</a>';
    }
    if(options.tag_underline){
      text = text + '<a href="#" id="u" title="Podkreślenie">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'underline.jpg" />';
      }else{
        text = text + 'Podkreślenie';
      }
      text = text + '</a>';
    }
    if(options.tag_quote){
      text = text + '<a href="#" id="quote" title="Cytat">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'quote.jpg" />';
      }else{
        text = text + 'Cytat';
      }
      text = text + '</a>';
    }
    if(options.tag_link){
      text = text + '<a href="#" id="url" title="Link">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'link.jpg" />';
      }else{
        text = text + 'Link';
      }
      text = text + '</a>';
    }
    if(options.tag_image){
      text = text + '<a href="#" id="img" title="Obraz">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'image.jpg" />';
      }else{
        text = text + 'Obraz';
      }
      text = text + '</a>';
    }
    text = text + '</div>';
    
    $(this).wrap('<div id="bbcode_container"></div>');
    $("#bbcode_container").prepend(text);
    $("#comment-bbcontrols a img").css("border", "none");
    var id = '#' + $(this).attr("id");
    var e = $(id).get(0);
    
    $('#comment-bbcontrols a').click(function() {
      var button_id = $(this).attr("id");
      var start = '['+button_id+']';
      var end = '[/'+button_id+']';

	  var param="";
	  if (button_id=='img')
	  {
	     param=prompt("Enter image URL","http://");
		 if (param)
			start+=param;
		 }
	  else if (button_id=='url')
	  {
			param=prompt("Enter URL","http://");
			if (param) 
				start = '[url href=' + param + ']';
		 }
      insert(start, end, e);
      return false;
    });
	}
  function insert(start, end, element) {
    if (document.selection) {
       element.focus();
       sel = document.selection.createRange();
       sel.text = start + sel.text + end;
    } else if (element.selectionStart || element.selectionStart == '0') {
       element.focus();
       var startPos = element.selectionStart;
       var endPos = element.selectionEnd;
       element.value = element.value.substring(0, startPos) + start + element.value.substring(startPos, endPos) + end + element.value.substring(endPos, element.value.length);
    } else {
      element.value += start + end;
    }
  }
  
})(jQuery)
