/**
* Tryst Framework
*
* @package Javascript
* @author hal.sascha
* @copyright (c) 2006-2008
*/
Actions = new Class(
{
	bbcodepanels: false,

	initialize: function()
	{
		this.Linkinizer();
	},

	Linkinizer: function()
	{
		// bbcode panel
		this.bbcodepanels = new Array();
		$$('textarea[class="bbcodepanel"]').each(function(container)
		{
			if(!this.bbcodepanels.length)
			{
				new Asset.css(root+'/skins/'+skin+'/css/bbcode.css');
			}
			this.bbcodepanels.push(new BBCode(container));
		}.bind(this));

		// post method
		$$('form[class="postcmd"]').each(function(form)
		{
			form.removeEvents('submit');
			form.addEvent('submit', function(e)
			{
				new Event(e).stop();
				var rel = form.get('rel');
					rel = rel.split(':');
					rel[1] = rel[1] * 1; //force to int

				var execute = ((rel[1] && confirm('Bist du sicher das du fortfahren möchtest?')) || !rel[1]) ? true : false;
				if(!execute) return false

				$(form).set('send',
				{
					method: 'post',
					evalScripts: true,
					data: $(form),
					onSuccess: function(responseText, responseXML)
					{
						$(rel[0]).set('html', responseText);
						this.Linkinizer();
						this.callCallback(rel[2]);
					}.bind(this)
				});
				$(form).send(form.action);
			}.bind(this));
		}.bind(this));

		// get method
		$$('a[class="getcmd"]').each(function(link)
		{
			link.removeEvents('click');
			link.addEvent('click', function(e)
			{
				new Event(e).stop();
				var rel = link.get('rel');
					rel = rel.split(':');
					rel[1] = rel[1] * 1; //force to int

				var execute = ((rel[1] && confirm('Bist du sicher das du fortfahren möchtest?')) || !rel[1]) ? true : false;
				if(!execute) return false

				$(link).set('send',
				{
					method: 'get',
					evalScripts: true,
					onSuccess: function(responseText, responseXML)
					{
						$(rel[0]).set('html', responseText);
						this.Linkinizer();
						this.callCallback(rel[2]);
					}.bind(this)
				});
				$(link).send(link.href);
			}.bind(this));
		}.bind(this));

		// autoget method
		$$('div[class="getcmd"]').each(function(container)
		{
			var rel = container.get('rel');
				rel = rel.split('|'); //rel[0] = url | rel[1] = loaded status

			if(rel[1] == 'x')
			{
				$(container).set('send',
				{
					method: 'get',
					evalScripts: true,
					onSuccess: function(responseText, responseXML)
					{
						container.set('rel', 'o');
						container.set('html', responseText);
						this.Linkinizer();
					}.bind(this)
				});
				container.send(rel[0]);
			}
		}.bind(this));

		// q(uestion) method
		$$('a[class="qcmd"]').each(function(link)
		{
			link.removeEvents('click');
			link.addEvent('click', function(e)
			{
				var rel	= link.get('rel');
				rel		= rel ? rel : 'Bist du sicher das du fortfahren möchtest?';
				if(confirm(rel)) {
					//load link normally
				} else {
					new Event(e).stop();
				}
			}.bind(this));
		}.bind(this));

		// showhide method
		$$('a[class="showhide"]').each(function(link)
		{
			link.removeEvents('click');
			link.addEvent('click', function(e)
			{
				new Event(e).stop();
				var element = $(link);
				var rel		= element.get('rel');
				if($(rel).getStyle('display') == 'none') {
					$(rel).setStyle('visibility', 'visible');
					$(rel).setStyle('display', 'block');
				} else {
					$(rel).setStyle('visibility', 'hidden');
					$(rel).setStyle('display', 'none');
				}
			}.bind(this));
		}.bind(this));
	},

	PeriodicalUpdate: function(url, target, callback)
	{
		var request = new Request.HTML(
		{
			update: $(target),
			onSuccess: function(responseText, responseXML)
			{
				this.callCallback(callback);
			}.bind(this)
		});
		request.get(url);
	},

	callCallback: function(callback)
	{
		if(callback) {
			if(typeof window[callback] == 'function') window[callback]();
		}
	}
});

