﻿///////// code obtained from http://www.webreference.com ///////////
/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

// - code from Peter Schmalfeldt, http://www.manifestinteractive.com/...entControl.html - //

function manifestElementControl(whichElement,whichFunction,whichProperty,whichValue){
	/* ----------------------------------------------------------------- //
		
	  EXAMPLES
	  --------
	  whichFunction = setCSS
	  	manifestElementControl("myElementID","setCSS","visibility","hidden");
		manifestElementControl("myElementID","setCSS","visibility","visible");
		manifestElementControl("myElementID","setCSS","left","100px");
		manifestElementControl("myElementID","setCSS","top","100px");
		manifestElementControl("myElementID","setCSS","backgroundColor","#466FC7");
		manifestElementControl("myElementID","setCSS","color","#FFFFFF");
		manifestElementControl("myElementID","setCSS","zIndex","1");
		
		manifestElementControl("leftBar,loader,Header","setCSS,setCSS,setCSS","visibility,visibility,backgroundColor","hidden,hidden,#466FC7");
		manifestElementControl("loader","setCSS","visibility","hidden");
	
	  EXAMPLES
	  --------
	  whichFunction = setTEXT
	  	manifestElementControl("myElementID","setTEXT","","<p>This is some text.</p>");
		manifestElementControl("myElementID","setTEXT","","<table><tr><td>This is some text.</td></tr></table>");
		
	// ----------------------------------------------------------------- */
	var ns6 = document.getElementById && !document.all;
	var ie = document.all;
	
	var whichElementArray = whichElement.split(",");
	var whichFunctionArray = whichFunction.split(",");
	var whichPropertyArray = whichProperty.split(",");
	var whichValueArray = whichValue.split(",");

	for(var i=0; i<whichElementArray.length; i++){
		if(ie){
			if(whichFunctionArray[i] == "setCSS"){ 	eval("document.all."+whichElementArray[i]+".style."+whichPropertyArray[i]+" = '"+whichValueArray[i]+"'");}
			if(whichFunctionArray[i] == "setTEXT"){	eval("document.all."+whichElementArray[i]).innerHTML = whichValueArray[i];}
		}
		else if(ns6){
			if(whichFunctionArray[i] == "setCSS"){	eval("document.getElementById('"+whichElementArray[i]+"').style."+whichPropertyArray[i]+" = '"+whichValueArray[i]+"'");}
			if(whichFunctionArray[i] == "setTEXT"){	document.getElementById(whichElementArray[i]).innerHTML = whichValueArray[i];}
		}
	}
}


// - code from http://simonwillison.net, thank you - //

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////

function SendDate()
{
   UserDate = new Date();
   local = UserDate.toLocaleString();
   document.Entry.localDate.value=local;
}

function setTextSize(textSize)
// function sets text size of main window & info bar and writes value to cookie.
// [textSize] - an integer between 1 & 3 representing text sizes small to larger 
// function returns nothing.
{

	if (textSize == 2)
	{
		document.getElementById("mainWindow_MainText").style.fontSize = "medium";
		document.getElementById("info").style.fontSize = "medium";
	};
	else if (textSize == 3)
	{
		document.getElementById("mainWindow_MainText").style.fontSize = "large";
		document.getElementById("info").style.fontSize = "large";
	};
	else
	{
		document.getElementById("mainWindow_MainText").style.fontSize = "small";
		document.getElementById("info").style.fontSize = "small";
	};
		
	ammendCookie(textSize);
};


function ammendCookie(textSize)
// function sets cookie to hold user's preferred text size.
// [textSize] - an integer between 1 & 3 representing text sizes small to larger
// function returns nothing.
{
	var now = new Date();
	fixDate(now);
	now.setTime(now.getTime() + 50 * 24 * 60 * 60 * 1000);
	setCookie("textSize", textSize, now);
};


function setDisplacementForTrailsBox()
// function detects screen size and sets 'left' property of trailsBox
// accordingly. Function requires no arguments and returns nothing.
{

	if (document.body.clientWidth >= 769 && screen.width < 960)
	{
		{document.getElementById("trailsBox").style.left = "70%"};
	};
	else if (document.body.clientWidth >= 1100)
	{
		{document.getElementById("trailsBox").style.left = "53%"};
	};
	else
	{
		{document.getElementById("trailsBox").style.left = "57%"};
	};
	
};


function toggleVisibilityOfTrails()
// function toggles visibility property of trailsBox. Function requires
// no arguments and returns nothing.
{

	if (document.getElementById("trailsBox").style.visibility == "hidden")
	{
		setDisplacementForTrailsBox();
		document.getElementById("trailsBox").style.visibility = "visible";
	};
	else
	{
		document.getElementById("trailsBox").style.visibility = "hidden";
	};

};


