/* Configuration variables */
var imgf_conf_reflection_p = 0.9;			// Sets the height of the reflection in % of the source image 
var imgf_conf_focus = 3;					// Sets the numbers of images on each side of the focussed one
var imgf_conf_slider_width = 15;			// Sets the px width of the slider div
var imgf_conf_images_cursor = 'pointer';	// Sets the cursor type for all images default is 'default'
var imgf_conf_slider_cursor = 'default';	// Sets the slider cursor type: try "e-resize" default is 'default'

var imgf_image_width_base = 142;
var imgf_image_height_base = 171;

/* Id names used in the HTML */
var imgf_conf_imageflow_base = 'imageflow';		// Default is 'imageflow'
var imgf_conf_loading_base = 'imgf_loading';     // Default is 'loading'
var imgf_conf_images_base = 'imgf_images';       // Default is 'images'
var imgf_conf_captions_base = 'imgf_captions';   // Default is 'captions'
var imgf_conf_scrollbar_base = 'imgf_scrollbar'; // Default is 'scrollbar'
var imgf_conf_slider_base = 'imgf_slider';       // Default is 'slider'

var imgf_conf_imageflow;
var imgf_conf_loading;
var imgf_conf_images;
var imgf_conf_captions;
var imgf_conf_scrollbar;
var imgf_conf_slider;

var imgf_images_width_init = [0,0,0,0];
var imgf_images_top_init = [0,0,0,0];
var imgf_images_left_init = [0,0,0,0];
var imgf_max_conf_focus_init = [0,0,0,0];
var imgf_size_init = [0,0,0,0];
var imgf_scrollbar_width_init = [0,0,0,0];
var imgf_conf_slider_width_init = [0,0,0,0];
var imgf_max_height_init = [0,0,0,0];
var imgf_images_pdnleft = 0;
var imgf_images_pdntop = 0;

/* Define global variables */
var imgf_caption_id = 0;
var imgf_new_caption_id = 0;
var imgf_current = 0;
var imgf_target = 0;
var imgf_mem_target = 0;
var imgf_timer = 0;
var imgf_array_images = new Array();
var imgf_new_slider_pos = 0;
var imgf_dragging = false;
var imgf_dragobject = null;
var imgf_dragx = 0;
var imgf_posx = 0;
var imgf_new_posx = 0;
var imgf_xstep = 150;
var imgf_inc = [0,0,0,0];
var imgf_scrollbar_width = 0;
var imgf_recall = [0,0,0,0];


function imgf_step()
{
	switch (imgf_target < imgf_current-1 || imgf_target > imgf_current+1) 
	{
		case true:
			imgf_moveTo(imgf_current + (imgf_target-imgf_current)/3);
			window.setTimeout(imgf_step, 50);
			imgf_timer = 1;
			break;

		default:
			imgf_timer = 0;
			break;
	}
}

function imgf_glideTo(x, new_caption_id)
{	
	/* Animate gliding to new x position */
	imgf_target = x;
	imgf_mem_target = x;
	if (imgf_timer == 0)
	{
		window.setTimeout(imgf_step, 50);
		imgf_timer = 1;
	}
	
	/* Display new caption */
	imgf_caption_id = new_caption_id;
	imgf_caption = img_div.childNodes.item(imgf_array_images[imgf_caption_id]).getAttribute('alt');
	if (imgf_caption == '') imgf_caption = '&nbsp;';
	caption_div.innerHTML = imgf_caption;

	/* Set scrollbar slider to new position */
	if (imgf_dragging == false)
	{
		imgf_new_slider_pos = (imgf_scrollbar_width * (-(x*100/((imgf_max-1)*imgf_xstep))) / 100) - imgf_new_posx;
		slider_div.style.marginLeft = (imgf_new_slider_pos - imgf_conf_slider_width) + 'px';
	}
	document.getElementById('bkg_gallery').value = img_div.childNodes.item(imgf_array_images[imgf_caption_id]).getAttribute('longdesc');
}

function imgf_moveTo(x)
{
	imgf_current = x;
	var zIndex = imgf_max;
	
	s = "";
	
	/* Main loop */
	for (var index = 0; index < imgf_max; index++)
	{
		var image = img_div.childNodes.item(imgf_array_images[index]);
		var current_image = index * -imgf_xstep;

		/* Don't display images that are not conf_focussed */
		if ((current_image+imgf_max_conf_focus) < imgf_mem_target || (current_image-imgf_max_conf_focus) > imgf_mem_target)
		{
			image.style.visibility = 'hidden';
			image.style.display = 'none';
		}
		else 
		{
			var z = Math.sqrt(10000 + x * x) + 100;
			var xs = x / z * imgf_size + imgf_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 * imgf_size;
			
			switch ( new_img_h > imgf_max_height )
			{
				case false:
					var new_img_w = image.pc / z * imgf_size;
					break;

				default:
					new_img_h = imgf_max_height;
					var new_img_w = image.w * new_img_h / image.h;
					break;
			}
			
			var new_img_top = (imgf_images_width * 0.34 - new_img_h) + imgf_images_top + ((new_img_h / (imgf_conf_reflection_p + 1)) * imgf_conf_reflection_p) + 10 + imgf_images_pdntop;
			if (isIE()) new_img_top += 40;
			
			/* Set new image properties */
			image.style.left = 20 + imgf_images_pdnleft + xs - (image.pc / 2) / z * imgf_size + imgf_images_left + 'px';
			if(new_img_w && new_img_h)
			{ 
				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 == imgf_caption_id )
			{
				case false:
					image.onclick = function() { imgf_glideTo(this.x_pos, this.i); }
					break;

				default:
					zIndex = zIndex + 1;
					image.onclick = function() { }
					break;
			}
			image.style.zIndex = zIndex;
		}
		x += imgf_xstep;
	}
}

