-function ajax_request(funcname, params, callback, error_callback)
+function show_message(text, timeout)
{
- if (params === null)
- params = 'func='+funcname;
- else if (typeof params == 'object')
- params['func'] = funcname
- else if (typeof params == 'string')
- params = 'func='+funcname+'&'+params
+ if (typeof timeout == 'undefined') timeout = 3;
+
+ var div = $('#message_div');
+ if (!div.length) {
+ div = $('<div>');
+ div.attr('id', 'message_div').css('z-index','1000');
+ div.hide();
+ $(document.body).append(div);
+ }
+
+ div.text(text);
+ div.center();
+ div.show();
+
+ window.setTimeout(hide_message,timeout*1000);
+}
+
+function hide_message(text)
+{
+ $('#message_div').hide();
+}
+
+var errorwindow = false;
+function show_error(text, timeout)
+{
+ if (!errorwindow) {
+ errorwindow = new Popup('Fehler', '500px', false, '<div id="errorbody"></div>');
+ errorwindow.setId('errorwindow');
+ $('#errorwindow').css('z-index','1000');
+ errorwindow.centerPopup();
+ } else {
+ errorwindow.openPopup();
+ }
+
+ $('#errorbody').html(text);
+
+ if (typeof timeout != 'undefined')
+ window.setTimeout(function(){errorwindow.closePopup()},timeout*1000);
+}
+
+function editable_callback(data)
+{
+ if (data.content) {
+ var elem = $(data.$el);
+ var route = elem.attr('route');
+ var item_id = elem.attr('item_id');
+ if (typeof(route) == 'string' && typeof(item_id) == 'string')
+ $.invoke(route,
+ {id: item_id,
+ name: elem.attr('name'),
+ content: data.content});
+ }
+
+ return false;
+}
+
+function make_editable(selector)
+{
+ var list = $(selector);
+ if (list.length)
+ list.editable({
+ closeOnEnter : true,
+ event : 'click',
+ callback: editable_callback
+ });
+}
+(function($){
+ $.fn.ltag = function() {
+ return this.prop("tagName").toLowerCase();
+ };
+
+ $.fn.center = function () {
+ this.css("position","absolute");
+ this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
+ $(window).scrollTop()) + "px");
+ this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
+ $(window).scrollLeft()) + "px");
+ return this;
+ };
+
+ $.fn.positionOn = function(element, align) {
+ return this.each(function() {
+ var target = $(this);
+ var position = element.offset();
+
+ var x = position.left;
+ var y = position.top;
+
+ if(align == 'right') {
+ x -= (target.outerWidth() - element.outerWidth());
+ } else if(align == 'center') {
+ x -= target.outerWidth() / 2 - element.outerWidth() / 2;
+ }
+
+ target.css({
+ position: 'absolute',
+ zIndex: 5000,
+ top: y,
+ left: x
+ });
+ });
+ };
+
+$.invoke = function(name, parms, callback) {
+ if (typeof(parms) == 'string' && parms.length)
+ parms = 'route='+name+'&'+parms;
+ else if (typeof(parms) == 'object')
+ parms['route'] = name;
else
- return false;
-
- $.post('/ajax.php', params,
- function(data){
- if (!data.status) {
- if (typeof data.error == 'string')
- var error = data.error;
- else
- var error = 'An error occurred'
- alert(error);
- if (typeof error_callback == 'function')
- error_callback(data);
- return;
- }
- if (typeof callback == 'function')
- callback(data);
- });
+ parms = 'route='+name;
+
+ $.post('/ajax.php',
+ parms,
+ function(data){
+ if (!data.status) {
+ if (data.error) {
+ if (data.redirect_login) {
+ show_message(data.error, 5);
+ setTimeout(function(){window.location.href = '/';}, 5000);
+ return;
+ } else
+ return show_error(data.error);
+ } else
+ return show_error('Fehler im Backend zu "'+name+'" aufgetreten');
+ }
+ if (typeof(data.html) == 'object')
+ for (id in data.html) {
+ var elem = $('#'+id);
+ if (elem.length) {
+ elem.html(data.html[id]);
+ }
+ }
+ if (typeof(data.values) == 'object')
+ for (id in data.values) {
+ var elem = $('#'+id);
+ if (elem.length)
+ elem.val(data.values[id]);
+ }
+
+ if (typeof(callback) == 'function')
+ return callback(data);
+ });
+};
+})(jQuery);
+
+function Popup(title, width, height, body) {
+ return this.initialize(title, width, height, body);
+}
+
+Popup.prototype = {
+ initialize: function(title, width, height, body)
+ {
+ this.createPopup(width, height);
+ if (title) this.setTitle(title);
+ if (body) this.setBody(body);
+ return this;
+ },
+
+ createPopup: function(width, height)
+ {
+ this.popup = $('<div class="popup">');
+ this.popup.css('width', width ? width : 'auto');
+ this.popup.css('height', height ? height : 'auto');
+ this.popup.hide().css('z-index', '1000');
+ var titlediv = $('<div class="popup_title" style="position:relative;">');
+ titlediv.append($('<p class="popup_title"></p>'));
+
+ var closeimg = $('<img src="/pix/close.gif" title="Close">');
+ titlediv.append(closeimg);
+
+ this.popup.append(titlediv);
+
+ this.popup.append($('<div class="popup_body" style="clear:both;">'));
+ $('body').append(this.popup);
+
+ this.popup.udraggable({'handle': 'div.popup_title'});
+ this.popup.find('div.popup_title img').bind('click', this.closePopup.bind(this));
+ },
+
+ openPopup: function()
+ {
+ this.popup.show();
+ },
+
+ centerPopup: function()
+ {
+ this.popup.center().show();
+ },
+
+ closePopup: function()
+ {
+ this.popup.hide();
+ },
+
+ setId: function(name)
+ {
+ this.popup.attr('id', name);
+ },
+
+ setTitle: function(title)
+ {
+ this.popup.find('div.popup_title p.popup_title').html(title);
+ },
+
+ setBody: function(body)
+ {
+ this.popup.find('div.popup_body').html(body);
+ }
}