
///////////////////////// LIBRARY SCRIPT REFERENCED FOR GALLERY ////////////////////////


// All obsolete library scripts removed 13.4.10


// ADJUSTED + ACTIVE LIBRARY SCRIPTS


//Get cookie routine by Shelley Powers - used in cookie functions and global variables 
function get_cookie(Name) {
	var search = Name + "=";
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);
		// if cookie exists
		if (offset != -1) { 
			offset += search.length;
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) {
				end = document.cookie.length;
			}
			returnvalue=unescape(document.cookie.substring(offset, end));
		}
	}
	return returnvalue;
}


/////////////////////////// END OF LIBRARY SCRIPT //////////////////////////



////////////////////////*/  WINGFINGER GALLERY SCRIPTS /*/////////////////////////////



// DISCRETE SET UP FOR JS GALLERY //

//Mask the content while php assembles
function firstoff() {
var phpmask = document.getElementById("maincontent"); 
Core.addClass(phpmask,"mask");
// Sitepoint Core function
}



// GLOBAL DECLARATIONS


// sets the gallery folder so the images can be found.
var galleryfolder = "gallery_1";

var pic = [];
var maxitems = 15; //human
var maxsubitems = 4; //human, inc main one = 0

var imax= 12; 
var cat=1;
var step=0;
var subnum=0;
var xlink=false;
var currentpic="";


// this replaces the longwinded "manual" system used previously to set up the array
for (var i1=0;i1<maxitems;i1++) {
	pic[i1] = [,];
	for (var i2=0;i2<=maxsubitems;i2++) {
		pic[i1][i2] = [,];
		for (var i3=0;i3<4;i3++) {
			pic[i1][i2][i3]="";
		}
	}
}
	


if (get_cookie("wingfinger_step")!=""){
	step = eval(get_cookie("wingfinger_step"));
	cat = eval(get_cookie("wingfinger_cat"));
	xlink = eval(get_cookie("wingfinger_xlink"));  //true if gallery is being accessed from another page
}
else{
	cat=1;
	step=0;
	subnum=0;
}
// This sets up the first view of the gallery on arriving - dependent on whether you've come in to a specific step from another link, looked at it before etc, i.e. has there been a cookie created previously


// FINAL SET UP FOR JAVASCRIPT-FRIENDLY GALLERY PAGE
var galHide =
{

	init: function() 
	{
		var body = document.getElementsByTagName("body")[0]; 
		body.setAttribute("id","jsStyle");
		
		//Find the 'nojs' class and change it to 'hidden'
		var changer = Core.getElementsByClass("nojs");
		
		//for each of the occurences of class 'nojs'			
		for (var i = 0; i < changer.length; i++) {
			//take off the nojs class
			Core.removeClass(changer[i], "nojs");
			//for each item add the class hidden
			Core.addClass(changer[i], "hidden");
		}
		
		// If it's the first item needing to be shown, take the hidden off 
		if (changer[step]) {
			Core.removeClass(currentpic,"hidden");
		}
		
		//Take off the masking class from firstoff function
		var phpmask = document.getElementById("maincontent"); 
		Core.removeClass(phpmask,"mask");
	}
	
};

Core.start(galHide);
// Using sitepoint.com Core.js to delay running til page loaded



// displays the right number of sub-buttons and highlights the current one
function sortsubtns(){

	// for each occurence of n, up to 4 (we only have 3 sub-images)
	for (var n=0;n<4;n++){ 
		
		var subname="subtn-" + n; //this is how we target the sub button
		var targetbtn = document.getElementById(subname);
		var nosubs = document.getElementById("galsubtns");
		
		//if n is not an empty string
		if (pic[step][n][0] !== ""){
			Core.removeClass(nosubs,"hidden"); // this removes the hidden class if the previous set did not have any subtns
			Core.removeClass(targetbtn,"hidden"); // this removes any hiddens from the previous step
			
			//Work out what state the button shows
			if (n==subnum) {
				// indicate that this is the current sub-image
				Core.addClass(targetbtn,"subtnon");
			}
			else {
				Core.removeClass(targetbtn,"subtnon");
			}
		}
		//if n is NOT not an empty string, hide it
		else {
		Core.addClass(targetbtn,"hidden");
		}
		
		// If the first sub item ie n=1 is an empty string, hide the whole subtn area
		if (pic[step][1][0] == ""){
		Core.addClass(nosubs,"hidden");
		}

	}
	
}


