function MorphObject(el,rounds, startvalue, endvalue, callback) {
	this.element=el;
	this.rounds=rounds;
	this.start_value=startvalue;
	this.end_value=endvalue;
	this.callback=callback;
	this.counter=0;
	this.cur_value = startvalue;
}
MorphObject.prototype.reverse=function(cb) {
	var oldC=this.counter;
	this.counter = this.rounds-this.counter;
	var sv=this.start_value;
	this.start_value=this.end_value;
	this.end_value = sv;
	this.callback=cb;
	//console.log("rounds: ",this.rounds," oldC: ",oldC, " newC: ", this.counter," start: ",this.start_value, " end: ",this.end_value);
}

var MyMorph=Class.create();

MyMorph.prototype = {
	duration: 300,
	PI12: Math.PI/2,
	initialize: function() {
		this.stack=new Array();
		this.pe=new PeriodicalExecuter(this.execute.bind(this), 1/25);
	},
	addMorph: function(el, start_value, end_value, callback) {
		var m_obj = new MorphObject(el, Math.round(this.duration/(1000/25)), start_value, end_value, callback );
		this.stack.push( m_obj );
		return m_obj;
	},
	execute: function(pe) {
		if (this.stack.length>0) {
			var pi12 = Math.PI/2;
			var finish_stack=[];
			this.stack.each(function(obj, i) {
				var last_run = false;
				obj.counter = (obj.counter >= obj.rounds) ? obj.rounds : obj.counter+1;
				// Sinuidale Bewegung
				var value = obj.start_value+ (Math.sin(pi12*( obj.counter/obj.rounds )) * (obj.end_value - obj.start_value));
				// Lineare Bewegung
				//var value=obj.start_value + (obj.counter/obj.rounds)*(obj.end_value-obj.start_value);
				if (obj.counter == obj.rounds) {
					value=obj.end_value;
					finish_stack.push(i);
					last_run=true;
				}
				obj.cur_value = value;
				//obj.element.setStyle({paddingTop: Math.round(value)+"px"});
				obj.callback.call(obj.element, value, last_run);
			});
			if (finish_stack.length>0) {
				for(var i=finish_stack.length-1; i>=0; i--) {
					this.stack.splice(i,1);
				}
			}
		}
	}
}

Event.observe(window, "load", function() {
	var spaltElement = $$(".b_1sp")[0];
	// CSS-Hacks, sonst vergrößert sich die weise Fläche im Hintergrund
	Position.absolutize( spaltElement );
	spaltElement.next(".clear").setStyle({height: "66px"});
	
	$$(".b_1sp>div").each(function(el) {
		try {
			var requiredHeight = el.down().scrollHeight+3; // -12
			var frameHeight = el.getHeight();
			var elPic =el.down(".home_pic_container img");
			var elPic_Container=el.down(".home_pic_container");
			var pic_height= elPic.naturalHeight | elPic.height | 126;
			var pic_width= elPic.naturalWidth | elPic.width | 207;
			var pic_margin= 4; // 6
			requiredHeight+=pic_height;
		}
		catch (e) {
			return;
		}
		var morpher = new MyMorph();
		var state="down";
		var picMorph, frameMorph;
		
		var frameUp_cb=function(new_val, last_run) {
			el.setStyle({ top: (-new_val-pic_margin)+"px", height: (frameHeight+new_val+pic_margin)+"px"});
			el.down(".home_pic_container").setStyle({ display: "block" });
			el.down(".container").setStyle({ height: (frameHeight+new_val)+"px" });
			if (last_run) {
				state="up";
				el.addClassName("container_up");
			}
		};
		var frameDown_cb=function(new_val, last_run) {
			el.setStyle({ top: (-new_val)+"px", height: (frameHeight+new_val)+"px"});
			el.down(".container").setStyle({ height: (frameHeight+new_val)+"px" });
			if (last_run) {
				state="down";
			}
		}
		var picUp_cb=function(new_val, last_run) {
			elPic_Container.setStyle({height: new_val+pic_margin+"px"});
			elPic.width=pic_width;
		};
		var picDown_cb=function(new_val, last_run) {
			elPic_Container.setStyle({height: new_val+"px"});
			elPic.width=pic_width;
		};
		///////////////////////////////////////////
		/// EVent-Handler für Container ausfahren
		el.observe("mouseover", function(ev) {
			if (state!="down") {
				/*if (state=="goingDown") {
					frameMorph.reverse(frameUp_cb);
					picMorph.reverse(picUp_cb)
					state="goingUp";
				}*/
				return;
			}
			
			//alert(frameHeight);
			if (requiredHeight > frameHeight) {
				state="goingUp";
				el.__mTargetValue = requiredHeight-frameHeight;
				frameMorph = morpher.addMorph(el, 0, requiredHeight-frameHeight, frameUp_cb);
				// Pic-Morpher
				picMorph = morpher.addMorph(el, 0, pic_height, picUp_cb);
			}
		});
		///////////////////////////////////////////////
		// EVent-Handler für Container zurückfahren
		el.observe("mouseout", function(ev){
			if (Position.within(el, Event.pointerX(ev), Event.pointerY(ev) ))
				return;
			if (state != "up") {
				if (state=="goingUp") {
					frameMorph.reverse(frameDown_cb);
					picMorph.reverse(picDown_cb);
					state="goingDown";
				}
				return;
			}
			el.removeClassName("container_up");
			state="goingDown";
			
			frameMorph=morpher.addMorph(el, el.__mTargetValue, 0, frameDown_cb);
			// Pic-Morpher
			picMorph=morpher.addMorph(el, pic_height, 0, picDown_cb);
		});
	});
});

///////////////////////////
///// Homepagefilme //////

flash_movies=[];
playing=false;

Event.observe(window, "load", function() {
	if (GetSwfVer()<9) {
		return;
	}
	$$(".a_navi td a").each(function(el, index){
		el.observe("mouseover", function(ev){
			if (playing == el) return;
			else if (playing != false) {
				playing.up("td").removeClassName("sel");
			}
			el.up("td").addClassName("sel");
			
			if (flash_movies[index+1] == undefined) return;
			
			try {
				var swf= Prototype.Browser.IE ? document.all.swf_manager : document.swf_manager
				swf.loadNextMovie(flash_movies[index+1]);
			}
			catch (e) {
				//$("flash_space").update('<img src="img/flash_default.jpg" alt="Flash-Ersatz" />');
			}
			playing=el;
		});
	});
});		
