
/* Configuration variables */
var empty_cover_image = new Image();
empty_cover_image.src = 'http://www.gurulib.com/_scripts/image_flow/reflect/empty_cover.jpg';
var temp_cover_image = new Image(); 
temp_cover_image.src = 'http://www.gurulib.com/_scripts/image_flow/reflect/cover_downloading.jpg';
var conf_reflection_p;         // Sets the height of the reflection in % of the source image 
var conf_focus;                  // Sets the numbers of images on each side of the focussed one
var conf_slider_width;          // Sets the px width of the slider div
var conf_images_cursor;  // Sets the cursor type for all images default is 'default'
var conf_slider_cursor;  // Sets the slider cursor type: try "e-resize" default is 'default'

/* Id names used in the HTML */
var conf_imageflow;    // Default is 'imageflow'
var conf_images;          // Default is 'images'
var conf_captions;      // Default is 'captions'
var conf_scrollbar;    // Default is 'scrollbar'
var conf_slider;          // Default is 'slider'

/* Define global variables */
var caption_id;
var new_caption_id;
var current;
var target;
var mem_target;
var timer;
var unloaded_images = new Array();
var loaded_images = new Array();
var new_slider_pos;
var dragging;
var dragobject;
var dragx;
var posx;
var new_posx;
var xstep;
var current_top_image = null;

var step_delay = 1;
var max_conf_focus;
var size;
var scrollbar_width;
var conf_slider_width;
var images_left;
var images_top;
var images_width;
var max_height;

function init_cover_flow_variables()
{
var i;
conf_reflection_p = 0.3;         // Sets the height of the reflection in % of the source image (0.5)
conf_focus = 6;                  // Sets the numbers of images on each side of the focussed one
conf_slider_width = 14;          // Sets the px width of the slider div
conf_images_cursor = 'pointer';  // Sets the cursor type for all images default is 'default'
conf_slider_cursor = 'default';  // Sets the slider cursor type: try "e-resize" default is 'default'


/* Define global variables */
caption_id = 0;
new_caption_id = 0;
current = 0;
target = 0;
mem_target = 0;
timer = 0;
for(i = 0; i < loaded_images.length; ++i)
{
if(loaded_images[i] != null)
	{
	img_div.removeChild(loaded_images[i]);
	loaded_images[i].onload = null;
	loaded_images[i] = null;
	}
}

for(i = 0; i < unloaded_images.length; ++i)
{
if(unloaded_images[i] != null)
	{
	unloaded_images[i].onload = null;
	unloaded_images[i] = null;
	}
}
unloaded_images = new Array();
loaded_images = new Array();
new_slider_pos = 0;
dragging = false;
dragobject = null;
dragx = 0;
posx = 0;
new_posx = 0;
xstep = 150;

max_conf_focus = 0;
size = 0;
scrollbar_width  = 0;
conf_slider_width  = 0;
images_left  = 0;
images_top  = 0;
images_width  = 0;
max_height  = 0;

}

function step()
{
	switch (target < current-1 || target > current+1) 
	{
		case true:
			moveTo(current + (target-current)/3);
			window.setTimeout(step, step_delay);
			timer = 1;
			break;

		default:
			timer = 0;
			if(current_top_image != null)
				{
				current_top_image.onclick = function() { document.location = this.url; }
				}
			break;
	}
}

function glideTo(x, new_caption_id)
	{	
	var start_timer = false;
	/* Animate gliding to new x position */
	//alert(x + "," + new_caption_id);
	target = x;
	mem_target = x;
	if (timer == 0)
	{
		start_timer = true;
		timer = 1;
	}
	
	/* Display new caption */
	caption_id = new_caption_id;
	if(loaded_images[caption_id] != null)
		{
		var caption = loaded_images[caption_id].getAttribute('alt');
		if (caption == '') caption = '&nbsp;';
		if(caption_div != null)
			caption_div.innerHTML = caption;
		//alert(caption);
		update_cover_flow_status(loaded_images[caption_id].id);
		}
	if(start_timer)
		{
		window.setTimeout(step, step_delay);
		}
	}

