/**
 * @author spetz
 */

(function ($) {
  	
	$.fn.bbCode = function(textarea, displayInfo) {
		
		this.find('input').click(function() {
			
			toDo = splitTitle($(this).attr('title'));
			
			before = toDo.find('before');
			after = toDo.find('after');
			info = toDo.find('info');
			type = toDo.find('type');
			
			surroundText(textarea, toDo[parseInt(before)+1],toDo[parseInt(after)+1]);
			
		});
		
		this.find('select').change(function() {
			
			toDo = splitTitle($(this).attr('title'));
			
			before = toDo.find('before');
			after = toDo.find('after');
			info = toDo.find('info');
			type = toDo.find('type');
			
			toDo[parseInt(before)+1] = toDo[parseInt(before)+1].replace(/{\?}/g, $(this).val());
			
			surroundText(textarea, toDo[parseInt(before)+1],toDo[parseInt(after)+1]);
			
		});
		
		this.find('input').mouseover(function() {
			
			toDo = splitTitle($(this).attr('title'));
			info = toDo.find('info');
			
			if(info)
				$("#"+displayInfo).val(toDo[parseInt(info)+1]);
			
		});
		
		return this;
		
  	};
	
})(jQuery);

function splitTitle(title) {
	
	var values = title.split(';');
	
	for (i = 0; i < values.length; i++) {
		values[i] = values[i].split(':');
	}
		
	toDo = new Array();
	for (i = 0; i < values.length; i++) {
		for (j = 0; j < values[i].length; j++) {
			toDo.push(values[i][j]);
		}
	}
	
	return toDo;
	
}

function surroundText(textarea, text1, text2){

	textarea = document.getElementById(""+textarea);

	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) {
	
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
		
		if (temp_length == 0) {
			caretPos.moveStart("character", -text2.length);
			caretPos.moveEnd("character", -text2.length);
			caretPos.select();
		} else {
			textarea.focus(caretPos);
		}
	} else {
	
		if (typeof(textarea.selectionStart) != "undefined") {
		
			var begin = textarea.value.substr(0, textarea.selectionStart);
			var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
			var end = textarea.value.substr(textarea.selectionEnd);
			var newCursorPos = textarea.selectionStart;
			var scrollPos = textarea.scrollTop;
			textarea.value = begin + text1 + selection + text2 + end;
			
			if (textarea.setSelectionRange) {
				if (selection.length == 0) 
					textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
				else 
					textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
				textarea.focus();
			}
			
			textarea.scrollTop = scrollPos;
			
		} else {
		
			textarea.value += text1 + text2;
			textarea.focus(textarea.value.length - 1);
			
		}
	}
}
	
function storeCaret(text) {
 	if (typeof(text.createTextRange) != "undefined")
    text.caretPos = document.selection.createRange().duplicate();
}

Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}

