// !! this file includes some fairly oldish functions (statusText, OpenPlainWindow, etc)
// !! we should rationalise it - miks


// print flag - can be used to prevent script execution when doc being printed via "print this page" jsp app.
var formatForPrint = (document.location.href.indexOf('print-version') != -1);


/* ----- DETECT STANDARDS-COMPLIANT BROWSERS ----- */
var W3CDOM = (document.createElement && document.getElementsByTagName);



// IDENTIFY BROWSER, VERSION AND OS
// FROM: http://www.quirksmode.org/js/detect.html
// I don't like browser detection, but some browsers cannot be reliably excluded by object detection, and need to be (eg - Mac IE)
// this script may look like overkill, but is quite ingenious (see url above) and reliable
// 08/2007 - THERE IS NOW A NEWER SCRIPT AVAILABLE AT THE SAME URL WHICH WE MIGHT WANT TO IMPLEMENT

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
if (checkIt('konqueror')) {
	browser = "Konqueror";
	OS = "Linux";
} 
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible')) {
	browser = "Netscape Navigator"
	version = detect.charAt(8);
} 
else browser = "An unknown browser";
if (!version) version = detect.charAt(place + thestring.length);
if (!OS) {
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
}
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
// prevent Mac IE from running scripts
if((browser == 'Internet Explorer') && (OS == 'Mac'))  W3CDOM = false;

/* ----- END DETECT STANDARDS-COMPLIANT BROWSERS ----- */




/* ----- added 26/03/2010 for SSO ----- */

function loadNonStaticHeader()
{
	document.getElementById('page-np-login').src='/login?originalURL=' + top.location.href;
}






/* ----- NEW - Sept 2009 ----------------------------------------------- */

function getIASection() {
	var urlpath = parent.location.pathname;				// get path
	urlpath = urlpath.substring(1);					// remove initial "/"
	var iaSection = urlpath.substring(0,urlpath.indexOf('/'));	// get first folder name in path
	//alert(iaSection)
	return iaSection;
}
/* ----- END NEW - Sept 2009 ------------------------------------------ */




/* ----- NEW FUNCTIONS - July 2007 ----------------------------------------------- */

/* ----- popup windows --------------------- */

function popup(url, windowName, width, height, scroll) {

	// NOTE: popup windows should always be resizable for various accessibility/usability reasons
	// NOTE: diff browsers display diff features - not always under developer control...

	var features = 'resizable=1,status=0,location=0' +
		',width=' + width +
		',height=' + height +
		',scrollbars=' + scroll

	return window.open(url,windowName,features);
}

function popupImage(url, windowName) {
	// need default windowName ??
	var thePopupImage = popup(url, windowName, 100, 100, 0);		// open tiny, as assume window will resize itself (so popupResize might be better name? - although current name is more transparent...)
	window.focus();													// focus current window to hide tiny initial popup (which should focus itself when loaded)
	return thePopupImage;
}

function popupSurvey(url, width, height) {
	return popup(url, 'survey', width, height, 1);
}

function popupTurningThePages(url) {
	return popup(url, 'TurningThePages', 950, 750, 1);
}




/* ----- get and set cookies ----------------- */
function getCookie(name) {
  var cookies = document.cookie;
  var start = cookies.indexOf(name + '=');
  if (start == -1) return null;
  var len = start + name.length + 1;
  var end = cookies.indexOf(';',len);
  if (end == -1) end = cookies.length;
  return unescape(cookies.substring(len,end));
}
function setCookie(name, value, expires, path, domain, secure) {
  value = escape(value);
  expires = (expires) ? ';expires=' + expires.toGMTString() :'';
  path    = (path)    ? ';path='    + path                  :'';
  domain  = (domain)  ? ';domain='  + domain                :'';
  secure  = (secure)  ? ';secure'                           :'';
  document.cookie = name + '=' + value + expires + path + domain + secure;
}

/* ----- create x-browser reqeust object ------ */
function createRequestObject() {
	var ro = null;
	try {
		ro = new ActiveXObject('Msxml2.XMLHTTP');
	} catch(e) {
		try {
			ro = new ActiveXObject('Microsoft.XMLHTTP');
		} catch(oc) {
			ro = null;
		}
	}
	if (!ro && typeof XMLHttpRequest != 'undefined') {
		ro = new XMLHttpRequest();
	}
	return ro;
}


/* ----- END NEW FUNCTIONS ------------------------------------------------------- */



