/*
Courtesy from  URL: http://www.corpocrat.com
*/
BBCEditor = {
	instance: null,
	instance_id: '', 
	
	init: function( textarea_id )
	{
		this.instance = document.getElementById( textarea_id );
		this.instance_id = textarea_id;
	},
	
	attachPanel: function()
	{
		$.get('/scripts/BBCEditor_panel.js','',function(data){
			var ids = "#" + BBCEditor.instance_id;
			$(ids).before( data );
		});
		
	}, 
	
	addImage: function()
	{
		var url = prompt('Enter URL:','http://');
		
		var scrollTop = this.instance.scrollTop;
		var scrollLeft = this.instance.scrollLeft;

		if (document.selection) 
		{
			this.instance.focus();
			var sel = document.selection.createRange();
			sel.text = '[img]' + url + '[/img]';
		}
	   else 
	    {
			var len = this.instance.value.length;
		    var start = this.instance.selectionStart;
			var end = this.instance.selectionEnd;
			
	        var sel = this.instance.value.substring(start, end);
		    //alert(sel);
			var rep = '[img]' + url + '[/img]';
	        this.instance.value =  this.instance.value.substring(0,start) + rep + this.instance.value.substring(end,len);
			
				
			this.instance.scrollTop = scrollTop;
			this.instance.scrollLeft = scrollLeft;
		}

	},
	
	addSmiley: function( smiley )
	{
		var scrollTop = this.instance.scrollTop;
		var scrollLeft = this.instance.scrollLeft;

		if( document.selection )
		{
			this.instance.focus();
			var sel = document.selection.createRange();
			//sel.text = '[img]' + smiley + '[/img]';
			sel.text = ' '+smiley;
		}
	   else 
	    {
			var len = this.instance.value.length;
		    var start = this.instance.selectionStart;
			var end = this.instance.selectionEnd;
			
	        var sel = this.instance.value.substring(start, end);
		    //alert(sel);
			//var rep = '[img]' + smiley + '[/img]';
			var rep = ' '+smiley;
	        this.instance.value =  this.instance.value.substring(0,start) + rep + this.instance.value.substring(end,len);
			
				
			this.instance.scrollTop = scrollTop;
			this.instance.scrollLeft = scrollLeft;
		}

	},
	
	addHyperlink: function()
	{

		var url = prompt('Enter URL:','http://');
		url = ((url==null) || (url==undefined)) ? '' : url ;
		
		var scrollTop = this.instance.scrollTop;
		var scrollLeft = this.instance.scrollLeft;

		if (document.selection) 
		{
			this.instance.focus();
			var sel = document.selection.createRange();
			
			if(sel.text==""){
				sel.text = '[url]'  + url + '[/url]';
			} else {
				sel.text = '[url=' + url + ']' + sel.text + '[/url]';
			}			

			//alert(sel.text);
			var url = prompt("URL:", sel_value);
			
		}
	   else 
	    {
			var len = this.instance.value.length;
		    var start = this.instance.selectionStart;
			var end = this.instance.selectionEnd;
			
	        var sel = this.instance.value.substring(start, end);
			
			if(sel==""){
				var rep = '[url]' + url + '[/url]';
			} else
			{
				var rep = '[url=' + url + ']' + sel + '[/url]';
			}
		    //alert(sel);
			
	        this.instance.value =  this.instance.value.substring(0,start) + rep + this.instance.value.substring(end,len);
			
				
			this.instance.scrollTop = scrollTop;
			this.instance.scrollLeft = scrollLeft;
		}
	},

	addTags: function(tag1,tag2)
	{

		// Code for IE
		if (document.selection) 
		{
			this.instance.focus();
			var sel = document.selection.createRange();
			//alert(sel.text);
			sel.text = tag1 + sel.text + tag2;
		}
		else 
	    {  // Code for Mozilla Firefox
			var len = this.instance.value.length;
		    var start = this.instance.selectionStart;
			var end = this.instance.selectionEnd;
			
			
			var scrollTop = this.instance.scrollTop;
			var scrollLeft = this.instance.scrollLeft;

			
	        var sel = this.instance.value.substring(start, end);
		    //alert(sel);
			var rep = tag1 + sel + tag2;
	        this.instance.value =  this.instance.value.substring(0,start) + rep + this.instance.value.substring(end,len);
			
			this.instance.scrollTop = scrollTop;
			this.instance.scrollLeft = scrollLeft;
			
			
		}
	},

	addList: function(tag1,tag2)
	{
		// Code for IE
		if (document.selection) 
		{
			this.instance.focus();
			var sel = document.selection.createRange();
			var list = sel.text.split('\n');
	
			for(i=0;i<list.length;i++) 
			{
			list[i] = '[*]' + list[i];
			}
			//alert(list.join("\n"));
			sel.text = tag1 + '\n' + list.join("\n") + '\n' + tag2;
		} else
		// Code for Firefox
		{

			var len = this.instance.value.length;
		    var start = this.instance.selectionStart;
			var end = this.instance.selectionEnd;
			var i;
			
			var scrollTop = this.instance.scrollTop;
			var scrollLeft = this.instance.scrollLeft;

			
	        var sel = this.instance.value.substring(start, end);
		    //alert(sel);
			
			var list = sel.split('\n');
			
			for(i=0;i<list.length;i++) 
			{
			list[i] = '[*]' + list[i];
			}
			//alert(list.join("<br>"));
	        
			
			var rep = tag1 + '\n' + list.join("\n") + '\n' +tag2;
			this.instance.value =  this.instance.value.substring(0,start) + rep + this.instance.value.substring(end,len);
			
			this.instance.scrollTop = scrollTop;
			this.instance.scrollLeft = scrollLeft;
		}
	}, 
	
	toggleSmily: function(){
		var div = document.getElementById('smileys_ctrl');
		if(div){
			if(div.style.display == 'none'){
				div.style.display = 'block';
			}else{
				div.style.display = 'none';
			}
		}
	}
};
