
Xsn.BbEditor = function (){
	this.range;
	this.colordlg;
	this.callback;
}	

Xsn.BbEditor.prototype = {

	init : function(){
		var colordlg = new Ext.LayoutDialog(Ext.id(), { 
	        autoTabs:true,
	        modal:true,
	        autoCreate:true,
	        title: 'Kleur kiezen',
	        width:300,
	        height:200,
	        resizable:false,
	        shadow:true,
	        center: {
				titlebar:false,
				autoScroll:false
			}
	    });
	    colordlg.addKeyListener(27, colordlg.hide, colordlg);
	    colordlg.addButton('Annuleren', colordlg.hide, colordlg);
		var p = colordlg.getLayout().getEl().createChild({tag:'div', id:'color-swatch-bd'});	    
		colordlg.getLayout().add('center',new Ext.ContentPanel(p, {fitToFrame:true}));										    
		this.colordlg = colordlg;	
	},
	
	initColors : function(colors){
	 	this.colordlg.getLayout().getRegion('center').getActivePanel().getEl().dom.innerHTML = '';
		var t = new Ext.MasterTemplate(
			'<ul class="color-palette clearfix" unselectable="on">',
				'<tpl name="colors"><li id="c-{text}" title="{text}" style="background-color:{value}"></li></tpl>',						
			'</ul>'
		);
								            
	    for (var i=0;i<colors.length;i++){
			t.add('colors', {value: colors[i].waarde, text: colors[i].naam});		             	
		}
		var swatch = t.append(this.colordlg.getLayout().getRegion('center').getActivePanel().getEl(),{}, true);
	    var els = swatch.select('li');
		els.on('click', function(e){
			this.colordlg.hide();
		 	this.execCmd('color', {color: e.getTarget().id.substring(2)});
			if (this.callback) this.callback();													
	    },this);
	},
	
	chooseColor : function(callback){
	 	if (!this.target) return;	 
	 	this.createRange();
		this.callback = callback;
		this.colordlg.show();
	},
	
	inputLink : function(callback){
	 	if (!this.target) return;	 
	 	this.createRange();	 	
		Ext.MessageBox.prompt('Link invoegen', 'Voer de url in', function(btn, text){
			if (btn != "cancel" && text != ''){
 				if ((text+'').indexOf('http://', 0) === -1){
					text = 'http://' + text;
				}
				this.execCmd('link', {url:  text});                         				 			
				callback();
			}
		}, this);
	},

	inputMailtoLink : function(callback){
	 	if (!this.target) return;	 
	 	this.createRange();	 	
		Ext.MessageBox.prompt('Email-link invoegen', 'Voer het emailadres in', function(btn, text){
			if (btn != "cancel" && text != ''){
				this.execCmd('link', {url: 'mailto:' + text});                         				 			
				callback();
			}
		}, this);
	},
	
	formatText : function(cmd){
	 	if (!this.target) return;
		this.createRange();
		return this.execCmd(cmd);
	},
	
	setTarget : function(target){
		this.target = target;
	},
 
	createRange : function(){
		if (Ext.isIE) this.range = document.selection.createRange();
	},
	
	execCmd : function(cmd, config){
	 	var tag, open, close;
	 	
		switch (cmd){
			case "bold":
				tag = 'vet';
				break;
			case "italic":
				tag = 'i';
				break;
			case "underline":
				tag = 'u';
				break;
			case "color":
				tag = config.color;
				break;
			case "link":
				tag = config.url;		
				break;				
			default:
		}
		
		if (tag){
			if (cmd == 'link'){
				open = "[link="+ tag + "]";
				close = "[/link]";			
			} else {
				open = '[' + tag + ']';
				close = '[/' + tag + ']';			
			} 
			return this.toggleTag(open, close);
		}
	},
	
	toggleTag : function(open, close){
	 	var target = this.target;
		var stop = target.scrollTop;
		if (target.selectionStart != null) {
			var ss = target.selectionStart;
			var se = target.selectionEnd;					
		
	  		var sl = (target.value).substring(target.selectionStart, target.selectionEnd);
			if (sl != "") {
				var chary = (target.value).substring(target.selectionEnd - 1, target.selectionEnd);
				var spacey = " ";
					if (sl.substr(0,open.length)==open) {
				 		var rpl = sl.replace(open,"");
						target.value = (target.value).substring(0, target.selectionStart) + rpl.replace(close,"") + (target.value).substring(target.selectionEnd);
						target.selectionStart = ss;
						target.setSelectionRange(ss, se - open.length - close.length); 
					} else {
						if (chary == spacey) {
			  				sl = (target.value).substring(target.selectionStart, target.selectionEnd - 1);
							target.value = (target.value).substring(0, target.selectionStart) + open + sl + close + " " + (target.value).substring(target.selectionEnd);
							se = se - 1;
						} else {
							target.value = (target.value).substring(0, target.selectionStart) + open + sl + close + (target.value).substring(target.selectionEnd);
						}
						target.selectionStart = ss;
						target.setSelectionRange(ss, se + open.length + close.length); 
					}
			}
		} else {
		 	var range = this.range;
			var myText = range.text;
			var chary = myText.substring(myText.length - 1, myText.length);
			var spacey = " ";
			
			if (myText.substr(0,open.length)==open) {
			 	myText = myText.replace(open,"");
				myText = myText.replace(close,"")
				range.text = myText;
			} else {
				if (chary == spacey) {
					range.text = open + myText.substring(0, myText.length-1) + close + " ";
				} else {
					range.text = open + myText + close;
				};
			};
			//range.select();
		};
		target.focus();
		target.scrollTop = stop;
		return true;			
	}
}

Xsn.BbEditor = new Xsn.BbEditor();

