/*******************************************************************************************************
	Script for dispatch to display and enlarge the thumbnail images.
	
	Gallery slide functionality from image-slideshow by 
	Alf Magne Kalleland @ dhtmlgoodies.com
********************************************************************************************************/

// Global variables
var g_curImagePath = "page01";
var g_initialWidth = null;
var g_initialHeight = null;

var g_activeImage = false;
var g_leftPos = false;
var g_galleryWidth = false;
var g_galleryObj = false;
var g_maxXPos = false;
var g_slideSpeed = 0;

/******************************************************************************************************
	Thumbnail Functions
*******************************************************************************************************/
function start_slide(e)
{
	if(document.all)e = event;
	var id = this.id;
	this.getElementsByTagName('IMG')[0].src = '/dispatch/images/' + this.id + '_over.gif';	
	if(this.id=='arrow_right')
	{
		g_slideSpeedMultiply = Math.floor((e.clientX - this.offsetLeft) / 5);
		g_slideSpeed = -1*g_slideSpeedMultiply;
		g_slideSpeed = Math.max(-10,g_slideSpeed);
	}else{			
		g_slideSpeedMultiply = 10 - Math.floor((e.clientX - this.offsetLeft) / 5);
		g_slideSpeed = 1*g_slideSpeedMultiply;
		g_slideSpeed = Math.min(10,g_slideSpeed);
		if(g_slideSpeed<0)g_slideSpeed=10;
	}
}

function release_slide()
{
	var id = this.id;
	this.getElementsByTagName('IMG')[0].src = '/dispatch/images/' + this.id + '.gif';
	g_slideSpeed=0;
}
	
function gallery_slide()
{
	if(g_slideSpeed!=0)
	{
		var leftPos = g_galleryObj.offsetLeft;
		leftPos = leftPos/1 + g_slideSpeed;
		if(leftPos>g_maxXPos)
		{
			leftPos = g_maxXPos;
			g_slideSpeed = 0;
			
		}
		if(leftPos<minGalleryXPos)
		{
			leftPos = minGalleryXPos;
			g_slideSpeed=0;
		}
		
		g_galleryObj.style.left = leftPos + 'px';
	}
	setTimeout('gallery_slide()',20);
}
function show_thumb()
{
	if(g_activeImage)
	{
		g_activeImage.style.filter = 'alpha(opacity=50)';	
		g_activeImage.style.opacity = 0.5;
	}	
	this.style.filter = 'alpha(opacity=100)';
	this.style.opacity = 1;	
	g_activeImage = this;
}
	
/******************************************************************************************************
	Display Functions
*******************************************************************************************************/
function show_image(imagePath)
{
	var images = document.getElementById('displayPanel').getElementsByTagName('IMG');
	if(images.length==0)
	{
		var img = document.createElement('IMG');
		document.getElementById('displayPanel').appendChild(img);
	}
	else
	{
		img = images[0];
	}
	
	if( (imagePath.match('page01') != null) || (imagePath.match('page28') != null) )
	{
		img.width = g_initialWidth;
		img.height = g_initialHeight;
	}
	else
	{
		// reverse widthxheight dimensions for horizontal page
		img.width = g_initialHeight;
		img.height = g_initialWidth;
	}
		g_curImagePath = imagePath;
	
	img.src = imagePath;
}


function enlarge_image()
{
	img = document.getElementById('displayPanel').getElementsByTagName('img')[0];
	
	// page 1 and page 28 are vertical pages
	var vertical = false;
	if( (g_curImagePath.match('page01') != null) || (g_curImagePath.match('page28')))
	{
		vertical = true;
	}
	
	if( img.height < 1600 )
	{	
		if(vertical) img.width = 1220;
		else img.width = 2600;
		
		img.height = 1600;
	}
	else
	{
		if(vertical)
		{
			img.width = g_initialWidth;
			img.height = g_initialHeight;
		}
		else
		{
			// reverse widthxheight dimensions for horizontal page
			img.width = g_initialHeight;
			img.height = g_initialWidth;
		}
	}
}

function init()
{
	img = document.getElementById('displayPanel').getElementsByTagName('img')[0];
	
	// Set the dimensions of the image for later use
	g_initialWidth = img.width;
	g_initialHeight = img.height;
	
	// initialize the thumbnail gallery slider
	document.getElementById('arrow_left').onmousemove = start_slide;
	document.getElementById('arrow_left').onmouseout = release_slide;
	document.getElementById('arrow_right').onmousemove = start_slide;
	document.getElementById('arrow_right').onmouseout = release_slide;
	
	g_galleryObj = document.getElementById('thumbnails');
	g_leftPos = g_galleryObj.offsetLeft;
	g_galleryWidth = document.getElementById('thumbContainer').offsetWidth - 80;
	g_maxXPos = g_galleryObj.offsetLeft; 
	minGalleryXPos = g_galleryWidth - document.getElementById('slideEnd').offsetLeft;
	var slideshow_images = g_galleryObj.getElementsByTagName('IMG');
	for(var no=0;no<slideshow_images.length;no++)
	{
		slideshow_images[no].onmouseover = show_thumb;
	}
	
	gallery_slide();		
}

window.onload = init;
