/*
 * 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_link: true,
      tag_image: false,
      button_image: true,
      image_url: 'http://igala.net/bbimage/',
	  cute_smiley16: true,
	  cute_smiley17: true,
	  cute_smiley18: false,
	  cute_smiley19: false,
	  cute_smiley2: true,
	  cute_smiley20: true,
	  cute_smiley23: true,
	  cute_smiley3: true,
	  cute_smiley75: true,
	  cute_smiley69: true,
	  cute_smiley65: true,
	  cool_byebye: true,
	  cool_look_down:true,
	  cool_hell_boy: true,
	  cool_beauty: true,
	  cool_beat_brick: true,
	  cool_cool: true,
	  cool_too_sad: true,
	  green5: true,
	  green14: true,
	  green22: true,
	  green26: true,
	  green29: true,
	  green31: true
    },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 + 'bold.png" />';
      }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 + 'italic.png" />';
      }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 + 'underline.png" />';
      }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 + 'link.png" />';
      }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 + 'image.png" />';
      }else{
        text = text + 'Image';
      }
      text = text + '</a>';
    }
	
    if(options.cute_smiley16){
      text = text + '<a href="#" id="chaymaumui" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'cute_smiley16.gif" />';
      }else{
        text = text + 'chaymaumui';
      }
      text = text + '</a>';
    }
    if(options.cute_smiley17){
      text = text + '<a href="#" id="dauhang" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'cute_smiley17.gif" />';
      }else{
        text = text + 'dauhang';
      }
      text = text + '</a>';
    }
    if(options.cute_smiley18){
      text = text + '<a href="#" id="leluoi" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'cute_smiley18.gif" />';
      }else{
        text = text + 'leluoi';
      }
      text = text + '</a>';
    }
	if(options.cute_smiley19){
      text = text + '<a href="#" id="meomat" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'cute_smiley19.gif" />';
      }else{
        text = text + 'meomat';
      }
      text = text + '</a>';
    }
	if(options.cute_smiley2){
      text = text + '<a href="#" id="liec" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'cute_smiley2.gif" />';
      }else{
        text = text + 'liec';
      }
      text = text + '</a>';
    }
	if(options.cute_smiley20){
      text = text + '<a href="#" id="phe" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'cute_smiley20.gif" />';
      }else{
        text = text + 'phe';
      }
      text = text + '</a>';
    }
	if(options.cute_smiley23){
      text = text + '<a href="#" id="khoc" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'cute_smiley23.gif" />';
      }else{
        text = text + 'khoc';
      }
      text = text + '</a>';
    }
	if(options.cute_smiley3){
      text = text + '<a href="#" id="soi" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'cute_smiley3.gif" />';
      }else{
        text = text + 'soi';
      }
      text = text + '</a>';
    }
	if(options.cute_smiley75){
      text = text + '<a href="#" id="yeu" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'cute_smiley75.gif" />';
      }else{
        text = text + 'yeu';
      }
      text = text + '</a>';
    }
	if(options.cute_smiley69){
      text = text + '<a href="#" id="choang" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'cute_smiley69.gif" />';
      }else{
        text = text + 'choang';
      }
      text = text + '</a>';
    }
	if(options.cute_smiley65){
      text = text + '<a href="#" id="xiu" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'cute_smiley65.gif" />';
      }else{
        text = text + 'xiu';
      }
      text = text + '</a>';
    }
	if(options.cool_byebye){
      text = text + '<a href="#" id="cool_byebye" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + '1cool_byebye.gif" />';
      }else{
        text = text + 'cool_byebye';
      }
      text = text + '</a>';
    }
	if(options.cool_look_down){
      text = text + '<a href="#" id="cool_look_down" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + '1cool_look_down.gif" />';
      }else{
        text = text + 'cool_look_down';
      }
      text = text + '</a>';
    }
	if(options.cool_hell_boy){
      text = text + '<a href="#" id="cool_hell_boy" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + '2cool_hell_boy.gif" />';
      }else{
        text = text + 'cool_hell_boy';
      }
      text = text + '</a>';
    }
	if(options.cool_beauty){
      text = text + '<a href="#" id="cool_beauty" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + '4cool_beauty.gif" />';
      }else{
        text = text + 'cool_beauty';
      }
      text = text + '</a>';
    }
	if(options.cool_beat_brick){
      text = text + '<a href="#" id="cool_beat_brick" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + '6cool_beat_brick.gif" />';
      }else{
        text = text + 'cool_beat_brick';
      }
      text = text + '</a>';
    }
	if(options.cool_cool){
      text = text + '<a href="#" id="cool_cool" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + '8cool_cool.gif" />';
      }else{
        text = text + 'cool_cool';
      }
      text = text + '</a>';
    }
	if(options.cool_too_sad){
      text = text + '<a href="#" id="cool_too_sad" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + '9cool_too_sad.gif" />';
      }else{
        text = text + 'cool_too_sad';
      }
      text = text + '</a>';
    }
	if(options.green5){
      text = text + '<a href="#" id="green5" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'green5.gif" />';
      }else{
        text = text + 'green5';
      }
      text = text + '</a>';
    }
	if(options.green14){
      text = text + '<a href="#" id="green14" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'green14.gif" />';
      }else{
        text = text + 'green14';
      }
      text = text + '</a>';
    }
	if(options.green22){
      text = text + '<a href="#" id="green22" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'green22.gif" />';
      }else{
        text = text + 'green22';
      }
      text = text + '</a>';
    }
	if(options.green26){
      text = text + '<a href="#" id="green26" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'green26.gif" />';
      }else{
        text = text + 'green26';
      }
      text = text + '</a>';
    }
	if(options.green29){
      text = text + '<a href="#" id="green29" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'green29.gif" />';
      }else{
        text = text + 'green29';
      }
      text = text + '</a>';
    }
	if(options.green31){
      text = text + '<a href="#" id="green31" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'green31.gif" />';
      }else{
        text = text + 'green31';
      }
      text = text + '</a>';
    }	
	
    text = text + '</div>';
    
    $(this).wrap('<div id="bbcode_container"></div>');
    $("#bbcode_container").prepend(text);
    $("#bbcode_bb_bar a img").css("border", "none");
    var id = '#' + $(this).attr("id");
    var e = $(id).get(0);
    
    $('#bbcode_bb_bar 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 + ']';
		 }
		else if ( (button_id=='chaymaumui')||(button_id=='dauhang')||(button_id=='leluoi')||(button_id=='meomat')||(button_id=='liec')||(button_id=='phe')||(button_id=='khoc')||(button_id=='soi')||(button_id=='yeu')||(button_id=='choang')||(button_id=='xiu')
		||(button_id=='cool_byebye')||(button_id=='cool_look_down')||(button_id=='cool_hell_boy')||(button_id=='cool_beauty')||(button_id=='cool_beat_brick')||(button_id=='cool_cool')||(button_id=='cool_too_sad')||(button_id=='green5')||(button_id=='green14')||(button_id=='green22')||(button_id=='green26')||(button_id=='green29')||(button_id=='green31')
		){
			end = '';
		}
		
      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;
    }
  }
 
// hotkeys 
$(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
{ 
	$("#b").click();
	return false;
} 
else if (e.which == 73 && isCtrl == true) // CTRL + I, italic
{ 
	$("#i").click();
	return false;
} 
else if (e.which == 85 && isCtrl == true) // CTRL + U, underline
{ 
	$("#u").click();
	return false;
}
})
  
})(jQuery)