var Site = Class.create({
	options: {
		multiplier:	123,
		seperator:	'#'
	},
	initialize: function(arg) {
		this.args				= arg || 0;
		this.options.color		= this.args.color		|| '#555555';	// STD RETURN COLOR
		this.options.mark		= this.args.mark		|| '#4b8fc2';	// STD MARKER COLOR
		this.options.light		= this.args.light		|| '#4b8fc2';	// STD LIGHT COLOR
		this.options.animation	= this.args.animation	|| 'Fade';
		this.options.slidespeed	= this.args.slidespeed	|| 1.0;
		this.options.slidedelay	= this.args.slidedelay	|| 2.5;
		this.options.slides		= this.args.slides		|| 0;

		var s	= $('header');
		var h	= $('h-f', 'h-c', 'h-r');
		var m	= $($$('#menu li ul'));
		var c	= $('callbackbox');

		if(s && this.args.slides)	new Slideshow(s);
		if(c)						new AutoForm(c);
		if(h.first())				new Startpage(h);
		if(m)						m.each(function(mn) {
										new Dropdown(mn);
									});
	}
});

var Slideshow = Class.create(Site, {
	initialize: function(s) {
		$('container').appendChild(new Element('img', { 'id': "header2", 'src': s.src, 'alt': s.alt }));
		
		this.i		= this.options.slides.length;
		this.s		= s.absolutize().setStyle({ zIndex : 98 });
		this.s2 	= $('header2').absolutize().clonePosition(this.s).setStyle({ zIndex : 99 });

		this.lower	= this.s;
		this.upper	= this.s2;
		this.toggleSlides();
	},
	toggleSlides: function() {
		this.i						= (this.i >= (this.options.slides.length-1)) ? 0 : this.i + 1;
		var tmpImg					= new Image();
			tmpImg.src				= this.options.slides[this.i];
			tmpImg.onload			= (function() {
				this.lower.src		= tmpImg.src;
				this.fadeUpper = new Effect[this.options.animation](this.upper, { duration: this.options.slidespeed, delay: this.options.slidedelay, afterFinish: (function() {
					var jehirn = this.upper;
					this.upper = this.lower.setStyle({ zIndex : 99 });
					this.lower = jehirn.setStyle({ zIndex : 98 });
					this.lower.show();
					this.toggleSlides();
				}).bind(this) });
			}).bind(this);
	}
});

var Startpage = Class.create(Site, {
	initialize: function(h) {
		h.each((function(a, i) {
			var sw	= $(a.select('.sw')[0]);
				sw.setOpacity(0);
				sw.show();
			var rgb	= $(a.select('.rgb')[0]);
				rgb.setOpacity(0);
				rgb.show();
			var anims = new Array();
			a.onmouseover = (function() {
				anims[0] = new Effect.Opacity(sw, { from: 0, to: 1, duration: .3, queue: 'end', scope: 'scope_' + a.id });
				anims[1] = new Effect.Opacity(rgb, { from: 0, to: 1, duration: .6, queue: 'end', scope: 'scope_' + a.id, delay: .4 });
			}).bind(this);
			a.onmouseout = (function() {
				anims.invoke('cancel');
				new Effect.multiple([rgb, sw], Effect.Opacity, { to: 0, duration: .16 });
			}).bind(this);
		}).bind(this));
	}
});

