//when the dom is ready...
window.addEvent('domready',function() {

	// Submitbutton in Quicknav beim Laden verstecken
	//hide the button 
	var submit = $$('.mod_quicknav span.button');
	submit.addEvent('load', function() {submit.hide();});
	submit.setStyle('display', 'none');
	//our event function
	var handler = function() {
		var href = this.options[this.selectedIndex].get('value');
		if(href) {
			window.location.replace(href);
		}
	};
	//add the event listener
	var target = $$('div.mod_quicknav select');
	target.addEvent('change', handler);
	
	//a few settings
	var labelColor = '#999', restingPosition = 2;

	// Definiere Tabreiter
	var reiter = $$('div#header li.level_1');
	//for every form field
	/*
// 	$$('#right form .input-text label').each(function(label){
// 		// Label ausblenden
// 		label.setStyles({
// 			//color: labelColor,
// 			//position: 'absolute',
// 			//top: restingPosition,
// 			//left: 2,
// 			display: 'none'
// 			//'z-index': 0
// 		});
// 		//get input value
// 		var input = label.getNext('input');
// 		// default styling
// //		var defaultFontSize = input.getStyle('font-size');
// 		var defaultColor = input.getStyle('color');
// 		var labelText = label.get('text');
// 		//onload, check if a field is empty, if so, set value to labelText
// 		// skipping Password fields
// 		if(input.get('type') != 'password') {
// 				if(input.get('value') == '') {
// 				input.set('value',labelText);
// 				input.setStyles({
// 					color: labelColor
// 				});
// 		}
// 		// if the input value is labelText on focus
// 		// if it's empty on blur, set value to labelText
// 		input.addEvents({
// 				focus: function(e) {
// 					if(input.get('value') == labelText) {
// 						input.set('value', '')
// 						input.setStyles({
// 							color: defaultColor
// 						});
// 				}
// 			},
// 			blur: function() {
// 				if(input.get('value') == ''){
// 						input.set('value',labelText);
// 						input.setStyles({
// 							color: labelColor
// 						});
// 				}
// 			}
// 		});
// 		}
	*/

	$$('div#header li.level_1').each(function(li){
		li.addEvents({
			mouseenter: function(e) {
				reiter.each(function (el) {
					var point = [];
					point.x = e.client.x;
					point.y = e.client.y;
					var dimension = el.getCoordinates();
					dimension.bottom = dimension.bottom + 17;
					var treffer =  PtInRect(point, dimension);
					if (el.hasClass('active')) {
						el.removeClass('active');
						el.addClass('active_muted');
					}
					if (el.hasClass('trail') && !treffer) {
						el.removeClass('trail');
						el.addClass('trail_muted');
					}
				});
			},
			mouseleave: function(e) {
				var target = e.target;
				reiter.each(function (el) {
					if (el.hasClass('active_muted')) {
						el.removeClass('active_muted');
						el.addClass('active');
					}
					if (el.hasClass('trail_muted')) {
						el.removeClass('trail_muted');
						el.addClass('trail');
					}
				});
			}
		});
	});
	
	$$('div.mod_quicknav option').each(function(option) {
		var frag = window.location.href.substr( window.location.href.indexOf(window.location.pathname)+1, window.location.href.length);
		if (frag == option.value) option.set('selected', true);
	});
	

	var sidebar = new ScrollSidebar('aside_banner',{
		offsets: {
			y: 155
		}
	});
	
});



function PtInRect(point, rect)
{
var result = new Boolean(true);
if (point.x<rect.left) result = false;
if (point.x>rect.right) result = false;
if (point.y>rect.bottom) result = false;
if (point.y<rect.top) result = false;
return result;
}


var ScrollSidebar = new Class({
	
	Implements: [Options],
	
	options: {
		offsets: { x:0, y:0 },
		mode: 'vertical',
		positionVertical: 'top',
		positionHorizontal: 'right',
		speed: 400
	},
	
	initialize: function(menu,options) {
		/* initial options */
		this.setOptions(options);
		this.menu = $(menu);
		this.move = this.options.mode == 'vertical' ? 'y' : 'x';
		this.property = this.move == 'y' ? 'positionVertical' : 'positionHorizontal';
		/* ensure a few things */
		var css = { position: 'absolute', display:'block' };
		css[this.options.positionVertical] = this.options.offsets.y;
		css[this.options.positionHorizontal] = this.options.offsets.x;
		this.menu.setStyles(css).set('tween',{ duration: this.options.speed });
		/* start listening */
		this.startListeners();
	},
	
	startListeners: function() {
		var action = function() {
			this.setPosition($(document.body).getScroll()[this.move] + this.options.offsets[this.move]);
		}.bind(this);
		window.addEvent('scroll',action);
		window.addEvent('load',action);
	},
	
	setPosition: function(move) {
		this.menu.tween(this.options[this.property],move);
		return this;
	}
});