function moveTo(x)
{
	current = x;
	var zIndex = loaded_images.length;
	for (var index = 0; index < loaded_images.length; index++)
	{
		//alert(index);
		var image = loaded_images[index];
		if(image == null)
			{
			continue;
			}
		//if((image.i) == false)
		//	continue;
		var current_image = index * -xstep;

		/* Don't display images that are not conf_focussed */
		if ((current_image+max_conf_focus) < mem_target || (current_image-max_conf_focus) > mem_target)
		{
			image.style.visibility = 'hidden';
			image.style.display = 'none';
		}
		else 
		{
			var z = Math.sqrt(20000 + x * x) + 120;
			var xs = x / z * size + size;

			/* Still hide images until they are processed, but set display style to block */
			image.style.display = 'block';
		
			/* Process new image height and image width */
			var new_img_h = (image.h / image.w * image.pc) / z * size;
			switch ( new_img_h > max_height )
			{
				case false:
					var new_img_w = image.pc / z * size;
					break;

				default:
					new_img_h = max_height;
					var new_img_w = image.w * new_img_h / image.h;
					break;
			}
			var new_img_top = (images_width * 0.30 - new_img_h) + images_top + ((new_img_h / (conf_reflection_p + 1)) * conf_reflection_p);
			//new_img_top = Math.max(new_img_top, 0);
			//var new_img_top = images_top + ((new_img_h / (conf_reflection_p + 1)) * conf_reflection_p);
			//var new_img_top = (max_image_height - new_img_h) + images_top + ((new_img_h / (conf_reflection_p + 1)) * conf_reflection_p);
			/* Set new image properties */
			image.style.left = xs - (image.pc / 2) / z * size + images_left + 'px';
			image.style.height = new_img_h + 'px';
			image.style.width = new_img_w + 'px';
			image.style.top = new_img_top + 'px';
			image.style.visibility = 'visible';

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

				default:
					zIndex = zIndex - 1;
					break;
			}
			
			/* Change zIndex and onclick function of the focussed image */
			switch ( image.i == caption_id )
			{
				case false:
					image.onmousedown = function() { start_glide(this.x_pos, this.i); }
					image.onmousemove = function() { glide_cover_flow_item(); }
					image.onmouseup = function() { start_new_cover_flow_glide(); }
					image.onclick = '';
					break;

				default:
					//alert(image.i);
					image.onmousedown = '';
					image.onmousemove = ""
					image.onmouseup = "";
					zIndex = zIndex + 1;
					current_top_image = image;
					//image.onclick = function() { alert(this.timer); if(this.timer == 0) {document.location = this.url;} }
					break;
			}
			image.style.zIndex = zIndex;
		}
		x += xstep;
	}
}

/* Main function */
function refresh_cover_flow()
{
	/* Cache global variables, that only change on refresh */
	images_width = parseInt(imageflow_div.style.width);
	images_top = 0;//parseInt(imageflow_div.style.top);
	
	images_left = 0;//parseInt(imageflow_div.style.left);
	max_conf_focus = conf_focus * xstep;
	size = parseInt(images_width * 0.5);
	scrollbar_width = parseInt(images_width * 0.6);
	conf_slider_width = parseInt(conf_slider_width * 0.5);
	max_height = parseInt(img_div.style.height);
	//alert(images_width + "," + images_top + "," + images_left + "," + max_height);

	/* Change imageflow div properties */
	//imageflow_div.style.height = max_height + 'px';

	//var high = parseInt(images_width * 0.338);
	/* Change images div properties */
	//img_div.style.height = high + 'px';
	//if(images_top + high > max_height)
	//	{
	//	images_top = max_height - high;
	//	}
		
	/* Change captions div properties */
	if(caption_div != null)
		caption_div.style.width = images_width + 'px';
	//caption_div.style.top = (images_top - 100)+'px';//'140px' + document.body.scrollTop;//(20 + images_width * 0.03) + 'px';
	//caption_div.style.left = '100px';
}

function get_max_cover_flow_height()
{
return parseInt(images_width*0.50) + 30;
}

function create_cover_image(id, title_str, path)
{
//alert(path);
var i = unloaded_images.length;
var img = null;
if(path.length != 0)
	{
	var img_id = 'cover_image_'+id;
	img = document.createElement('img');
	img.setAttribute('alt', img_id);
	//img.setAttribute('id',img_id);
	img.onload = image_loaded;
	img.onerror = image_failed;
	unloaded_images[i] = img;
	load_cover_image(temp_cover_image, i, id, title_str);
	img.setAttribute('src',path);
	}
else
	{
	//alert(path);
	unloaded_images[i] = img;
	load_cover_image(empty_cover_image, i, id, title_str);
	}	
if(i == 0)
	{
	if(caption_div != null)
		caption_div.innerHTML = title_str;
	}
}

function image_failed()
{
alert("failed="+this.src);
var id = this.getAttribute('alt');
set_image(id, empty_cover_image);
}

function image_loaded()
{
var id = this.getAttribute('alt');
set_image(id, this);
}

function set_image(id, img)
{
var obj = document.getElementById(id);
if(obj == null)
	{
	return;
	}			
obj.src = img.src;

/* Add width and height as attributes ONLY once onload */
obj.w = img.width;
obj.h = img.height;

/* Check source image format. Get image height minus reflection height! */
switch ((obj.w + 1) > (obj.h / (conf_reflection_p + 1))) 
	{
	/* Landscape format */
	case true:
		obj.pc = 118;
		break;

	/* Portrait and square format */
	default:
		obj.pc = 100;
		break;
	}

//moveTo(current);
var remove_index = obj.i;
//alert(remove_index);	
unloaded_images[remove_index] = null;
moveTo(current);
}

