
var ua = navigator.userAgent.toLowerCase();
Event.observe( window, "load", function() {
    // Extract H2 and replace with image
    var h2s = $A($("content").getElementsByTagName("h2"));
    h2s = h2s.concat($A($("subcontent").getElementsByTagName("h2")));
    $A(h2s.flatten()).each( function(h2) {
        Element.cleanWhitespace(h2);
        if (!(h2.firstChild && h2.firstChild.nodeType == 1 && h2.firstChild.nodeName && h2.firstChild.nodeName.toLowerCase() == "img")) {
            // Replace!
            var t = h2.innerText || h2.textContent;
            if (t) {
                t = t.replace(/<br ?\/?>/g, '\n');
                var color = typeof(kcColorScheme) != 'undefined' ? 'color=' + escape(kcColorScheme) + '&amp;' : '';
                h2.innerHTML = '<img src="/tools/GetHeadline?' + color + 't=' + escape(t + " //") + '" alt="' + t + '" />';
            }
        }
    });
    
    
    // Enable toggling of menu images
    $A($("menu").getElementsByTagName("img")).each( function(a) {
        if (!Element.hasClassName(a.parentNode, "selected")) {
            Event.observe(a, "mouseover", toggleMenuImage.bindAsEventListener());
            Event.observe(a, "mouseout", toggleMenuImage.bindAsEventListener());
        }
    });
    
    initEmailLinks();
    initBreadCrumbSeparators();
    initPageFunction();
});
function toggleMenuImage( e ) {
    var img = e.target || e.srcElement;
    if (/\.off\./.test(img.src)) {
        img.src = img.src.replace(/\.off\./, ".on.");
    } else {
        img.src = img.src.replace(/\.on\./, ".off.");
    }
}
function initBreadCrumbSeparators() {
    // For IE7 and below, instead of :last-child:after in CSS...
    if (/msie [4-7]/.test(ua) && !/opera/.test(ua)) {
        var lis = $$("#breadcrumb li");
        for (var i=0; i<lis.length-1; i++) {
            new Insertion.Bottom(lis[i], ' // ');
            //lis[i].style.backgroundImage = 'url(/chrome/slashes.gray.gif)';
        }
    }
}


function colorize() {
    $$('div').each( function(div) {
        div.style.backgroundColor = "rgb(" + [1,2,3].collect( function() {
            return parseInt(100 + 130 * Math.random());
        }).join(",") + ")";
    });
}

function textBigger() {
    textSize( 1 );
}
function textSmaller() {
    textSize( -1 );
}
function textSize( d ) {
    var cls = document.body.className || '';
    var size = 1
    if (/^text(\d)/.test(cls)) {
        size = parseInt(RegExp.$1);
    }
    size += d;
    if (size < 1) size++;
    if (size > 3) size--;
    document.body.className = "text" + size;
}

function tipAFriendForm(key) {
    window.open('/tools/tip-a-friend?key=' + escape(key), '', 'width=270,height=350');
}

function initPageFunction() {
    $A($("page-functions").getElementsByTagName("img")).each( function(img) {
        Event.observe(img, "mouseover", function() {
            img.src = img.src.replace(/\.gif/, '.hover.gif');
        });
        Event.observe(img, "mouseout", function() {
            img.src = img.src.replace(/\.hover/, '');
        });
    });
}


function initEmailLinks() {
    var ua = navigator.userAgent;
    var ie6 = /msie [4-6]/i.test(ua) && !/opera/i.test(ua) && /win/i.test(ua);
    var as = document.getElementsByTagName("a");
    for (var i=0; i<as.length; i++) {
        var a = as[i];
        if (a.href.match(/^mailto:spammenot-/)) {
            var href = "mailto:" + rot13(a.href.replace(/(mailto:|spammenot-)/g, '').replace(/\(a\)/, '@'));
            if (ie6) {
                var t = a.innerHTML;
                a.setAttribute("href", href);
                a.innerHTML = t;
            } else {
                a.setAttribute("href", href);
            }
        }
    }
}
function rot13(s) {
    var a = s.split('');
    var out = "";
    var s1 = "abcdefghijklmnopqrstuvwxyz".split('');
    var s2 = "nopqrstuvwxyzabcdefghijklm".split('');
    for (var p=0; p<a.length; p++) {
        var pos = s1.indexOf(a[p]);
        if (pos >= 0) {
            out += s2[pos];
        } else {
            out += a[p];
        }
    }
    return out;
}

Array.prototype.indexOf = function(val) {
    for (var i=0; i<this.length; i++) {
        if (this[i] == val) {
            return i;
        }
    }
    return -1;
}
