var CPDCodeUtils = Class.create({
	initialize: function() {
		this.target = null;
	},

	setTarget: function(txt) {
		this.target = $(txt);
	},

	getSelection: function()
	{
		if (Prototype.Browser.IE) {
			return document.selection.createRange().text;
		} else {
			return this.target.value.substring(this.target.selectionStart, this.target.selectionEnd);
		}
	},

	replaceSelection: function(replace, offset)
	{
		var scroll = this.target.scrollTop;
		if (Prototype.Browser.IE) {
			document.selection.createRange().text = replace;
		} else {
			var end = this.target.selectionStart + replace.length;
			this.target.value = 
				this.target.value.substring(0, this.target.selectionStart)
				+ replace
				+ this.target.value.substring(this.target.selectionEnd);
			this.target.selectionStart = this.target.selectionEnd = end - offset;
		}
		this.target.scrollTop = scroll;
		this.target.focus();
	},

	emptySelection: function()
	{
		return ((Prototype.Browser.IE && document.selection.createRange().text.length == 0)
			|| (this.target.selectionStart == this.target.selectionEnd));
	},

	addText: function (add)
	{
		var scroll = this.target.scrollTop;
		if (Prototype.Browser.IE) {
			sel = document.selection.createRange().text;
			document.selection.createRange().text = add + sel;
		} else {
			var end = this.target.selectionEnd + add.length;
			this.target.value = 
				this.target.value.substring(0, this.target.selectionStart)
				+ add
				+ this.target.value.substring(this.target.selectionStart);
			this.target.selectionStart = this.target.selectionEnd = end;
		}
		this.target.scrollTop = scroll;
		this.target.focus();
	},

	enTag: function(openTag, closeTag)
	{
		var selection = this.getSelection();
		this.replaceSelection(openTag + selection + closeTag, (selection.length == 0 ? closeTag.length : 0));
	},

	url: function(url)
	{
		if (url.length > 0 && !this.emptySelection()) {
			this.enTag('[url='+url+']', '[/url]');
		} else if (url.length > 0) {
			this.addText('[url]'+url+'[/url]');
		} else if (!this.emptySelection()) {
			this.enTag('[url]', '[/url]');
		} else {
		}
	},
 	img: function(url)
	{
		if (url.length > 0) {
			this.addText('[img]' + url + '[/img]');
		} else if (!this.emptySelection()) {
			this.enTag('[img]', '[/img]');
		}
	}
});

// effetti per il quote di quote di quote in modalita' lettura
var CPDCodeQuote = Class.create({
	filter: function(e) {
		return e.tagName == 'FIELDSET';
	},
	// espando me e i miei padri
	expand: function(fld)
	{
		var grow = function(e){ 
			e.addClassName('standard').writeAttribute('zoom');
		};

		var asc = fld.ancestors().findAll(this.filter);
		asc.push(fld);
		asc.each(grow);
	},

	// comprimo me e miei figli
	collapse: function(fld)
	{
		var shrink = function(e){ 
			e.removeClassName('standard').removeAttribute('zoom');
		};

		var desc = fld.descendants().findAll(this.filter);
		desc.push(fld);
		desc.each(shrink);
	},

	over: function(event)
	{
		var fld = event.element();
		if (fld.tagName == 'LEGEND') fld = fld.up();

		fld.addClassName('over');

		var asc = fld.ancestors().findAll(this.filter);
		asc.invoke('removeClassName', 'over');

		event.stop();
	},

	out: function(event)
	{
		var fld = event.element();
		if (fld.tagName == 'LEGEND') fld = fld.up();

		fld.removeClassName('over');
		event.stop();
	},

	toggleZoom: function(event)
	{
		var fld = event.element();
		if (fld.tagName == 'LEGEND') fld = fld.up();

		if (fld.readAttribute('zoom')) {
			this.collapse(fld);
		} else {
			this.expand(fld);
		}
		event.stop();
	},

	observe: function(post)
	{
		var fieldsets = (post ? $$('#post_'+post+' fieldset') : $$('fieldset'));
		// sui fieldset
		fieldsets.invoke('observe', 'click', this.toggleZoom.bindAsEventListener(this))
			.invoke('observe', 'mouseover', this.over.bindAsEventListener(this))
			.invoke('observe', 'mouseout', this.out.bindAsEventListener(this));
		// sui legend
		fieldsets.invoke('firstDescendant').compact().
			invoke('observe', 'click', this.toggleZoom.bindAsEventListener(this))
			.invoke('observe', 'mouseover', this.over.bindAsEventListener(this))
			.invoke('observe', 'mouseout', this.out.bindAsEventListener(this));
	}
});

