/* *************************************************** 


 **************************************************** */

var brainCMS = {};
brainCMS.mainPopup = false;
brainCMS.openPopup = function(url) {
	if (this.mainPopup) {
		this.mainPopup.close();
	}
	brainCMS.mainPopup = window.open(url, 
									'brainCMSPopup',
									'width=590,height=590,resizable=yes,scrollbars=yes');
}
/**
 * Standard-Funktion zum Auslösen eines xajax-Callbacks
 */
brainCMS.xajaxCallback = function(p1, p2, p3, p4, p5) {
	return xajax_CallModul(p1, p2, p3, p4, p5);
};

/**
 * Standard-Funktion für eine Bestätigung
 */
brainCMS.confirm = function(id, title, url, msg) {
	var idKey = "#" + id;
	jQuery(idKey).dialog("option", "title", title);
	obj = document.getElementById(id);
	if (msg) {
		obj.innerHTML = msg;
	} else {
		obj.innerHTML = "(Ja) setzt die Funktion fort, (Nein) bricht den Vorgang ab.";
	}

	jQuery(idKey).dialog("option", "buttons", {
		"Nein" : function() {
			jQuery(this).dialog("close");
		},
		"Ja" : function() {
			jQuery(this).dialog("close");
			eval(url);
		}
	});
	jQuery(idKey).dialog('open');
	return false;
}

/**
 * Standard-Dialog (kein Popup-Up)
 */
brainCMS.dialog = function(id, params, bind) {
	var idKey = "#" + id;
	if (jQuery(idKey)) {	
		if (params && typeof params == 'object') {
			for ( var option in params) {
				jQuery(idKey).dialog('option', option, params[option]);
			}
		}
		
		if (bind && typeof bind == 'function') {
			
			jQuery(idKey).bind("dialogopen", bind);
			jQuery(idKey).dialog('open');
		} else if (bind) {
			jQuery(idKey).dialog('open');
			eval(bind);
		} else {
			jQuery(idKey).dialog('open');
		}
	}
}


brainCMS.dialogWithXajax = function(title, mid, modul, data, params, id) {	
	if (!id) {
		id = "braincms-dialog";	
	} 
	var idKey = "#" + id;
	
	jQuery(idKey).dialog('option', 'title', title);
		
	if (!(params && typeof params == 'object')) {
		params = {resizable : true, width: 600, height: 450 };
	}
	for (var option in params) {
		jQuery(idKey).dialog('option', option, params[option]);
	}
	
	jQuery(idKey).dialog('open');
	return brainCMS.xajaxCallback(mid, modul, data, id, "innerHTML");
}

/** 
 * Cookie-Objekt und Zugriffmethoden
 */
brainCMS.cookie = {
	set : function(name, value, expires) {
		var value = escape(value);
		var cookieStr = name + '=' + value + ';path=/';

		if (typeof expires !== "undefined" && expires !== null) {
			cookieStr += ";expires=" + expires.toGMTString();
			alert(cookieStr);
		}
		document.cookie = cookieStr;

	},
	get : function(name, defaultValue) {
		var reg = name + "=([^;]*)";
		var fullCookieStr = document.cookie;
		var result = document.cookie.match(new RegExp(reg));
		
		if (result && result.length == 2) {
			return unescape(result[1]);
		} else {
			if (typeof defaultValue !== "undefined") {
				return defaultValue;
			} else {
				return null;
			}
		}



	},

	kill : function(name) {
		var currentValue = this.get(name);
		if (currentValue !== null) {
			var timeObj = new Date();
			var killTime = timeObj.getTime() - 100;
			timeObj.setTime(killTime);

			this.set(name, currentValue, timeObj);
		}
	},

	show : function(message) {
		if (typeof message === "undefined") {
			message = 'DOCUMENT.COOKIE';
		}

		if (typeof window.console === "undefined" || window.console === null) {
			alert(message + ': ' + document.cookie);
		} else {
			window.console.debug(message + ': ' + document.cookie);
		}
	}
}

/**
 * Debug-Meldungen an Firebug-Console
 */
brainCMS.debug = function(value, msg, type) {
	if (typeof (console) == "undefined") {
		return;
	}
	if (!msg) {
		msg = 'BrainCMS Debug';
	}
	if (!type) {
		type = 'debug';
	}

	switch (type) {
	case 'debug':
		console.debug(value);
		break;
	case 'info':
		console.info(msg + ' : ' + value);
		break;
	case 'warn':
		console.warn(msg + ' : ' + value);
		break;
	case 'error':
		console.error(msg + ' : ' + value);
		break;
	default:
		window.status = msg + ' : ' + value;
	}
}

