// Funkcje CKEditor'a
jsPackage("epoint.ow.ckeditor");

// FIXME: remove from this 
ajaxExplode = function() {
	$.post('?action=explode.explode', [], function() {alert('ajax request success');}, "json");
};
ajaxExplodeSql = function() {
	$.post('?action=explode.explodeSql', [], function() {alert('ajax request success');}, "json");
};
ajaxExplodeFileUploadSize = function() {
	$.post('?action=explode.explodeFileUploadSize', [], function() {alert('ajax request success');}, "json");
};
ajaxExplodeFileUpload = function() {
	$.post('?action=explode.explodeFileUpload', [], function() {alert('ajax request success');}, "json");
};
ajaxExplodeBadRequest = function() {
	$.post('?action=explode.explodeBadRequest', [], function() {alert('ajax request success');}, "json");
};
ajaxExplodeNoPermission = function() {
	$.post('?action=explode.explodeNoPermission', [], function() {alert('ajax request success');}, "json");
};
ajaxExplodeResourceNotFound = function() {
	$.post('?action=explode.explodeResourceNotFound', [], function() {alert('ajax request success');}, "json");
};
ajaxExplodeOtherException = function() {
	$.post('?action=explssadas', [], function() {alert('ajax request success');}, "json");
};
//FIXME: remove to this 

epoint.ow.ckeditor.replaceWithEditor = function(/*Object*/ ckeditorData, /*object?*/ config) {
	config == null
		? {}
		: config;
	config.fieldId = ckeditorData.fieldId;
	config.selectProductUrl = ckeditorData.selectProductUrl;
	config.selectProductCategoryUrl = ckeditorData.selectProductCategoryUrl;
	config.selectPageUrl = ckeditorData.selectPageUrl;
	
	var editor = CKEDITOR.replace(ckeditorData.fieldId, config);
	if (config.css) {
		editor.addCss(config.css);
	}
	var field = document.getElementById(ckeditorData.fieldId);	
	
	// [Opera] fix blur
	if ($.browser.opera) {
	  editor.on('instanceReady', function(e) {
	    $( editor.window.$.frameElement ).contents().find("body").blur(function() { editor.fire('blur'); })
	  });
	}

	editor.on('blur', function(e) {
    this.updateElement();
    epoint.ow.forms2.Utils.fieldValueChanged(field);
    if(epoint.Screen.env.saveComponentConfigurationTemporarilyAction) {
		  epoint.Util.saveComponentConfigurationTemporarily();
		}
  }, null, null, 1);

}

epoint.ow.ckeditor.removeEditor = function(/*String*/ fieldId) {
	var editor = null;
	for (var editorName in CKEDITOR.instances) {
		var e = CKEDITOR.instances[editorName];
		if (e.element.getId() == fieldId) {
			editor = e;
			break;
		}
	}
	if (editor) {
		editor.updateElement();
		editor.destroy();
	}
}

epoint.ow.ckeditor.configs = {};
epoint.ow.ckeditor.getConfig = function(/*String*/ fieldId) {
	return this.configs[fieldId] || {};
}

epoint.ow.ckeditor.setConfig = function(/*String*/ fieldId, /*object*/ config) {
	this.configs[fieldId] = config;
}

epoint.ow.DocumentReadyManager.addOnewebAction(function() {
	var handler = function(event, jqXHR, ajaxSettings, thrownError) {
		var errorString = "event.type : " + event.type + "\n\n" +
		"jqXHR.status : " + jqXHR.status + "\n\n" +
		"jqXHR.statusText : " + jqXHR.statusText + "\n\n" +
		"jqXHR.readyState : " + jqXHR.readyState + "\n\n" +
		"ajaxSettings.url : " + ajaxSettings.url + "\n\n" +
		"ajaxSettings.type : " + ajaxSettings.type + "\n\n" +
		"ajaxSettings.contentType : " + ajaxSettings.contentType + "\n\n" +
		"ajaxSettings.dataType (expected): " + ajaxSettings.dataType;
		if (thrownError != null) {
			errorString = errorString + "\n\nthrownError : " + thrownError;
		}
		
		var getDiv = function() {
			var DIV_ID = "ajax_error_container";
			var div = document.getElementById(DIV_ID);
			if (div == null) {
				div = document.createElement('div');
				div.setAttribute('id', DIV_ID);
				div.setAttribute('className', "ajax_error_container");
				div.setAttribute('style', "position:absolute");
				document.getElementById('layout').appendChild(div);
			}
			return div;
		};
		
		var showErrorFn = function() {
			fn.hidePopups(true);
			
			$(getDiv()).centreLayer();
	 		var $cover = $("#cover");
	 		if (!$cover.exists()) {
	 			fn.cover();
	 		} else {
	 			if($cover.is(':hidden')) {
	 				$cover.show();
	 			}
	 		}
		};
		
		var showOneWebErrorFn = function() {
			var text = jqXHR.responseText;
			text = text.replace(/.*<span id="layout_error_cut_begin"><\/span>/, "");
			text = text.replace(/<span id="layout_error_cut_end".*/, "");
			$(getDiv()).html(text);
			
			showErrorFn();
		};
		
		var showDefaultErrorFn = function() {
			var div = getDiv();
			
			var DEFAULT_ERROR_DIV_ID = "default-ajax-error-content";
			var defaultError = document.getElementById(DEFAULT_ERROR_DIV_ID);
			if (defaultError != null) {
				div.innerHTML = defaultError.innerHTML;
			} else {
				div.innerHTML = "<span>There was a problem communicating with the server.</span>";
			}
			
			showErrorFn();
		};
		
		if (jqXHR.statusText == "timeout") {
			showDefaultErrorFn();
			return;
		}
		
		if ((jqXHR.statusText == "parsererror")) {
			if ((JSESSIONID != null)
					&& (JSESSIONID != $.cookie("JSESSIONID"))) {
				showDefaultErrorFn();
				return;
			}
			throw errorString;
		}
		if (jqXHR.getResponseHeader('OneWeb-error-handler-response') == "true" &&
				jqXHR.responseText != null) {
				showOneWebErrorFn();
				if (typeof console != 'undefined' && console.log != null) {
					console.log(errorString);
				}
				return;
		}
		if (jqXHR.readyState == 0 && jqXHR.status == 0) {
			// abort request, do nothing
			return;
		}
		
		if (jqXHR.status != 0 && jqXHR.status != 200) {
			showDefaultErrorFn();
		}
		throw errorString;
 	};
	
 	JSESSIONID = $.cookie("JSESSIONID");
	epoint.ow.JSErrorManager.setAjaxErrorHandler(handler);
	}, 999, true);

