var ZeitschriftenCarousel = function(){
	this.interval = false;
	this.autoTimer = 3000;
	this.maxDuration = 0.5;
	this.minDuration = 0.1;
	this.transition = Effect.Transitions.sinoidal;
	this.duration = this.maxDuration;
	this.active = false;
	this.info = false;
	this.top = false;
	this.step = 0;
	this.direction = 'right';
	this.maxSteps = 30;
	this.objects = [];
	this.moveToObject = false;
	this.positions = [
		{
			top: 100,
			left: 95,
			width: Math.floor(122 * (1 - (0.1 * 7))),
			height: Math.floor(200 * (1 - (0.1 * 7))),
			zIndex: 97
		},{
			top: 70,
			left: 115,
			width: Math.floor(122 * (1 - (0.1 * 4))),
			height: Math.floor(200 * (1 - (0.1 * 4))),
			zIndex: 98
		},{
			top: 50,
			left: 165,
			width: Math.floor(122 * (1 - (0.1 * 1.5))),
			height: Math.floor(200 * (1 - (0.1 * 1.5))),
			zIndex: 99
		},{
			top: 30,
			left: 255,
			width: Math.floor(122 * (1 - (0.1 * 0))),
			height: Math.floor(200 * (1 - (0.1 * 0))),
			zIndex: 100
		}
	];
	this.positions[4] = {
		top: 50,
		left: Math.floor( this.positions[3].left + this.positions[3].width - Math.floor(122 * (1 - (0.1 * 1.5))) + 90 ),
		width: Math.floor(122 * (1 - (0.1 * 1.5))),
		height: Math.floor(200 * (1 - (0.1 * 1.5))),
		zIndex: 99
	};
	this.positions[5] = {
		top: 70,
		left: Math.floor( this.positions[4].left + this.positions[4].width - Math.floor(122 * (1 - (0.1 * 4))) + 50 ),
		width: Math.floor(122 * (1 - (0.1 * 4))),
		height: Math.floor(200 * (1 - (0.1 * 4))),
		zIndex: 98
	};
	this.positions[6] = {
		top: 100,
		left: Math.floor( this.positions[5].left + this.positions[5].width - Math.floor(122 * (1 - (0.1 * 7))) + 20 ),
		width: Math.floor(122 * (1 - (0.1 * 7))),
		height: Math.floor(200 * (1 - (0.1 * 7))),
		zIndex: 97
	};
}

