var m = {
		
	d : document,
	w : window,
	checkflag : false,
	
	req : null,
	events : new Array(),
	
	init : function() {    	
    	m.evAdd(null, 'ajaxstart', m.toggleWaiting);
    	m.evAdd(null, 'ajaxend', m.toggleWaiting);
    },
    
    id : function(id) {
    	id = this.d.getElementById(id);
    	if (!id) return false;
    	return id;
    },
    
    evAdd : function(obj, event, funcname) {
    	if (event != 'load' && event !='mousedown' && event !='mousemove' && event !='mouseup') {    		
    		var ev = {
    			obj : obj,
    			ev : event,
    			func : funcname
    		};
    		m.events.push(ev);
    	}
    	else {    		
    		if(obj.addEventListener) {
    			obj.addEventListener(event,funcname,false);
    		} else if(obj.attachEvent) {
    			if(obj==m.w && (event.indexOf('mouse')!=-1 || event.indexOf('click')!=-1)) obj = m.d;
    			obj.attachEvent('on'+event,funcname);
    		}
    	}
    },
    
    evCall : function(event, source) {
    	var i;
    	var length = this.events.length;
    	for (i = 0; i<length; ++i) {
    		if (m.events[i].ev == event) {
    			m.events[i].func(m.events[i]);
    		}
    	}
    },        
	
	query : function(data, url, callback) {
		var xhttp = null;		
		this.req = {
			quest : data,
			status : null,
			state : null,
			response : null
		};
		
		if (window.ActiveXObject) { 
            try { 
               // IE 6 and higher
               xhttp = new ActiveXObject("MSXML2.XMLHTTP");
            } catch (e) {
                try {
                    // IE 5
                    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    xhttp=false;
                }
            }
        }
        else if (window.XMLHttpRequest) {
            try {
                // Mozilla, Opera, Safari ...
                xhttp = new XMLHttpRequest();
            } catch (e) {
                xhttp=false;
            }
        }
        else {
        	return false;
        }
		if (!xhttp) return false;
		xhttp.onreadystatechange = function() {			
			m.req.state = xhttp.readyState;
			if (xhttp.readyState==4 && xhttp.status==200) {
				m.req.status = xhttp.status;
				if (typeof(callback) == 'function') {
					m.req.response = xhttp.responseText;
					m.evCall('ajaxend', null);
					callback(xhttp.responseText);
				}
				else {
					return false;
				}
			}
		};
		m.evCall('ajaxstart', null);
		xhttp.open('POST', m.d.location + '?ajax=true', true);
		xhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhttp.send('ajax=true&' + data);
		return true;
    },
    
    toggle : function(id, showstyle) {
    	if (!showstyle) showstyle = '';
    	if (!m.id(id)) return false;
    	if (m.id(id).style.display == showstyle) {
    		m.id(id).style.display = 'none';
    	}
    	else {
    		m.id(id).style.display = showstyle;    		
    	}
    },        
    
    toggleWaiting : function() {
    	m.toggle('wait', 'block');
    },
    
    close : function(id) {
		var obj = m.id(id);
		var parent = obj.parentNode;		
		parent.innerHTML = '';
	},
	
	reloadImage : function() {
		fader.fade_out('pictureofday', m.reloadPictureOfDay);
	},
	
	reloadPictureOfDay : function() {
		m.query('mode=reload', '', m.reloadedImage);
	},
	
	reloadedImage : function(ret) {
		var im = new Image();
		im.src = ret;		
		m.id('reisebild').src = im.src;
		fader.fade_in('pictureofday');
	},
	
	showPreview : function(id) {
		this.query('preview=true&id=' + id, '', box.open);
	},		
	
	addImageFilter : function(id, name, value) {
		this.query('imgid=' + id + '&imagefilter=' + name + '&value=' + value, '', m.imageFilterAdded);
	},
	
	imageFilterAdded : function(ret) {
		var im = new Image();
		im.src = ret;
		m.id('imageedit').src = im.src;
	},
	
	fadeMessages : function() {
		setTimeout('fader.fade_out(\'ok\', m.close)', 3000);
		setTimeout('fader.fade_out(\'fail\', m.close)', 3000);
		setTimeout('fader.fade_out(\'info\', m.close)', 3000);
	},
	
	selectAllCheckboxes : function(id) {
		var obj = this.id('formular')[id]; 		
		
		if (!this.checkflag) {
			for (i = 0; i < obj.length; i++) {
				obj[i].checked = true;
			}
			this.checkflag = true;
			return true;
		}
		else {
			for (i = 0; i < obj.length; i++) {
				obj[i].checked = false;
			}
			this.checkflag = false;
			return true;
		}
	}
};

var fader = {
		
	fade_in : function(id, delay, o) {
		if(o>100)return;
		if (!delay) delay = 50;
		if (o == null) o = 0;
		this.setOpacity(document.getElementById(id),o);
		o+=20;
		setTimeout("fader.fade_in('"+id+"',"+delay+","+o+")", delay);
	},
		
	fade_out : function(id, callback, delay, o) {
		if(!delay) delay = 50;
		if(o == null) o = 100;
		var element=document.getElementById(id);
		if (!element) return;
		if(o<0) {
	  		element.style.zIndex = -20;
	  		if (typeof(callback) == 'function') {
	  			callback(id);
	  		}
			return true;
		}		
		this.setOpacity(element,o);
		o-=20;
		setTimeout("fader.fade_out('"+id+"',"+callback+","+delay+","+o+")", delay);
	},		

	setOpacity : function(obj, opacity) {
	  if (!obj) return;
	  opacity = (opacity == 100)?99.999:opacity;
	  
	  obj.style.filter = "alpha(opacity="+opacity+")";	  
	  obj.style.KHTMLOpacity = opacity/100;	  
	  obj.style.MozOpacity = opacity/100;	  
	  obj.style.opacity = opacity/100;
	}
};

