/**************************
 * hum javascript library *
 **************************/
 
///////////////////////
// hum utility object
var hum = {
    ELEMENT_NODE: 1,
    ATTRIBUTE_NODE: 2,
    TEXT_NODE: 3,
    CDATA_SECTION_NODE: 4,
    ENTITY_REFERENCE_NODE: 5,
    ENTITY_NODE: 6,
    PROCESSING_INSTRUCTION_NODE: 7,
    COMMENT_NODE: 8,
    DOCUMENT_NODE: 9,
    DOCUMENT_TYPE_NODE: 10,
    DOCUMENT_FRAGMENT_NODE: 11,
    NOTATION_NODE: 12,
    DEBUG_NONE: 0,
    DEBUG_USE_CONSOLE: 1,
    DEBUG_USE_ALERT: 2,
    DEBUG_ALWAYS_ALERT: 6,
    DEBUG_MESSAGES: 0,
    addClass: function (node, classname) {
        if (!hum.hasClass(node, classname)) node.className += node.className ? " " + classname : classname;
    },
    addListener: function (obj, evt, handler, captures) {
        if (document.addEventListener)
        {
            obj.addEventListener(evt, handler, captures);
        }
        else
        {
            // IE
            obj.attachEvent('on' + evt, handler);
        }
    },
    analytics: function () {
        //Trigger loading of the Google Analytics Script
        if (!gaId) {
            return null;
        }
        gaJsHost = (("https:" == document.location.protocol) ? "https://ssl.": "http://www.");
        hum.loadScript(gaJsHost + "google-analytics.com/ga.js", hum.analyticsLoaded);
    },
    analyticsLoaded: function () {
        // Load Google Analytics, once the script has loaded
        if (!gaId)
        {
            return null;
        }
        try {
             var pageTracker = _gat._getTracker(gaId);
                // Limit Google Analytics to the full hostname domain only, rather than default of parent domain.
                pageTracker._setDomainName(BASE_URL.replace(/http:\/\//, '').replace(/\/.*$/, ''));
                pageTracker._trackPageview();
        }
        catch (err) {}
    },
    createCookie: function (name, value, days) {
        var date,
        expires;
        if (days) {
            date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toGMTString();
        }
        else
        {
            expires = "";
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    },
    debugMessage: function (message) {
        if (!hum.DEBUG_MESSAGES) return null;
        var hasConsole = (typeof console != "undefined" && typeof console.log != "undefined")
        if (hum.DEBUG_MESSAGES & hum.DEBUG_USE_CONSOLE & hasConsole)
        {
            console.log(message);
        }
        if (hum.DEBUG_MESSAGES & hum.DEBUG_USE_ALERT & !hasConsole)
        {
            alert(message);
        }
        if (hum.DEBUG_MESSAGES & hum.DEBUG_ALWAYS_ALERT & hasConsole)
        {
            alert(message);
        }
    },
    enableImageSelector: function () {
        var imageselector,
        images,
        image,
        i;
        imageselector = document.getElementById('imageselector');
        if (imageselector)
        {
            images = imageselector.getElementsByTagName('img');
            image = new Image();
            for (i = 0; i < images.length; i++)
            {
                image.src = FILES_URL + '/' + images.item(i).src.match(/([^\\\/]+)$/)[1];
            }
        }
        jQuery('#imageselector img').click(
        function() {
            var largeimgsrc,
            imageviewer,
            largeimage;
            largeimgsrc = FILES_URL + '/' + this.src.match(/([^\\\/]+)$/)[1];
            jQuery('#imageviewer img').hide('slow',
            function() {
                imageviewer = document.getElementById('imageviewer');
                largeimage = imageviewer.getElementsByTagName('img').item(0);
                largeimage.src = largeimgsrc;
            });
            jQuery('#imageviewer img').show('slow');
        }
        );
    },
    eraseCookie: function (name) {
        hum.createCookie(name, "", -1);
    },
    externalLinks: function ()
    {
        // for xhtml strict websites, set rel="external" on your links instead of target="_blank"
        // then call this function on domloaded
        var anchors,
        i;
        anchors = document.getElementsByTagName('a');
        for (i = 0; i < anchors.length; ++i)
        {
            anchor = anchors[i];
            if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external')
            {
                anchor.target = '_blank';
            }
        }
    },
    fileExtension: function (path) {
        return path.substr(path.lastIndexOf(".") + 1, path.length);
    },
    fileIcon: function (path) {
        // a selection of file icons are avaible in the assets folder. This
        // function will determine the appropriate icon file name to load 
        // from any of the sets of icons. The choice is based on the fileextension 
        // of the path argument
        var ext, icon;
        ext = hum.fileExtension(path);
        switch (ext.toLowerCase())
        {
            case 'jpg':
            case 'jpeg':         icon = 'jpeg.jpg';
                                 break;
            case 'gif':          icon = 'gif.jpg';
                                 break;
            case 'png':          icon = 'png.jpg';
                                 break;
            case 'doc':
            case 'dot':          icon = 'doc.jpg';
                                 break;
            case 'xls':
            case 'xlm':
            case 'xla':
            case 'xlc':
            case 'xlt':
            case 'xlw':          icon = 'xls.jpg';
                                 break;
            case 'ppt':
            case 'pps':
            case 'pos':          icon = 'ppt.jpg';
                                 break;
            case 'pdf':          icon = 'pdf.jpg';
                                 break;
            case 'tiff':
            case 'tif':          icon = 'tiff.jpg';
                                 break;
            case 'zip':          icon = 'zip.jpg';
                                 break;
            case 'gz':           icon = 'gz.jpg';
                                 break;
            case 'bz':           icon = 'bz.jpg';
                                 break;
            case 'bz2':          icon = 'bz2.jpg';
                                 break;
            case 'rtf':          icon = 'rtf.jpg';
                                 break;
            case 'swf':          icon = 'swf.jpg';
                                 break;
            case 'psd':          icon = 'psd.jpg';
                                 break;
            case 'html':
            case 'htm':          icon = 'html.jpg';
                                 break;
            default:             icon = 'unknown.jpg';
                                 break;
        }
        return icon;
    },
    formSubmissionCheck: function (form, message) {
        var existingWarnings, listItems, element, elements = [], i, value;
        ///////////
        // Clear up from any previous attempts to submit the form, where class names and 
        // warning messages may have been added to form elements.
        existingWarnings = hum.getElementsByClassName(form, 'warning');
        if (existingWarnings.length)
        {
            for (i = 0; i < existingWarnings.length; ++i)
            {
                hum.removeClass(existingWarnings[i], 'warning');
                existingWarnings[i].onfocus = null;
                if (    existingWarnings[i].tagName.toLowerCase() == 'input' 
                    ||  existingWarnings[i].tagName.toLowerCase() == 'textarea'
                    &&  existingWarnings[i].value == message)
                {
                    existingWarnings[i].value = '';
                }
            }
        }
        ////////////
        // Find all required elements
        listItems = hum.getElementsByClassName(form, 'required');
        if (listItems)
        {
            for (i = 0; i < listItems.length; ++i)
            {
                // inputs are the most common aren't they?
                element = listItems[i].getElementsByTagName('input');
                if (!element.length) 
                {
                    element = listItems[i].getElementsByTagName('textarea');
                }
                // no inputs? then try textarea
                if (!element.length) 
                {
                    element = listItems[i].getElementsByTagName('select');
                }
                // still nothing? try select box
                if (element.length)
                {
                    element = element.item(0);
                    // filter out inputs that don't need to be validated
                    if (    element.getAttribute('type') == 'hidden'
                        ||  element.getAttribute('type') == 'submit'
                        ||  element.getAttribute('type') == 'reset' ) 
                    {
                        continue;
                    }
                    elements.push(element);
                }
            }
        }
        //////////////
        // Go over required elements and look for those which have not been filled in. 
        for (i = 0; i < elements.length; ++i)
        {
            if (elements[i].tagName.toLowerCase() == 'select')
            {
                value = elements[i].options[elements[i].selectedIndex].value;
            }
            else
            {
                value = elements[i].value;
            }
            if (!hum.trim(value))
            {
                hum.addClass(elements[i], 'warning');
                if (    elements[i].tagName.toLowerCase() != 'select'
                        && !(    elements[i].tagName.toLowerCase() == 'input'
                            &&  elements[i].getAttribute('type') == 'password') )
                {
                    elements[i].value = message;
                }
                elements[i].onfocus = function () {
                    hum.debugMessage("focusing on "+this.tagName);
                    if (this.value == message)
                    {
                        this.value = '';
                    }
                    hum.removeClass(this, 'warning');
                }
            }
        }
        return (hum.getElementsByClassName(form, 'warning').length == 0);
    },
    getElementsByClassName: function (node, classname, tagName) {
        // Like node.getElementsByClassName(classname)
        // except you must pass the node (eg. document or document.getElementById('menu'))
        // as the first argument. Optionally specify the tagName to limit the search to 
        // only a specific type of tag (for performance).
    	var failedMatch, matchingElements = [], re, els, i, j, classname, elClassnames;
    	if (node && typeof node.getElementsByClassName != "undefined")
    	{
    	    // Need to be consistent and always return an Array, never a NodeList
    	    els = node.getElementsByClassName(classname);
    	    if (!els)
    	    {
    	        hum.debugMessage("hum.getElementsByClassName() nothing found, returning empty array");
    	        return matchingElements;
    	    }
    	    for(i = 0; i < els.length; ++i)
    	    {
    	        if (hum.isSafari31())
    	        {
    	            // Safari does something very bloody weird. Class attribute is empty, 
    	            // WebInspector agrees, but safari still "finds" these elements. GGrrrrr.
    	            // Non at all rather than broken please.
    	            if (!hum.hasClass(els.item(i), classname))
    	            {
    	                continue;
    	            }
    	        }
    	        matchingElements[i] = els.item(i);
    	    }
        	//delete els;
    	    return matchingElements;
    	}
    	classnames = classname.split(" ");
    	if (tagName)
    	{
    	    els = node.getElementsByTagName(tagName);
    	}
    	else
    	{
    	    els = node.all ? node.all : node.getElementsByTagName("*");
    	}
    	for(i=0; i < els.length; ++i)
    	{
    	    failedMatch = false;
    		if (!els.item(i).className)
    		{
    			continue;
    		}
    		for (j = 0; j < classnames.length; ++j)
    		{
    			if(els.item(i).className.indexOf(classnames[j]) < 0)
    			{
    			    failedMatch = true;
    			}
    		}
    		if (!failedMatch)
    		{
    		    matchingElements.push(els.item(i));
    		}
    	}
    	return matchingElements;
    },
    getKeyCode: function (e) {
        var key;
        if (e)
        {
            key = e.which;
        }
        else
        {
            key = window.event.keyCode;
        }
        return key;
    },
    hasClass: function (node, classes) {
        //return node.className.match(new RegExp('(\\s|^)' + classname + '(\\s|$)'));
        var i;
        if (!node) return false;
        if (typeof classes == 'string')
        {
            classes = hum.trim(classes.replace(/\s{2,}/g, ''));
            classes = classes.split(" ");
        }
        if (hum.isArray(classes))
        {
            for (i = 0; i < classes.length; ++i)
            {
                if (!node.className.match(classes[i]))
                {
                    return false;
                }
            }
            return true;
        }
        return false;
    },
    isArray: function (array) {
        return (array.constructor && array.constructor == Array);
    },
    isIE8: function () {
        return hum.isIE() && !!document.querySelector;
    },
    isIE7: function () {
        return hum.isIE() && !!document.documentElement && typeof document.documentElement.style.maxHeight != 'undefined' && !hum.isIE8();
    },
    isIE6: function () {
        return hum.isIE() && !!document.compatMode && !hum.isIE7() && !hum.isIE8() && typeof document.createAttribute != "undefined";
    },
    isIE: function () {
        return !! (document.all && !window.opera);
    },
    isMozilla3: function () {
        return typeof document.getElementsByClassName != "undefined" && hum.isMozilla();
    },
    isMozilla2: function () {
        return hum.isMozilla() && !hum.isMozilla3();
    },
    /*
    isMozilla15: function () {
            return typeof Array.every != "undefined" && !hum.isMozilla3() && hum.isMozilla();
        },*/
    isMozilla: function () {
        return typeof document.getElementsByTagName('script').item(0).contains == "undefined" && !document.all;
    },
    isOpera: function () {
        return !! window.opera;
    },
    isSafari31: function () {
        return hum.isSafari3() && typeof document.getElementsByClassName != "undefined";
    },
    isSafari30: function () {
        return hum.isSafari3() && !hum.isSafari31();
    },
    isSafari3: function () {
        return hum.isSafari() && window.devicePixelRatio;
    },
    isSafari2: function () {
        return hum.isSafari() && !hum.isSafari3();
    },
    isSafari: function () {
        return navigator && navigator.vendor && navigator.vendor.toLowerCase().indexOf('apple') != -1;
    },
    isWebsafeImage: function (path) {
        ext = hum.fileExtension(path);
        switch (ext)
        {
            case 'jpg':
            case 'jpeg':
            case 'gif':
            case 'png': return true;
                        break;
            default:    return false;
                        break;
        }
    },
    loadScript: function (url, aFunction) {
        // Load an additional script after the domloaded event has already fired.
        // Safe for XHTML Strict web sites. 
        // url - the url to load, aFunction - function to call once script has loaded
        var e = document.createElement("script");
        e.onreadystatechange = function()
        {
            if ((this.readyState == 'completed' || this.readyState == 'loaded') && !this.loadScriptDone)
            {
                this.loadScriptDone = true;
                aFunction();
            }
        }
        e.onload = aFunction;
        e.type = "text/javascript";
        e.src = url;
        document.getElementsByTagName("head")[0].appendChild(e);
        return e;
    },
    ltrim: function (string) {
        // trim white space from the left of a string.
        if (typeof string == 'string') return string.replace(/^\s+/, '');
        return string;
    },
    readCookie: function (name) {
        var nameEQ = name + "=",
        ca,
        i,
        c;
        ca = document.cookie.split(';');
        for (i = 0; i < ca.length; i++) {
            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;
    },
    reCaptcha: function () {
        if (!reCaptchaKey || !document.getElementById('recaptchaplaceholder'))
        {
            return null;
        }
        loadScript((("https:" == document.location.protocol) ? "https://api-secure": "http://api") + '.recaptcha.net/js/recaptcha_ajax.js', reCaptchaLoaded);
    },
    reCaptchaLoaded: function () {
        if (!reCaptchaKey || !document.getElementById('recaptchaplaceholder'))
        {
            return null;
        }
        Recaptcha.create(reCaptchaKey, document.getElementById('recaptchaplaceholder'));
    },
    removeClass: function (node, classname) {
        if (hum.hasClass(node, classname))
        {
    		node.className = node.className.replace(new RegExp('(\\s|^)' + classname + '(\\s|$)'),' ');
    	}
    },
    rtrim: function (string) {
        // trim white space from the right of a string.
        if (typeof string == 'string') return string.replace(/\s+$/, '');
        return string;
    },
    setClass: function (node, classname) {
        node.className = classname;
    },
    textToImage: function (menuId_or_links, tti_name, capitalise) {
        var primarymenu,
        sectionmenu,
        links = [],
        i,
        img,
        text,
        j;
        if (hum.isArray(menuId_or_links))
        {
            links = menuId_or_links;
        }
        else
        {
            menu = document.getElementById(menuId_or_links);
            if (menu)
            {
                links = menu.getElementsByTagName('a');
            }
        }
        if (links.length)
        {
            
            for (i = 0; i < links.length; ++i)
            {
                text = '';
                for (j = 0; j < links[i].childNodes.length; ++j)
                {
                    text = text + links[i].childNodes[j].nodeValue;
                    links[i].childNodes[j].nodeValue = '';
                }
                if (capitalise && text.length) 
                {
                    text = text.toUpperCase();
                }
                url = STATIC_URL + '/tti/' + tti_name + '/' + escape(text) + '.png';
                if (hum.isIE6())
                {
                    links[i].style.cursor = 'pointer';
                    var textNode = document.createTextNode(text);
                    div = document.createElement('div');
                    div.appendChild(textNode);
                    img = document.createElement('img');
                    img.src = url;
                    img.wrapperDiv = div;
                    div.filterImage = img;
                    //div.appendChild(img);
                    //img.style['display'] = 'none';
                    if (img.width && img.height)
                    {
                        div.style.width = img.width + 'px';
                        div.style.height = img.height + 'px';
                    }
                    else
                    {
                        img.onreadystatechange = function () {
                            if (this.readyState == 'complete')
                            {
                                this.wrapperDiv.style.width = this.width +'px';
                                this.wrapperDiv.style.height = this.height +'px';
                            }
                        };
                    }
                    div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='image',src='" + url + "')";
                    div.style.cursor = "pointer";
                    div.style.display = "block";
                    div.style.textIndent = '-2000px';
                    links[i].appendChild(div);
                }
                else
                {
                    img = document.createElement('img');
                    img.src = url;
                    img.alt = text;
                    links[i].appendChild(img);
                }
            }
        }
    },
    trim: function (string) {
        // trim white space from the left and right of a string.
        if (typeof string == 'string') return string.replace(/^\s+|\s+$/g, '');
        return string;
    }
};
// hum
///////

///////////////////////////////////
// DomLoaded - ondomdocumentready
/*! DomLoaded courtesy of Dean Edwards and contributors at http://dean.edwards.name
 * Released under an MIT License http://www.opensource.org/licenses/mit-license.php 
 * Modified by Kieran Whitbread */
var DomLoaded =
{
    onload: [],
    loaded: function()
    {
        var i;
        if (arguments.callee.done) return;
        arguments.callee.done = true;
        for (i = 0; i < DomLoaded.onload.length; i++) DomLoaded.onload[i]();
    },
    load: function(fireThis)
    {
        this.onload.push(fireThis);
        if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", DomLoaded.loaded, null);
        if (/KHTML|WebKit/i.test(navigator.userAgent))
        {
            var _timer = setInterval(function()
            {
                if (/loaded|complete/.test(document.readyState))
                {
                    clearInterval(_timer);
                    delete _timer;
                    DomLoaded.loaded();
                }
            },
            10);
        }
/*@cc_on @*/
/*@if (@_win32)
var proto = "src='javascript:void(0)'";
if (location.protocol == "https:") proto = "src=//0";
document.write("<scr"+"ipt id=__ie_onload defer " + proto + "><\/scr"+"ipt>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
    if (this.readyState == "complete") {
        DomLoaded.loaded();
    }
};
/*@end @*/
        window.onload = DomLoaded.loaded;
    }
};
//DomLoaded
/////////////

///////////////////////////////////////////////
// Add the indexOf() method to Array Objects
if (!Array.indexOf)
 {
    Array.prototype.indexOf = function(obj, start)
    {
        for (var i = (start || 0); i < this.length; ++i)
        {
            if (this[i] == obj)
            {
                return i;
            }
        }
    }
}

/////////////////////////
// jQuery Compatibility
if (typeof jQuery != "undefined") {
    // Comment out the next line to keep the default jQuery "$" reference
    jQuery.noConflict(); // Might be using other JS libs such as prototype
}

/////////////////////////////////
// Configure Debugging Messages
//hum.DEBUG_MESSAGES = hum.DEBUG_USE_CONSOLE | hum.DEBUG_USE_ALERT;
hum.DEBUG_MESSAGES = hum.DEBUG_NONE;