/* Main function */
function imgf_refresh(onload, num)
{
	imageflow_div = document.getElementById(imgf_conf_imageflow);
	img_div = document.getElementById(imgf_conf_images);
	scrollbar_div = document.getElementById(imgf_conf_scrollbar);
	slider_div = document.getElementById(imgf_conf_slider);
	caption_div = document.getElementById(imgf_conf_captions);
	
	/* Cache global variables, that only change on refresh */
	if (imgf_recall[num] == 0)
	{
		imgf_images_width_init[num] = img_div.offsetWidth;
		imgf_images_top_init[num] = imageflow_div.offsetTop;
		imgf_images_left_init[num] = imageflow_div.offsetLeft;
		imgf_max_conf_focus_init[num] = imgf_conf_focus * imgf_xstep;
		imgf_size_init[num] = imgf_images_width_init[num] * 0.5;
		imgf_scrollbar_width_init[num] = imgf_images_width_init[num] * 0.6;
		imgf_conf_slider_width_init[num] = imgf_conf_slider_width * 0.5;
		imgf_max_height_init[num] = imgf_images_width_init[num] * 0.51;
		
		imgf_recall[num] = 1;	// impedisce il ricarico di tutto!
	}
	
	imgf_images_width = imgf_images_width_init[num];
	imgf_images_top = imgf_images_top_init[num];
	imgf_images_left = imgf_images_left_init[num];
	imgf_max_conf_focus = imgf_max_conf_focus_init[num];
	imgf_size = imgf_size_init[num];
	imgf_scrollbar_width = imgf_scrollbar_width_init[num];
	imgf_conf_slider_width = imgf_conf_slider_width_init[num];
	imgf_max_height = imgf_max_height_init[num];
	
	// re-initialize
	imgf_max = 0;
	imgf_new_posx = 0;
	imgf_new_slider_pos = 0;
	slider_div.style.marginLeft = 0;
	slider_div.style.left = 0;
	imgf_caption_id = 0;
	imgf_new_caption_id = 0;
	imgf_array_images = new Array();
	imgf_target = 0;
	imgf_mem_target = 0;
	imgf_timer = 0;
	imgf_new_slider_pos = 0;
	imgf_dragging = false;
	imgf_dragobject = null;
	imgf_dragx = 0;
	imgf_posx = 0;
	imgf_new_posx = 0;
	imgf_xstep = 150;

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

	/* Change images div properties */
	img_div.style.height = imgf_images_width * 0.34 + 'px';

	/* Change captions div properties */
	caption_div.style.width = imgf_images_width + 'px';

	/* Change scrollbar div properties */
	scrollbar_div.style.marginTop = imgf_images_width * 0.02 + 'px';
	scrollbar_div.style.marginLeft = imgf_images_width * 0.2 + 'px';
	scrollbar_div.style.width = imgf_scrollbar_width + 'px';
	
	/* Set slider attributes */
	slider_div.onmousedown = function () { imgf_dragstart(this); };
	slider_div.style.cursor = imgf_conf_slider_cursor;

	/* Cache EVERYTHING! */
	imgf_max = img_div.childNodes.length;
	var i = 0;
	for (var index = 0; index < imgf_max; index++)
	{ 
		var image = img_div.childNodes.item(index);
		if (image.nodeType == 1)
		{
			imgf_array_images[i] = index;
			
			// Set image onclick by adding i and x_pos as attributes!
			image.onclick = function() { imgf_glideTo(this.x_pos, this.i); }
			image.x_pos = (-i * imgf_xstep);
			image.i = i;
			
			// Add width and height as attributes ONLY once onload
			if (onload == true)
			{
				if (isIE())
				{
					image.w = image.width;
					image.h = image.height;
				}
				else
				{
					image.w = imgf_image_width_base;
					image.h = imgf_image_height_base;
				}
			}

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

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

			// Set ondblclick event
			imgf_new_label_id = image.getAttribute('longdesc');
			image.ondblclick = function() { document.getElementById('bkg_gallery').value = imgf_new_label_id; }

			// Set image cursor type
			image.style.cursor = imgf_conf_images_cursor;
			i++;
		}
	}
	imgf_max = imgf_array_images.length;

	/* Display images in current order */
	imgf_moveTo(imgf_current);
	var image = img_div.childNodes.item(imgf_array_images[imgf_current]);
	
	imgf_glideTo(image.x_pos, image.i);
}

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

