// =============================================================================================================================================================================
// File: 								popup.js
// Associated files:		gem-rels.js / photo-rels.js
//											add-event.js
// Functions listed:		getElementsByClassName
//											closeWin
//											popUpWin
//											doPopUp
//											findPopUps
// Description: 				This file enables designated 'anchor' and 'map area' elements to bring up a popup window of specified dimensions when clicked to show a close-up photo.
//											If javascript is disabled, the close-up photo appears in a new document with a return link - so fails gracefully. 
// Original Author(s): 	Idea taken and modified from tutorial by Ian Lloyd from website http://www.accessify.com/features/tutorials/the-perfect-popup/.
//											Ian Lloyd's own website: http://lloydi.com/.
// Modifier:						Modified by Derek Norcott to standardise notation and to use: getElementsByClassName function (developed by Robert Nyman) to get details stored
//                      in an array instead of the 'rel' attribute - as this is no longer used for the '<map> area' attribute.
// =============================================================================================================================================================================


var oNewWindow = null;


// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Function: 						getElementsByClassName
// Description: 				Acquires Element by specifying both the tagname and the classname.
// Author:					Developed by Robert Nyman, http://www.robertnyman.com
// Code/licensing: 		Open Source Initiative OSI - The MIT License:Licensing [OSI Approved License] The MIT License - http://code.google.com/p/getelementsbyclassname/
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Function: 						closeWin
// Description: 				Forces a close of window.
// Original Author(s): 	Idea taken and modified from tutorial by Ian Lloyd from website http://www.accessify.com/features/tutorials/the-perfect-popup/.
//											Ian Lloyd's own website: http://lloydi.com/.
// Modifier:						Modified by Derek Norcott to standardise notation.
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function closeWin(){
	if (oNewWindow != null) {
		if(!oNewWindow.closed)
			oNewWindow.close();
	}
}


// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Function: 						popUpWin
// Description: 				After closing any exiting popups, determines the dimensions and display options of the new popup and then opens it.
// Original Author(s): 	Idea taken and modified from tutorial by Ian Lloyd from website http://www.accessify.com/features/tutorials/the-perfect-popup/.
//											Ian Lloyd's own website: http://lloydi.com/.
// Modifier:						Modified by Derek Norcott to standardise notation.
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function popUpWin(sURL, sType, sWidth, sHeight) {
	closeWin();
	sType = sType.toLowerCase();
	if (sType == "fullscreen") {
		sWidth = screen.availWidth;
		sHeight = screen.availHeight;
	}
	
	var sTools="";
	if (sType == "standard") sTools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+sWidth+",height="+sHeight+",top=0,left=0";
	if (sType == "console" || sType == "fullscreen") sTools = "resizable,toolbar=no,location=no,scrollbars=no,width="+sWidth+",height="+sHeight+",left=0,top=0";
	oNewWindow = window.open(sURL, 'newWin', sTools);
	oNewWindow.focus();
}


// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Function: 						doPopUp
// Description: 				Extracts the associated dimension and display attributes for the selected popup (originally defined in the 'rel' attribute, now defined in an array).
// Original Author(s): 	Idea taken and modified from tutorial by Ian Lloyd from website http://www.accessify.com/features/tutorials/the-perfect-popup/.
//											Ian Lloyd's own website: http://lloydi.com/.
// Modifier:						Modified by Derek Norcott to standardise notation and also to replace the 'rel' attributes with a separately-defined 'rel' array of values.
//											The reason this was necessary was because the 'rel' attribute is deprecated for the <area> tag, and may later be deprecated for the <a> tag.
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function doPopUp(e) {
	//set defaults - if nothing in rel attrib, these will be used
	var sType 	= "standard";
	var sWidth 	= "780";
	var sHeight = "580";
	
	//
	var s_href 	 = this.href;
	var s_id		 = this.id;
	var nPointer = Number(s_id);
	
	//look for parameters
	var asAttribs = gasRels[nPointer].split(" ");
	if (asAttribs[1]!=null) {sType   = asAttribs[1];}
	if (asAttribs[2]!=null) {sWidth  = asAttribs[2];}
	if (asAttribs[3]!=null) {sHeight = asAttribs[3];}
	
	//call the popup script
	popUpWin(s_href, sType, sWidth, sHeight);
	
	//cancel the default link action if pop-up activated
	if (window.event) {
		window.event.returnValue  = false;
		window.event.cancelBubble = true;
	}
	else if (e) {
		e.stopPropagation();
		e.preventDefault();
	}
}


// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Function: 						findPopUps
// Description: 				Finds all elements that have the class: 'popup' and attaches an onclick eventhandler, plus defines the window dimensions.
// Original Author(s): 	Idea taken and modified from tutorial by Ian Lloyd from website http://www.accessify.com/features/tutorials/the-perfect-popup/.
//											Ian Lloyd's own website: http://lloydi.com/.
// Modifier:						Notation standardised by Derek Norcott and some modifications made.
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function findPopUps() {

	var aoPopups = getElementsByClassName("popup", gsTag, document);
	for (i=0; i<aoPopups.length; i++)	{
	
		// attach popup behaviour
		aoPopups[i].onclick = doPopUp;
		
		// add popup indicator
		aoPopups[i].style.backgroundPosition = "0 center";
		aoPopups[i].style.backgroundRepeat = "no-repeat";
		aoPopups[i].style.paddingLeft = "15px";
		
		// add info to title attribute to alert fact that it's a pop-up window
		if (aoPopups[i].alt) {
			aoPopups[i].title = aoPopups[i].alt + " [Opens in pop-up window]";
		}
		else {
			aoPopups[i].title = "[Opens in pop-up window]";
		}
	}
}


addEventHandler(window, 'load', findPopUps, false);