function generateInfoBarContent(pageTitle)
// function generates content held within info bar on page. Function generates 
// a random number between 0 & 8 [9 integers inc 0] and the number picked determines
// what content is loaded. 

// Function requires one argument:
// pageTitle - [the title of the page being loaded]
// If pageTitle is equal to one of three values this function shall exit before 
// generating content for the info bar.
{
	// check value of pageTitle
	if (pageTitle == "lodberries" || pageTitle == "street" || pageTitle == "dutchCon" || pageTitle == "uha")
	{return};

	// generate random number and save value to randomNo
	var randomNo = Math.floor(Math.random()*8);
	
	// if loop to determine content
	if (randomNo == 0)
	{
		manifestElementControl("info","setTEXT","","<div id='rImage'><br /><img alt='North Harbour - Lerwick' src='InfoBar/infoBar01_sm.jpg'><p>North Harbour - Lerwick</p><br /><br /><img alt='A Dutch vessel at Victoria Pier' src='InfoBar/infoBar02_sm.jpg'><p>A Dutch vessel at Victoria Pier</p></div>");
	}
	else if (randomNo == 1)
	{
		manifestElementControl("info","setTEXT","","<div id='rText'><br /><p>Lodberries lie like anchored ships</p><p>And they are anchored well</p><p>Ah if their oaken doors were lips</p><p>What stories they could tell!</p><p style='font-style: italic;'>L J Nicolson</p><br /><br /><p>A Lodberrie is a house built on its own pier so that cargo could be loaded and unloaded straight from the boat.</p></div>");
	}
	else if (randomNo == 2)
	{
		manifestElementControl("info","setTEXT","","<div id='rImage'><br /><img alt='A view from Fort Charlotte' src='InfoBar/infoBar03_sm.jpg'><p>A view from Fort Charlotte</p><br /><br /><img alt='The Old Tolbooth' src='InfoBar/infoBar04_sm.jpg'><p>The Old Tolbooth</p></div>");
	}
	else if (randomNo == 3)
	{
		manifestElementControl("info","setTEXT","","<div id='rImage'><br /><img alt='South End of Lerwick' src='InfoBar/infoBar05_sm.jpg'><p>The South End of Lerwick</p><br /><br /><img alt='The great sails of a tall ship' src='InfoBar/infoBar06_sm.jpg'><p>The great sails of a tall ship</p></div>");
	}
	else if (randomNo == 4)
	{
		manifestElementControl("info","setTEXT","","<div id='rImage'><br /><img alt='Drawing of the Lodberries by Fred Irvine' src='InfoBar/lodberries-drawing.jpg'><p>Drawing of the Lodberries, by Fred Irvine</p><br /><br /><p>There are many tales of the lodberries being used for smuggling purposes. These rumours came to a little more light when unknown underground tunnels were found beneath structures in Lerwick.</p></div>");
	}
	else if (randomNo == 5)
	{
		manifestElementControl("info","setTEXT","","<div id='rText'><br /><p>Prior to 1469 Shetland belonged to the Danish Crown. It was then used as a mortgage by King Christian I to pay for his daughter’s marriage to King James III of Scotland.</p><br /><br /><p>Three forts have been built on the site of Fort Charlotte. The latest of the three was built in 1781 which is what you would see today.</p></div>");
	}
	else if (randomNo == 6)
	{
		manifestElementControl("info","setTEXT","","<div id='rText'><br /><p>Lerwick became a Burgh of Barony in 1818. It was only when the Lerwick Town Hall was built from 1882 that Lerwick became the capital of Shetland.</p><br /><br /><p>Most buildings along Lerwick’s waterfront date to the 18th century. The oldest inhabited house in Lerwick is The Old Manse bought as a manse in 1683.</p></div>");
	}
	else if (randomNo == 7)
	{
		manifestElementControl("info","setTEXT","","<div id='rImage'><br /><img alt='The Hamburg Window at the Town Hall' src='InfoBar/infoBar07_sm.jpg'><p>The Hamburg window at the Town Hall</p><br /><img alt='The Amsterdam window at the Town Hall' src='InfoBar/infoBar08_sm.jpg'><p>The Amsterdam window at the Town Hall</p></div>");
	}
	
};


function initialiseDocument(pageTitle)
// function initialises page by hiding trailsBox DIV and setting text size
// first by checking for cookie then by default. Function requires no
// arguments and returns nothing.
{
	// "fill" info box
	generateInfoBarContent(pageTitle);
	
	// 
	if (pageTitle == "input") {SendDate()};

	// look for text size cookie
	var size = getCookie("textSize")
	// if cookie is not found set text size to 1
	if (!size) {size = 1};	
	setTextSize(size);
};
