﻿/* Space Browse Copyright © 2006 PHD Computer Consultants Ltd v2.02 */

var space_displayWidth = 640;
var space_displayHeight = 480;
var space_displayWidthRatio = 1;
var space_displayHeightRatio = 1;

var space_resdir = "";
var space_basedir = "";
var space_DOM = null;
var space_frameno = 0;
var space_frame = null;
var space_frameCount = 0;

var space_borderColourOn = "orange";
var space_borderColourVisited = "green";
var space_borderColourOff = "gray";

var space_nav_StartedAutoOff = 0;
var space_nav_LastMovedDate = null;
var space_nav_UserShowing = true;
var space_nav_TimerId = null;
var space_nav_Showing = true;

var space_history = null;
var space_historyPos = 0;
var space_historySize = 0;

var space_PhotosVisited = new Array();
var space_progress_left = false;
var space_progress_right = false;

var space_world = false;
var space_back = false;
var space_forward = false;
var space_home = false;
var space_show = false;
var space_title = false
var space_unzoom = false;
var space_up = false;
var space_down = false;
var space_left = false;
var space_right = false;
var space_later = false;
var space_earlier = false;
var space_turn = false;
var space_imageDiv = false;
var space_imgPhoto1 = false;
var space_imgPhoto2 = false;
var space_text = null;

var space_supportsCursor = false;

var space_imgPhoto = false;	// Set to space_imgPhoto1, space_imgPhoto2 or null
var space_imgNext = false;	// ditto

var space_nexttabindex = 212;

function px(v) { return v+'px'; }

function res(v) { return space_resdir+v; }
function resurl(v) { return "url("+space_resdir+v+")"; }

function space_setControls()
{
	space_world = document.getElementById("space_world");
	space_back = document.getElementById("space_back");
	space_forward = document.getElementById("space_forward");
	space_home = document.getElementById("space_home");
	space_show = document.getElementById("space_show");
	space_title = document.getElementById("space_title");
	space_unzoom = document.getElementById("space_unzoom");
	space_up = document.getElementById("space_up");
	space_down = document.getElementById("space_down");
	space_left = document.getElementById("space_left");
	space_right = document.getElementById("space_right");
	space_later = document.getElementById("space_later");
	space_earlier = document.getElementById("space_earlier");
	space_turn = document.getElementById("space_turn");
	space_imageDiv = document.getElementById("space_imageDiv");
	space_imgPhoto1 = document.getElementById("space_imgPhoto1");
	space_imgPhoto2 = document.getElementById("space_imgPhoto2");
	space_progress_left = document.getElementById("space_progress_left");
	space_progress_right = document.getElementById("space_progress_right");
	space_text = document.getElementById("space_text");

	space_supportsCursor = space_left.style.cursor;

	space_imgPhoto = space_imgPhoto1;
	space_imgNext = null;
}