// ----- GET ELEMENTS BY CLASS NAME -----
// FROM: http://www.dynamicdrive.com/dynamicindex17/switchcontent_dev.htm
function getElementsByClassName(classname) {
	ccollect=new Array();
	var inc=0;
	var alltags = document.all? document.all : document.getElementsByTagName("*");			// this is inefficient - should do test the other way around - but IE still inefficient if many elements anyway... - miks - 09/2009
	
	for (i=0; i<alltags.length; i++){
		//if (alltags[i].className==classname) ccollect[inc++]=alltags[i];			// this only handles single classes, not space-seperated
		if (alltags[i].className != undefined) {
			if (alltags[i].className.indexOf(classname) > -1) ccollect[inc++]=alltags[i];	// but this works - miks (except in Opera...until added the test for undefined)
		}
	}
	return ccollect;
}



// IMAGE ROLLOVER
function swap(img,isrc) {
	if (!document.images) return;
	document.images[img].src = isrc;
}

// POPUP WINDOW 
// - supply height/width as pixels
// - supply resize/scroll as 1 or 0
function OpenPlainWindow(url, windowName, width, height, resize, scroll) {
	var features =  'width=' + width +
		',height=' + height +
		',directories=0' +
		',location=0' +
		',menubar=0' +
		',scrollbars=' + scroll + 
		',toolbar=0' +
		',resizable=' + resize
	window.open(url,windowName,features);	
}

// STATUSBAR TEXT FUNCTION 
function statusText(text){
	window.setTimeout('window.status="' + text + '"', 1); 	// timeout allegedly fixes IE5 bug (?) - this works for nn (mac and pc) and iepc
	return true;
}
// ASSIGN A CSS CLASS
function setClass(theID, theClass){
		eval('document.all.' + theID + '.className = "' + theClass + '"');
}
// NAVIGATE WINDOW
function navToURL(theURL) {
	window.location = theURL;
}
// CREATE AN AUTO-RESIZING POPUP IMAGE LINK
function setUpPopup(theURL, theWinName) {
	varLinkString = "<a href=javascript:OpenPlainWindow('" + theURL + "','" + theWinName + "',100,100,1,0)>";
	document.write(varLinkString);
	//alert('varLinkString');
}
// CHANGE CSS CLASS 
function changeClassById(id,newClass) {
	if (document.getElementById) { document.getElementById(id).className  = newClass; }
}


// ADD ONLOAD HANDERS - allows attachment of multiple function calls to window.onload
// THIS IS OVERKILL REALLY - THERE ARE EASIER WAYS (see note below) - BUT IT WORKS...
// FROM: http://www.tek-tips.com/gfaqs.cfm/pid/216/fid/4862 "Code courtesy of jemminger"
// USEAGE:
//	- window.addOnload( foo );
//	- window.addOnload( function() { foo(arg); } );
//	- window.addOnload( function() { bar(a, b, c); } );
window.addOnload = function (fn) {
    if (!window.OnloadCache) window.OnloadCache = [];
    var ol = window.OnloadCache;
    //ol.push(fn);			// push method not supported in IE5...
    ol[ol.length] = fn;		// but this works fine - miks
}
window.onload = function () {
    var ol = window.OnloadCache;
    if (ol)
        for (var x = 0; x < ol.length; x++)
            ol[x]();
}

// NOTE - if use window.attachEvent and window.addEventListener, then above function poss redundant???






/*********************************************************

FOLLOWING SHOULD BE REMOVED - USE SEPEARTE SCRIPT FILE - swfobject/swfobject-v1-5.js
There is now also an updated version - swfobject/swfobject-v2.js
miks - 06/2008

**********************************************************/

/**
 * SWFObject v1.5: Flash Player detection and embed - http://blog.deconcept.com/swfobject/
 *
 * SWFObject is (c) 2007 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */
if(typeof deconcept=="undefined"){var deconcept=new Object();}if(typeof deconcept.util=="undefined"){deconcept.util=new Object();}if(typeof deconcept.SWFObjectUtil=="undefined"){deconcept.SWFObjectUtil=new Object();}deconcept.SWFObject=function(_1,id,w,h,_5,c,_7,_8,_9,_a){if(!document.getElementById){return;}this.DETECT_KEY=_a?_a:"detectflash";this.skipDetect=deconcept.util.getRequestParameter(this.DETECT_KEY);this.params=new Object();this.variables=new Object();this.attributes=new Array();if(_1){this.setAttribute("swf",_1);}if(id){this.setAttribute("id",id);}if(w){this.setAttribute("width",w);}if(h){this.setAttribute("height",h);}if(_5){this.setAttribute("version",new deconcept.PlayerVersion(_5.toString().split(".")));}this.installedVer=deconcept.SWFObjectUtil.getPlayerVersion();if(!window.opera&&document.all&&this.installedVer.major>7){deconcept.SWFObject.doPrepUnload=true;}if(c){this.addParam("bgcolor",c);}var q=_7?_7:"high";this.addParam("quality",q);this.setAttribute("useExpressInstall",false);this.setAttribute("doExpressInstall",false);var _c=(_8)?_8:window.location;this.setAttribute("xiRedirectUrl",_c);this.setAttribute("redirectUrl","");if(_9){this.setAttribute("redirectUrl",_9);}};deconcept.SWFObject.prototype={useExpressInstall:function(_d){this.xiSWFPath=!_d?"expressinstall.swf":_d;this.setAttribute("useExpressInstall",true);},setAttribute:function(_e,_f){this.attributes[_e]=_f;},getAttribute:function(_10){return this.attributes[_10];},addParam:function(_11,_12){this.params[_11]=_12;},getParams:function(){return this.params;},addVariable:function(_13,_14){this.variables[_13]=_14;},getVariable:function(_15){return this.variables[_15];},getVariables:function(){return this.variables;},getVariablePairs:function(){var _16=new Array();var key;var _18=this.getVariables();for(key in _18){_16[_16.length]=key+"="+_18[key];}return _16;},getSWFHTML:function(){var _19="";if(navigator.plugins&&navigator.mimeTypes&&navigator.mimeTypes.length){if(this.getAttribute("doExpressInstall")){this.addVariable("MMplayerType","PlugIn");this.setAttribute("swf",this.xiSWFPath);}_19="<embed type=\"application/x-shockwave-flash\" src=\""+this.getAttribute("swf")+"\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\" style=\""+this.getAttribute("style")+"\"";_19+=" id=\""+this.getAttribute("id")+"\" name=\""+this.getAttribute("id")+"\" ";var _1a=this.getParams();for(var key in _1a){_19+=[key]+"=\""+_1a[key]+"\" ";}var _1c=this.getVariablePairs().join("&");if(_1c.length>0){_19+="flashvars=\""+_1c+"\"";}_19+="/>";}else{if(this.getAttribute("doExpressInstall")){this.addVariable("MMplayerType","ActiveX");this.setAttribute("swf",this.xiSWFPath);}_19="<object id=\""+this.getAttribute("id")+"\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\" style=\""+this.getAttribute("style")+"\">";_19+="<param name=\"movie\" value=\""+this.getAttribute("swf")+"\" />";var _1d=this.getParams();for(var key in _1d){_19+="<param name=\""+key+"\" value=\""+_1d[key]+"\" />";}var _1f=this.getVariablePairs().join("&");if(_1f.length>0){_19+="<param name=\"flashvars\" value=\""+_1f+"\" />";}_19+="</object>";}return _19;},write:function(_20){if(this.getAttribute("useExpressInstall")){var _21=new deconcept.PlayerVersion([6,0,65]);if(this.installedVer.versionIsValid(_21)&&!this.installedVer.versionIsValid(this.getAttribute("version"))){this.setAttribute("doExpressInstall",true);this.addVariable("MMredirectURL",escape(this.getAttribute("xiRedirectUrl")));document.title=document.title.slice(0,47)+" - Flash Player Installation";this.addVariable("MMdoctitle",document.title);}}if(this.skipDetect||this.getAttribute("doExpressInstall")||this.installedVer.versionIsValid(this.getAttribute("version"))){var n=(typeof _20=="string")?document.getElementById(_20):_20;n.innerHTML=this.getSWFHTML();return true;}else{if(this.getAttribute("redirectUrl")!=""){document.location.replace(this.getAttribute("redirectUrl"));}}return false;}};deconcept.SWFObjectUtil.getPlayerVersion=function(){var _23=new deconcept.PlayerVersion([0,0,0]);if(navigator.plugins&&navigator.mimeTypes.length){var x=navigator.plugins["Shockwave Flash"];if(x&&x.description){_23=new deconcept.PlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/,"").replace(/(\s+r|\s+b[0-9]+)/,".").split("."));}}else{if(navigator.userAgent&&navigator.userAgent.indexOf("Windows CE")>=0){var axo=1;var _26=3;while(axo){try{_26++;axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+_26);_23=new deconcept.PlayerVersion([_26,0,0]);}catch(e){axo=null;}}}else{try{var axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");}catch(e){try{var axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");_23=new deconcept.PlayerVersion([6,0,21]);axo.AllowScriptAccess="always";}catch(e){if(_23.major==6){return _23;}}try{axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash");}catch(e){}}if(axo!=null){_23=new deconcept.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));}}}return _23;};deconcept.PlayerVersion=function(_29){this.major=_29[0]!=null?parseInt(_29[0]):0;this.minor=_29[1]!=null?parseInt(_29[1]):0;this.rev=_29[2]!=null?parseInt(_29[2]):0;};deconcept.PlayerVersion.prototype.versionIsValid=function(fv){if(this.major<fv.major){return false;}if(this.major>fv.major){return true;}if(this.minor<fv.minor){return false;}if(this.minor>fv.minor){return true;}if(this.rev<fv.rev){return false;}return true;};deconcept.util={getRequestParameter:function(_2b){var q=document.location.search||document.location.hash;if(_2b==null){return q;}if(q){var _2d=q.substring(1).split("&");for(var i=0;i<_2d.length;i++){if(_2d[i].substring(0,_2d[i].indexOf("="))==_2b){return _2d[i].substring((_2d[i].indexOf("=")+1));}}}return "";}};deconcept.SWFObjectUtil.cleanupSWFs=function(){var _2f=document.getElementsByTagName("OBJECT");for(var i=_2f.length-1;i>=0;i--){_2f[i].style.display="none";for(var x in _2f[i]){if(typeof _2f[i][x]=="function"){_2f[i][x]=function(){};}}}};if(deconcept.SWFObject.doPrepUnload){if(!deconcept.unloadSet){deconcept.SWFObjectUtil.prepUnload=function(){__flash_unloadHandler=function(){};__flash_savedUnloadHandler=function(){};window.attachEvent("onunload",deconcept.SWFObjectUtil.cleanupSWFs);};window.attachEvent("onbeforeunload",deconcept.SWFObjectUtil.prepUnload);deconcept.unloadSet=true;}}if(!document.getElementById&&document.all){document.getElementById=function(id){return document.all[id];};}var getQueryParamValue=deconcept.util.getRequestParameter;var FlashObject=deconcept.SWFObject;var SWFObject=deconcept.SWFObject;




/*********************************************************

FOLLOWING SCRIPTS COPIED FROM popup.js

LATTER FILE SHOULD NEVER HAVE EXISTED INDEPENDENTLY OF 
THIS ONE AND ALL REFERENCES TO IT CAN BE REMOVED

*********************************************************/


// the functions in this file require the supplementary library lib.js [ this appears not to be true - or maybe it is true, as lib is reproduced in nhm.js - miks - 06/2006 ]

// These defaults should be changed the way it best fits your site
var _POPUP_FEATURES = 'width=400,height=350,menubar=false,scrollbars=false,toolbar=false,resizable=false';

function raw_popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, features) {
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

function event_popup(e) {
    // to be passed as an event listener
    // pops up a window grabbing the url from the event source's href
    link_popup(e.currentTarget);
    e.preventDefault();
}

function event_popup_features(features) {
    // generates an event listener similar to event_popup, but allowing window features
    return function(e) { link_popup(e.currentTarget, features); e.preventDefault() }
}



/* 
following code (to end of file) added for print version (and other?) functionality 
looks like overkill to me for that purpose, but the functions do look useful - miks
*/

/*

This file contains only functions necessary for the article features
The full library code and enhanced versions of the functions present
here can be found at http://v2studio.com/k/code/lib/


ARRAY EXTENSIONS

push(item [,...,item])
    Mimics standard push for IE5, which doesn't implement it.


find(value [, start])
    searches array for value starting at start (if start is not provided,
    searches from the beginning). returns value index if found, otherwise
    returns -1;


has(value)
    returns true if value is found in array, otherwise false;


FUNCTIONAL

map(list, func)
    traverses list, applying func to list, returning an array of values returned
    by func

    if func is not provided, the array item is returned itself. this is an easy
    way to transform fake arrays (e.g. the arguments object of a function or
    nodeList objects) into real javascript arrays.

    map also provides a safe way for traversing only an array's indexed items,
    ignoring its other properties. (as opposed to how for-in works)

    this is a simplified version of python's map. parameter order is different,
    only a single list (array) is accepted, and the parameters passed to func
    are different:
    func takes the current item, then, optionally, the current index and a
    reference to the list (so that func can modify list)


filter(list, func)
    returns an array of values in list for which func is true

    if func is not specified the values are evaluated themselves, that is,
    filter will return an array of the values in list which evaluate to true

    this is a similar to python's filter, but parameter order is inverted


DOM

getElem(elem)
    returns an element in document. elem can be the id of such element or the
    element itself (in which case the function does nothing, merely returning
    it)

    this function is useful to enable other functions to take either an    element
    directly or an element id as parameter.

    if elem is string and there's no element with such id, it throws an error.
    if elem is an object but not an Element, it's returned anyway


hasClass(elem, className)
    Checks the class list of element elem or element of id elem for className,
    if found, returns true, otherwise false.

    The tested element can have multiple space-separated classes. className must
    be a single class (i.e. can't be a list).


getElementsByClass(className [, tagName [, parentNode]])
    Returns elements having class className, optionally being a tag tagName
    (otherwise any tag), optionally being a descendant of parentNode (otherwise
    the whole document is searched)


DOM EVENTS

listen(event,elem,func)
    x-browser function to add event listeners

    listens for event on elem with func
    event is string denoting the event name without the on- prefix. e.g. 'click'
    elem is either the element object or the element's id
    func is the function to call when the event is triggered

    in IE, func is wrapped and this wrapper passes in a W3CDOM_Event (a faux
    simplified Event object)


mlisten(event, elem_list, func)
    same as listen but takes an element list (a NodeList, Array, etc) instead of
    an element.


W3CDOM_Event(currentTarget)
    is a faux Event constructor. it should be passed in IE when a function
    expects a real Event object. For now it only implements the currentTarget
    property and the preventDefault method.

    The currentTarget value must be passed as a paremeter at the moment    of
    construction.


MISC CLEANING-AFTER-MICROSOFT STUFF

isUndefined(v)
    returns true if [v] is not defined, false otherwise

    IE 5.0 does not support the undefined keyword, so we cannot do a direct
    comparison such as v===undefined.
*/

// ARRAY EXTENSIONS

if (!Array.prototype.push) Array.prototype.push = function() {
    for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
    return this.length;
}

Array.prototype.find = function(value, start) {
    start = start || 0;
    for (var i=start; i<this.length; i++)
        if (this[i]==value)
            return i;
    return -1;
}

Array.prototype.has = function(value) {
    return this.find(value)!==-1;
}

// FUNCTIONAL

function map(list, func) {
    var result = [];
    func = func || function(v) {return v};
    for (var i=0; i < list.length; i++) result.push(func(list[i], i, list));
    return result;
}

function filter(list, func) {
    var result = [];
    func = func || function(v) {return v};
    map(list, function(v) { if (func(v)) result.push(v) } );
    return result;
}


// DOM

function getElem(elem) {
    if (document.getElementById) {
        if (typeof elem == "string") {
            elem = document.getElementById(elem);
            if (elem===null) throw 'cannot get element: element does not exist';
        } else if (typeof elem != "object") {
            throw 'cannot get element: invalid datatype';
        }
    } else throw 'cannot get element: unsupported DOM';
    return elem;
}

function hasClass(elem, className) {
    return getElem(elem).className.split(' ').has(className);
}

function getElementsByClass(className, tagName, parentNode) {
    parentNode = !isUndefined(parentNode)? getElem(parentNode) : document;
    if (isUndefined(tagName)) tagName = '*';
    return filter(parentNode.getElementsByTagName(tagName),
        function(elem) { return hasClass(elem, className) });
}


// DOM EVENTS

function listen(event, elem, func) {
    elem = getElem(elem);
    if (elem.addEventListener)  // W3C DOM
        elem.addEventListener(event,func,false);
    else if (elem.attachEvent)  // IE DOM
        elem.attachEvent('on'+event, function(){ func(new W3CDOM_Event(elem)) } );
        // for IE we use a wrapper function that passes in a simplified faux Event object.
    else throw 'cannot add event listener';
}

function mlisten(event, elem_list, func) {
    map(elem_list, function(elem) { listen(event, elem, func) } );
}

function W3CDOM_Event(currentTarget) {
    this.currentTarget  = currentTarget;
    this.preventDefault = function() { window.event.returnValue = false }
    return this;
}


// MISC CLEANING-AFTER-MICROSOFT STUFF

function isUndefined(v) {
    var undef;
    return v===undef;
}

