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

// 
// This function was originally written to assign targets to links based on their classes but I'm no longer
// trying to differentiate between links in that way. However it is still used to call the routine to munge the 
// links for images.
//
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];

	       // document.write('<br><span class="n9defluff">Note for webmaster: href ['+d.href+']</span>');	
	       	
		// Make sure the link is for one of our images.
		if (d.className == "n9images" )
		{
				// Some links may now be to Fotopic so leave them alone (EEK! have to fix that...)
				if  ( d.href.slice(0,14) != "http://fotopic" )
				{
	       				// document.write('<br><span class="n9defluff">Note for webmaster: href ['+d.href+']</span>');	
						n9sethref(d);	// don't want a target at all but munge link
				}
		}
	}
}


//
// 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.
//
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;

	// 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')
	{
	       // 27/10/2011.
	       // No longer using Alt text.
	       //
		// 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:")
	{
	 	// 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.

	       // 27/10/2011.
	       // No longer using Alt text.	 	
	 	// var newhref = '/lower-edmonton/support/images/image.cgi?url='+thelink.getAttribute('href')+';title='+n9title+';alt='+alt;
	 	var newhref = '/lower-edmonton/support/images/image.cgi?url='+thelink.getAttribute('href')+';title='+n9title;
	       thelink.setAttribute('href', newhref);
	}
}