var CPDCode = Class.create({
	initialize: function() 
	{
		this.target = null;
		this.Utils = new CPDCodeUtils();
		this.Quote = new CPDCodeQuote();
		this.shortcut = new CShortcut();
	},

	setTarget: function(txt)
	{

// 		if (this.target == null || this.target != txt) {
			this.target = txt;
			this.Utils.setTarget(txt);

			this.shortcut.removeAll();

			if (txt) {
				this.shortcut.add('Ctrl+B', this.manageShortcut.curry(this.tag.curry('b')).bind(this), {target: this.target});
				this.shortcut.add('Ctrl+I', this.manageShortcut.curry(this.tag.curry('i')).bind(this), {target: this.target});
				this.shortcut.add('Ctrl+U', this.manageShortcut.curry(this.tag.curry('u')).bind(this), {target: this.target});
				this.shortcut.add('Ctrl+S', this.manageShortcut.curry(this.tag.curry('s')).bind(this), {target: this.target});
// 				this.shortcut.add('Ctrl+P', this.manageShortcut.curry(this.tag.curry('pre')).bind(this), {target: this.target});
// 				this.shortcut.add('Ctrl+K', this.manageShortcut.curry(this.tag.curry('code')).bind(this), {target: this.target});
				this.shortcut.add('Ctrl+L', this.manageShortcut.curry(this.list.curry(null)).bind(this), {target: this.target});
// 				this.shortcut.add('Ctrl+M', this.manageShortcut.curry(this.img).bind(this), {target: this.target});
// 				this.shortcut.add('Ctrl+H', this.manageShortcut.curry(this.url).bind(this), {target: this.target});
// 				this.shortcut.add('Ctrl+E', this.manageShortcut.curry(Emo.toggle.curry(this.target).bind(Emo)).bind(this), {target: this.target});
			}
// 		}
	},

	manageShortcut: function(f) 
	{
		if ($(this.target))
			f.call(this);
	},

	emo: function(keys)
	{
		this.Utils.addText(keys);
	},

	tag: function(tag)
	{
		var op = '['+tag+']' ;
		var cl = '[/'+tag+']';
		this.Utils.enTag(op, cl);
	},

	list: function(type)
	{
		var sel = this.Utils.getSelection();
		var l;
		switch(type) {
		case '1':
			l = '[list=1]'
			break;
		case 'a':
			l = '[list=a]'
			break;
		default:
			l = '[list]'
			break;
		}
		l += '\n\t[*]' + sel.split('\n').join('\n\t[*]') + '\n[/list]\n';
		
		this.Utils.replaceSelection(l, 0);
	},

	hr: function()
	{
		this.Utils.addText('\n[hr]\n');
	},

	url: function()
	{
		var url = prompt(Locale.string(4212));
		if (url != null) {
			this.Utils.url(url);
		}
	},

	img: function()
	{
		var url = prompt(Locale.string(4213));
		if (url != null) {
			this.Utils.img(url);
		}
	},

	color: function(color)
	{
		this.Utils.enTag('[color='+color+']', '[/color]');
	},

	size: function(size)
	{
		this.Utils.enTag('[size='+size+']', '[/size]');
	},

	lang: function(lang)
	{
		this.Utils.enTag('[lang='+lang+']', '[/lang]');
	}

});