function objectByPath(ipiObject, spiPath, spiDelimiter)
{
	var iElement = ipiObject;
	
	var saPath = spiPath.split(spiDelimiter);
	
	for(var nAktu = 0; nAktu < saPath.length; nAktu++)
	{
		iElement = iElement.getChildren()[saPath[nAktu]];
	}
	
	return iElement;
}

function playerReady(thePlayer){
	var player = window.document[thePlayer.id];
	player.addModelListener("STATE", "redirectToHomepage");
	player.addModelListener("TIME", "fadeOutVideo");
}

var animation_started = false;
function fadeOutVideo(obj){
	var currentPosition = obj.position;
	if(currentPosition > 33 && animation_started == false){
		animation_started = true;
		$('makingOfVideo').set('morph',
			{
				duration: 1200
			}
		);
		$('makingOfVideo').get('morph').start( { 'opacity': [1,0] } ).chain(
			function(){
				$('makingOfVideo').dispose();
			}
		);
	}
}

function redirectToHomepage(obj){
	if(obj.newstate == 'PAUSED' || obj.newstate == 'COMPLETED'){
		$('makingOfVideo').set('morph',
			{
				duration: 1200
			}
		);
		$('makingOfVideo').get('morph').start( { 'opacity': [1,0] } ).chain(
			function(){
				$('makingOfVideo').dispose();
			}
		);
	}
}

function setBackgroundImageStyles(){
	var background_wrapper = $('main_bg');
	if(background_wrapper){
		background_wrapper.setStyle('opacity', 0.5);
	}
}

function resizeBackgroundImage(){
	var background_wrapper = $('main_bg');
	if(background_wrapper){
		var window_size = window.getSize();
		var background_image = background_wrapper.getElement('img');
		var background_image_size = background_image.getSize();
		var image_ratio = background_image_size.x / background_image_size.y;
		var window_ratio = window_size.x / window_size.y;
		if(image_ratio < window_ratio){
			background_image.setStyles(
				{
					'width': '100%',
					'height': 'auto'
				}
			);
		}else{
			background_image.setStyles(
				{
					'width': 'auto',
					'height': '100%'
				}
			);
		}
	}
}

function initBackgroundImage(){
	resizeBackgroundImage();
	var show_intro = $('show_intro');
	if(show_intro){
		var main_bg = $('main_bg');
		var cloned_main_bg = main_bg.clone();
		cloned_main_bg.set('id', 'main_bg_clone');
		cloned_main_bg.setStyles(
			{
				'opacity': 1
			}
		);
		var text = new Element('div',
			{
				'html': 'autumn/winter 2011',
				'class': 'intro_text',
				'styles': {
					'opacity': 0
				}
			}
		);
		var logo = $('logo');
		var navi = $('navi');
		logo.set('morph', { 'duration': 1000 });
		navi.set('morph', { 'duration': 1000 });
		logo.setStyle('opacity', 0);
		navi.setStyle('opacity', 0);
		cloned_main_bg.inject(document.body);
		cloned_main_bg.set('morph', { 'duration': 1000 });
		text.inject(document.body);
		text.set('morph', { 'duration': 1000 }).get('morph').start( { 'opacity': 1 } ).chain(
			function(){
				logo.get('morph').start( { 'opacity': 1 } ).chain(
					function(){
						navi.get('morph').start( { 'opacity': 1 } ).chain(
							function(){
								var fx = function(){
									text.get('morph').start( { 'opacity': 0 } );
									cloned_main_bg.get('morph').start( { 'opacity': 0 } ).chain(
										function(){
											cloned_main_bg.dispose();
											text.dispose();
										}
									);
								}
								fx.delay(4000);
							}
						);
					}
				);
			}
		);
	}else{
		setBackgroundImageStyles();		
	}
}

function initSelectedLine(){
	var content = $('content');
	if(content){
		var selected_line = content.getElement('.selected_line');
		if(selected_line){
			var article_numbers = content.getElements('em');
			article_numbers.each(
				function(nr, index){
					if(index > 0){
						var selected_line_copy = selected_line.clone();
						var position = nr.getPosition(content);
						/* selected_line_copy.setStyles(
							{
								'top': position.y
							}
						); */
					//	selected_line_copy.inject(nr, 'before');
					}
				}
			);
		}
	}
}

window.addEvent('domready',
	function()	{
		var fx=function() {
			if ($('wrapper').className == 'intro') // $$('.intro')[0]
			{
				$('header').get('morph').start (
				{
					'opacity': [0,1]
				}
			);	
			}
		}
		fx.delay(6000);
		
		if($('art_thumb_vorne'))
		{
			$('art_thumb_vorne').addEvent('click',
				function(){
					var div = $('art_bild');
					var bild = div.getChildren()[0].getChildren()[0];
					var neuesBild = $('art_thumb_vorne').getChildren()[0].getProperty('src');
					var link = div.getChildren()[0];

					neuesBild = 'index.php?rex_resize=180w__' + neuesBild.split('__')[3];
					bild.setProperty('src',neuesBild);

					var sBild = neuesBild.split('__')[1];
					link.setProperty('href','files/' + sBild);
					//div.getChildren()[0].getChildren()[1].getChildren()[0].getChildren()[0].setProperty('src','files/' + sBild);
					link.getChildren('div')[0].getChildren('div')[0].getChildren('img')[0].setProperty('src', 'files/' + sBild);
					//var newImg = objectByPath(div,'0 1 0 0',' ');
					//newImg.set('src', 'files/' + sBild);

					// MagicZoom.update();
				}
			);
			if($('art_thumb_hinten'))
			{
				$('art_thumb_hinten').addEvent('click',
					function(){
						var div = $('art_bild');
						var bild = div.getChildren()[0].getChildren()[0];
						var neuesBild = $('art_thumb_hinten').getChildren()[0].getProperty('src');
						var link = div.getChildren()[0];

						neuesBild = 'index.php?rex_resize=180w__' + neuesBild.split('__')[3];
						bild.setProperty('src',neuesBild);

						var sBild = neuesBild.split('__')[1];
						link.setProperty('href','files/' + sBild);
						//div.getChildren()[0].getChildren()[1].getChildren()[0].getChildren()[0].setProperty('src','files/' + sBild);
						// var newImg = objectByPath(div,'0 1 0 0',' ');
						link.getChildren('div')[0].getChildren('div')[0].getChildren('img')[0].setProperty('src', 'files/' + sBild);
						// MagicZoom.update();
					}
				);
			}
			
		}
		initBackgroundImage();
		initSelectedLine();
	}
);

window.addEvent('resize', resizeBackgroundImage);
