function FacebookShare() {
    u = location.href;
    t = document.title;
    return window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
}

Element.addMethods( 'TEXTAREA', {
	
	addFormatingGuides : function ( element ) {
		element = $( element );
		new Ajax.Updater( element, base_url + 'ajax/formating_guides', {
			parameters : { id : element.identify() },
			insertion : 'after',
			evalScripts : true
		});
		
		return element;
	}
	
} );

Comment = {
	
	reply : function ( evt ) {
		var commentElm = evt.element().up( '.comment' );
		var commentField = $( 'comment' );
		var userElm = commentElm.down( '.user a' );
		var permalinkElm = commentElm.down( '.stamp a' );
		
		if ( commentField.value != '' ) { commentField.value = commentField.value + "\n\n"; }
		commentField.value = commentField.value + '"' + userElm.innerHTML.toUpperCase() + '":[' + permalinkElm.href + ']: ';
		comemntField.focus();
	}
	
}

var FormatingTools = Class.create({
	
	actions : [
		{ id : 'link',			title : 'odkaz' },
		{ id : 'illustration',	title : 'ilustrácia' },
	],
	
	initialize : function ( field, holder, target ) {
		this.field = $( field );
		this.holder = $( holder );
		this.target = $( target );
		this.actions.each( this.addAction.bind( this ) );
	},
	
	initAction : function () {
		$( 'MB_content' ).select( 'form' ).invoke( 'observe', 'submit', this.handleForm.bindAsEventListener( this ) );
	},
	
	handleForm : function ( evt ) {
		evt.stop();
		var form = evt.findElement( 'form' );
		new Ajax.Request( form.action, {
			parameters : form.serialize( true ),
			method : form.method,
			onSuccess : this.appendContent.bind( this )
		} );
		Modalbox.hide();
	},
	
	appendContent : function ( response ) {
		this.field.value += response.responseText;
	},
	
	doAction : function ( action ) {
		// new Ajax.Updater( this.target, base_url + 'ajax/formating/' + action );
		Modalbox.show( base_url + 'ajax/formating/' + action, {
			afterLoad : this.initAction.bind( this )
		} );
	},
	
	addAction : function ( data ) {
		var link = new Element( 'a', { href : 'javascript:void(0);' } );
		link.update( data.title );
		link.observe( 'click', this.doAction.bind( this, data.id ) );
		
		var item = new Element( 'li' );
		item.appendChild( link );
		
		this.holder.appendChild( item );
		return item;
	}
	
});

function show_formating_info(elm) {
	target = $((elm) ? elm : 'formating_info');
	if (target) {
		display_indicator(target);
		new Ajax.Updater(target, base_url + 'ajax/formating');
		return true;
	}
	return false;
}

function preview (source, target) {
	source = $(source);
	target = $(target);
	if (source && target) {
		if (target.id) {document.location = "#" + target.id;}
		display_indicator(target);
		var url = base_url + "ajax/preview";
		var params = { content : source.value };
		var realms_list_updater = new Ajax.Updater(target, url, {method: "post", parameters: params});
		return true;
	}
	return false;
}

function display_indicator (elm) {
	elm = $(elm);
	if (elm) {
		var indicator = document.createElement("div");
		indicator.className = "indicator";
		var elm_height = Element.getDimensions(elm).height;
		elm_height = (elm_height < 50) ? 50 : elm_height;
		indicator.style.height = elm_height + "px";
		empty_element(elm);
		elm.appendChild(indicator);
		return true;
	}
	return false;
}

function empty_element (elm) {
	elm = $(elm);
	if (elm) {
		while (elm.firstChild) {elm.removeChild(elm.firstChild);}
		return true;
	}
	return false;
}

document.observe( 'dom:loaded', function () {
	$$( '.commentReply' ).invoke( 'observe', 'click', Comment.reply );
	$$( 'textarea.formatedContent' ).invoke( 'addFormatingGuides' );
} );