ZeitschriftenCarousel.prototype = {
	init: function(el){
		this.el = $(el);
		
		this.el.observe("mouseover", this.stop.bind(this));
		this.el.observe("mouseout", this.start.bind(this));
		
		this.load();
		
		this.interval = setInterval(this.auto.bind(this), this.autoTimer);
	},
	
	load: function(){
		var images = $$('img.carousel');
		
		images.each(function(image, index){
			this.objects.push({
				el: image,
				info: 'info'+(index+1),
				pos: index,
				top: (index == 3) ? true : false
			});
			
			if(this.positions[index]){
				image.setStyle({
					top: this.positions[index].top+"px",
					left: this.positions[index].left+"px",
					width: this.positions[index].width+"px",
					height: this.positions[index].height+"px",
					zIndex: this.positions[index].zIndex,
					display: 'block'
				});
			}else{
				image.setStyle({
					top: "0px",
					left: "0px",
					width: "0px",
					height: "0px",
					zIndex: 97
				});
			}
			
			image.observe("mouseover", this.showInfo.bind(this.objects[index]));
			//image.observe("click", this.moveTo.bind(this));
		}.bind(this));
		
		$('loading').setStyle({
			display: 'none'
		});
	},
	
	switchLeft: function(){
		if(this.active == true) return;
		
		this.duration = this.maxDuration;
		this.transition = Effect.Transitions.sinoidal;
		this.direction = "right";
		
		this.animate();
	},
	
	switchRight: function(){
		if(this.active == true) return;
		
		this.duration = this.maxDuration;
		this.transition = Effect.Transitions.sinoidal;
		this.direction = "left";
		
		this.animate();
	},
	
	closeInfo: function(event){
		
	},
	
	showInfo: function(){
		if(parseInt(this.el.getStyle('zIndex')) != 100) return;
		
		var info = $(this.info);
		
		info.setStyle({
			display: 'block'
		});
		
		info.observe("mouseout", function(event){
			var offset = this.cumulativeOffset();
			var dimensions = this.getDimensions();
			var pointerX = event.pointerX();
			var pointerY = event.pointerY();
			
			if(pointerX <= offset.left || pointerX >= offset.left + dimensions.width || pointerY <= offset.top || pointerY >= offset.top + dimensions.height){
				this.setStyle({
					display: 'none'
				});
			}
		});
	},
	
	auto: function(){
		this.duration = this.maxDuration;
		this.transition = Effect.Transitions.sinoidal;
		this.active = true;
		this.step++;
		this.direction = "left";
		this.animate();
	},
	
	animate: function(){
		var top, left, height, width, zIndex, reset = true;
		this.active = true;
		
		if(this.step > this.maxSteps) this.step = 1;
		if(this.step < 0) this.step = this.maxSteps - 1;
		
		this.objects.each(function(object, index){
			switch(this.direction){
				case 'right':
					if(object.pos == 6) object.el.setStyle({ display: 'none' });
					
					else if(object.pos < 6){
						try{
							new Effect.Move(object.el, {
								x: this.positions[object.pos+1].left, 
								y: this.positions[object.pos+1].top, 
								duration: this.duration,
								mode: 'absolute',
								transition: this.transition
							});
							new Effect.Morph(object.el, {
								style: 'width: '+this.positions[object.pos+1].width+'px; height: '+this.positions[object.pos+1].height+'px;',
								duration: this.duration,
								transition: this.transition,
								afterUpdate: function(effect){
									if(effect.currentFrame < this.duration * 100 / 2) return;
									
									try{
										object.el.setStyle({
											zIndex: this.positions[object.pos].zIndex
										});
									}catch(e){}
								}.bind(this),
								afterFinish: function(){
									this.active = false;
								}.bind(this)
							});
						}catch(e){}
					}
					
					if(object.pos == this.objects.length - 1){
						object.pos = 0;
						
						new Effect.Move(object.el, {
							x: this.positions[0].left,
							y: this.positions[0].top,
							mode: 'absolute',
							transition: this.transition,
							duration: this.duration / 2,
							afterFinish: function(){
								object.el.setStyle({
									width: this.positions[0].width+"px",
									height: this.positions[0].height+"px",
									zIndex: this.positions[0].zIndex,
									display: 'block'
								});
							}.bind(this)
						});
					}else{
						object.pos++;
					}
				break;
				case 'left':
					if(object.pos == 0) object.el.setStyle({ display: 'none' });
					
					else if(object.pos < 7){
						try{
							new Effect.Move(object.el, {
								x: this.positions[object.pos-1].left, 
								y: this.positions[object.pos-1].top, 
								duration: this.duration,
								mode: 'absolute',
								transition: this.transition
							});
							new Effect.Morph(object.el, {
								style: 'width: '+this.positions[object.pos-1].width+'px; height: '+this.positions[object.pos-1].height+'px;',
								duration: this.duration,
								transition: this.transition,
								afterUpdate: function(effect){
									if(effect.currentFrame < this.duration * 100 / 2) return;
									
									try{
										object.el.setStyle({
											zIndex: this.positions[object.pos].zIndex
										});
									}catch(e){}
								}.bind(this),
								afterFinish: function(){
									this.active = false;
								}.bind(this)
							});
						}catch(e){}
					}
					
					if(object.pos == 0){
						object.pos = this.objects.length - 1;
					}else{
						object.pos--;
					}
					
					if(object.pos == this.positions.length - 1){
						new Effect.Move(object.el, {
							x: this.positions[6].left,
							y: this.positions[6].top,
							mode: 'absolute',
							transition: this.transition,
							duration: this.duration / 2,
							afterFinish: function(){
								object.el.setStyle({
									width: this.positions[6].width+"px",
									height: this.positions[6].height+"px",
									zIndex: this.positions[6].zIndex,
									display: 'block'
								});
							}.bind(this)
						});
					}
				break;
			}
		}.bind(this));
	},
	
	stop: function(){
		if(this.moveToObject !== false) return;
		
		clearInterval(this.interval);
		this.active = false;
	},
	
	start: function(event){
		if(this.moveToObject !== false) return;
		
		var offset = this.el.cumulativeOffset();
		var dimensions = this.el.getDimensions();
		var pointerX = event.pointerX();
		var pointerY = event.pointerY();
		
		if(pointerX <= offset.left || pointerX >= offset.left + dimensions.width || pointerY <= offset.top || pointerY >= offset.top + dimensions.height){
			this.interval = setInterval(this.auto.bind(this), this.autoTimer);
		}
	},
	
	moveTo: function(event){
		var el = event.findElement();
		var curObject = false;
		
		this.objects.each(function(object){
			if(object.el == el){
				curObject = object;
			}
		}.bind(this));
		
		if(curObject.pos == 3) return;
		
		this.moveToObject = curObject;
		this.transition = Effect.Transitions.linear;
		this.duration = this.minDuration;
		
		switch(curObject.pos){
			case 0:
			case 1:
			case 2:
				this.direction = "right";
				this.interval = setInterval(this.animateTo.bind(this), 10);
			break;
			default:
				this.direction = "left";
				this.interval = setInterval(this.animateTo.bind(this), 10);
			break;
		}
	},
	
	animateTo: function(){
		if(this.active) return;
		
		if(this.moveToObject.pos == 3){
			clearInterval(this.interval);
		}else{
			this.animate();
		}
	}
}