// Adjusted from MWJ library to make the gallery work
function getRefToDiv(divID,oDoc) {
	if( document.getElementById ) { return document.getElementById(divID); }
	if( document.all ) { return document.all[divID]; }
	if( !oDoc ) { oDoc = document; }
	if( document.layers ) {
		if( oDoc.layers[divID] ) {
			return oDoc.layers[divID];
		} else {
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
				y = getRefToDiv(divID,oDoc.layers[x].document);
			}
			return y;
		}
	}
	return false;
} 

// Adjusted funcion to change gallery step number	
function captionswap(divID_as_a_string) {
	var myReference = getRefToDiv(divID_as_a_string);
	if( !myReference ) { 
		window.alert('Nothing works in this browser'); 
		return; 
	}
	if( myReference.innerHTML ) { 
		myReference.innerHTML = ('<p>' + (step+1) +' / ' + (imax+1) +'<\/p>'); 
	} 
	else {
		if( myReference.document && myReference.document != window.document ) {
			myReference.document.open();
			myReference.document.write('<p>' + (step+1) +' / ' + (imax+1) +'<\/p>');
			myReference.document.close();
		} 
		else {
			window.alert('Your browser does not allow the contents to be re-written.'); 
			return; 
		}
    } 
}




// Changed for new JS friendly gallery to read php content on page as well
function showpic(){
	
	// For testing!!
	// alert("cat=" + cat + "  step=" + step + "  subnum=" + subnum + "  " + ('item-'+ step + '-' + subnum) + "  " + imax);
	var arrowForward = document.getElementById("forward");  
	var arrowBack = document.getElementById("back");  
	
	if (!document.images) {
		return;
	}
	if (step==imax) {
		Core.addClass(arrowForward,"arrowoff");
	}
	else {
		Core.removeClass(arrowForward,"arrowoff");
	}
	if (step===0) {
		Core.addClass(arrowBack,"arrowoff");
	}
	else {
		Core.removeClass(arrowBack,"arrowoff");
	}
	sortsubtns();

	//New bits to make use of images on the page from non-js set up
	if (currentpic != "") {
	Core.addClass(currentpic,"hidden");
	}
	
	if (subnum === 0) {
		currentpic=document.getElementById("item-" + step + "-" + subnum);
	}
	else {
		currentpic=document.getElementById("subItem-" + step + "-" + subnum);
	}
	Core.removeClass(currentpic,"hidden");
	
	captionswap("galstep");

	document.cookie="wingfinger_step="+step;
	document.cookie="wingfinger_cat="+cat;
	
}



// makes the arrow buttons work in their appropriate direction (and to show appropriate button arrow)	
function sortbtns(direction){
	if (!document.images) {
		return;
	}
	subnum=0;
	if (direction=="right"){
		if (step==imax) {
			return;
		}
		step++;
	}
	else {
		//direction is left
		if (step===0) {
			return;
		}
		step--;
	}
	showpic();
}







// this sets the cookie routines going when you leave the page	
function goingNow(){
	document.cookie="wingfinger_step="+step;
	document.cookie="wingfinger_cat="+cat;
	document.cookie="wingfinger_xlink="+xlink;  
		//true if gallery is being accessed from another page
}



// Pulls everything together. Fills pic[] with correct pics and captions for category cat, sets imax and displays first image.
function runit(xcat,xstep) {

	cat=xcat;
	step=xstep;
	
	for (var i1=0;i1<11;i1++) {
		for (var i2=0;i2<5;i2++) {
			for (var i3=0;i3<4;i3++) {
				pic[i1][i2][i3]="";
			}
		}
	}
	switch(cat){
		case 1:
		runit_1();
		break;
		
		case 2:
		runit_2();
		break;
		
		case 3:
		runit_3();
		break;
		
		case 4:
		runit_4();
		break;
		
		case 5:
		runit_5();
		break;
	}
	
	subnum=0;
	showpic();
}


