/*
 * Plugin jQuery.BBCode
 * Version 0.2 
 *
 * Based on jQuery.BBCode plugin (http://www.kamaikinproject.ru)
 */
(function($){
  jQuery.fn.bbcode = function(options){
		// default settings
    var options = jQuery.extend({
	  tag_bold: true,
	  tag_italic: true,
	  tag_underline: true,
	  tag_link: true,
	  tag_image: true,
		tag_quote: true,
		tag_ol: true,
		tag_ul: true,
		tag_big: true,
		tag_small: true,
		tag_hr: true,
		tag_code: true,
		tag_php: true,
      button_image: true,
      image_url: 'bbimage/'
    },options||{});


	//  panel
    var text = '<div id="bbcode_bb_bar">'
    if(options.tag_bold){
      text = text + '<a href="#" id="b" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-bold.gif" />';
      }else{
        text = text + 'Bold';
      }
      text = text + '</a>';
    }
    if(options.tag_italic){
      text = text + '<a href="#" id="i" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-italic.gif" />';
      }else{
        text = text + 'Italic';
      }
      text = text + '</a>';
    }
    if(options.tag_underline){
      text = text + '<a href="#" id="u" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-underline.gif" />';
      }else{
        text = text + 'Undescore';
      }
      text = text + '</a>';
    }
    if(options.tag_link){
      text = text + '<a href="#" id="url" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-link.gif" />';
      }else{
        text = text + 'Link';
      }
      text = text + '</a>';
    }
    if(options.tag_image){
      text = text + '<a href="#" id="img" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-image.gif" />';
      }else{
        text = text + 'Image';
      }
      text = text + '</a>';
    }
//	  tag_quote: true,
    if(options.tag_quote){
      text = text + '<a href="#" id="quote" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-quote.gif" />';
      }else{
        text = text + 'Quote';
      }
      text = text + '</a>';
    }
//	  tag_ol: true,
    if(options.tag_ol){
      text = text + '<a href="#" id="ol" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-ol.gif" />';
      }else{
        text = text + 'Ordered list';
      }
      text = text + '</a>';
    }
//	  tag_ul: true,
    if(options.tag_ul){
      text = text + '<a href="#" id="ul" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-ul.gif" />';
      }else{
        text = text + 'Unordered list';
      }
      text = text + '</a>';
    }
//	  tag_big: true,
    if(options.tag_big){
      text = text + '<a href="#" id="big" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-big.gif" />';
      }else{
        text = text + 'Big';
      }
      text = text + '</a>';
    }
//	  tag_small: true,
    if(options.tag_small){
      text = text + '<a href="#" id="small" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-small.gif" />';
      }else{
        text = text + 'Small';
      }
      text = text + '</a>';
    }
//	  tag_hr: true,
    if(options.tag_hr){
      text = text + '<a href="#" id="hr" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-hr.gif" />';
      }else{
        text = text + 'HR';
      }
      text = text + '</a>';
    }
//	  tag_code: true,
    if(options.tag_code){
      text = text + '<a href="#" id="code" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-code.gif" />';
      }else{
        text = text + 'Code';
      }
      text = text + '</a>';
    }
//	  tag_php: true,
    if(options.tag_php){
      text = text + '<a href="#" id="php" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bb-php.gif" />';
      }else{
        text = text + '&lt;?PHP?&gt;';
      }
      text = text + '</a>';
    }

    text = text + '</div>';
    
    jQuery(this).wrap('<div id="bbcode_container"></div>');
    jQuery("#bbcode_container").prepend(text);
    jQuery("#bbcode_bb_bar a img").css("border", "none");
    var id = '#' + jQuery(this).attr("id");
    var e = jQuery(id).get(0);
    
    jQuery('#bbcode_bb_bar a').click(function() {
      var button_id = jQuery(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=' + param + ']';
		 }
      insert(start, end, e);
      return false;
    });
	}
  function insert(start, end, element) {
	  console.debug(element);
	  console.debug(element.selectionStart);
    if (document.selection) {
		alert(10);
       element.focus();
       sel = document.selection.createRange();
       sel.text = start + sel.text + end;
    } else if (element.selectionStart || element.selectionStart == '0') {
		alert(20);
       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 {
		alert(30);
      element.value += start + end;
    }
  }
 
// hotkeys 
//jQuery(document).keyup(function (e)
//{if(e.which == 17) isCtrl=false;}).keydown(function (e)
//{if(e.which == 17) isCtrl=true;
//if (e.which == 66 && isCtrl == true) // CTRL + B, bold
//{
//	jQuery("#b").click();
//	return false;
//}
//else if (e.which == 73 && isCtrl == true) // CTRL + I, italic
//{
//	jQuery("#i").click();
//	return false;
//}
//else if (e.which == 85 && isCtrl == true) // CTRL + U, underline
//{
//	jQuery("#u").click();
//	return false;
//}
//})
  
})(jQuery)
