/**
 * Bind JS effects to tags.
*/

var Loader = {
	stack: [],
	initialized: 0,

	/** push function on stack */
	push: function(funct) {
		this.stack.push(funct);
	},
	
	/** execute functions stored in stack */
	start: function() {
		if (this.initialized != 1) {
			$A(this.stack).each( function(f) { f.bind(this); f(); f=null; });
			this.stack = [];
		}
		this.initialized += 1;
	}
}

/*----------------------------------------------------------------------------------------------------------------------------
 * Calendar
 * @author Ondrej Hatala <ontheray@pobox.sk>
 */
var Calendar = {
	buffer: {},

	/** attach calendar on unattached input */
	attach: function(inputId) {
		if (typeof this.buffer[inputId] == 'undefined') {
			this.buffer[inputId] = new Epoch('epoch_popup', 'popup' , $(inputId));
		}
		//return true;
	},
	/** show calendar */
	show: function(inputId, toggle) {
		this.attach(inputId);
		this._show(inputId, toggle);
		return true;
	},
	/** show || toggle calendar popup */
	_show: function(inputId, toggle) {
		if (typeof this.buffer[inputId] != 'undefined') {
			if (toggle) {
				this.buffer[inputId].toggle();
			} else {
				this.buffer[inputId].show();
			}
		}
	}
}

/*----------------------------------------------------------------------------------------------------------------------------
 * UserListAction
 * @author Ondrej Hatala <ontheray@pobox.sk>
 */
var UserListAction = {
	
	change: function(element) {
/*		if($(element).selectedIndex == 1) {
			$('user_list_group_select').show();
		} else {
			$('user_list_group_select').hide();
		}*/
		
		$('user_list_action_submit').show();
	}
}

var Subscribe = {

	send: function() {
		form = $('subscribe_form');
		if(typeof(form) != 'undefined')
		{
			form.getElements()[0].value = 'no_spam_plx';
			form.submit();
		}
	}
}


/*----------------------------------------------------------------------------------------------------------------------------
 * TinyMCE holder
 */
var EditorHolder = {
	editors: [],

	attach: function(element) {
		if($(element))
		{
			tinyMCE.execCommand('mceAddControl', false, element);
			this.editors.push(element);
		}
	}
}

if(typeof tinyMCE != 'undefined')
{
	tinyMCE.init({
		mode : "exact",
		elements : "tinymce,tinymce2,tinymce3,tinymce4,tinymce5",
		theme : "advanced",
		theme_advanced_layout_manager : "SimpleLayout",
		theme_advanced_buttons1 : "bold,italic,underline,undo,redo,link,unlink,forecolor,removeformat,code",
		theme_advanced_buttons1_add: "justifyleft,justifycenter,justifyright,justifyfull",
		theme_advanced_buttons2 : "tablecontrols",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		forced_root_block : false,
		force_br_newlines : true,
		force_p_newlines : false,
		plugins : "table"
	});

//	if(typeof($('tinymce')) != 'undefined') Loader.push(function() { EditorHolder.attach('tinymce')});

//	Loader.push(function() {
//		EditorHolder.attach("blog_write_message");
//		EditorHolder.attach("blog_write_lead");
//	});
}
var _js_hide = function(element) {
	var hide_list = $A(document.getElementsByClassName('js_hide', $(element)||document));
	hide_list.each( function(h) { h.hide(); });
	
	var show_list = $A(document.getElementsByClassName('js_show', $(element)||document));
	show_list.each( function(h) { h.toggle(); });
//	console.log('js_hide ok');
}

Loader.push(function() { _js_hide(); });

/*
Loader.push(function() {
//	console.log(InputBoxHelper);
	var help_list = $A(document.getElementsByClassName('input-helper'));

	help_list.each( function(element) {
//		InputBoxHelper.onFocus.bindAsEventListener(element.onFocus);
		element.onFocus = InputBoxHelper.onClick;
//		element.onUnFocus = helper.onUnFocus;
	});

});*/