// University of New England JavaScript Document //


function launchMap(Place){
  var win = null;
  w = 700;
  h = 450;
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings = 'scrollbars=yes, resizable=no, height= '+h+',width= '+w+',top='+TopPosition+',left='+LeftPosition+''
  map = window.open('map.html?'+Place,'Map', settings);

}

function launchPopup(filename){
  var win = null;
  w = 600;
  h = 500;
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings = 'scrollbars=yes, resizable=no, height= '+h+',width= '+w+',top='+TopPosition+',left='+LeftPosition+''
  map = window.open(filename,'Popup', settings);

}


// Macromedia scripts //

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



// Popup from A List Apart
function raw_popup(url, target, 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) {
  return
    raw_popup(src.getAttribute('href'),
    src.getAttribute('target') || '_blank',
    features);
}

//******************************************************************************************************************
// General useful DOM manipulation classes from http://www.hrcerqueira.com/my-dom-handling-methods-re-designed/

function hasClass(ele,cls) {
	return ele ? ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)')) : false;
}

function addClass(ele,cls) {
        if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}

function removeClass(ele,cls) {
        if (hasClass(ele,cls)) {
                var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
                ele.className=ele.className.replace(reg,' ');
        }
}

// Event attacher
// Commented out as this function name conflicts with one used within jQuery which uPortal uses
/*Object.prototype.addEvent = function(evtType, func) {
   if (this.addEventListener) { //gecko
      this.addEventListener(evtType, func, true);
   } else if (this.attachEvent) { //ie
      this.attachEvent('on' + evtType, func);
   } else { //all the others
      this['on' + evtType] = func;
   }
}*/


// Element removal
// I know that for this job there is already a function called removeChild, but because our little friend memory leaking bug in IE, we need a little retouching.
//removed on 18/11/2009 by Matthew Debus and Ben Funnell because the getElementsByTagName was causeing an error in all browsers and it was blocking event handling in IE.
/* Object.prototype.removeElement = function(oldElement) {

   var childElements = oldElement.getElementsByTagName('*');
 
   for (var i = 0; i < childElements.length; i++) { //purge all elements contained in the element to remove
      purge(childElements[i]);
   }
 
   purge(oldElement); //purge the element to remove
   this.removeChild(oldElement); //remove it
 
   //function that came from Douglas Crockford site, that removes all the event handlers from de elements
   function purge(d) {
      var a = d.attributes, i, l, n;
      if (a) {
          l = a.length;
          for (i = 0; i < l; i += 1) {
              n = a[i].name;
              if (typeof d[n] === 'function') {
                  d[n] = null;
              }
          }
      }
      a = d.childNodes;
      if (a) {
          l = a.length;
          for (i = 0; i < l; i += 1) {
              purge(d.childNodes[i]);
          }
      }
   }

} */

//************************************************************************************************************************
//Other useful functions

//Simply add a class name to the beginning of the funciton and the 2nd and 3rd arguments are optional and the magic is done for you!
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	//var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		//if ( pattern.test(els[i].className) ) {
		if ( hasClass(els[i], searchClass) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

//******************************************************************
// Cookie functions from http://www.quirksmode.org/js/cookies.html

function createCookie(name,value,minutes) {
	if (minutes) {
		var date = new Date();
		date.setTime(date.getTime()+(minutes*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function readCookieAsArray() {
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		while (ca[i].charAt(0)==' ') ca[i] = ca[i].substring(1,ca[i].length);
	}
	return ca;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function includeJavascript(src) {
    if (document.createElement && document.getElementsByTagName) {
        var head_tag = document.getElementsByTagName('head')[0];
        var script_tag = document.createElement('script');
        script_tag.setAttribute('type', 'text/javascript');
        script_tag.setAttribute('src', src);
        head_tag.appendChild(script_tag);
    }
}

function includeCSSfile(href) {
    if (document.createElement && document.getElementsByTagName) {
	    var head_node = document.getElementsByTagName('head')[0];
	    var link_tag = document.createElement('link');
	    link_tag.setAttribute('rel', 'stylesheet');
	    link_tag.setAttribute('type', 'text/css');
	    link_tag.setAttribute('href', href);
	    head_node.appendChild(link_tag);
	}
}

function windowSize(dim) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if (dim == 'x' || dim == 'X' || dim == 'width') return myWidth;
  else if (dim == 'y' || dim == 'Y' || dim == 'height') return myHeight;
}