function space_setSizes( calcWidthAndHeight, displayWidth, displayHeight, siteWidth, siteHeight, siteTextLines)
{
	if( calcWidthAndHeight)
	{
		if( displayHeight<=0) displayHeight = 240;
		
		var ImgHeight = displayHeight - 10 - 19 - 6 - (siteTextLines*21);
		if( ImgHeight<100)
		{
			siteTextLines = 1;
			ImgHeight = displayHeight - 10 - 19 - 6 - 21;
		}
		var ImgWidth = Math.floor(ImgHeight*siteWidth/siteHeight);
		if( displayWidth>0)
		{
			if( (ImgWidth+6) > displayWidth)
			{
				ImgWidth = displayWidth-6;
				ImgHeight = Math.floor(ImgWidth*siteHeight/siteWidth);
			}
		}
		if( (ImgWidth+6) > document.body.scrollWidth)
		{
			ImgWidth = document.body.scrollWidth - 6;
			ImgHeight = Math.floor(ImgWidth*siteHeight/siteWidth);
		}
		displayHeight = ImgHeight;
		displayWidth = ImgWidth;
	}

	space_displayWidthRatio = displayWidth/siteWidth;
	space_displayHeightRatio = displayHeight/siteHeight;
	space_displayWidth = displayWidth;
	space_displayHeight = displayHeight;

	space_setControls();

	if( displayWidth<125 || displayHeight<100)
	{
		space_world.style.visibility = "hidden";
		space_world.style.display = "inline";
		var space_toosmall = document.getElementById("space_toosmall");
		space_toosmall.style.border = "1px dotted orange";
		space_toosmall.style.visibility = "visible";
		space_toosmall.style.width = "75px";
		space_toosmall.style.height = "75px";
		space_toosmall.innerHTML = "Space Browse too small";
		return;
	}

	space_world.style.height = px(displayHeight+33+(siteTextLines*21));
	space_world.style.width = px(displayWidth+4);
	space_imageDiv.style.height= px(displayHeight);
	space_imageDiv.style.width= px(displayWidth);
	space_imgPhoto.height = displayHeight;
	space_imgPhoto.width = displayWidth;
//	space_imgNext.height = displayHeight;
//	space_imgNext.width = displayWidth;
	space_title.style.width = px(displayWidth-124);
	
	space_progress_left.style.top = px(displayHeight+27);
	space_progress_right.style.top = px(displayHeight+27);
	space_progress_right.style.width = px(displayWidth-2);
	
	space_text.style.height = px(siteTextLines*21 + 2);
	space_text.style.width = px(displayWidth);
	space_text.style.top = px(displayHeight+30);

	space_left.style.left = px(displayWidth-88);
	space_up.style.left = px(displayWidth-61);
	space_right.style.left = px(displayWidth-34);
	space_down.style.left = px(displayWidth-61);
	space_unzoom.style.left = px(displayWidth-61);
	space_earlier.style.left = px(displayWidth-88);
	space_later.style.left = px(displayWidth-34);
	space_turn.style.left = px(displayWidth-61);
}

function space_showTitle( siteURL, title, description, username, userURL)
{
	space_title.style.display = "block"
	if( !siteURL)
		space_title.innerHTML = "No site found";
	else
		space_title.innerHTML = "<a href=\""+siteURL+"\" title=\""+description+"\" target='_top' tabindex='203'>" + title + "</a> by "+
							"<a href='"+userURL+"' alt=\""+username+"\" target='_top' tabindex='203'>" + username + "</a>";
}


function space_processJSON( AppBaseDir, basedir, JSON)
{
	space_resdir = AppBaseDir+"space/";
	space_basedir = AppBaseDir+basedir;
	space_DOM = JSON.parseJSON();
	if( !space_DOM)
	{
		alert("Sorry, your photo-site contains text that is unsafe to display.  Please contact us.");
		return;
	}
	space_DOM.space.frame = ObjectToArray(space_DOM.space.frame);
	space_frameCount = space_DOM.space.frame.length;

	space_history = new Array();
	space_historyPos = -1;
	space_historySize = 0;

	if( !space_world) space_setControls();
	if( space_world.style.visibility=="hidden") return;

	space_show["onclick"] = function() { return space_showHideNavigation(); }

	space_goHome();
}