BBCode = new Class(
{
	id: false,
	opentags: false,

	initialize: function(container)
	{
		var id = container.get('id');
		if(!id)
		{
			alert('No "id" attribute with the bbcodepanel-ID set in bbcode panel div-element');
			return;
		}
		this.id = id; // type: string
		this.opentags = ''; //type: string
		this.createPanel();
	},

	createPanel: function()
	{
		// Create panel elements
		var panel = new Element('div', {
			'id': this.id+'panel',
			'class': 'bbcodepanel'
		});
		var section = new Element('ul', {});
		var divider = new Element('li', {'class': 'div'});

		var bold = new Element('li', {
			'title': 'Bold',
			'id': this.id+'bold',
			'html': 'B',
			'styles': {
				'font-weight': 'bold'
			},
			'events': {
				'click': function(){
					this.bbphrc(new Array('[b]','[/b]'));
				}.bind(this)
			}
		});

		var italic = new Element('li', {
			'title': 'Italic',
			'id': this.id+'italic',
			'html': 'I',
			'styles': {
				'font-style': 'italic'
			},
			'events': {
				'click': function(){
					this.bbphrc(new Array('[i]','[/i]'));
				}.bind(this)
			}
		});

		var underline = new Element('li', {
			'title': 'Underline',
			'id': this.id+'underline',
			'html': 'U',
			'styles': {
				'text-decoration': 'underline'
			},
			'events': {
				'click': function(){
					this.bbphrc(new Array('[u]','[/u]'));
				}.bind(this)
			}
		});

		var deleted = new Element('li', {
			'title': 'Deleted',
			'id': this.id+'deleted',
			'html': 'D',
			'styles': {
				'text-decoration': 'line-through'
			},
			'events': {
				'click': function(){
					this.bbphrc(new Array('[d]','[/d]'));
				}.bind(this)
			}
		});

		var headline = new Element('li', {
			'title': 'Headline',
			'id': this.id+'headline',
			'html': 'headline',
			'events': {
				'click': function(){
					this.bbphrc(new Array('[headline]','[/headline]'));
				}.bind(this)
			}
		});

		var left = new Element('li', {
			'title': 'Left',
			'id': this.id+'left',
			'html': 'Left',
			'events': {
				'click': function(){
					this.bbphrc(new Array('[align=left]','[/align]'));
				}.bind(this)
			}
		});

		var right = new Element('li', {
			'title': 'Right',
			'id': this.id+'right',
			'html': 'Right',
			'events': {
				'click': function(){
					this.bbphrc(new Array('[align=right]','[/align]'));
				}.bind(this)
			}
		});

		var center = new Element('li', {
			'title': 'Center',
			'id': this.id+'center',
			'html': 'Center',
			'events': {
				'click': function(){
					this.bbphrc(new Array('[align=center]','[/align]'));
				}.bind(this)
			}
		});

		var image = new Element('li', {
			'title': 'Image',
			'id': this.id+'image',
			'html': 'Image',
			'events': {
				'click': function(){
					this.bbphrc(new Array('[img]','[/img]'));
				}.bind(this)
			}
		});

		var link = new Element('li', {
			'title': 'Link',
			'id': this.id+'link',
			'html': 'URL',
			'events': {
				'click': function(){
					this.bbphrc(new Array('[url]','[/url]'));
				}.bind(this)
			}
		});

		var quote = new Element('li', {
			'title': 'Quote',
			'id': this.id+'quote',
			'html': 'Quote',
			'events': {
				'click': function(){
					this.bbphrc(new Array('[quote]','[/quote]'));
				}.bind(this)
			}
		});

		var code = new Element('li', {
			'title': 'Code',
			'id': this.id+'code',
			'html': 'Code',
			'events': {
				'click': function(){
					this.bbphrc(new Array('[code]','[/code]'));
				}.bind(this)
			}
		});

		var emotes = new Element('li', {
			'title': 'Emotes',
			'id': this.id+'emotes',
			'html': 'Emotes',
			'events': {
				'click': function(){
					alert('clicked emotes - no function available');
				}.bind(this)
			}
		});

		var closeall = new Element('li', {
			'title': 'Close all',
			'id': this.id+'closeall',
			'html': 'Close all',
			'events': {
				'click': function(){
					this.bbCloseTags();
				}.bind(this)
			}
		});

		var length = new Element('li', {
			'id': this.id+'length',
			'html': '0'
		});

		// inject objects
		var sectionA = section.clone();
		panel.adopt(sectionA);
		sectionA.adopt(bold);
		sectionA.adopt(divider.clone());
		sectionA.adopt(italic);
		sectionA.adopt(divider.clone());
		sectionA.adopt(underline);
		sectionA.adopt(divider.clone());
		sectionA.adopt(deleted);
		sectionA.adopt(divider.clone());
		sectionA.adopt(headline);
		var sectionB = section.clone();
		panel.adopt(sectionB);
		sectionB.adopt(left);
		sectionB.adopt(divider.clone());
		sectionB.adopt(right);
		sectionB.adopt(divider.clone());
		sectionB.adopt(center);
		var sectionC = section.clone();
		panel.adopt(sectionC);
		sectionC.adopt(image);
		sectionC.adopt(divider.clone());
		sectionC.adopt(link);
		var sectionD = section.clone();
		panel.adopt(sectionD);
		sectionD.adopt(quote);
		sectionD.adopt(divider.clone());
		sectionD.adopt(code);
		sectionD.adopt(divider.clone());
		sectionD.adopt(emotes);
		sectionD.adopt(divider.clone());
		sectionD.adopt(closeall);
		var sectionE = section.clone();
		panel.adopt(sectionE);
		sectionE.adopt(length);
		//var ta = panel.adopt(textarea);

		//add the panel
		panel.wraps($(this.id));
	},

	//Set the length of the textlength div
	setTextLength: function()
	{
		var element = $(this.id);
		if(element.createTextRange)
		{
			element.caretPos = document.selection.createRange().duplicate();
		}
		$(this.id+'length').set('html', element.value.length);
	},

	// Tag selected text
	bbphrc: function(hrc)
	{
		if(Browser.Engine.trident)
		{
			var theSelection = document.selection.createRange().text;
			if(theSelection)
			{
				// Add tags around selection
				document.selection.createRange().text = hrc[0] + theSelection + hrc[1];
				$(this.id).focus();
				theSelection = '';
				return;
			}
			else
			{
				this.bbOpenTag(hrc);
			}
		}
		else if($(this.id).selectionEnd && ($(this.id).selectionEnd - $(this.id).selectionStart > 0))
		{
			this.bbmozWrap($(this.id), hrc[0], hrc[1]);
			return;
		}
		else
		{
			this.bbOpenTag(hrc);
		}
		$(this.id).focus();
		this.setTextLength();
	},

	//Single tag open/close
	bbOpenTag: function(hrc)
	{
		var addcode;
		if(this.opentags.indexOf(hrc[1]) != -1)
		{
			addcode = hrc[1];
			this.opentags = this.opentags.replace(hrc[1],'');
		}
		else
		{
			addcode = hrc[0];
			this.opentags = hrc[1] + this.opentags;
		}
		this.bbAddToField(addcode);
	},

	// Tag add
	bbAddToField: function(addcode)
	{
		if($(this.id).createTextRange && $(this.id).caretPos)
		{
			var caretPos = $(this.id).caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? addcode + ' ' : addcode;
		}
		else
		{
			$(this.id).value += addcode;
		}
		$(this.id).focus();
	},

	// From http://www.massless.org/mozedit/
	bbmozWrap: function(txtarea, open, close)
	{
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		if (selEnd == 1 || selEnd == 2)
			selEnd = selLength;

		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + open + s2 + close + s3;
		return;
	},

	// Closes all open tags
	bbCloseTags: function()
	{
		this.bbAddToField(this.opentags);
		this.opentags = '';
		this.setTextLength();
	}
});

window.addEvent('domready', function() {
	trystActions = new Actions();
});

// Debugging stuff
//window.addEvent('unload', function() {
//	window.clipboardData.setData("text",document.documentElement.innerHTML)
//});
