//so das der arme IE auch functionieren kann...
/*if (!window.console || !console.firebug) {
	var names = ["log","debug","info","warn","error","assert","dir","dirxml",
		"group","groupEnd","time","timeEnd","count","trace","profile","profileEnd"];

  window.console = {};
  for (var i = 0; i < names.length; ++i)
		window.console[names[i]] = function() {}
}*/

var Dom = YAHOO.util.Dom;

var hp24 = {};
hp24.log = function(msg) {
	if (!window.console || !console.log) {
		//alert( hp24.objdump(msg));
		/*
		var container = document.createElement('div');
		container.style.width = "400px";
		container.innerHTML = msg;
		document.body.appendChild(container);
		*/
		return;
	}
	console.log(msg);
};
hp24.logEx = function(e) {
	hp24.log("Exception: " + e.name + " - " + e.message);
}
hp24.tip = {};
hp24.tip.toggle = function(e) {
	var tips = YAHOO.util.Dom.get('tips');
	if (YAHOO.util.Dom.hasClass(tips, 'show')) {
		tips.alt  = 'Tipps zu dieser Seite einblenden';
		tips.title  = 'Tipps zu dieser Seite einblenden';
		YAHOO.util.Dom.removeClass(tips, 'show');
		YAHOO.util.Dom.addClass(tips, 'hide');
	} else {
		tips.alt = 'Tipps zu dieser Seite ausblenden';
		tips.title = 'Tipps zu dieser Seite ausblenden';
		YAHOO.util.Dom.removeClass(tips, 'hide');
		YAHOO.util.Dom.addClass(tips, 'show');
	}
};

hp24.tip.init = function() {
	var tips = YAHOO.util.Dom.get('tips-button');
	YAHOO.util.Event.addListener(tips, 'click', this.toggle, this);
	tips.alt  = 'Tipps zu dieser Seite einblenden';
	tips.title  = 'Tipps zu dieser Seite einblenden';
}

if (YAHOO.widget && YAHOO.widget.Dialog) {
	hp24.log("YAHOO.widget.Dialog exists");
	hp24.widget = {};

	hp24.widget.Dialog = function(el, cfg) {
		this.constructor.superclass.constructor.call(this, el, cfg);
		
		var esc = new YAHOO.util.KeyListener(document, { keys:13 }, { 
			fn:function (str, keyEvent) {
				try {
					if (keyEvent[1].target.tagName.toLowerCase() == 'textarea') {
						return;
					}
					var buttons = this.footer.getElementsByTagName('button');

					for (i=0; i < buttons.length; i++) {
						if (YAHOO.util.Dom.hasClass(buttons[i], "default") || YAHOO.util.Dom.hasClass(buttons[i].parentNode.parentNode, "default")) {
							buttons[i].click();
						}
					}
				} catch (e) {
					hp24.log(e);
				}
			},
			scope:this, 
			correctScope:true } );
		
		var enter = new YAHOO.util.KeyListener(document, 
			{ keys:27 }, { fn:this.hide, scope:this, correctScope:true } );
		this.cfg.queueProperty("keylisteners", [esc, enter]);

	}

	try {
		hp24.log("extending YAHOO.widget.Dialog");
		YAHOO.lang.extend(hp24.widget.Dialog, YAHOO.widget.Dialog);
	} catch (e) {
		hp24.log(e);
	}

	hp24.widget.Dialog.prototype.focusFirst = function() {
	  hp24.log(YAHOO.util.Dom.getElementsByClassName('focusfirst','',
			this.body,function (n) {n.focus();}));
  }

	hp24.widget.Dialog.prototype.render = function (appendToNode , moduleElement) {
		this.constructor.superclass.render.call(this, appendToNode, moduleElement);
	}
}

hp24.dialog = function(title, msg, icon) {
	if (YAHOO.widget.SimpleDialog == null) {
		hp24.log('include container.js');
		return;
	}
	var dialog = new hp24.widget.Dialog('dialog',
		{
			modal:true,
			visible:true,
			dragable:false,
			fixedcenter:true,
			width:"300px",
			icon:icon,
			constraintoviewport:true,
			buttons:[{text:'ok',isDefault:true,handler:function() { this.hide(); }}]
		}
	);
	dialog.setHeader(title);
	dialog.setBody(msg);
	
	dialog.render(document.body);
}

