
qg = {};
qg.expertmode = function(v){
	v = !!v;
	$Set({expertmode:v});
	document.body[(v?'add':'remove')+'Class']('expert');
	qg.expertmode.fireEvent('change', v);
}
qg.expertmode.toggle = function(){
	var v = !document.body.hasClass('expert');
	qg.expertmode( v );
	return v;
}
window.addEvent('load', function(){
	!$(document.body).hasClass('expert') && qg.expertmode.fireEvent('change', false);
})
$extend(qg.expertmode, Events.prototype);




tagFocus = {};
tagFocus.set = function(el){
	var mouseout = function(evt){
		tagFocus.over = false;
	};
	var mouseover = function(evt){
		tagFocus.over = true;
	};
	if(tagFocus.target){
		if(tagFocus.blur){
			tagFocus.blur();
		}
		tagFocus.target.removeEvent('mouseout', mouseout);
		tagFocus.target.removeEvent('mouseover', mouseover);
	}
	tagFocus.target = el;
	tagFocus.over = true;
	tagFocus.blur = false;
	el.addEvent('mouseout', mouseout);
	el.addEvent('mouseover', mouseover);
};

document.addEvent('click', function(){
	if(!tagFocus.over){
		if(tagFocus.blur){
			tagFocus.blur();
			tagFocus.blur = false;
		}
	}
});


Mouse = {
	x:0,
	y:0,
	isOver:function(el){
		el = $(el).getCoordinates();
		return (Mouse.x > el.left && Mouse.x < el.right && Mouse.y < el.bottom && Mouse.y > el.top);
	}
};
document.addEvent('mousemove', 	function(e){ Mouse.x = e.page.x; Mouse.y = e.page.y } );




Function.prototype.wait = function(time){
	var fn = this;
	var timeout = null;
	return function(){
		var inst = this;
		var args = arguments;
		$clear(timeout);
		timeout = window.setTimeout( function(){
				var ret = fn.apply(inst,args);
		}, time);
	};
};
Function.prototype.limit = function(times){
	var accessCount = times || 1;
	var fn = this; 
	return function(arguments){
		return accessCount-- > 0 ? fn(arguments) : $empty; 
	}
}

Element.notUserSelect = function(el){
	el.style.KthmlUserSelect 	= 'none';
	el.style.MozUserSelect 		= 'none';
	el.style.UserSelect 		= 'none';
	el.onselectstart = function(){ return false; };
};
Element.userSelect = function(el){
	el.style.KthmlUserSelect 	= '';
	el.style.MozUserSelect 		= '';
	el.style.UserSelect 		= '';
	el.onselectstart = function(){ return true; };
};


