/**
 *	ImageFlow 0.4
 *
 *	This code is based on Michael L. Perrys Cover flow in Javascript.
 *	For he wrote that "You can take this code and use it as your own" [1]
 *	this is my attempt to improve some things. Feel free to use it! If
 *	you have any questions on it leave a message in my shoutbox [2].
 *
 *	The reflection is generated server-sided by a slightly hacked  
 *	version of Richard Daveys easyreflections [3] written in PHP.
 *	
 *	The mouse wheel support is an implementation of Adomas Paltanavicius
 *  JavaScript mouse wheel code [4].
 *
 *
 *	[1] http://www.adventuresinsoftware.com/blog/?p=104#comment-1981
 *	[2] http://shoutbox.finnrudolph.de/
 *	[3] http://reflection.corephp.co.uk/v2.php
 *	[4] http://adomas.org/javascript-mouse-wheel/
 */


function dostep(id){
	var tP = $(id);
	switch (tP.target < tP.current-1 || tP.target > tP.current+1) 
	{
		case true:
			tP.Flows.moveTo(tP.current + (tP.target-tP.current)/3, id);
			window.setTimeout("dostep('"+id+"')" , 50);
			tP.timer = 1;
			break;

		default:
			tP.timer = 0;
			//mySlide.slideIn();
			break;
	}
}

var temporaryTimers = [];

var Flows = new Class({
	init: function(options) {


		this.options = Object.extend({
			caption_id : "i3",
			container_id: "", /* where all the covers are*/
			new_caption_id : "",
			current : 0,
			target : 0,
			timer : 0,
			reflection_p: 0.5
		   }, options || {});
		this.array_images = [];
		this.reflection_p = this.options.reflection_p;
		this.container = $(this.container_id);
		this.refresh( coverContainerId );
	},

	topOffset: function (height){
		return (height*0.8) +0; // die höhen positionierung
	},
	
	glideTo: function (e, xx, id, master){
		// wheel
		if(typeof(xx)=="undefined"){ // mouseclick
			var obj = this;
			var new_caption_id = obj.getAttribute("cover") ;  
			var x  = obj.getAttribute("step") ; 
			var tP = this.master;
		}else{ // wheel
			var new_caption_id = id;  
			var x  = xx ; 
			var tP = master;
		}

		tP.new_caption_id = new_caption_id
		
		//mySlide.slideOut();
		AfillSlides(new_caption_id);
		/* Animate gliding to new x position */
		tP.target = x;
		if (tP.timer == 0){
			window.setTimeout("dostep('"+tP.id+"')", 50);
			tP.timer = 1;
		}
	},

	moveTo: function (x, id){
		var tP = $(id);
		tP.current = x;
		var zIndex = tP.max;
		/* Loop */

		for (var index = 0; index < tP.max; index++){ 
			var image = tP.childArr.childNodes.item(tP.array_images[index]);
			var z = Math.sqrt(10000 + x * x)+100;
			var xs = x / z * tP.size + tP.size;

			/* Get current image properties */
			var img_h = image.height;
			var img_w = image.width;

			/* Check source image format. Get image height minus reflection height! */
			switch ((img_w + 1) > (img_h / (tP.reflection_p + 1))) {
				/* Landscape format */
				case true:
					var img_percent = 118;
					break;

				/* Portrait and square format */
				default:
					var img_percent = 100;
					break;
			}

			/* Process new image height and top spacing */
			var new_img_h = (img_h / img_w * img_percent) / z * tP.size;
			var new_img_top = (tP.images_width * 0.33 - new_img_h) + tP.images_top + ((new_img_h / (tP.reflection_p + 1)) * tP.reflection_p);

			/* Set new image properties */
			image.style.left = xs - (img_percent / 2) / z * tP.size + tP.imageflow_left + "px";
			image.style.height = new_img_h + "px";
			image.style.width= "";
			image.style.top = tP.Flows.topOffset(new_img_top)-40 + "px";

			/* Set image layer through zIndex */
			switch ( x < 0) {
				case true:
					zIndex++;
					break;

				default:
					zIndex = zIndex -1;
					break;
			}
			image.style.zIndex = zIndex;

			x += 150;
		}
	},

	/* Main function */
	refresh: function (id){
		var tP = $(id);
		tP.current =0;
		tP.target =0;
		tP.timer =0;
		tP.new_caption_id = "0";
		tP.array_images = [];
		tP.reflection_p = 0.7;
		tP.childArr = $("images");

		/* Change images div properties */
		tP.images_width = tP.childArr.offsetWidth;
		var images_height = tP.images_width * 0.33;
		tP.style.height = images_height + "px";

		/* Cache global variables, that only change on refresh */
		tP.imageflow_left = $("imageflow").offsetLeft;
		tP.images_top = 0; //tP.offsetTop;
		tP.size = tP.images_width * 0.5;
		tP.max = tP.childArr.childNodes.length;
		/* Cache correct node type indices in an array */
		var count=0;
		for (var index = 0; index < tP.max; index++){ 
			var image = tP.childArr.childNodes.item(index);
			if (image.nodeType == 1)
			{
				tP.array_images[count] = index;
				image.master = tP;
				count++;
			}
		}
		tP.max = tP.array_images.length;

		/* Display images in current order */
		tP.Flows.moveTo(tP.current, id);
	}

});
//Flows.step


var reflection_p = 0.7;


/* Show/hide functions */
function Ashow(id) {
	var element = $(id);
	element.style.visibility = "visible";
}
function Ahide(id) {
	var element = $(id);
	element.style.visibility = "hidden";
}