function space_showHideNavigation(AutoOn)
{
	var prevShowing = space_nav_Showing;
	var setUserImg = false;
	if( AutoOn==null)
	{
		setUserImg = true;
		space_nav_UserShowing = !space_nav_UserShowing;
		space_nav_Showing = space_nav_UserShowing;
	}
	else if( AutoOn)
		space_nav_Showing = true;
	else
		space_nav_Showing = false;

	if( prevShowing!=space_nav_Showing)
	{
		if( space_nav_Showing)
		{
			if( setUserImg)
				space_show.src = res("shownavig23.gif");
			space_unzoom.style.display = "block";
			space_up.style.display = "block";
			space_down.style.display = "block";
			space_left.style.display = "block";
			space_right.style.display = "block";
			space_later.style.display = "block";
			space_earlier.style.display = "block";
			space_turn.style.display = "block";
			space_startNavigation(null);
			space_setNavigation();  // Mac Safari 1.0 needs this
		}
		else
		{
			if( setUserImg)
				space_show.src = res("shownavig23off.gif");
			space_unzoom.style.display = "none";
			space_up.style.display = "none";
			space_down.style.display = "none";
			space_left.style.display = "none";
			space_right.style.display = "none";
			space_later.style.display = "none";
			space_earlier.style.display = "none";
			space_turn.style.display = "none";

			// Hide existing Forward INPUTs
			var divKids = space_imageDiv.childNodes;
			for (var i = 0; i < divKids.length; i++) 
			{
				if( divKids[i].nodeName=="INPUT")
				{
					divKids[i].style.display="none";
				}
			}
		}
	}
	if( window.event) window.event.returnValue = false;  
	return false;
}

function space_goHome()
{
	var SiteStartPhotoNo = space_DOM.space.SiteStartPhotoNo;
	if( SiteStartPhotoNo)
		SiteStartPhotoNo += ".jpg";
	else
		SiteStartPhotoNo = space_DOM.space.frame[0].photo.img;

	return space_gotoFrame(SiteStartPhotoNo, true, null);
}

function space_gotoFrame( framename, addToHistory, ruleType)
{
	MarkAsVisited(framename);
	// Search for frame that matches the given image name
	var foundFrame = false;
	for( var frameno=0; frameno<space_frameCount; frameno++)
	{
		if( framename==space_DOM.space.frame[frameno].photo.img)
		{
			foundFrame = true;
			break;
		}
	}
	if( !foundFrame)
	{
		window.status = "Could not find frame "+framename;
		if( framename.indexOf("unknown")!=-1)
			alert("The photo-site designer has not set this link");
		return;
	}
	// Set frame number
	space_frameno = frameno;
	space_frame = space_DOM.space.frame[space_frameno];

	// Add to History
	if( addToHistory)
	{
		space_historyPos++;
		space_historySize = space_historyPos+1;
		space_history[space_historyPos] = space_frame;
	}

	space_startNavigation(ruleType);
	if( window.event) window.event.returnValue = false;  
	return false;
}

var space_setNavigationCalled = false;

function space_startNavigation(ruleType)
{
	space_setNavigationCalled = false;

	// Remove existing Forward INPUTs
	var divKids = space_imageDiv.childNodes;
	for (var i = 0; i < divKids.length; i++) 
	{
		if( divKids[i].nodeName=="INPUT")
		{
			space_imageDiv.removeChild(divKids[i]);
			i--;
		}
	}

	// Disable LRUDB
	disable(space_left,"left");
	disable(space_right,"right");
	disable(space_up,"up");
	disable(space_down,"down");
	disable(space_unzoom,"unzoom");
	space_unzoom.style.display = "none";
	disable(space_earlier,"earlier");
	space_earlier.style.display = "none";
	disable(space_later,"later");
	space_later.style.display = "none";
	disable(space_turn,"turn");
	space_turn.style.display = "none";
	
	space_text.innerHTML = "";
	if( space_supportsCursor)
	{
		space_left.style.cursor = "default";
		space_right.style.cursor = "default";
		space_up.style.cursor = "default";
		space_down.style.cursor = "default";
		space_unzoom.style.cursor = "default";
		space_text.style.cursor = "default";
	}

    // Start photo loading now
	if( space_supportsCursor)
		space_imgPhoto.style.cursor = "wait";
	
	var isLeft = ruleType=="l";
	var isRight = ruleType=="r";
	var isUp = ruleType=="u";
	var isDown = ruleType=="d";
	if( isLeft||isRight||isUp||isDown)
	{
		space_slideXinc = 0;
		space_slideYinc = 0;
		slideCount = 0;
		if( isLeft) space_slideXinc = -16;
		if( isRight) space_slideXinc = 16;
		if( isUp) space_slideYinc = -16;
		if( isDown) space_slideYinc = 16;
		if( isLeft||isRight) slideTotal = space_displayWidth/16;
		else slideTotal = space_displayHeight/16;
		slideTotal = Math.floor(slideTotal);
		space_imgNext = space_imgPhoto==space_imgPhoto1 ? space_imgPhoto2 : space_imgPhoto1;
		space_imgPhoto["onload"] = null;
		space_imgNext["onload"] = function() { space_slideNavigation(); }
		space_imgNext.width = 0;
		space_imgNext.height = 0;
		space_imgNext.style.left = "0px";
		space_imgNext.style.top = "0px";
		space_imgNext.src = space_basedir+space_frame.photo.img;
		space_imgNext.title = space_frame.photo.PhotoDescription;
		// space_setNavigation will be fired when photo finally in place: only then add new navigation
	}
	else
	{
		space_imgPhoto["onload"] = function() { space_setNavigation(); }
		space_imgPhoto.src = space_basedir+space_frame.photo.img;
		space_imgPhoto.title = space_frame.photo.PhotoDescription;
		// space_setNavigation will fire when photo finally loaded: only then add new navigation
	}
}