Element.implement({
	getParentOrSelf: function(m){
		return this.match(m)?this:this.getParent(m);
	},
	hasChildOrSelf: function(el){
		return this === el || this.hasChild(el);
	},
	hasAttribute: function(v) {
		var an = this.getAttributeNode(v);
		return an && an.specified
	},
	print: function(){
		var w = window.open();
		w.document.write(
			'<html><head>'+document.getElement('head').innerHTML+'</head><body>'+this.innerHTML+
			'</body></html>' );
		if(Browser.Engine.name == 'trident'){
			w.document.write('<script>window.setTimeout(function(){window.print();},500)</script>');
			w.location.reload();
		} else {
			w.print();
			if(Browser.Engine.name !== 'presto') 
			w.close();
		}
	},
	getLabel: function(){
		if(this.label===undefined){
			this.label = $$('label[for='+this.id+']')[0] || this.getParent('label');
		}
		return this.label;
	},
	getV: function(){
		if( this.type === 'radio' ){
			var v = '';
			$each(this.form[this.name],function(el){ 
				el.checked && (v = el.value);
			})
			return v;
		} else if( this.type === 'checkbox' ){
			return this.checked ? this.value : '';
		} else if( this.value === undefined ){
			return this.get('html');
		}
		return this.get('value');
	},
	setV: function(v){
		if( this.type === 'radio' ){
			$each(this.form[this.name],function(el){ 
				if(el.value === v){
					el.checked = "checked";
				}
			})
			return;
		} else {
			if(this.hasAttribute('value')){
				this.value = v;
			} else {
				this.set('html',v);
			}
		}
	},
	qgDelegate: function(type, sel, fn, inner){
		var check = $type(sel)==='function'?sel:function(el){
			var ok = false;
			sel.split(',').each(function(s){
				ok = Element.match(el,s)?true:ok;
			})
			return ok;
		};
		var rfn = function(e){
			e = new Event(e);
			el = $(e.target);
			//if(!el){ return; }
			while(el){
				if( check(el) ){
					return fn(e);
				}
				el = inner ? el.getParent() : false;
			}
		}
		switch(type){
			case 'blur':
				this.attachEvent && this.attachEvent('onfocusout', rfn);
				this.addEventListener && this.addEventListener('blur', rfn, true);
				break;
			case 'focus':
				this.attachEvent && this.attachEvent('onfocusin', rfn);
				this.addEventListener && this.addEventListener('focus', rfn, true);
				break;
			default:
				this.addEvent(type, rfn );
		}
	},
	zTop: function(){
		var p = this.getParent();
		var z = p.get('zTopV');
		!z && p.getChildren().each( function(el){
			var elZ = el.getStyle('zIndex').toInt();
			z = elZ > z ? elZ : z; 
		})
		z = (z||0).toInt();
		p.set('zTopV',z+ 1);
		return this.setStyle('zIndex',z+ 1);
	},
	injectFx: function(to,who){
		var oldCss = this.style.cssText;
		var from = this.getCoordinates();
		var tmpFrom = new Element('div',{styles:{height:from.height}}).inject(this,'after');
		this.inject(to,who);
		var to = this.getCoordinates();
		this.style.height = '0px';

		var ghost = this.clone().inject(document.body)
		.setStyles({position:'absolute',top:from.top,left:from.left,width:from.width,height:from.height,overflow:'hidden'})
		.morph({height:to.height,width:to.width,top:to.top,left:to.left});

		this.style.visibility = 'hidden';
		
		var complete = function(){
			this.style.cssText = oldCss;
			ghost.dispose();
			tmpFrom.dispose();
		}.bind(this);
		this.set('tween',{onComplete:complete});
		this.tween('height',to.height);

//		this.set('morph',{onComplete:complete});
//		this.morph({height:to.height,width:to.width});

		tmpFrom.tween('height',0);
	}
});



Element.NativeEvents.drop = 2;
Element.NativeEvents.dragenter = 2;
Element.NativeEvents.dragover = 2;
Element.NativeEvents.dragend = 2;
Element.NativeEvents.dragleave = 2;
Element.NativeEvents.paste = 2;

function qgRemoveNode(el){
	if(el.removeNode){ return el.removeNode(false); }
	var range = document.createRange();
	range.selectNodeContents( el );
	return el.parentNode.replaceChild( range.extractContents(), el );
}
function qgReplaceNode(el, nel){
	nel.inject(el,'after');
	el.inject(nel);
	qgRemoveNode(el);
	return nel;
}


function nl2br(str){
	return str.replace(/\n/g, '<br />');
}
function br2nl(str){
	return str.replace(/<[bB][rR][.^>]*>/g, '\n');
}

function center(el, what){
	var _ = center;
	_.el = $(el);
	_.what = what;
	_.center()
	window.addEvent('scroll', _.center);
	window.addEvent('resize', _.center);
}
center.center = function(){
	var _ = center;
	var wScroll = document.getScroll();
	var wSize = window.getSize();
	if( _.what !== 'x' ){ _.el.style.top  	= wScroll.y + (wSize.y /2 - _.el.offsetHeight/2).toInt()+'px'; }
	if( _.what !== 'y' ){ _.el.style.left 	= wScroll.x + (wSize.x /2 - _.el.offsetWidth /2).toInt()+'px';	 }
}

function implementBeforeBlur(e){
	e = new Event(e);
	$(e.target).get && e.target.fireEvent('beforeBlur', e);
}
document.attachEvent && document.attachEvent('onfocusout', implementBeforeBlur);
window.addEventListener && window.addEventListener('blur', implementBeforeBlur, true);


function inputSelectionPosGet(inp){
	if (inp.selectionStart){
		return {start:inp.selectionStart, end:inp.selectionEnd};
	} else if( document.selection ) {
		var r = document.selection.createRange();
		var start 	= -r.moveStart("character", -1000000);
		r.moveStart("character", start);
		return {start: start, end: start+r.text.length};
	}
	return 0;
}
function inputSelectionPosSet(inp, x){
	var pos = { start: x.start || x  }
	pos.end = x.end || pos.start;
	if( inp.selectionStart ){
		inp.selectionStart = pos.start;
		inp.selectionEnd = pos.end;
	} else if( inp.createTextRange ){
		var r = inp.createTextRange();
		r.collapse(true);

		var start = pos.start - (inp.value.substr(0,pos.start).match(/\n/g)||[]).length;

 		r.moveStart("character", start);
		r.moveEnd("character", pos.end-pos.start);
		r.select();
	}
}