function load_cover_image(img_obj, index, id, title_str)
{
//alert(image.src);
var obj = document.createElement('img');
obj.style.display = 'none';
obj.id = 'cover_image_'+id;
obj.i = index;
/* Set image onclick by adding i and x_pos as attributes! */
obj.onmousedown = function() { start_glide(this.x_pos, this.i); }
obj.onmousemove = function() { glide_cover_flow_item(); }
obj.onmouseup = function() { start_new_cover_flow_glide(); }

obj.x_pos = (-index * xstep);

/* Add width and height as attributes ONLY once onload */
obj.w = img_obj.width;
obj.h = img_obj.height;

/* Check source image format. Get image height minus reflection height! */
switch ((obj.w + 1) > (obj.h / (conf_reflection_p + 1))) 
	{
	/* Landscape format */
	case true:
		obj.pc = 118;
		break;

	/* Portrait and square format */
	default:
		obj.pc = 100;
		break;
	}

/* Set ondblclick event */
//var link = id.substr(12);
//alert(link);
obj.url = "http://www.gurulib.com/title/"+id;
//obj.ondblclick = function() { document.location = this.url; }
/* Set image cursor type */

obj.alt = title_str;
//alert(image.alt);
obj.style.display = 'none';
obj.style.cursor = conf_images_cursor;	
obj.style.visibility = 'hidden';
obj.style.display = 'none';
obj.style.position = 'absolute';
obj.style.top = '0px';
obj.style.border='none';
obj.src = img_obj.src;	
obj.style.display = '';
loaded_images[index] = obj;
img_div.appendChild(obj);
obj.style.display = '';
//moveTo(current);
}

/* Show/hide element functions */
function show(id)
{
	var element = document.getElementById(id);
	element.style.visibility = 'visible';
}
function hide(id)
{
	var element = document.getElementById(id);
	element.style.visibility = 'hidden';
	element.style.display = 'none';
}


function init_cover_flow()
{
/* Id names used in the HTML */
conf_imageflow = 'cover_flow_area';    // Default is 'imageflow'
conf_images = 'images';          // Default is 'images'
conf_captions = 'captions';      // Default is 'captions'
conf_scrollbar = 'scrollbar';    // Default is 'scrollbar'
conf_slider = 'slider';          // Default is 'slider'

/* Cache document objects in global variables */
imageflow_div = document.getElementById(conf_imageflow);
img_div = document.getElementById(conf_images);
//scrollbar_div = document.getElementById(conf_scrollbar);
//slider_div = document.getElementById(conf_slider);
caption_div = document.getElementById(conf_captions);

}

/* Refresh ImageFlow on window resize */
/*window.onresize = function()
{
	if(document.getElementById(conf_imageflow)) refresh();
}*/

/* Handle the wheel angle change (delta) of the mouse wheel */
function handle(delta)
{
	var change = false;
	switch (delta > 0)
	{
		case true:
			if(caption_id >= 1)
			{
				target = target + xstep;
				new_caption_id = caption_id - 1;
				change = true;
			}
			break;

		default:
			if(caption_id < (loaded_images.length-1))
			{
				target = target - xstep;
				new_caption_id = caption_id + 1;
				change = true;
			}
			break;
	}

	/* Glide to next (mouse wheel down) / previous (mouse wheel up) image */
	if (change == true)
	{
		glideTo(target, new_caption_id);
	}
}

/* Event handler for mouse wheel event */
function wheel(event)
{
	var delta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta)
	{
		delta = event.wheelDelta / 120;
	}
	else if (event.detail)
	{
		delta = -event.detail / 3;
	}
	if (delta) handle(delta);
	if (event.preventDefault) event.preventDefault();
	event.returnValue = false;
}

/* Initialize mouse wheel event listener */
function enable_image_flow_controls()
{
	if(window.addEventListener) 
		imageflow_div.addEventListener('DOMMouseScroll', wheel, false);
	imageflow_div.onmousewheel = wheel;
	document.onkeydown = image_flow_key_down;
	
	//moveTo(current);
}

function disable_image_flow_controls()
{
	if(window.addEventListener) 
		imageflow_div.removeEventListener('DOMMouseScroll', wheel, false);
	imageflow_div.onmousewheel = '';

document.onkeydown = '';

}



function getKeyCode(event)
{
	event = event || window.event;
	return event.keyCode;
}

function image_flow_key_down(event)
{
	var charCode  = getKeyCode(event);
	switch (charCode)
	{
		/* Right arrow key */
		case 39:
			handle(-1);
			break;
		
		/* Left arrow key */
		case 37:
			handle(1);
			break;
	}
return true;
}