function disable( ctrl, nav)
{
	ctrl.disabled = true;
	ctrl["onclick"] = null;
	ctrl.src = res(nav+"23gray.gif");
	ctrl.style.borderColor = space_borderColourOff;
}


var slideTimerId = 0;
var space_slideXinc = 0;
var space_slideYinc = 0;
var slideX = 0;
var slideY = 0;
var slideCount = 0;
var slideTotal = 0;

function space_slideNavigation()
{
	if( space_imgNext==null) return;
	space_imgNext["onload"] = null;
	space_imgNext.width = space_displayWidth;
	space_imgNext.height = space_displayHeight;
	slideX = space_slideXinc;
	slideY = space_slideYinc;
	if( space_slideXinc<0)
		space_imgNext.style.left = px(-slideX-space_displayWidth);
	if( space_slideXinc>0)
		space_imgNext.style.left = px(space_displayWidth-slideX);
	if( space_slideYinc<0)
		space_imgNext.style.top = px(-slideY-space_displayHeight);
	if( space_slideYinc>0)
		space_imgNext.style.top = px(space_displayHeight-slideY);
	slideTimerId = self.setInterval('space_slidePhoto()',50);
}

function space_slidePhoto()
{
	slideCount++;
	if( slideCount==slideTotal)
	{
		self.clearInterval(slideTimerId);
		space_imgPhoto.width = 0;
		space_imgPhoto.height = 0;
		space_imgPhoto["onload"] = null;
		space_imgPhoto.src = "space/onebyone.gif";
		space_imgPhoto = space_imgNext;
		space_imgNext = null;
		space_imgPhoto.style.left = "0px";
		space_imgPhoto.style.top = "0px";
		space_setNavigation();
		return;
	}
	if( space_slideXinc!=0)
		slideX += space_slideXinc;
	if( space_slideYinc!=0)
		slideY += space_slideYinc;
	if( space_slideXinc<0)
	{
		space_imgNext.style.left = px(-slideX-space_displayWidth);
		space_imgPhoto.style.left = px(-slideX);
	}
	if( space_slideXinc>0)
	{
		space_imgNext.style.left = px(space_displayWidth-slideX);
		space_imgPhoto.style.left = px(-slideX);
	}
	if( space_slideYinc<0)
	{
		space_imgNext.style.top = px(-slideY-space_displayHeight);
		space_imgPhoto.style.top = px(-slideY);
	}
	if( space_slideYinc>0)
	{
		space_imgNext.style.top = px(space_displayHeight-slideY);
		space_imgPhoto.style.top = px(-slideY);
	}
}

