﻿/* Standard Javascript functions library for IMG projects
written (unless otherwise stated) and compiled by Niall King */


// library to replace window.onload
// addLoadEvent waits till the whole page has loaded, inluding images, video and so forth.
// addDOMLoadEvent only waits for the DOM to load. 
// Where your script does not need to wait for the layout to complete you can use this to speed things up
// http://simonwillison.net/2004/May/26/addLoadEvent/

function addLoadEvent(func) { 
	var o = window.onload; 
		if (typeof window.onload != 'function') { 
			window.onload = func; 
		} else { 
			window.onload = function() { 
				if (o) { 
				o(); 
			} 
			func(); 
		} 
	} 
} 
 
addDOMLoadEvent=(function(){var e=[],t,s,n,i,o,d=document,w=window,r='readyState',c='onreadystatechange',x=function(){n=1;clearInterval(t);while(i=e.shift())i();if(s)s[c]=''};return function(f){if(n)return f();if(!e[0]){d.addEventListener&&d.addEventListener("DOMContentLoaded",x,false);/*@cc_on@*//*@if(@_win32)d.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");s=d.getElementById("__ie_onload");s[c]=function(){s[r]=="complete"&&x()};/*@end@*/if(/WebKit/i.test(navigator.userAgent))t=setInterval(function(){/loaded|complete/.test(d[r])&&x()},10);o=w.onload;w.onload=function(){x();o&&o()}}e.push(f)}})();


/* Cookie functions */
function getCookie(cookieName){
	var cookieJar=document.cookie.split("; ");
	for(var x=0;x<cookieJar.length;x++){
		var oneCookie=cookieJar[x].split("=");
		if(oneCookie[0]==escape(cookieName)){
			return unescape(oneCookie[1]);
		}
	}
	return null;
}

// general add event function
function add(event, body, d) {
	if (event.addEventListener) { return event.addEventListener(body, d,false); }
	if (event.attachEvent) { return event.attachEvent('on'+body, d); }
}


function setCookie(cookieName,cookieValue,lifeTime,path,domain,isSecure){
	if(!cookieName){
		return false;
	}
	if(lifeTime=="delete"){
		lifeTime=-10;
	}
	document.cookie=escape(cookieName)+"="+escape(cookieValue)+(lifeTime?";expires="+(new Date((new Date()).getTime()+(1000*lifeTime))).toGMTString():"")+(path?";path="+path:"")+(domain?";domain="+domain:"")+(isSecure?";secure":"");
	if(lifeTime<0){
		if(typeof(getCookie(cookieName))=="string"){
			return false;
		}return true;
	}
	if(typeof(getCookie(cookieName))=="string"){
		return true;
	}
	return false;
}

function deleteCookie(cookieName) {
	setCookie(cookieName, "", "delete");
}

/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}

NDownloadUrl = function(url, func) {
/*
	This function replicates the google map GDownloadURL functionality for ajax file download.
	url    : Call url
	func   : custom function which is used to process returned data
*/
	var httpObj=null;     
	if (window.XMLHttpRequest) { // proper browser
		httpObj=new XMLHttpRequest();
	}
	else if (window.ActiveXObject) { // IE6 and older
		httpObj=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (httpObj!=null){
		httpObj.open('GET', url, true);
		httpObj.onreadystatechange = function() {
			if(httpObj.readyState == 4){ // 'loaded'
				if (httpObj.status == 200) { // 'OK'
					var contenttype = httpObj.getResponseHeader('Content-Type');
					if (contenttype.indexOf('xml')>-1) {
						func(httpObj.responseXML);
					} else {
						func(httpObj.responseText);
					}
				} else {
					func('Error: '+httpObj.status);
			   }
			}
		};
		httpObj.send(null);
	} else {
		// browser cannot do ajax.
	}
}

/* Simple and horrible JS popup window */

function popup(url,height,width) {
	window.open(url,'videowindow', 'menubar=no,location=no,resizable=no,scrollbars=no,status=no,width='+width+',height='+height);
	return false;
}

function getNumbers(idstr) { // extracts only numbers from a string.
    return idstr.replace(/[^0-9]/g, '');
}

// get Elements By class name. returns an array of objects
// params are class name, an optional node to search (default is document), an optional tag name to restrict it further
function getElementsByClass(searchClass, node, tag) {
    var classElements = new Array();
    if (node == null) node = document;
	if (tag == null) tag = '*';
    var els = node.getElementsByTagName(tag); // use "*" for all elements
    var elsLen = els.length;
    var pattern = new RegExp("\\b"+searchClass+"\\b");
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

// a Prototype-style dollar function
// can take in a string or an object
// you can pass it as many params as you like.
// it returns either an object or an array of objects
//function $() {
//	var elements = new Array();
//	for (var i = 0; i < arguments.length; i++) {
//		var element = arguments[i];
//		if (typeof element == 'string')
//			element = document.getElementById(element);
//		if (arguments.length == 1)
//			return element;
//		elements.push(element);
//	}
//	return elements;
//}

// but as we are only ever using it for a getElementById substitute, this is all we need.
function $(element) {
    return document.getElementById(element);
}
