var forum = {
  snippets: {
    "strong" : ["[strong]\n\t","something here","\n[/strong]"],
    "em" : ["[em]\n\t","something here","\n[/em]"],
    "quote" : ["[quote]\n\t","something here","\n[/quote]"],
    "code" : ["[code]\n\t","something here","\n[/code]"],
    "$" : {
      snippet:["$('","id')","."],
      tab:['id','']
    },
    "ul" : {
      snippet:["[ul]\n\t[li]","something here","[/li]\n[/ul]"],
      tab:['something here',''],
      start: 5
    },
    "ol" : {
      snippet:["[ol]\n    [li]","something here","[/li]\n[/ol]"],
      tab:['something',''],
      completion: {
        'something':['text','snippet']
      },
      loop: true, //optional, default true
      start: 5 //position snippet[2] default false == snippet[2].length, true == 0
    },
    "li" : {
      snippet:["[li]","something here","[/li]"],
      tab:['something here','']
    },
    "[/li]" : {
      snippet:["[/li]\n[li]","something here","[/li]"],
      tab:['something here','']
    },
    "date" : {
      command: function(k) {
        var dayNames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],
            monthNames = ["January","February","March","April","May","June","July","August","September","October","November","December"],
            dt = new Date(),
            y  = dt.getYear();
        if (y < 1000) y +=1900;
        return {
          //key:"date", optional
          snippet:['',dayNames[dt.getDay()] + ", " + monthNames[dt.getMonth()] + " " + dt.getDate() + ", " + y,' '],
          tab:[dayNames[dt.getDay()] + ", " + monthNames[dt.getMonth()] + " " + dt.getDate() + ", " + y,'']
        };
      }
    },
    "url" : {
      snippet:['[url=','http://]text','[/url]'],
      tab:['http://', 'text','']
    },
    "mootorial" : {
      snippet:['[url=','http://clientside.cnet.com/wiki/mootorial]the mootorial','[/url]'],
      tab:['the mootorial','']
    },
    "{" : ["{\n\t","","\n"]
  },
  smartTypingPairs: {
    '"' : '"',
    '(' : ')',
    '{' : '}',
    '[' : ']',
    "<" : ">",
    "`" : "`",
    "'" : {
      scope:{
        "[code]":"[/code]"
      },
      pair:"'"
    }
  },
  //ctrl+shift+number
  selections: {
    "0": function(sel) {
  		return ['[strong]',sel,'[/strong]'];
  	},
  	"1": function(sel) {
  		return ['[em]',sel,'[/em]'];
  	},
  	"2": function(sel) {
  		return ['[quote]',sel,'[/quote]'];
  	},
  	"3": function(sel) {
  		return ['[code]',sel,'[/code]'];
  	},
  	"4": function(sel) {
  		return ['[url=',sel,']text[/url]'];
  	},
  	"5": function(sel) {
  		return {
  		  selection: [this.ss(),this.se()],
  		  snippet: ['',sel.toLowerCase(),'']
  		};
  	},
  	"6": function(sel) {
  		return ['',sel.toUpperCase(),''];
  	},
  	"7": function(sel) {
  		var mtoc = /<([^<>]*)>/g;
  		return ['',sel.replace(mtoc,"&lt;$1&gt;"),''];
  	}
  }
};

var Forum = {

	init: function(){
		
		if (window.khtml){
			$$('pre').each(function(pre){
				pre.setStyle('max-width', 1);
				var div = pre.getParent().getParent();
				pre.setStyle('max-width', div.offsetWidth - 30);
			});
		}
		
		$$('textarea').each(function(textarea){
			Forum.parse(textarea);
		});
		
		if (window.khtml) $$('input.longinput').setStyle('width', '100%');
	},
	
	parse: function(txa){
		if (window.khtml) txa.setStyle('width', '100%');
		new PostEditor(txa, forum);
		var toolbar = $(txa.id + '-tool');
		if (!toolbar) return;		
		toolbar.getElements('a.tool').each(function(link){
			link.onclick = function(){
				if (this.hasClass('br')) Textarea.tag(txa, this.rel, "\n");
				else Textarea.tag(txa, this.rel);
				return false;
			}
		});
	}

};

window.addEvent('domready', Forum.init);

var Textarea = {

	tag: function(txa, xpr, appnd){
		appnd = appnd || '';
		var start = txa.selectionStart;
		var end = txa.selectionEnd
		var bits = xpr.split('$');
		var sel = txa.value.substring(start, end);
		if (!sel.length) sel = "something here";
		var content = appnd+bits[0]+appnd+sel+appnd+bits[1]+appnd;
		txa.value = txa.value.substring(0, start) + content + txa.value.substring(end, txa.value.length);
	},
	
	write: function(txa, content){
		var start = txa.selectionStart;
		txa.value = txa.value.substring(0, start) + content + txa.value.substring(start, txa.value.length);
	}
};