function space_setNavigation()
{
	if( !space_imgPhoto) return;
	if( space_supportsCursor)
		space_imgPhoto.style.cursor = "default";
	if( space_setNavigationCalled)
		return;
	space_setNavigationCalled = true;

	// Set Back
	if( space_historyPos>0)
	{
		space_back.src = res("back23.gif");
		space_back["onclick"] = function() { return space_backInHistory(); }
		space_back.disabled = false;
		space_back.style.borderColor = space_borderColourOn;
		if( space_supportsCursor)
			space_back.style.cursor = "pointer";
	}
	else
	{
		space_back.src = res("back23gray.gif");
		space_back["onclick"] = null;
		space_back.disabled = true;
		space_back.style.borderColor = space_borderColourOff;
		if( space_supportsCursor)
			space_back.style.cursor = "default";
	}

	// Set Forward
	if( space_historyPos<(space_historySize-1))
	{
		space_forward.src = res("forward23.gif");
		space_forward["onclick"] = function() { return space_forwardInHistory(); }
		space_forward.disabled = false;
		space_forward.style.borderColor = space_borderColourOn;
		if( space_supportsCursor)
			space_forward.style.cursor = "pointer";
	}
	else
	{
		space_forward.src = res("forward23gray.gif");
		space_forward["onclick"] = null;
		space_forward.disabled = true;
		space_forward.style.borderColor = space_borderColourOff;
		if( space_supportsCursor)
			space_forward.style.cursor = "default";
	}

	if( !space_nav_UserShowing) return;

	if( space_supportsCursor)
		space_text.style.cursor = "auto";
	space_text.innerHTML = "";
	space_text.title = "";

	// Show frame text
	if( space_frame)
	{
		space_frame.rule = ObjectToArray(space_frame.rule);

		// Process any rules
		space_nexttabindex = 212;
		for( var ruleno=0; ruleno<space_frame.rule.length; ruleno++)
		{
			rule = space_frame.rule[ruleno];
			processFrameRule(rule);
		}
	}
	space_startShowNavigationTimeout();
}

function ShowRule( ctrl, nav, VisitedImg, ruleType)
{
	if( space_supportsCursor)
		ctrl.style.cursor = "pointer";
	ctrl.disabled = false;
	ctrl.src = res(nav+"23.gif");
	if( VisitedImg)
		ctrl.style.borderColor = space_borderColourVisited;
	else
		ctrl.style.borderColor = space_borderColourOn;

	ctrl.style.display = "block";
	ctrl.alt = rule.img;
	ctrl["onclick"] = function() { return space_gotoFrame(this.alt,true,ruleType); }
}

function processFrameRule( rule)
{
	rule.type = rule.type.toLowerCase();
	if( rule.type=="t")
	{
		space_addText(rule);
		return;
	}
	var VisitedImg = Visited(rule.img);
	if( rule.type=="l")
		ShowRule(space_left,"left",VisitedImg,rule.type);
	else if( rule.type=="r")
		ShowRule(space_right,"right",VisitedImg,rule.type);
	else if( rule.type=="u")
		ShowRule(space_up,"up",VisitedImg,rule.type);
	else if( rule.type=="d")
		ShowRule(space_down,"down",VisitedImg,rule.type);
	else if( rule.type=="b")
		ShowRule(space_unzoom,"unzoom",VisitedImg,rule.type);
	else if( rule.type=="a")
		ShowRule(space_earlier,"earlier",VisitedImg,rule.type);
	else if( rule.type=="z")
		ShowRule(space_later,"later",VisitedImg,rule.type);
	else if( rule.type=="x")
		ShowRule(space_turn,"turn",VisitedImg,rule.type);
	else if( rule.type=="f")
	{
		var x = parseInt(rule.x);
		var y = parseInt(rule.y);
		var w = parseInt(rule.w);
		var h = parseInt(rule.h);
		x *= space_displayWidthRatio;
		y *= space_displayHeightRatio;
		w *= space_displayWidthRatio;
		h *= space_displayHeightRatio;
		w -= 4;
		h -= 4;
		if( w<=0) w = 2;
		if( h<=0) h = 2;

		var fInput = document.createElement('input');
		if( !fInput) return;
		fInput.type = "image";
		fInput.src = res("onebyone.gif");
		fInput.tabindex = space_nexttabindex++;	// Seems to be ignored
		if( space_supportsCursor)
			fInput.style.cursor = "pointer";
		fInput.style.position = "absolute";
		fInput.style.left = px(x);
		fInput.style.top = px(y);
		fInput.style.width = px(w);
		fInput.style.height = px(h);
		if( VisitedImg)
			fInput.style.border = "2px dotted "+space_borderColourOn;
		else
			fInput.style.border = "2px solid "+space_borderColourOn;
		fInput.alt = rule.img;
		fInput.title = rule.img;
		fInput["onclick"] = function() { return space_gotoFrame(this.alt,true,null); }
		space_imageDiv.appendChild(fInput);
	}
}

