﻿if(typeof(pcmr) == 'undefined')
    pcmr = {}

if (!Array.prototype.push) {
    Array.prototype.push = function() {
        var startLength = this.length;
        for (var i = 0; i < arguments.length; i++)
            this[startLength + i] = arguments[i];
        return this.length;
    }
}

/* Register onLoad events, supporting as many browser styles as are known. */
pcmr.registerLoader = function(func) {
    if (window.addEventListener) 
        window.addEventListener("load",func,false);
    else if (window.attachEvent) 
        window.attachEvent("onload",func);   
}


/* Element Finders - used to work text resize magic. :
   pcmr.getAllChildren and pcmr.getElementsBySelector based on code by Simon
   Willison.

   (C) Simon Willison 2004
 */
pcmr.getAllChildren = function(e) {
    // Returns all children of element. Workaround required for IE5/Windows.
    // Ugh.
    return e.all ? e.all : e.getElementsByTagName('*');
}

pcmr.getElementsBySelector = function(selector) {
    // Attempt to fail gracefully in lesser browsers
    if (!document.getElementsByTagName) {
        return new Array();
    }
    // Split selector in to tokens
    var tokens = selector.split(' ');
    var currentContext = new Array(document);
    for (var i = 0; i < tokens.length; i++) {
        token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
        if (token.indexOf('#') > -1) {
            // Token is an ID selector
            var bits = token.split('#');
            var tagName = bits[0];
            var id = bits[1];
            var element = document.getElementById(id);
            if(!element) {
                continue;
            }
            if (tagName && element.nodeName.toLowerCase() != tagName) {
                // tag with that ID not found, return false
                return new Array();
            }
            // Set currentContext to contain just this element
            currentContext = new Array(element);
            continue; // Skip to next token
        }
        if (token.indexOf('.') > -1) {
            // Token contains a class selector
            var bits = token.split('.');
            var tagName = bits[0];
            var className = bits[1];
            if (!tagName) {
                tagName = '*';
            }
            // Get elements matching tag, filter them for class selector
            var found = new Array;
            var foundCount = 0;
            for (var h = 0; h < currentContext.length; h++) {
                var elements;
                if (tagName == '*') {
                        elements = pcmr.getAllChildren(currentContext[h]);
                } else {
                        elements = currentContext[h].getElementsByTagName(tagName);
                }
                for (var j = 0; j < elements.length; j++) {
                    found[foundCount++] = elements[j];
                }
            }
            currentContext = new Array;
            var currentContextIndex = 0;
            for (var k = 0; k < found.length; k++) {
                if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
                    currentContext[currentContextIndex++] = found[k];
                }
            }
            continue; // Skip to next token
        }
        
        if (!currentContext[0]){
            return;
        }
        
        // If we get here, token is JUST an element (not a class or ID selector)
        tagName = token;
        var found = new Array;
        var foundCount = 0;
        for (var h = 0; h < currentContext.length; h++) {
            var elements = currentContext[h].getElementsByTagName(tagName);
            for (var j = 0; j < elements.length; j++) {
                found[foundCount++] = elements[j];
            }
        }
        currentContext = found;
    }
    return currentContext;
}


/* Font Resizing Support */
pcmr.fontSizes = {
    'small': '10px',
    'medium': '11px',
    'large': '14px'
}
pcmr.smallTypeFontSizes = {
    'small': '9px',
    'medium': '9px',
    'large': '12px'
}

pcmr.resizableSelectors = [
    'body',
    '#home_left h1',
    // Uncomment the following to resize left-side navigation.
    // 'div#subnav div',

    // pcmr_old.css styles 
    '.subhead_white',
    '.subhead_grey',
    '.subhead_black',
    '.subhead_red',
    '.body',
    '.subheadnew',
    '.subheaddark',
    '.snowreport',
    '.snowreport_winter'
    ]

pcmr.smallTypeSelectors = [
    '.smalltype',
    'td.smalltype',
    'td.smalltype div'
    ]


pcmr.hilightResizeButton = function(styleType) {
    var sml = ['small', 'medium', 'large'];
    for(var i=0; i<sml.length; i++) {
        var instyle = sml[i];
        var button = document.getElementById('resize_' + instyle);
        if(!button)
            continue

        if(instyle == styleType)
            button.className = 'selected';
        else
            button.className = ''
    }
}

pcmr.setSmallTypeStyle = function(styleType) {
    var fontSize = pcmr.smallTypeFontSizes[styleType];
    for(var i=0; i<pcmr.smallTypeSelectors.length; i++) {
        var selector = pcmr.smallTypeSelectors[i];
        var elements = pcmr.getElementsBySelector(selector);
        if(!elements) {
            // The requested item isn't on this page.
            continue;
        }
        for(var j=0; j<elements.length; j++) {
            var element = elements[j];
            element.style.fontSize = fontSize;
        }
    }
}

pcmr.setActiveStyle = function(styleType, reset) {
    var fontSize = pcmr.fontSizes[styleType];
    for(var i=0; i<pcmr.resizableSelectors.length; i++) {
        var selector = pcmr.resizableSelectors[i];
        var elements = pcmr.getElementsBySelector(selector);
        if(!elements) {
            // The requested item isn't on this page.
            continue;
        }
        for(var j=0; j<elements.length; j++) {
            var element = elements[j];
            element.style.fontSize = fontSize;
        }
    }
    pcmr.setSmallTypeStyle(styleType);

    if(reset) {
        pcmr.createCookie("psize", styleType, 365);
    }
    pcmr.hilightResizeButton(styleType);
}

pcmr.setStyle = function() {
    var style = pcmr.readCookie("psize");
    // Nothing has to be done for no cookie or medium style
    if ((style == null) || (style == 'medium'))
        pcmr.hilightResizeButton('medium');
    else
        pcmr.setActiveStyle(style, false);
}
pcmr.registerLoader(pcmr.setStyle);


pcmr.createCookie = function(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else 
        expires = "";
    document.cookie = name+"="+escape(value)+expires+"; path=/;";
}

pcmr.readCookie = function(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 unescape(c.substring(nameEQ.length,c.length));
    }
    return null;
}



/* Menu Support */
function sfHover() {
  var sfEls = document.getElementById("nav").getElementsByTagName("LI");

  for(var i=0; i<sfEls.length; i++) {
    sfEls[i].onmouseover = function() {
      this.className += " sfhover";
    }
    sfEls[i].onmouseout = function() {
      this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
    }
  }
}

if (window.attachEvent)
  window.attachEvent("onload", sfHover);


