function setEditors(){
	$( 'textarea.editor' ).each(function(e,i){
		var instance = CKEDITOR.instances[$(this).attr('name')];
		if(instance)
		{
			CKEDITOR.remove(instance);
		}
		$(this ).ckeditor(function(){
				if($('.popup').height() > 0)
					$('#fancybox-content').css('height', $('.popup').height() + 40);
				
			},
			{
				filebrowserBrowseUrl : '/Libs/ckfinder/ckfinder.html',
				filebrowserImageBrowseUrl : '/Libs/ckfinder/ckfinder.html?type=Images',
				filebrowserFlashBrowseUrl : '/Libs/ckfinder/ckfinder.html?type=Flash',
				filebrowserUploadUrl : '/Libs/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
				filebrowserImageUploadUrl : '/Libs/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
				filebrowserFlashUploadUrl : '/Libs/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
			}
		);
	})
	if($('.popup').height() > 0)
		$('#fancybox-content').css('height', $('.popup').height() + 40);
}

var reloadPage = false;
var systemFancyOptions = {
	'autoDimensions'		: false,
	'width'        			: 800,
	'height'       			: 'auto',
	'hideOnOverlayClick'	: false,
	onComplete				:function(){
		setEditors();
	},
	onClosed				:function(){
		if(reloadPage == true)
			document.location.reload();
	}
}


/**
 *	Klasa JS obsługijąc ajaxowe wywoływanie
 *	kontrollerów
 */
function Controller(){
	this.url = '/';
	this.controller = '';
	this.method = '';
	this.data = '';
	this.response = '';
	this.callback = '';
	
	/**
	 *	Wysyłanie danych przez AJAX
	 */
	this.send = function(){
		$.controller = this;
		
		$.ajax({
			type: 'POST',
			url: this.url + this.controller + '/' + this.method,
			data: this.data,
			success: this.success,
			error: function(data){
				alert(data);
			}
		});
	}
	
	/**
	 *	Uruchamianie kontrollera
	 */
	this.run = function(controller, method, data, callback){
		this.controller = controller;
		this.method = method;
		this.data = data;
		this.callback = callback;
		
		this.send();
	}
	
	
	/**
	 *	OK
	 */
	this.success = function(data){
		$.controller.response = data;
		if(typeof $.controller.callback == 'function'){
			$.controller.callback(data);
		}else
			eval($.controller.callback);
	}
	
	
	/**
	 *	Metody callback
	 */
	this.callbackReload = function(){
		document.location.reload();
	}
	this.callbackRedirect = function(url){
		document.location.href = url
	}
	this.callbackPopup = function(){
		$.fancybox(
			this.response,
			systemFancyOptions
		);
	}
}

var controller = new Controller();




