//
// © Graham Johnson 2002-2010
//
// Scripts to support the lower-edmonton.co.uk site.
//

// 
// Targets are not part of HTML 4.01 Strict so to keep the code clean
// I'll add them once the page has been parsed by calling code in the footer.
//
// We only really wanted to handle _top to break out of the web hosting frame
// and handle offsite links that would otherwise get caught in the frame but the
// site is no longer framed so this is no longer needed.  However might as well
// leave the functionality there in case anyone does try and frame the site.
//
// The old dynamic menu system confused things as it had classes and targets 
// but it is long gone.  However the search engine can still get in the way so all
// our targets begin with 'n9'.
//
// If any targets are found without a class then assign the target to the class
// and display a warning on screen. We do this because GoLive makes it 
// easy to set the target for a link so we can set the class there and do a
// search and replace later to change it.  This corrects things in the meantime.
//
function n9targets()
{
	// Let us search through every link on the page.
	for (var i = 0 ; i < document.links.length; i++)
	{
		var d = document.links[i];

		// Look for stray targets but bear in mind menu has class already.
		// Menu was actually removed in December 2004 but still a good idea.
		// Now using Atomz search must make sure only dealing with our own targets.
		if ( d.target != "" && d.className == "" && d.target.slice(0,2) == "n9")
		{
			 d.className = d.target;
			 document.write('<span class="n9defluff">Note for webmaster: class '+d.className+' assigned to link to '+d.href+'</span><br>');
		}
		
		// Make sure the link is one we created.
		if (d.className.slice(0,2) == "n9" )
		{
			switch (d.className)
			{
				case "n9top":		d.target = "_top";
								break;
				case "n9offsite":	d.target = "_top";
								break;
				// case "n9online":	d.target = "_top";
				//				break;
				case "n9images":	// Some links may now be to Fotopic so leave them alone
								if  ( d.href.slice(0,14) != "http://fotopic" )
								{
									n9sethref(d);	// don't want a target at all but munge link
								}
								break;
				default:			d.target = d.className;
								break;
			}
			
			// document.write('<span class="n9defluff">Note for webmaster: target '+d.target+' assigned to link to '+d.href+'</span><br>');
		}
	}
}


//
// This function is called with the link associated with an image. The href that points to the full size image is
// munged to call a cgi script that tarts up the presentation of the image.
//
// It also sets the title attribute of the image to be the same as the link as GoLive doesn't make that nice and easy to
// set and Internet Explorer will otherwise use the alt text and that isn't really on. The size information will have been
// stripped off the end to make it more appropriate to the small image.
//
// Now the image.cgi script handles the image size the Size stuff has been removed from all
// the images.
//
function n9sethref(thelink)
{
	var n9title = '';
	var width = 0;
	var height = 0;
	var alt="";
	
	// document.write('<br><span class="n9defluff">Note for webmaster: href ['+thelink.href+']</span>');

	// If the link has a title then grab it.
	if (thelink.title != null && thelink.title != "") n9title = thelink.title;

	// Search for '-Size: <width>x<height>' at the end of the link.
/* OBSOLETED
	var n9size = n9title.indexOf('- Size: ');
	if (n9size != -1)
	{
		// Take it on trust that if '- Size: ' is found the format is OK and extract the width and height.
		var xybit = n9title.substr(n9size + 8);
		var xy = xybit.split('x');
		
		width  = xy[0];
		height = xy[1];
		
		// Strip the size information off the end.
		n9title = n9title.substring(0, n9size);		
	}
*/

	// Set a default title if there isn't one.
	if (n9title == "") n9title = 'Lower Edmonton Image - Webmaster to identify later';
	
	// document.write('<br><span class="n9defluff">Note for webmaster: nodeName ['+thelink.firstChild.nodeName.toLowerCase()+']</span>');
	
	// We can actually link to an image from a text link so...
	if (thelink.firstChild.nodeName.toLowerCase() == 'img')
	{
		// Need to grab the "alt" text from the linked image.
		alt = thelink.firstChild.getAttribute('alt');
		// document.write('<br><span class="n9defluff">Note for webmaster: alt ['+alt+']</span>');

		// While we are here, might as well set the image title attribute too ;-)
		thelink.firstChild.setAttribute('title',n9title);
	}
	
	// That will still break on an empty link but we can hope there aren't any.

	// The cgi script only works on the live site so if testing locally leave the link alone.
	if (thelink.protocol != "file:")
	{
		// Munge the link (I did have pathname here but incredibly Safari doesn't seem to have it).
		// thelink.href = 'image.cgi?url='+thelink.href.replace(/.*lower-edmonton.co.uk\//, "")+';title='+n9title+';width='+width+';height='+height+';alt='+alt;
	 	// The link was made absolute in December 2004 to allow for change to a source hierarchy.
	 	// thelink.href = '/lower-edmonton.co.uk/support/images/image.cgi?url='+thelink.href.replace(/.*lower-edmonton.co.uk\//, "")+';title='+n9title+';alt='+alt;
	 	// thelink.href = '/lower-edmonton/support/images/image.cgi?url='+thelink.href.replace(/.*lower-edmonton\//, "")+';title='+n9title+';alt='+alt;
	 	
	 	// 15/3/2010.
	 	// I have suddenly become aware that thelink.href is returning absolute URLs and has broken the image handling. That probably
	 	// should have been the case all along and I am surprised browsers like FireFox ever worked. Using getAttribute
	 	// should consistently return the actual value from the HTML source and not an interpreted version.
	 	
	 	var newhref = '/lower-edmonton/support/images/image.cgi?url='+thelink.getAttribute('href')+';title='+n9title+';alt='+alt;
	       thelink.setAttribute('href', newhref);
	}
}