/////////////////////////// END OF GALLERY SCRIPTS ////////////////////////





/////////////////////// CONTACT RELATED SCRIPTS ///////////////////

function emailDiv(divID_as_a_string) {
// for hiding email address from robots	
var part2 = "wingfinger.co.uk";
var at = "@";
var part1 = "pteri";
	var myReference = getRefToDiv(divID_as_a_string);
	if( !myReference ) { window.alert('Nothing works in this browser'); return; }
	if( myReference.innerHTML ) { myReference.innerHTML = (part1+at+part2); } else {
		if( myReference.document && myReference.document != window.document ) {
			myReference.document.open();
			myReference.document.write(part1+at+part2);
			myReference.document.close();
		} else {
			window.alert('Your browser does not allow the contents to be re-written.\nI have not implemented an iframe here.\nSee my generic re-writable elements example for how this should be done.'); return; }
    }
}



function linkDiv(divID_as_a_string) {
// for hiding address in mailto from robots	
var part2 = "wingfinger.co.uk";
var at = "@";
var part1 = "pteri";
	var myReference = getRefToDiv(divID_as_a_string);
	if( !myReference ) { window.alert('Nothing works in this browser'); return; }
	if( myReference.innerHTML ) { myReference.innerHTML = ('<a href="mailto:'+part1+at+part2+'">Email</a>'); } else {
		if( myReference.document && myReference.document != window.document ) {
			myReference.document.open();
			myReference.document.write('<a href="mailto:'+part1+at+part2+'">Email</a>');
			myReference.document.close();
		} else {
			window.alert('Your browser does not allow the contents to be re-written.\nI have not implemented an iframe here.\nSee my generic re-writable elements example for how this should be done.'); return; }
    }
}


function generate_address( part1, part2 ) {
// more email hiding
	var atsign = "&#64;";
	var addr = part1 + atsign + part2;
	document.write( "<" + "a" + " " + "href=" + "mail" + "to:" + addr + ">" +addr +"<\/a>");
}


function generate_email(part1, part2, linktext, x) {
// more email hiding
	var atsign = "&#64;";
	var addr = part1 + atsign + part2;
	document.write( "<a " + "href=" + "\"mail" + "to:" + addr + x +"\">" +linktext +"<\/a>");
}







/////////////////////// EXTERNAL LINK SCRIPT - references core.js  ///////////////////

// Adapted from www.phpBB-SEO.com SEO TOOLKIT

var extLink =
	{

		init: function() 
	
		{
	
		   
		   var current_domain = document.domain; 
		   
		   if (!current_domain || !document.getElementsByTagName) return;
		   
		   var aTag = new Array;
		   var aTagLength = 0;
		   var aTagInner = '';
		   
		   aTag = document.getElementsByTagName("a");
		   // Brings in each a tag to an array
		   aTagLength = aTag.length;
		   
		   for (var i = 0; i < aTagLength; i++) 
		   // Going through each item in the array up to the final one it has found
		   
			   {
					aTagInner = aTag[i].innerHTML.toLowerCase();
					// So that indexOf is able to read it and return a value
					
					if ( (aTag[i].href == '') || (aTag[i].href.indexOf('javascript') >=0 ) || (aTagInner.indexOf('<a') >= 0) || (aTag[i].href.indexOf(current_domain) >= 0) || (aTag[i].onclick != null) || (aTag[i].href.indexOf('mailto') >= 0))
						{
							continue;
						}
						// If any of these exceptions are met, the function will not be applied	

					
					// Otherwise, open a new window on mouse click...
					aTag[i].onclick = function () { window.open(this.href); return false; };
				
				}
		
		}
	
	};

Core.start(extLink);
// Using sitepoint.com structure via their core.js to stop the script running until html page is loaded