// tested in ie7, ff3.6, chrome 5, opera 10.6 with no issues
// ie6 on osprey and ie8 on redkite slow when clicking link, resolved when tested via iis on panther. caused by mapped drive

$(document).ready(function() {
	
	writeTopMenu();
	
	//initialise slideshow
	$('#1').css('font-weight','bold')
	$currentFrame = 1;
	$startFrame = 1;
	$endFrame = 3;
	//20000 = 20 seconds
	$theDelay = 16000;
	$fadeDuration = 850;
	$disableControlsDuration = 1000;
	$interrupt = false;
	$cancelSlideshow1 = 0;
	$cancelSlideshow2 = 0;
	$cancelSlideshow3 = 0;
	
	//kick off slideshow
	setup_slideshow();
	
	//if slideshow links are clicked, cancel existing slideshow
	$('.control').click(function(){
		clearTimeout($cancelSlideshow3);
		$interrupt = true;
		clickframe = $currentFrame;
		$target = $(this).attr('id');
		//uncomment slideshowAni, comment out slideshowStop if you want slideshow to continue when user clicks a link
		//slideshowAni(clickframe, $target);
		slideshowStop(clickframe, $target);
		$currentFrame = $target;
		return false;
	});
	 
	//Login button
	$('#fader').hover(function() {
		$(this).find('img:eq(1)').stop(true,true).fadeIn();
		}, function() {
		$(this).find('img:eq(1)').fadeOut();
	})	
});

 
//called when slideshow initially started & after no user interaction
function setup_slideshow() {
	$interrupt = false;
	//wait until $theDelay period before changing slideshow
	$cancelSlideshow1 = setTimeout(slideshow(), $theDelay);
}

//now start slideshow
function slideshow() {
	return (function() {
		//get frames
		frame = $currentFrame;
		if ($currentFrame == $endFrame) {
			$currentFrame = $startFrame;
		} else { 
			$currentFrame++;
		}
		newframe = $currentFrame;
		//do fading animation
		slideshowAni(frame, newframe);
	})
}

//fade the slideshow out/in, disable controls during fade
function slideshowAni(frame, newframe) {
	disableControls();
	$('#slideshow' + frame).fadeOut();
	setTimeout("$('#slideshow" + newframe + "').fadeIn();", $fadeDuration);
	//check if controls clicked
	if ($interrupt == false)
	{
		clearTimeout($cancelSlideshow3);
		$cancelSlideshow2 = setTimeout(slideshow(), $theDelay + $fadeDuration);
		setTimeout(enableControls, $disableControlsDuration);
	} else if ($interrupt == true) {
		clearTimeout($cancelSlideshow1);
		clearTimeout($cancelSlideshow2);
		$cancelSlideshow3 = setTimeout(setup_slideshow, $theDelay);
		setTimeout(enableControls, $disableControlsDuration);
	}
	$('.control').css('color','');
	$('#'+newframe).css('color','#fff');
}

//if user clicks control then slideshow stops
function slideshowStop(frame, newframe) {
	disableControls();
	$('#slideshow' + frame).fadeOut();
	setTimeout("$('#slideshow" + newframe + "').fadeIn();", $fadeDuration);
	clearTimeout($cancelSlideshow1);
	clearTimeout($cancelSlideshow2);
	setTimeout(enableControls, $disableControlsDuration);
	$('.control').css('color','');
	$('#'+newframe).css('color','#fff');
}

//prevent users clicking controls during fade
function disableControls() {
	$('#controlsdisabled').css('display','block');
	$('#controls').css('display','none');
}

//enable controls once fade completed
function enableControls() {
	$('#controls').css('display','block');
	$('#controlsdisabled').css('display','none');
}
	
	
var mx;
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
timerID = setTimeout('ha()',20000);
function sd(l, p){
	k = l;
	ha();
	sn = 1;
	s(l, p);
}
function s(l, p){
	if(isIE4 || isIE5){
		eval('document.all.menu' + l + '.style.visibility = "visible"');
		eval('document.all.menu' + l + '.style.left = p');
	}
	if(isNS4){
		eval('document.layers.menu' + l + '.visibility = "show"');
		pos = p;
		eval('document.layers.menu' + l + '.left =pos');
		eval('document.layers.menu' + l + '.top = 106');
	}
	if(isNS6){
		var myElement = document.getElementById("menu" + l);
		myElement.style.left = p;
		myElement.style.visibility = "visible";
	}
}
function hd(){
	k = -1;
	setTimeout('ha()',500);
}
k = -1;
function ha(){
	for(i = 1; i < 5; i++){ 
		h = 0;
		chkmp(i);
		if((isIE4 || isIE5) && k != i)if(h)eval('document.all.menu' + i + '.style.visibility = "hidden"');
		if(isNS4 && k != i)if(h)eval('document.layers.menu' + i + '.visibility = "hide"');
		if(isNS6 && k != i)if(h)document.getElementById('menu'+i).style.visibility = "hidden";
	}
}
document.onmousemove = mp;
if (isNS4)document.captureEvents(Event.MOUSEMOVE);
function mp(x){
	if(isNS4){
		mx = x.pageX;
		my = x.pageY;
	}
	if(isNS6){
		mx = x.pageX;
		my = x.pageY;
	}
	if(isIE4 || isIE5){
		mx = event.clientX;
		my = event.clientY;
	}
}
function chkmp(i){ 
	if(isNS4){   
		var xmin = eval('document.layers.menu' + i + '.left');
		var xmax = xmin + eval('document.layers.menu' + i + '.clip.width');
		var ymin = eval('document.layers.menu' + i + '.top');
		var ymax = ymin + eval('document.layers.menu' + i + '.clip.height');
	}    
	if(isIE4 || isIE5){
		var xmin = eval('document.all.menu' + i + '.style.pixelLeft');
		var xmax = xmin + eval('document.all.menu' + i + '.style.pixelWidth');
		var ymin = eval('document.all.menu' + i + '.style.pixelTop');
		var ymax = ymin + eval('document.all.menu' + i + '.style.pixelHeight');
	}
	if(isNS6){
		var myElement = document.getElementById('menu' + i);
		var xmin = eval('myElement.style.pixelLeft');
		var xmax = xmin + eval('myElement.style.pixelWidth');
		var ymin = eval('myElement.style.pixelTop');
		var ymax = ymin + eval('myElement.style.pixelHeight');
	}
	if (mx >= xmin && mx <= xmax && my >= ymin && my <= ymax){
		h = 0;
		clearTimeout(timerID);
		timerID = setTimeout('ha()',100);
	} else { 
		h = 1;
	}
	return h;
}
