/**
 * @author Jon Heather
 * @copyright Network Mapping Ltd 2009
 */

var slides = new Array;
var captions = new Array;

slides[0] = 'SKG1.jpg';
slides[1] = 'DamIntElev.jpg';
slides[4] = 'riverside.jpg';
slides[5] = 'alexander.jpg';
slides[2] = 'spaghetti.jpg';
slides[3] = 'gloucester.jpg';


captions[0] = ' ';
captions[1] = ' ';
captions[4] = 'Aerial survey data of the Riverside Stadium in Middlesbrough, coloured by orthophotography.';
captions[5] = 'Aerial survey data of Alexander Stadium in Birmingham, coloured by orthophotography.';
captions[2] = 'Aerial survey data of Spaghetti Junction in Birmingham, coloured by orthophotography.';
captions[3] = 'Aerial survey data of Gloucester Cathedral, coloured by orthophotography.';



var slide_store = '/media/images/slide';
var slide_speed = 2800;

var slide_caption = captions[0];
var slide_id = -1;
var slide_loaded = 0;
var slide_count = parseInt(slides.length);

var slide_state = false;
var slide_pause = false;
var slide_timer = false;

function preload_slides()
{
	if(document.getElementById('slide_img') == null)
	{
		return;
	}
	
	for(i=0; i < slide_count; i++)
	{
		img = document.createElement('img');
		
		img.id = 'slide_' + i;
		
		img.onload = function ()
		{
			slide_loaded++;
			
			if(slide_loaded == slide_count)
			{
				start_slide();
			}
		}
		
		img.src = slide_store + '/' + slides[i];
	}
	
}

function start_slide()
{
	if(slide_id == -1)
	{
		slide_timer = setInterval("change_slide();", slide_speed);
		
		slide_id = 1;
	}
}

function change_slide()
{
	if(document.getElementById('slide_img') == null)
	{
		return;
	}
	
	if(slide_pause == false)
	{
		img = document.getElementById('slide_img');
		
		img.src = slide_store + '/' + slides[slide_id];
		
		slide_caption = captions[slide_id];
		
		if((slide_count - slide_id) == 1)
		{
			slide_id = 0;
		}
		else
		{
			slide_id++;
		}	
	}
}

function reset_slide()
{
	clearTimeout(slide_timer);
	
	slide_pause = false;
	
	slide_timer = setInterval("change_slide();", slide_speed);
}

function pause_slide()
{
	slide_pause = true;
}

function image_preview(image, width, height, caption)
{
	pause_slide();
	
	boundry = 30;
	
	dimmer_on();
	
	source = image.src.toString();
	
	large_source = str_replace('.jpg', '_big.jpg', source);
	large_source = str_replace('.gif', '_big.gif', large_source);
	
	preview = document.createElement('img');
	preview.id = 'preview_obj';
	
	preview.className = 'preview';
	
	max_width = f_clientWidth() - (boundry * 2);
	max_height = f_clientHeight() - (boundry * 2);
	
	if(width > max_width)
	{
		ratio = max_width / width;
		
		width = max_width;		
		height = parseInt(height * ratio);
	}
	
	if(height > max_height)
	{
		ratio = max_height / height;
				
		height = max_height;		
		width = parseInt(width * ratio);
	}
	
	itop = parseInt((max_height - (height + 18)) / 2);
	ileft = parseInt((max_width - (width + 18)) / 2);
	
	itop = itop + boundry + f_scrollTop();
	ileft = ileft + boundry + f_scrollLeft();
	
	preview.style.top = itop + 'px';
	preview.style.left = ileft + 'px';
	
	preview.style.width = width + 'px';
	preview.style.height = height + 'px';
	
	preview.style.position = 'absolute';
	preview.style.cursor = 'auto';
	
	preview.style.zIndex = 101;
	
	preview.src = large_source;
	
	closer_on((itop - 9), ((ileft + width + 18) - 16));
	
	document.body.appendChild(preview);
	
	preview.style.visibility = 'visible';
	
	if(caption != false)
	{
		preview.title = caption;
		
		capt = document.createElement('div');
		capt.id = 'caption_obj';
		
		capt.className = 'caption';
		
		capt.style.position = 'absolute';
		capt.style.padding = '5px';
		
		capt.style.zIndex = 102;
		
		capt.innerHTML = caption;
		
		capt.style.top = (itop + height) - 30 + 'px';
		capt.style.left = (ileft + 20) + 'px';
		
		document.body.appendChild(capt);
	}
}

function float_close()
{
	reset_slide();
	
	dimmer_off();
	
	sitemap_off();
	
	video = document.getElementById('video_obj');
	preview = document.getElementById('preview_obj');
	caption = document.getElementById('caption_obj');
	closer = document.getElementById('closer_obj');
	
	if(closer)
	{
		document.body.removeChild(closer);
	}
		
	if(caption)
	{
		document.body.removeChild(caption);
	}
	
	if(preview)
	{
		document.body.removeChild(preview);
	}
	
	if(video)
	{
		while(video.childNodes[0])
	 	{
	 		video.removeChild(video.childNodes[0]);
	 	}
	 	
	 	document.body.removeChild(video);
	}
}

