

var EvUSite = Class.create({
	options: {
		multiplier:	100,
		seperator:	'#'
	},
	initialize: function(arg) {
		this.args				= arg || 0;
		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');

		if(s && this.args.slides)	new Slideshow(s);
	}
});
var Slideshow = Class.create(EvUSite, {
	initialize: function(s) {
		$('header-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 Options = {
	keyImgId					: 'key-image',
	keyImgDefault				: 'portal-key-default.jpg',
	pearl						: ['c6c7074', 'portal-key-perl.jpg'],
	innovativeUmwelttechnik		: ['c18487c', 'portal-key-umwelttechnik.jpg'],
	anlagenbauInternational		: ['ce1832e', 'portal-key-solar.jpg'],
	ingenieurbuero				: ['c9abf0d', 'portal-key-ingenieurbuero.jpg'],
	acts						: ['cc34047', 'portal-key-china-business-center.jpg'],
	kielce						: ['c73b0c1', 'portal-key-kielce.jpg'],
	china						: ['caa3737', 'portal-key-china.jpg'],

	imgPath						: '/img/',
	animType					: 'Fade',
	animSpeed					: .3,
	animDuration				: .2,
	mouseOverDelay				: .05,
	mouseOutDelay				: 1
};

var Page = Class.create({
	initialize: function(arguments) {
		var args				= arguments || 0;
		this.options			= Options;
		this.color				= args.color || this.options[document.body.className.camelize()][0];
		
		this.menuslider			= new MenuSlider();
	}
});

var MenuSlider = Class.create({
	elements: new Object(),
	initialize: function() {
		this.options			= Options;
		this.oldy				= -50;
		
		this.elements.menu		= $('menu');
		this.elements.slider	= new Element('img', { src: '/img/arrow-right.gif', alt: 'Menu Slider' }).absolutize();
		this.elements.active	= $(this.elements.menu.select('li.active')[0]) || 0;
		this.elements.selected	= 0;
		this.elements.selected.mouseOut	= this.mouseOut;
		this.elements.link		= 0;
		this.elements.links		= this.elements.menu.select('li a');
		this.elements.links.each((function(l) {
			l.parent	= this;
			l.newy		= l.viewportOffset().top - this.elements.menu.viewportOffset().top;
			l.mouseOver	= this.mouseOver;
			l.mouseOut	= this.mouseOut;
			l.normal	= $(l.select('span.normal')[0]);
			l.teaser	= $(l.select('span.teaser')[0]);
			l.addClassName('nohover');
			l.parentNode.linkTarget = l;
			l.descendants().each((function(d) {
				d.linkTarget = this;
			}).bind(l));
		}).bind(this));
		
		if (this.elements.active) {
			this.elements.active.removeClassName('active');
			this.elements.link = this.elements.active.down('a');
			this.oldy = this.elements.link.viewportOffset().top - this.elements.menu.viewportOffset().top;
			this.elements.link.mouseOver();
		}
		
		this.elements.slider.setStyle({
			width: '5px',
			height: '12px',
			top: this.oldy + 'px',
			left: '-20px',
			border: '0px solid #f00'
		});
		
		this.elements.menu.insert({
			top: this.elements.slider
		});
		
		$$('body').first().observe('mouseover', (function(ev) {
			var el = Event.element(ev);
			
			if (el.descendantOf('menu') && el != this.elements.menu) {
				if (el.tagName == "A")
					el.mouseOver();
				else el.linkTarget.mouseOver();
			} else {
				/*new Effect.Morph(this.elements.slider, {
					style: 'top: ' + this.oldy + 'px',
					delay: this.options.mouseOutDelay,
					duration: this.options.animSpeed
				});*/
				if (this.elements.link)
					this.elements.link.mouseOver();
				else {
					if (this.elements.selected)
						this.elements.selected.mouseOut();
					new Effect.Morph(this.elements.slider, {
						style: 'top: ' + this.oldy + 'px',
						duration: this.options.animSpeed
					});
				}
			}
		}).bind(this));

	},
	mouseOver: function() {
		if (this != this.parent.elements.selected) {
			new Effect.Parallel([
				new Effect.Morph(this.parent.elements.slider, { sync: true, style: 'top: ' + this.newy + 'px' }),
				new Effect.Morph(this.normal, { sync: true, style: 'margin-left: 5px' }),
				new Effect.Morph(this.teaser, { sync: true, style: 'margin-left: 10px' })
			], { duration: this.parent.options.animSpeed });

			if (this.parent.elements.selected)
				this.parent.elements.selected.mouseOut();
			
			this.parent.elements.selected = this;
		}
	},
	mouseOut: function() {
		new Effect.Parallel([
			new Effect.Morph(this.parent.elements.selected.normal, { sync: true, style: 'margin-left: 0px' }),
			new Effect.Morph(this.parent.elements.selected.teaser, { sync: true, style: 'margin-left: 0px' })
		], { duration: this.parent.options.animSpeed });
		
		this.parent.elements.selected = 0;
	}
});

var Portal = Class.create({
	initialize: function(arguments) {
		var rems				= $$('a.skipNavigation').invoke('remove');

		this.options			= Options;
		this.loader				= $('loader');
		this.keyImgsLoaded		= new Array();
		this.keyImgId			= this.options.keyImgId;
		this.animType			= this.options.animType;
		this.animDuration		= this.options.animDuration;
		this.animSpeed			= this.options.animSpeed;
		this.animDelay			= this.options.animDelay;

		this.footer				= $($$('p.footer')[0]);
		this.container			= $($$('div.container')[0]);
		this.container.insert({ bottom: this.container.innerHTML });

		this.headings			= this.container.select('div.heading');
		this.options.imgPath	= this.options.imgPath;
		
		this.keyImg				= $(this.keyImgId);
		this.keyImg2			= this.keyImg.insert({ 'after' : new Element('img', {
			'id': this.keyImgId + "2",
			'class': this.keyImg.className,
			'src': this.keyImg.src,
			'alt': this.keyImg.alt
		}) });
		
		this.keyImg				= this.keyImg.setStyle({ zIndex : 98 });
		this.keyImg2 			= $(this.keyImgId + "2").clonePosition(this.keyImg).absolutize().setStyle({ zIndex : 99 });

		this.lower				= this.keyImg;
		this.upper				= this.keyImg2;

		this.headings.each((function(heading) {
			this.prepareItem(heading);
		}).bind(this));
	},

	focusItem: function() {
		switch (true) {
			case (typeof this.parent.open == "undefined"):
				this.parent.rotateItems(this);
				this.parent.showItem(this);
				this.parent.open = this.descr;
				break;
			case (this.parent.open == this.descr):
				this.parent.hideItem(this, true);
				delete this.parent.open;
				break;
			default:
				this.parent.hideItem(this, false);
				this.parent.rotateItems(this);
				this.parent.showItem(this);
				this.parent.open = this.descr;
				break;
		}
	},

	rotateItems: function(target) {
		var h = new Array();
		var i = target.parent.headings.indexOf(target);
		var j = new Number(0);

		if (i > 0) {
			new Effect.Tween(
				this.container,
				this.container.scrollTop,
				i * 50, {
					duration: this.animDuration * i,
					/*transition: Effect.Transitions.spring,*/
					queue: { position:'end', scope: 'global', limit: 3 },
					afterFinish: (function() {
						for (; j < i; j++) {
							h[j] =	this.headings.shift();
									this.headings.push(h[j]);
							this.container.insert(h[j]);
							this.container.insert(h[j].descr);
							this.prepareItem(target);
							this.swapKeyImg(target.keyImg);
						}
						this.container.scrollTop = 0;
					}).bind(this)
				},
				'scrollTop'
			);
		} else {
			this.prepareItem(target);
			this.swapKeyImg(target.keyImg);
			return;
		}
	},
	showItem: function(item) {
		//alert(parseInt(item.descr.getHeight() + 350));
		new Effect.Parallel([
			new Effect.BlindDown(item.descr, { sync: true }),
			new Effect.Morph(this.container, { sync: true, style: 'height: ' + parseInt(item.descr.getHeight() + 350) + 'px' }),
			new Effect.Morph(this.footer, { sync: true, style: 'top: ' + parseInt(item.descr.getHeight() + 745) + 'px' })
		], {
			duration: this.animSpeed,
			queue: { position:'end', scope: 'global', limit: 3 }
		});
	},
	hideItem: function(item, swapper) {
		new Effect.Parallel([
			new Effect.BlindUp(item.parent.open, { sync: true }),
			new Effect.Morph(item.parent.container, { sync: true, style: 'height: 350px' }),
			new Effect.Morph(item.parent.footer, { sync: true, style: 'top: 745px' })
		], {
			duration: item.parent.animSpeed,
			queue: { position:'end', scope: 'global', limit: 3 },
			afterFinish: (function() {
				this.parent.prepareItem(this);
				if (swapper)
					this.parent.swapKeyImg();
			}).bind(item)
		});
	},

	prepareItem: function(item) {
		item.descr					= item.next(0);
		item.descr.heading			= item;
		item.descr.heading.parent	= this;
		item.parent					= this;
		item.colorClass				= item.className.split(" ")[0].camelize();
		item.keyImg					= this.options[item.colorClass][1];

		item.observe('click', this.focusItem);
		//item.observe('mouseover', this.newKeyImg);
		//item.observe('mouseout', this.swapKeyImg.bind(this));
	},
	freezeItem: function(item) {
		item.stopObserving('click');
		//item.stopObserving('mouseover');
		//item.stopObserving('mouseout');
	},

	changeKeyImg: function(newSrc) {
		/*
		new Effect.Opacity(this.keyImg, { to: 0, duration: .2, queue: { scope: 'keyImg' }, afterFinish: (function() {
			new Effect.Opacity(this.keyImg, { to: 1, duration: .2, queue: { scope: 'keyImg' }, beforeStart: (function() {
				this.keyImg.src	= this.options.imgPath + newSrc;
			}).bind(this)});
		}).bind(this) });
		*/

		this.lower.src = this.options.imgPath + newSrc;
		this.fadeUpper = new Effect[this.animType](this.upper, { duration: this.animSpeed, queue: { scope: 'keyImg' }, afterFinish: (function() {
			var jehirn = this.upper;
			this.upper = this.lower.setStyle({ zIndex : 99 });
			this.lower = jehirn.setStyle({ zIndex : 98 });
			this.lower.show();
		}).bind(this) });
	},
	swapKeyImg: function(override) {
		var newSrc		= (typeof override == 'string') ? override : this.options.keyImgDefault;
		
		function in_array(needle, haystack, strict) {
			var found = false, key, strict = !!strict;
			for (key in haystack) {
				if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
					found = true;
					break;
				}
			}
			return found;
		}
		
		if (in_array(newSrc, this.keyImgsLoaded))
			this.changeKeyImg(newSrc);
		else {
			var tmpImg					= new Image();
				tmpImg.src				= this.options.imgPath + newSrc;
				tmpImg.onload			= (function() {
					this.changeKeyImg(newSrc);
					this.keyImgsLoaded.push(newSrc);
				}).bind(this);
		}
	}
});