var box = {
		
	obj : null,
	objx : 0,
	objy : 0,
	posx : 0,
	posy : 0,
	
	load : function(params) {
		if (!params) return;
		m.query(params, '', box.open);
	},
	
	open : function(ret) {
		check = m.id('box');
		if (check) return;
		
		var el = document.createElement("div");
		el.innerHTML = ret;
		var body = document.getElementsByTagName('body')[0];
		body.appendChild(el);
		
		var inner = m.id('box');
		var objW = inner.offsetWidth;
		var objH = inner.offsetHeight;
        var pageX = (document.all) ? document.body.offsetWidth : window.innerWidth;
        var pageY = (document.all) ? document.body.offsetHeight : window.innerHeight;
        inner.style.left = ((pageX/2)-(objW/2))+"px";
        inner.style.top = ((pageY/2)-(objH/2))+"px";
        
		fader.fade_in('box', 50, 0);
	},
	
	select : function(id) {
		var obj = m.id(id);
		this.obj = obj;
		this.objx = this.posx - obj.offsetLeft;
	    this.objy = this.posy - obj.offsetTop;
	    this.obj.style.cursor = 'move';
	    m.evAdd(m.w, 'mouseup', box.unselect);
	},

	move : function(e) {
		var t = box;
	    t.posx = (e.clientX) ? e.clientX : e.pageX;
	    t.posy = (e.clientY) ? e.clientY : e.pageY;
	    if (box.obj === null) return;
	    t.obj.style.left = (t.posx - t.objx) + "px";
	    t.obj.style.top = (t.posy - t.objy) + "px";
	},
	
	unselect : function() {
		t = box;
		if (!t.obj) return;
		t.obj.style.cursor = '';
		t.obj = null;
	},
	
	minimize : function(id) {
		m.toggle('boxcont');
	},
	
	close : function(id, remove) {
		var obj = m.id(id);		
		if (remove == true) {
			var parent = obj.parentNode;
			var body = document.getElementsByTagName("body")[0];
			body.removeChild(parent);
		}
		else {
			fader.fade_out(id, function(id) {box.close(id, true);});
		}
	}
};

var editor = {
		
	obj : null,
	topobj : null,
	top : 0,
	height : 0,	
	browser : '',
		
	init : function() {
	
		editor.browser = navigator.appName;
		var content = m.id('editor_nachricht').value;
		
		// if IE
		if (editor.browser == "Microsoft Internet Explorer") {
			
			var id = "editordiv";
			if(!m.id(id)) return;
			
			this.obj = m.id(id);
			editor.obj = this.obj;
			
			this.obj.style.display = 'inline';
			this.obj.innerHTML = content;
		}
		// other browsers
		else {
			
			var id = "editor";
			if(!m.id(id)) return;
			
			this.obj = m.id(id).contentWindow.document;
			editor.obj = this.obj;
			
			m.id(id).style.display = 'inline';
			
			var el = document.createElement("link");
			el.href = '/themes/editor.css';
			el.rel = 'stylesheet';
			el.type = 'text/css';
											
			var head = this.obj.getElementsByTagName('head')[0];		
			head.appendChild(el);
					
			var body = this.obj.getElementsByTagName('body')[0];
			body.innerHTML = content;
		}
		this.obj.designMode = "On";
	},		

	format : function(command, parms, value) {
		if (editor.browser == "Microsoft Internet Explorer") {
			m.id("editordiv").focus();
			m.d.execCommand(command, false, parms);
		}
		else {
			m.id("editor").focus();
			this.obj.execCommand(command, false, parms);
		}			
	},
	
	addImage : function() {
		var path = prompt('Bild-Pfad angeben:');
		if(!path) return;
		this.format('insertImage', path);
	},
	
	addLink : function() {
		var path = prompt('Link-Pfad (URL) angeben:');
		if(!path) return;
		this.format('createLink', path);
	},
	
	getContent : function() {
		if (editor.browser == "Microsoft Internet Explorer") {
			var content = m.id("editordiv").innerHTML;
		}
		else {
			var body = m.id("editor").contentWindow.document.getElementsByTagName('body')[0];
			var content = body.innerHTML;
		}
		m.id('editor_nachricht').value = content;
	},
	
	select : function(id) {
		t = editor;
		var obj = m.id(id);		
		t.topobj = obj;
		t.top = 0;
		t.height = parseFloat(obj.style.height);		
	    m.evAdd(m.w, 'mouseup', editor.unselect);	    
	},

	move : function(e) {
		var t = editor;
		if (t.topobj === null) return;
		if(!t.top) {
			t.top = (e.clientY) ? e.clientY : e.pageY;
		}
		var diffY = (e.clientY) ? t.top - e.clientY : t.top - e.pageY;
		if((t.height - diffY) > 0) {
			var height = (t.height - diffY);
			if(height < parseFloat(t.topobj.style.minHeight)) return;
			t.topobj.style.height = height + "px";
		}
	},
	
	unselect : function() {
		t = editor;
		if (!t.topobj) return;
		t.topobj = null;
	}
};

m.evAdd(m.w, 'load', m.init);
m.evAdd(m.w, 'load', m.fadeMessages);
m.evAdd(m.w, 'load', editor.init);
m.evAdd(m.w, 'mousemove', box.move);
m.evAdd(m.w, 'mousemove', editor.move);