Event.add( window, "DOMContentLoaded", function() {
	//var el = $('title');
	//if (el && el.name == 'post_subject')
	//	new SpellChecker ( $("title") );

	var myRTE;
	PNGFix.fix();

	if (typeof useRte != "undefined" && useRte) {
		RTE.prototype.options.className = "rte";

		if ($('rte-body'))
		{
			RTE.prototype.options.barConfig = "formatblock|fontname|fontsize| |createlink|unlink| |img|video| |table|bold|italic|underline|strikethrough |\n| |justifyleft|justifycenter|justifyright| |insertunorderedlist|insertorderedlist| |outdent|indent| |forecolor|hilitecolor| |removeformat|inserthorizontalrule| |source| |copy|paste| |spellchecker|linkchecker|toggleFS";
			myRTE = new RTE ( "rte-body", {       className : "rte" });
		}
		else if ($('post_body'))
		{
			myRTE = new RTE ( "post_body", {       className : "rte" });
		}

	}

	// Image protection
	if (document.spaceball)
	{
		var image = document.getElement('.protected');
		var spaceball = $C('img');
		spaceball.setProperties({
				'src' 		: 'http://static.pathfinder.gr/Clubs/images/spaceball.gif',
				'width'		: image.width,
				'height' 	: image.height
		});
		spaceball.setStyles({
				'position' 	: 'relative',
				'top'		: -image.height,
				'margin-bottom' : -image.height,
				'display'	: 'block'
		});
		spaceball.injectAfter(image);
	}

	/* inserts a video tag in rte */
	function insVid(w) {
		if (url = prompt("Δώστε το URL", ""))
		{
			if (w==1)
				str = "[youtube="+ url +"]";
			else
				str = "[googlevideo="+ url +"]";
			var el = myRTE;
			if (el && $("interface_data"))
				el.execCommand("insertHTML", str);
			else
				$("n-body").value += str;
		}
		return false;
	}


/*	
	if (typeof useRte != "undefined" && useRte) {
		if ($('interface_data'))
		myRTE = new RTE ( "interface_data", {
			className : "rte",
			forceCleanup: false
		});

		if ($('custom_html'))
		myRTE = new RTE ( "custom_html", {
			className : "rte"
		});
	}*/
/*	$A($T("textarea")).forEachBind( function() {
		if ( !this._rteEnabled && this._rteEnabled != "undefined")
			new SpellChecker ( this );
	});
*/
	var pagesAjax = new Ajax.RPC({
		'onSuccess' : function(q) {
			var res = q.responseJSON;
			if (res && res.faultCode)
				alert(res.faultString);
		}
	});
	var t = new Sorter('#pages tbody', 'tr', (window.webkit ? 'th, td' : 'th font,td,th'), { 
		'anyKnobElement' : false,
		'onChange' : function (el) {
			var order = [];
			this.getElements().each(function(element) {
				order.push(element.getProperty('id').replace(/_/g, '').toInt());
			});
			pagesAjax.send("clubs.SetPagesOrder", club, order);
		},
		'getDragger': function() {
			return $C('div').
			setStyles({
				'background': '#ccc',
				'width'	: '600px'
			}).
			setHTML(this.start.element.cells[0].getText());
		}
	});

	var x = new Sorter('#albums tbody', 'tr', 'th, td', { 
		'anyKnobElement' : false,
		'onChange' : function (el) {
			var order = [];
			this.getElements().each(function(element) {
				order.push(element.getProperty('id').replace(/_/g, '').toInt());
			});
			pagesAjax.send("clubs.SetAlbumsOrder", order);
		}
	});

	if($('my-friends'))	{
		var _checkboxes = $T('input', $('my-friends'));
		
		_checkboxes.forEach(function() {
			this.addEvent('click', function() { 
				$('invite_ids').value += this.id;
			});
		});
	}
});

function toggleCPicker ( node, e, theme ) {
	var xy = Pos.get(node);
	if ($('customTh'))
		$('customTh').checked = true;
	if (!node.cpicker ) {
		var coords = node.getCoordinates();

		node.cpicker = new CPicker ({
			color 	: "#"+ node.value,
			theme	: theme,
			//onClose : function()  { node.cpicker = null ; },
			onChange : function() { 
				var c = this.color.rgbToHex();

				node.value = c.replace(/#/g,'').toUpperCase();
				node.style.color = new Color(this.color).invert().hex;
				node.style.backgroundColor = c;
			}
		}).open(null, {'position': { 'top': coords.top, 'left': coords.left + coords.width}});
	}
	else 
		node.cpicker.toggle();
		
	//Event.stop(e);
}

/* update a page ajaxing */
/*
	we have a problem here:
	when page name has html entities, core replace these entities (in rpcQuery ) to some wired chars
	probably when the data converts to utf-8

	temporary save it using another post form
*/
function UpdatePage(form, id, club) {
	var n = eval("form.newname"+ id +".value");
	var p = eval("form.new_privacy"+ id +".value");
	$('status').innerHTML = "Αποθήκευση...";

	if( 1 ) {
		var f = document.updatePagesForm;
		f.newname.value = n;
		f.id.value = id;
		f.privacy.value = p;
		f.submit();
		return;
	}
	else  {
		//console.log( n, id, club, p );
		Ajax.rpc ("clubs.UpdatePage", "'"+club+"'", n, id, parseInt(p), {
			onSuccess: function(req) {
				res = req.responseJSON;
				if( res && res.faultCode ) {
					alert( res.faultString );
				}
				else
				{
					$('status').innerHTML = "Αποθήκευση επιτυχής";
				}
			}
		});
	}
}
