Interface.Projector = Class.create({
	initialize: function() {
		app = this;
		app.isRunning = false;
		
		app.objProjector = $('projector');
		app.topics = $$('#projector ul.topics li.topic_item');
		app.images = $$('#projector ul.images li img');
		app.loop = null;
		
		app.queue = Effect.Queues.get('projector');
		
		var i = 0;
		app.topics.each(function(topic) {
			topic.currentIndex = i;
			topic.style.cursor = 'pointer';
			if(i>0) {
				//topic.setOpacity(0.7);
			}
			topic.observe('mouseover', app.toggle.bind(topic));
			i++;
		});
		
		app.active_topic = app.topics[0];
		
		var i = 0;
		app.images.each(function(image) {
			image.currentIndex = i;
			app.init(image);
			i++;
		});
		
		app.objProjector.observe('mouseout', app.starttoggle);
		app.objProjector.observe('mouseover', app.stoptoggle);
		
		//app.move(app.images[0]);						//soll ohne Zoom auskommen laut Krohn 12.08.2010
		app.autotoggle(app.images[0].currentIndex);		//soll ohne Zoom auskommen laut Krohn 12.08.2010
	},
	
	starttoggle: function () {
		app.isRunning = false;
		app.autotoggle(app.active_topic.currentIndex);
	},
	
	stoptoggle: function () {
		app.isRunning = true;
		window.clearTimeout(app.timer);
	},
	
	autotoggle: function (index) {
		if(!app.isRunning) {
			index++;
			if(index+1 > app.topics.length) {
				index=0;
			}
			app.timer = window.setTimeout(function(){
				app.toggle(index);
				},9000);	// mit Zoom 2000
		}
	},
	
	toggle: function(index) {
		if(index>=0) {
			app.active_topic = app.topics[index];
		} else {
			app.active_topic = this;
		}
		
		if(!app.active_topic.hasClassName('active') ) {
			app.topics.each(function(topic) {
				topic.removeClassName('active');
				//topic.setOpacity(0.7);
			});
			app.active_topic.addClassName('active');
			app.active_topic.setOpacity(1);
			
			var i = 0;
			app.images.each(function(image) {
				if(i != app.active_topic.currentIndex) {
					image.addClassName('notactive');
				}
				i++;
			});
			notactive = $$('#projector ul.images li img.notactive');
			
			Effect.Queues.get('projector').each(function(effect) { 
				effect.cancel();
			});
			
			new Effect.Parallel([
				Effect.multiple(notactive, Effect.Fade, { queue:{scope: 'projector'} }),
				Effect.Appear(app.images[app.active_topic.currentIndex],{
					duration:1,
					queue:{scope: 'projector'}, 
					afterFinish:(function () {
						//app.move(app.images[app.active_topic.currentIndex]);						//soll ohne Zoom auskommen laut Krohn 12.08.2010
						app.autotoggle(app.images[app.active_topic.currentIndex].currentIndex);		//soll ohne Zoom auskommen laut Krohn 12.08.2010
						}) 
					})
				], {
					sync: true,
					queue:{scope: 'projector'}
				});
			
			notactive.each(function(image) {
				image.removeClassName('notactive');
				app.init(image);
			});
			
			links = $$('#projector_stage div.teaser a');
			links.each(function(link) {
				link.writeAttribute('href', app.active_topic.getElementsByTagName('a')[0].getAttribute('href'));
				link.writeAttribute('title', app.active_topic.getElementsByTagName('a')[0].getAttribute('title'));
				
				if(link.up().nodeName=='H1') {
					link.innerHTML = app.active_topic.getElementsByTagName('a')[0].getAttribute('title');
				} else if(link.up().nodeName=='P' && !link.hasClassName('more_new')) {
					link.innerHTML = app.active_topic.getElementsByTagName('p')[0].innerHTML;
				}
			});
			
			links_related = $$('#projector_stage div.teaser ul.more');
			links_related.each(function(related) {
				related.innerHTML = app.active_topic.getElementsByTagName('ul')[0].innerHTML;
			});
		}
	},
	
	init: function(image) {
		Element.setStyle(image, {height:'100%',width:'100%',left:'0',top:'0'});
	},
	
	move: function(image) {
		var config = new Array(4);
		config[1]=new Array(2);
		config[1]['w'] = 'left';
		config[1]['h'] = 'top';
		config[2]=new Array(2);
		config[2]['w'] = 'right';
		config[2]['h'] = 'top';
		//ruckelt etwas mit bottom
		/*config[3]=new Array(2);
		config[3]['w'] = 'left';
		config[3]['h'] = 'bottom';
		config[4]=new Array(2);
		config[4]['w'] = 'right';
		config[4]['h'] = 'bottom';*/
		
		style = Math.max(Math.round(Math.random()*2),1);
		
		if(config[style]['h']=='top') image.style.removeProperty('bottom');	//IE mag nur removeAttribute
		if(config[style]['h']=='bottom') image.style.removeProperty('top');
		if(config[style]['w']=='left') image.style.removeProperty('right');
		if(config[style]['w']=='right') image.style.removeProperty('left');
		
		var steps = 400;
		var speed = 20;
		c=0;
		
		if(app.loop) {
			window.clearInterval(app.loop);
			app.loop=null;
		}
		app.loop = window.setInterval(function(){
			app.moveObject(image, config[style]['w'], config[style]['h'], steps);
			}, speed);
	},
	
	moveObject: function (object, w, h, steps) {
		c++;
		
		if(w=='right') {
			object.style.right = 0;
		} else {
			object.style.left = 0;
		}
		
		if(h=='bottom') {
			object.style.bottom = 0;
		} else {
			object.style.top = 0;
		}
		
		object.style.width = (100+(c*(30/steps)))+'%';
		object.style.height = (100+(c*(30/steps)))+'%';
		
		if(steps == c){ // || Math.abs(parseInt(object.style.left))>Math.abs(xStart+x) || Math.abs(parseInt(object.style.top))>Math.abs(yStart+y) ) {
			window.clearInterval(app.loop);
			app.loop=null;
			app.autotoggle(object.currentIndex);
		}
	}
});