function space_addText( textObj)    //  (each one at same posn at moment)
{
	if( !textObj) return;

	if( space_supportsCursor)
		space_text.style.cursor = "help";
	space_text.innerHTML = textObj.string;
	space_text.title = textObj.string;
}

function space_backInHistory()
{
	if( space_historyPos>0)
	{
		space_historyPos--;
		var frame = space_history[space_historyPos];
		space_gotoFrame(frame.photo.img,false,null);
	}
	if( window.event) window.event.returnValue = false;  
	return false;
}

function space_forwardInHistory()
{
	if( space_historyPos<(space_historySize-1))
	{
		space_historyPos++;
		var frame = space_history[space_historyPos];
		space_gotoFrame(frame.photo.img,false,null);
	}
	if( window.event) window.event.returnValue = false;  
	return false;
}

function ObjectToArray( obj)
{
	if( !obj) return new Array();
	if( !obj.length) return new Array(obj);
	return obj;
}

function MarkAsVisited( new_framename)
{
	if( Visited(new_framename)) return;
	space_PhotosVisited[space_PhotosVisited.length] = new_framename;
	if( space_frameCount==0) return;
	var percentDone = space_PhotosVisited.length/space_frameCount;
	var width = space_displayWidth-4;
	var split = Math.round(percentDone*width);
	percentDone = Math.round(percentDone*100);
	space_progress_left.style.width = px(split);
	space_progress_right.style.left = px(split+2);
	space_progress_right.style.width = px(width-split);
	space_progress_left.title = percentDone+"% done";
	if( percentDone>99) space_progress_right.style.display = "none";
	var todo = space_frameCount-space_PhotosVisited.length;
	space_progress_right.title = todo+" photos to find";
}

function Visited( framename)
{
	if( !framename) return false;
	var VisitedLen = space_PhotosVisited.length;
	for( var visitedno=0;visitedno<VisitedLen;visitedno++)
	{
		if( framename==space_PhotosVisited[visitedno])
			return true;
	}
	return false;
}

function space_startShowNavigationTimeout()
{
	if( space_nav_StartedAutoOff>0)
	{
		space_nav_StartedAutoOff = 2;
		if( space_nav_UserShowing && space_nav_TimerId==null)
		{
			space_nav_LastMovedDate = new Date();
			space_nav_TimerId = window.setInterval("space_ShowNavigationInterval()",1000);
		}
	}
	else
		space_nav_StartedAutoOff = 1;
}

function space_ShowNavigationInterval()
{
	if( !space_nav_LastMovedDate) return;

	var now = new Date();
	var timeSinceLastMove = now.getTime()-space_nav_LastMovedDate.getTime();
	if( timeSinceLastMove<3000)
		return;

	window.clearInterval(space_nav_TimerId);
	space_nav_TimerId = null;
	space_nav_LastMovedDate = null;
	space_showHideNavigation(false);
}

function space_mouseMoved()
{
	if( space_nav_StartedAutoOff<2) return;

	if( !space_nav_UserShowing) return;
	
	space_showHideNavigation(true);
	space_nav_LastMovedDate = new Date();
	if( !space_nav_TimerId)
		space_nav_TimerId = window.setInterval("space_ShowNavigationInterval()",1000);
}