hp24.error = function(msg) {
	if (YAHOO.widget.SimpleDialog == null) {
		hp24.log('include container.js');
		return;
	}
	hp24.dialog('Fehler', msg, YAHOO.widget.SimpleDialog.ICON_WARN);
}

hp24.info = function(msg) {
	if (YAHOO.widget.SimpleDialog == null) {
		hp25.log('include container.js');
		return;
	}
	hp24.dialog('Info', msg, YAHOO.widget.SimpleDialog.ICON_INFO);
}

hp24.question = function(msg,ok,cancel,title) {
	try {
		title=(title==null)?'Sicher?':title;
		if (YAHOO.widget.SimpleDialog == null) {
			hp24.log('include container/container.js missing');
			return;
		}
		var dialog = new hp24.widget.Dialog('dialog', {
			modal:true,
			visible:true,
			dragable:false,
			fixedcenter:true,
			width:"300px",
			icon:YAHOO.widget.SimpleDialog.ICON_WARN,
			constraintoviewport:true,
			buttons:[
				{text:'Ja', isDefault:true, handler:function() { this.hide(); if (ok)  ok() }},
				{text:'Nein',isDefault:true,handler:function() { this.hide(); if (cancel) cancel(); }}
			]
		});
		dialog.setHeader(title);
		dialog.setBody(msg);
		dialog.render(document.body);
	} catch (e) {
		hp24.log(e);
	}
}

hp24.ajax = {};
hp24.ajax.lock = 0;

hp24.ajax.call = function(url, func, args) {
	if (hp24.ajax.lock == 1) {
		hp24.log('ignoring call to ' + func);
		return;
	} else {
		hp24.ajax.lock = 1;
	}

	if (!YAHOO.lang ||!YAHOO.lang.JSON) {
		hp24.error('yui modul "json" fehlt');
		return;
	}
	php_args = [];
	for (i = 0; i < args.length; i++) {
		if (args[i].innerHTML == null) {
			php_args.push(args[i]);
		}
	}
	cb = {
		success: function(o) {
			var result = null;
			//hp24.log(o.responseText);
			try {
				result = YAHOO.lang.JSON.parse(o.responseText);
			} catch (e) {
				hp24.error("Could not parse Response:\n" + e + "\n" + o.responseText);
				hp24.ajax.lock = 0;
				return;
			}
			if (result != null && result.error != null) {
				hp24.error("ajax call returned error: " + result.error);
				hp24.ajax.lock = 0;
				return;
			}
			if (hp24.ajax.client[func] == null) {
				hp24.error("Missing funktion: hp24.ajax.client." + func);
				hp24.ajax.lock = 0;
				return;
				//hp24.log(hp24.ajax.client);
			} else {
				var callback = hp24.ajax.client[func];
				try {
					callback(result, args);
				} catch(e) {
					hp24.log(e);
				}
			}
			hp24.ajax.lock = 0;
		},
		failure: function(o) {
			hp24.error('Es ist ein Fehler bei der Verbindung mit dem homepage24 Server aufgetreten (' + o.statusText + ')');
			hp24.ajax.lock = 0;
		}
	}
	/*
	if (!YAHOO.lang.isArray(args)) {
		hp24.log("fixing pseudo array");
		args = Array.prototype.slice.call(args);
	}
	*/
	var json_args;
	try {
		json_args = YAHOO.lang.JSON.stringify(php_args);
	} catch (e) {
		hp24.log(e.name + " " + e.message);
	}
	hp24.log('calling ' + func);
	YAHOO.util.Connect.asyncRequest('POST', url, cb, 'func=' + escape(func) + '&args=' + json_args); 
}

hp24.maske_show = function (title,body) {
	hp24.maske=new YAHOO.widget.Panel("wait",{width:"200px",close:false,modal:true,visible:false,fixedcenter:true});
	hp24.maske.setHeader(title);
	hp24.maske.setBody(body);

	hp24.maske.render(document.body);
	hp24.maske.show();
}

hp24.maske_hide = function() {
	hp24.maske.hide();
}
hp24.ajax.server = {};
hp24.ajax.client = {};

hp24.init = function() {
	hp24.tip.init();
}

YAHOO.util.Event.onDOMReady(hp24.init);
