/*
(function(){
	if(document.querySelectorAll){
		var x$$ = window.$$;
		window.$$ = function(sel){
			if(typeof sel != 'string'){
				return x$$(sel);
			}
			var res = document.querySelectorAll(sel);
			if( res[0] && !res[0].addEvent ){
				alert(1)
				$each(res, $);
			}
			return (res.each ? res : $A(res));
		}
	}
})()
*/


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);
	};
};


String.prototype.stripTags=function(){return this.replace(/<([^>]+)>/g,'');};


function flashObject(name) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[name];
	} else {
		return document[name];
	}
}

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({
	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();
		}
	},
	outline: function(v){
		v = this.outlineVal = v || this.outlineVal;
		if( !Browser.Engine.trident ){
			this.setStyle('outline',v);
			return this;
		}
		if(!this.outlineDivs){
			var el = new Element('div', {styles:{position:'absolute', fontSize:0,lineHeight:0,zIndex:3000}})
			this.outlineDivs = { t:el,l:el.clone(),b:el.clone(),r:el.clone() };
			$each(this.outlineDivs, function(el){el.injectInside(document.body)})

			this.addEvent('resize', function(){
				this.outline();
			})
		}
		var cords = this.getCoordinates();

		var width = v.replace(/([0-9]*)px/,'$1').toInt();

		$each(this.outlineDivs, function(el){el.setStyles({border:v, borderWidth:0})})
		this.outlineDivs.t.setStyles({ top:cords.top-width, 		width:cords.width+2*width, 	borderTopWidth:width, 			left:cords.left-width });
		this.outlineDivs.l.setStyles({ top:cords.top-width, 		borderRightWidth:width, 	height:cords.height+2*width, 	left:cords.left+cords.width });
		this.outlineDivs.b.setStyles({ top:cords.top+cords.height, 	width:cords.width+2*width, 	borderTopWidth:width, 			left:cords.left-width });
		this.outlineDivs.r.setStyles({ top:cords.top-width, 		borderLeftWidth:width, 		height:cords.height+2*width, 	left:cords.left-width });
	},
	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 : '';
		}
		return this.value;
	},
	setV: function(v){
		if( this.type === 'radio' ){
			$each(this.form[this.name],function(el){ 
				if(el.value === v){
					el.checked = "checked";
				}
			})
			return;
		} else {
			this.value = v;
		}
	},
	qgDelegate: function(type, sel, fn){
		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); check(e.target) && fn(e) }
		if(type === 'blur'){
			this.attachEvent && this.attachEvent('onfocusout', rfn);
			this.addEventListener && this.addEventListener('blur', rfn, true);
		} else {
			this.addEvent(type, rfn );
		}
	} 
});


function nl2br(string){
	return string.replace(/\n/g, '<br />');
}
function br2nl(string){
	return string.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;
	if( _.what !== 'x' ){ _.el.style.top  	= document.getScroll().y + (window.getSize().y /2 - _.el.offsetHeight/2).toInt()+'px'; }
	if( _.what !== 'y' ){ _.el.style.left 	= document.getScroll().x + (window.getSize().x /2 - _.el.offsetWidth /2).toInt()+'px';	 }
}


implementBeforeBlur = function(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);
 		r.moveStart("character", pos.start);
		r.moveEnd("character", pos.end-pos.start);
		r.select();
	}
}



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);
	});
});







/*
maxZIndex = 1;
function setZIndex(el, index){
	el.style.zIndex = index;
	maxZIndex = Math.max(index, maxZIndex);
}
function setZIndexTop(el){
	setZIndex(el, maxZIndex+2);
}
*/
/*
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;
	};
};
*/

/*
Element.prototype.originalGetOffsets = Element.prototype.getOffsets;
Element.implement({
	getOffsets: function(){
		var BUGGY_GECKO = Browser.Engine.gecko && document.getBoxObjectFor && (this.style.top == '' || this.style.left == '') && this.getStyle('position') == 'absolute';
		if (this.getBoundingClientRect) {
			var box = this.getBoundingClientRect();
			return {x:box.left + document.getScrollLeft(),	y:box.top + document.getScrollTop()}
		} else if (document.getBoxObjectFor && !BUGGY_GECKO ) {
			var box = doc.getBoxObjectFor(el);
			var vpBox = doc.getBoxObjectFor(document.body);
			return {x:box.screenX - vpBox.screenX, y:box.screenY - vpBox.screenY}
		} else {
			return Element.prototype.originalGetOffsets.bind(this)();
		}
	}
})
*/