// 
// Display the preferred URL for the page as a link.
//
function n9bookmark()
{
	// var bookmark = self.location.href.replace(/.*lower-edmonton.co.uk\//, "http://lower-edmonton.co.uk/").replace(/\?.*/,"");
	// var bookmark = self.location.href.replace(/.*lower-edmonton\//, "http://lower-edmonton.co.uk/").replace(/\?.*/,"");
	
	// 15 Mar 2010.
	// This section needed fixing because of the introduction of the http://lower-edmonton.anidea.co.uk/ URL.
	var bookmark = self.location.href.replace(/.*\.anidea\.co\.uk\//, "http://lower-edmonton.co.uk/").replace(/\?.*/,"");
	document.write('<a class="n9top" href="'+bookmark+'">'+bookmark+'</a>');
}


//
// We have an HTML 4.01 validation image on the pages but we may also have some leakage of GoLive tags.
// So check for these tags and only display the image if they aren't present. Can leave the link there even if
// image invisible as it is handy for checking.
//
// OBSOLETED
//
/*
function n9validations()
{
	var golivetags = document.getElementsByTagName('csobj');
	// document.write('<br><span class="n9defluff">Note for webmaster: '+golivetags.length+'</span>');

	if (golivetags.length == 0) document.getElementById('n9html401').style.visibility = 'visible';
}
*/

//
// Set the body font size by the argument or from a cookie
//
function n9bodyfontsize(size)
{
	var mysize = size;
	
	if (mysize == '')
	{
		var cookies = document.cookie;
	
		var position = cookies.indexOf("n9bodyfontsize=");
		
		if (position != '-1')
		{
			var start = position +  15;
			var end = cookies.indexOf(';', start);
			if (end == -1) end = cookies.length;
			
			mysize = cookies.substring(start,end);
		}
	}
	
	if (mysize != '')
	{
		document.body.style.fontSize = mysize;

		var nextyear = new Date();
		nextyear.setFullYear(nextyear.getFullYear() + 1);
		
		// Need the domain and path now have a hierarchical site
		document.cookie = 'n9bodyfontsize=' + mysize + '; expires=' +  nextyear.toGMTString() + '; path=/lower-edmonton.co.uk/; domain=anidea.co.uk;';
	}
}


//
// Activate an alternate stylesheet.  Seems only one must be enabled at a time so make sure 
// all the rest are disabled.  I didn't have alternate stylesheets for long but I'll keep this
// around for possible future use.
//
function n9alternatestyle(title)
{
  	var i, linkelement;

  	for (i=0; (linkelement = document.getElementsByTagName("link")[i]); i++)
	{
    		if (linkelement.getAttribute("rel").indexOf("style") != -1 && linkelement.getAttribute("title"))
		{
      		linkelement.disabled = true;

			if (linkelement.getAttribute("title") == title) linkelement.disabled = false;
    		}
  	}
}


//
// Return today's date as a string with optional weekday on the front.
//
function n9date(longer)
{
	var now = new Date();
	var weekdays = [ "Sunday", "Monday", "Tuesday", "Wednesday",
			 "Thursday", "Friday", "Saturday", "Sunday" ];
	var weekday = weekdays[now.getDay()];
	var days = [ "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th",
			"11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th",
			"21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th", "31st" ];
	var day = days[now.getDate() - 1]; 
	var months = [ "January", "February", "March", "April", "May", "June", "July", "August",
			"September", "October", "November", "December" ];
	var month = months[now.getMonth()]; 
	var year = now.getFullYear(); 
	return (longer ? weekday + " " : "") + day + " " + month + " " + year; 
}

