var Home = {
	blendImages: [],
	blendImagesLoaded: [false, false, false, false, false],
	init: function() {
		$('vision').setStyles({
			'height': 58
		});
		$('vision_content').setStyles({
			'opacity': 0,
			'overflow': 'hidden',
			'height': 0
		});
		$$('ul#values li blockquote').each(function(quote, i) {
			quote.setStyles({
				'position': 'absolute',
				'opacity': 0,
				'left': 0,
				'top': 62,
				'overflow': 'hidden',
				'height': 0
			});
		});
		$$('ul#values li').each(function(value, i) {
			value.setStyle('opacity', 0.2);
		});
		if (Browser.Engine.webkit) {
			$('values').setStyle('height', 62);
			$$('ul#values li').each(function(value, i) {
				value.setStyle('float', 'left');
			});
		}
		if (window.screen.availHeight <= 768) {
			if (Browser.Engine.trident4) {
				$('teaser_content').setStyle('height', 67);
			}
			else {
				$('teaser_content').setStyle('height', 'auto');
				$('teaser_content').setStyle('min-height', 67);
			}
		}
		var content_margin;
		if (Browser.Engine.trident) {
			content_margin = Math.max(0, (Math.min(1024, window.screen.availHeight)-250-450-144-40)/2);
		}
		else {
			content_margin = Math.max(0, (Math.min(1024, window.screen.availHeight)-250-450-144)/2);
		}
		$('home_content').setStyles({
			'margin-top': content_margin,
			'margin-bottom': content_margin
		});
		var content_margin_with_style = Math.max(0, content_margin-71);
		
		$('special_content').setStyle('top', 360+content_margin);
		if ((Browser.Plugins.Flash.version >= 8)) {
			Home.flash = new Swiff('fileadmin/flash/gras.swf',{
				id: 'gras',
				width: 704,
				height: 584,
				container: $('special_content')
			});
		}
		var visionAndValuesFx = new Fx.Elements([$('vision'), $('home_content'), $('vision_content'), $('values'), $('claim'), $('blendimage'), $('logo_neg'), $('blendimage2')], {
			duration: 650, 
			link: 'cancel', 
			transition: Fx.Transitions.Quad.easeInOut
		});
		visionAndValuesFx.set({
			'5': {
				'opacity': 0
			},
			'6': {
				'opacity': 0
			},
			'7': {
				'opacity': 0
			}
		});
		$('vision').addEvent('mouseenter', function(e) {
			visionAndValuesFx.start({
				'0': {
					'height': 208
				},
				'1': {
					'margin-top': content_margin_with_style,
					'margin-bottom': content_margin_with_style
				},
				'2': {
					'height': 150
				},
				'3': {
					'height': 62
				},
				'5': {
					'opacity': 0
				},
				'6': {
					'opacity': 0
				},
				'7': {
					'opacity': 0
				}
			}).chain(function() {
				visionAndValuesFx.start({
					'0': {
						'background-color': '#fff',
						'opacity': 0.9
					},
					'2': {
						'opacity': 1
					},
					'3': {
						'background-color': '#fff',
						'opacity': 0.9
					},
					'4': {
						'background-color': '#fff',
						'opacity': 0.9
					},
					'5': {
						'background-image': "url(" + Home.blendImages[0].src + ")",
						'opacity': 1,
						'display': 'block'
					},
					'6': {
						'opacity': 1
					},
					'7': {
						'opacity': 0
					}
				});
			});
		});
		$('vision').addEvent('mouseleave', function(e) {
			visionAndValuesFx.start({
				'0': {
					'height': 58,
					'opacity': 1
				},
				'1': {
					'margin-top': content_margin,
					'margin-bottom': content_margin
				},
				'2': {
					'opacity': 0,
					'height': 0
				},
				'3': {
					'height': 62,
					'opacity': 1
				},
				'4': {
					'opacity': 1
				},
				'5': {
					'opacity': 0
				},
				'6': {
					'opacity': 0
				},
				'7': {
					'opacity': 0
				}
			}).chain(function(){
				$('vision').setStyle('background-color', 'transparent');
				$('values').setStyle('background-color', 'transparent');
				$('claim').setStyle('background-color', 'transparent');
				$('blendimage').setStyle('display', 'none');
				$('blendimage2').setStyle('display', 'none');
			});
		});
		
		$('values').addEvent('mouseenter', function(e) {
			visionAndValuesFx.start({
				'0': {
					'height': 58,
					'opacity': 1
				},
				'1': {
					'margin-top': content_margin_with_style,
					'margin-bottom': content_margin_with_style
				},
				'2': {
					'opacity': 0,
					'height': 0
				},
				'3': {
					'height': 204,
					'opacity': 1
				},
				'4': {
					'opacity': 1
				},
				'5': {
					'opacity': 0
				},
				'6': {
					'opacity': 0
				},
				'7': {
					'opacity': 0
				}
			}).chain(function() {
				$('blendimage').setStyle('display', 'none');
				$('blendimage2').setStyle('display', 'none');
			});
		});
		$('values').addEvent('mouseleave', function(e) {
			visionAndValuesFx.start({
				'0': {
					'height': 58,
					'opacity': 1
				},
				'1': {
					'margin-top': content_margin,
					'margin-bottom': content_margin
				},
				'2': {
					'opacity': 0,
					'height': 0
				},
				'3': {
					'height': 62,
					'opacity': 1
				},
				'4': {
					'opacity': 1
				},
				'5': {
					'opacity': 0
				},
				'6': {
					'opacity': 0
				},
				'7': {
					'opacity': 0
				}
			}).chain(function(){
				$('vision').setStyle('background-color', 'transparent');
				$('values').setStyle('background-color', 'transparent');
				$('claim').setStyle('background-color', 'transparent');
				$('blendimage').setStyle('display', 'none');
				$('blendimage2').setStyle('display', 'none');
			});
		});
		var valueQuotes = $$('ul#values li blockquote');
		var valueQuotesFx = new Fx.Elements(valueQuotes, {
			duration: 650, 
			link: 'cancel', 
			transition: Fx.Transitions.Quad.easeInOut
		});
		$$('ul#values li').each(function(value, i) {
			value.addEvent('mouseenter', function(e) {
				value.tween('opacity', 1);
				var obj = {};
				valueQuotes.each(function(valueQuote, j) {
					obj[j] = {
						'opacity': (i!=j) ? 0 : 1,
						'height': 142
					}
				});
				valueQuotesFx.start(obj).chain(function() {
					var blendObj = {};
					blendObj[0] = {
						'background-color': '#fff',
						'opacity': 0.9
					};
					blendObj[2] = {
						'opacity': 1
					};
					blendObj[3] = {
						'background-color': '#fff',
						'opacity': 0.9
					};
					blendObj[4] = {
						'background-color': '#fff',
						'opacity': 0.9
					};
					blendObj[6] = {
						'opacity': 1
					};
					if ($('blendimage').getStyle('opacity') < 1) {
						blendObj[5] = {
							'background-image': "url(" + Home.blendImages[i+1].src + ")",
							'opacity': 1,
							'display': 'block'
						};
						blendObj[7] = {
							'opacity': 0
						};
					}
					else {
						blendObj[5] = {
							'opacity': 0
						};
						blendObj[7] = {
							'background-image': "url(" + Home.blendImages[i+1].src + ")",
							'opacity': 1,
							'display': 'block'
						};
					}
					visionAndValuesFx.start(blendObj);
				});
			});
			value.addEvent('mouseleave', function(e) {
				var obj = {};
				valueQuotes.each(function(valueQuote, j) {
					obj[j] = {
						'opacity': 0,
						'height': 0
					}
				});
				valueQuotesFx.start(obj);
				value.tween('opacity', 0.2);
			});
		});
	},
	preloadBlendImages: function() {
		var blendImagePaths = ['fileadmin/images/home/background_driven.jpg', 'fileadmin/images/home/background_substanziell.jpg', 'fileadmin/images/home/background_smart.jpg', 'fileadmin/images/home/background_verantwortungsvoll.jpg', 'fileadmin/images/home/background_direkt.jpg',]

		blendImagePaths.each(function(blendImagePath, i) {
			Home.blendImages[i] = new Asset.image(blendImagePath, {
				onload: function() {
					Home.blendImagesLoaded[i] = true;
				}
			});
		});
	}
};
window.addEvent('domready', Home.init);
window.addEvent('load', Home.preloadBlendImages);