// ändern
window.addEvent('domready', function(e){
	// bei allen radio-boxen mit gleichem namen den event "onchange" auslösen wenn "onchange" bei einer radio-box ausgelöst wird.
	window.fireRadioChange = true;
	$$('input').each(function(el){
		if(el.type !== 'radio'){ return; }
		var click = function(e){
			if(!this.form){ return; }
			$(this.form).getElements('input[name='+el.name+']').each( function(radio){
				if(!window.fireRadioChange){ return; }
				if(el === radio ){ return; }
				window.fireRadioChange = false;
				radio.fireEvent('change', e);
				window.fireRadioChange = true;
			})
		}
		el.addEvent('click', click);
	});
});




/// zzz sobald in mootools integriert
(function(){
var getByClass = (function(){
	var testee = document.createElement('div');
	testee.innerHTML = '<a name="' + $time() + '" class="â‚¬ b"></a>';
	testee.appendChild(document.createComment(''));

	if (!testee.getElementsByClassName || !testee.getElementsByClassName('b').length) return false;
	testee.firstChild.className = 'c';
	return (testee.getElementsByClassName('c').length == 1);
})();

Selectors.Utils.search = function(self, expression, local){
	var splitters = [];

	var selectors = expression.trim().replace(Selectors.RegExps.splitter, function(m0, m1, m2){
		splitters.push(m1);
		return ':)' + m2;
	}).split(':)');

	var items, filtered, item;

	for (var i = 0, l = selectors.length; i < l; i++){

		var selector = selectors[i];

		/* CUSTOM */
		if (i == 0 && self.querySelectorAll && (/^(?:\w+|\*)?(?:#|\.)?(?:\w+|\*)$/).test(selector)){
			try { items = $A(self.querySelectorAll(selector)); } catch (e){}
			if (items) continue;
		}

		if (i == 0 && getByClass && (/^\.(?:\w+|\*)$/).test(selector)){
			items = $A(self.getElementsByClassName(selector.substr(1)));
			continue;
		}
		/* END CUSTOM */

		if (i == 0 && Selectors.RegExps.quick.test(selector)){
			items = self.getElementsByTagName(selector);
			continue;
		}

		var splitter = splitters[i - 1];

		var tagid = Selectors.Utils.parseTagAndID(selector);
		var tag = tagid[0], id = tagid[1];

		if (i == 0){
			items = Selectors.Utils.getByTagAndID(self, tag, id);
		} else {
			var uniques = {}, found = [];
			for (var j = 0, k = items.length; j < k; j++) found = Selectors.Getters[splitter](found, items[j], tag, id, uniques);
			items = found;
		}

		var parsed = Selectors.Utils.parseSelector(selector);

		if (parsed){
			filtered = [];
			for (var m = 0, n = items.length; m < n; m++){
				item = items[m];
				if (Selectors.Utils.filter(item, parsed, local)) filtered.push(item);
			}
			items = filtered;
		}
	}
	return items;
};
})();



/* zzz
Function.prototype.onceIn = function(time){
	var self = this;
	var fn = self;
	return function(){
		var ret = null;
		if(fn !== null){
			if( time !== undefined ){
				window.setTimeout( function(){
					fn = self;
				}, time);
			}
			var ret = fn.apply(self,arguments);
			fn = null;
		}
		return ret;
	};
};
Function.before = function(obj,name,nfn){
	var old = obj[name];
	obj[name] = function(){
		if(nfn.apply(obj,arguments) !== false){
			return old.apply(obj,arguments);
		}
	}
}
Function.after = function(obj,name,nfn){
	var old = obj[name];
	obj[name] = function(){
		obj.retrunValue = old.apply(obj,arguments);
		nfn.apply(obj,arguments);
	}
}
*/

onerror = function(e){
	var x = arguments;
	x = {msg:x[0],file:x[1],line:x[2],x:x[3]};
	window.setTimeout( function(){
		window.$fn && $fn('errorReport')(x);
	},200);
}