function closer_on(top, left)
{
	closer = document.createElement('img');
	closer.id = 'closer_obj';
	
	closer.style.top = top + 'px';
	closer.style.left = left + 'px';
	
	closer.style.position = 'absolute';
	closer.style.backgroundColor = 'transparent';
	
	closer.style.padding = 0 + 'px';
	closer.style.borderWidth = 0;
	closer.style.zIndex = 105;
	
	closer.style.width = 24 + 'px';
	closer.style.height = 24 + 'px';
	
	closer.src = '/media/images/close.gif';
	
	document.body.appendChild(closer);
	
	closer.style.visibility = 'visible';
	
	closer.onclick = function() {
	
		float_close();
	
	}
}

function video_show(file, width, height)
{
	boundry = 30;
	
	dimmer_on();
	
	max_width = f_clientWidth() - (boundry * 2);
	max_height = f_clientHeight() - (boundry * 2);
	
	if(width > max_width)
	{
		ratio = max_width / width;
		
		width = max_width;		
		height = parseInt(height * ratio);
	}
	
	if(height > max_height)
	{
		ratio = max_height / height;
				
		height = max_height;		
		width = parseInt(width * ratio);
	}
	
	itop = parseInt((max_height - (height + 18)) / 2);
	ileft = parseInt((max_width - (width + 18)) / 2);
	
	itop = itop + boundry + f_scrollTop();
	ileft = ileft + boundry + f_scrollLeft();
	
	video = document.createElement('div');
	video.id = 'video_obj';
	
	video.style.position = 'absolute';
	
	video.style.top = itop + 'px';
	video.style.left = ileft + 'px';
	
	video.style.width = width + 'px';
	video.style.height = height + 'px';
	
	closer_on((itop - 9), ((ileft + width + 18) - 16));
	
	video.style.padding = 8 + 'px';
	video.style.border = 'solid 1px #81D961';
	video.style.zIndex = 103;
	
	video.style.backgroundColor = '#FFF';
	
	document.body.appendChild(video);
	
	var so = new SWFObject('/media/player.swf','simple', width, height, '9');
	
	so.addParam('allowfullscreen','false');
	
	so.addVariable('file','/media/video/' + file);
	so.addVariable('displayheight', height);
	
	so.addVariable('bufferlength','5');
	so.addVariable('showdownload','false');
	so.addVariable('showstop','false');
	so.addVariable('frontcolor','000000');
	so.addVariable('lightcolor','81D961');
	so.addVariable('screencolor','FFFFFF');
	so.addVariable('volume','60');
	so.addVariable('repeat','false');
	so.addVariable('autostart','true');
	so.addVariable('smoothing','true');
	so.addVariable('deblocking','1');
	
	so.write('video_obj');
	
	document.getElementsByTagName('embed')[0].style.zIndex = 104;
	
	video.style.visibility = 'visible';
}

function dimmer_on()
{
	dimmer = document.getElementById('dimmer_obj');
	
	ow = f_clientWidth();
	
	document.body.style.overflowX = 'hidden';
	document.body.style.overflowY = 'hidden';
	
	nw = f_clientWidth();
	
	wrapper = document.getElementsByTagName('div')[2];
	
	wrapper.style.width = (wrapper.clientWidth + (nw - ow)) + 'px';
	
	dimmer.style.left = f_scrollLeft() + 'px';
	dimmer.style.top = f_scrollTop() + 'px';
	
	dimmer.style.width = f_clientWidth() + 'px';
	dimmer.style.height = f_clientHeight() + 'px';
	
	dimmer.style.visibility = 'visible';
}

function dimmer_off()
{
	dimmer = document.getElementById('dimmer_obj');
	
	ow = f_clientWidth();
	
	document.body.style.overflowX = 'hidden';
	document.body.style.overflowY = 'auto';
	
	nw = f_clientWidth();
	
	wrapper = document.getElementsByTagName('div')[2];
	
	wrapper.style.width = (wrapper.clientWidth + (nw - ow)) + 'px';
	
	dimmer.style.visibility = 'hidden';
}

function image_tooltip()
{
	for (var i = 0; i < document.images.length; i++)
	{
		if(document.images[i].style.cursor != 'auto')
		{
			document.images[i].title = 'Click to Enlarge';
		}
	}
}

function sitemap_on()
{
	dimmer_on();
	
	itop = 22 + f_scrollTop();
	
	closer_on(itop, 22);
	
	sm = document.getElementById('sm_obj');
	
	itop = 30 + f_scrollTop();
	
	sm.style.top = itop + 'px';
	sm.style.visibility = 'visible';
}

function sitemap_off()
{
	sm = document.getElementById('sm_obj');
	
	sm.style.visibility = 'hidden';
}