var Dropdown = Class.create(Site, {
	initialize: function(menu, handle) {
		this.menu	= menu;
		this.active	= $($$('#menu li.active ul'));
		this.active_link = $($$('#menu li.active ul a'));
		
		this.menu.setOpacity((this.menu.up('li.active')) ? .8 : 0);
		this.menu.show();
		this.handle	= handle || this.menu.previous('a');
		
			
		if(!this.menu.up('li.active')) {
			
			Event.observe(this.handle, 'mouseover', (function() {
				if(this.menu.getStyle('opacity') <= 0) {
				
					var pairs = new Effect.Parallel([
						new Effect.Opacity(this.menu, { sync: true, from: 0, to: .8 }),
						new Effect.Morph(this.handle, { sync: true, style: 'color: ' + this.options.mark })
					], { duration: .3, scope: 'end' });
					if(this.active.first())
						new Effect.multiple(this.active, Effect.Opacity, { from: .8, to: 0, duration: .3 });
					
					if(this.active_link.first())
						new Effect.multiple(this.active_link, Effect.Opacity, { from: .8, to: 0, duration: .3 });
					
				}
			}).bind(this));
			
			Event.observe(document.body, 'mouseover', (function(event) {
				var el = Event.element(event);
				if(this.menu.getStyle('opacity') >= .8 && (!el.up('ul#menu') || ($$('#menu>li>a').indexOf(el) != -1 && el != this.handle))) {
					new Effect.Parallel([
						new Effect.Opacity(this.menu, { sync: true, from: .8, to: 0, duration: .3 }),
						new Effect.Morph(this.handle, { sync: true, style: 'color: ' + this.options.color })
					], { duration: .3, scope: 'end' });
					if(this.active.first())
						new Effect.multiple(this.active, Effect.Opacity, { from: 0, to: .8, duration: .3 });
						
					if(this.active_link.first())
						new Effect.multiple(this.active_link, Effect.Opacity, { from: 0, to: .8, duration: .3 });
						
					
				}
			}).bind(this));
		}
	}
});

var AutoForm = Class.create(Site, {
	initialize: function(e) {
		this.e = e;
		this.f = $(e.select('form')[0]);
		this.a = this.f.down('.submit');
		//this.e.update(this.unHash(this.e.title));
		//this.e.title = '';
		this.f.select('input[type=text]', 'textarea').each((function(el) {
			el.onfocus = (function() {
				this.formCheck(el);
				if(el.tagName == 'TEXTAREA' && el.innerHTML == el.title) {
					el.value = '';
				} else if(el.value == el.title)
					el.value = '';
			}).bind(this);
			el.onblur = (function() {
				this.formCheck(el);
				if(el.tagName == 'TEXTAREA' && el.innerHTML.blank()) {
					el.value = el.title;
				} else if(el.value.blank())
					el.value = el.title;
			}).bind(this);
			el.onkeyup = (function() {
				this.formCheck(el);
			}).bind(this);
		}).bind(this));

		Event.observe(this.a, 'click', (function(event) {
			if(!Event.element(event).hasClassName('disabled')) {
				var params	= this.f.serialize(true);
				var error	= $('error');
				
				this.f.disable();
				this.a.addClassName('disabled');
				
				this.f.removeAttribute('action');
				new Ajax.Request('/mail.php', {
					method: 'post',
					parameters: params,
					onFailure: (function(e) {
						this.f.enable();
						this.a.removeClassName('disabled');
						error.update('Keine Serververbindung').setOpacity(1);
						new Effect.Opacity(error, { from: 1, to: 0, duration: 1.5, delay: 5 });
					}).bind(this),
					onComplete: (function(e) {
						this.f.enable();
						this.a.removeClassName('disabled');
						error.update(e.responseText).setOpacity(1);
						new Effect.Opacity(error, { from: 1, to: 0, duration: 1.5, delay: 5 });
					}).bind(this)
				});
			}
			return false;
		}).bind(this));
	},
	formCheck: function(el) {
		var complete = new Array();
		this.f.select('.input:not([name~=firma])','textarea').each((function(d) {
			if(d.up().tagName == 'FORM' || (d.up().tagName == 'DIV' && d.up().visible()))
				complete.push((d.title != d.value && !d.value.blank()) ? true : false);
		}).bind(this));
		
		if(complete.uniq().length == 1 && complete.uniq().first()) {
			this.a.enable();
			this.a.removeClassName('disabled');
		} else {
			this.a.disable();
			this.a.addClassName('disabled');
		}
	},
	unHash: function(args) {
		var rs	= new Array();
		var ls	= args.split(this.options.seperator);
		for(var j = 0; j < ls.length; j++) {
			rs.push(String.fromCharCode(ls[j] - this.options.multiplier));
		}
		return(rs.join(''));
	},
	doHash: function(args) {
		var rs = new Array();
		for(var i = 0; i < args.length; i++) {
			rs[i] = new String();
			for(var j = 0; j < args[i].length; j++) {
				rs[i] += args[i].charCodeAt(j) + this.options.multiplier;
			}
		}
		return(rs.join(this.options.seperator));
	}
});