/* Hide loading bar, show content and initialize mouse event listening after loading */
function imgf_check_image_flow(num, tot_images, current)
{
	imgf_conf_imageflow = imgf_conf_imageflow_base + '_' + num;
	imgf_conf_loading = imgf_conf_loading_base + '_' + num;
	imgf_conf_images = imgf_conf_images_base + '_' + num;
	imgf_conf_captions = imgf_conf_captions_base + '_' + num;
	imgf_conf_scrollbar = imgf_conf_scrollbar_base + '_' + num;
	imgf_conf_slider = imgf_conf_slider_base + '_' + num;
	
	if(document.getElementById(imgf_conf_imageflow) && (tot_images <= imgf_inc[num]))
	{
		imgf_hide(imgf_conf_loading);
		imgf_current = current;
		imgf_refresh(true, num);
		imgf_show(imgf_conf_images);
		imgf_show(imgf_conf_scrollbar);
		imgf_initMouseWheel();
		//imgf_initMouseDrag();
	}
	else
		setTimeout('imgf_check_image_flow('+num+', '+tot_images+', '+current+')', 300);
}

/* Fixes the back button issue
window.onunload = function()
{
  document = null;
}*/


/* Handle the wheel angle change (delta) of the mouse wheel */
function imgf_handle(delta)
{
	var change = false;
	switch (delta > 0)
	{
		case true:
			if(imgf_caption_id >= 1)
			{
				imgf_target = imgf_target + imgf_xstep;
				imgf_new_caption_id = imgf_caption_id - 1;
				change = true;
			}
			break;

		default:
			if(imgf_caption_id < (imgf_max-1))
			{
				imgf_target = imgf_target - imgf_xstep;
				imgf_new_caption_id = imgf_caption_id + 1;
				change = true;
			}
			break;
	}

	/* Glide to next (mouse wheel down) / previous (mouse wheel up) image */
	if (change == true)
	{
		imgf_glideTo(imgf_target, imgf_new_caption_id);
	}
}

/* Event handler for mouse wheel event */
function imgf_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) imgf_handle(delta);
	if (event.preventDefault) event.preventDefault();
	event.returnValue = false;
}

/* Initialize mouse wheel event listener */
function imgf_initMouseWheel()
{
	if(window.addEventListener) imageflow_div.addEventListener('DOMMouseScroll', imgf_wheel, false);
	imageflow_div.onmousewheel = imgf_wheel;
}

/* This function is called to drag an object (= slider div) */
function imgf_dragstart(element)
{
	imgf_dragobject = element;
	imgf_dragx = imgf_posx - imgf_dragobject.offsetLeft + imgf_new_slider_pos;
}

/* This function is called to stop dragging an object */
function imgf_dragstop()
{
	imgf_dragobject = null;
	imgf_dragging = false;
}

/* This function is called on mouse movement and moves an object (= slider div) on user action */
function imgf_drag(e)
{
	imgf_posx = document.all ? window.event.clientX : e.pageX;
	if(imgf_dragobject != null)
	{
		imgf_dragging = true;
		imgf_new_posx = (imgf_posx - imgf_dragx) + imgf_conf_slider_width;

		/* Make sure, that the slider is moved in proper relation to previous movements by the glideTo function */
		if(imgf_new_posx < ( - imgf_new_slider_pos)) imgf_new_posx = - imgf_new_slider_pos;
		if(imgf_new_posx > (imgf_scrollbar_width - imgf_new_slider_pos)) imgf_new_posx = imgf_scrollbar_width - imgf_new_slider_pos;
		
		var slider_pos = (imgf_new_posx + imgf_new_slider_pos);
		var step_width = slider_pos / ((imgf_scrollbar_width) / (imgf_max-1));
		var image_number = Math.round(step_width);
		var new_target = (image_number) * -imgf_xstep;
		var new_caption_id = image_number;

		imgf_dragobject.style.left = imgf_new_posx + 'px';
		imgf_glideTo(new_target, new_caption_id);
	}
}

/* Initialize mouse event listener */
function imgf_initMouseDrag()
{
	document.onmousemove = imgf_drag;
	document.onmouseup = imgf_dragstop;

	/* Avoid text and image selection while dragging  */
	document.onselectstart = function () 
	{
		if (imgf_dragging == true)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

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

document.onkeydown = function(event)
{
	var charCode  = imgf_getKeyCode(event);
	switch (charCode)
	{
		// Right arrow key
		case 39:
			imgf_handle(-1);
			break;
		
		// Left arrow key
		case 37:
			imgf_handle(1);
			break;
	}
}