// JScript File

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

var util = {
    cacheVersion: null,
    cacheQuery: null,

    getMSIEVersion: function() {
        if (util.cacheVersion != null) return util.cacheVersion;
        msv = 0;
        if (navigator.appVersion.indexOf('MSIE') > -1) {
            msv = navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE') + 5, 10);
            msv = msv.substr(0, msv.indexOf(';'));
        }
        util.cacheVersion = msv * 1;
        return util.cacheVersion;
    },

    addEvent: function(object, eventName, fn) {
        if (document.addEventListener) object.addEventListener(eventName, fn, false);
        else object.attachEvent('on' + eventName, fn);
    },

    byId: function(id) {
        return document.getElementById(id);
    },

    QueryString: function(name) {
        var result = '';
        if (util.cacheQuery == null) {
            util.cacheQuery = new Array();
            var url = document.location.href;
            if (url.indexOf('?') > -1) {
                url = url.substring(url.indexOf('?') + 1);
                queryVals = url.split(/&/);
                for (var i = 0; i < queryVals.length; i++) {
                    util.cacheQuery[i] = new Object();
                    util.cacheQuery[i].name = queryVals[i].substring(0, queryVals[i].indexOf('='))
                    util.cacheQuery[i].value = queryVals[i].substring(queryVals[i].indexOf('=') + 1)
                }
            }
        }
        for (var i = 0; i < util.cacheQuery.length; i++) {
            if (util.cacheQuery[i].name.toLowerCase().trim() == name) {
                result = util.cacheQuery[i].value;
            }
        }
        return result;
    }
}

$(function () {
    if (window == window.top) {
        if (window.heroFiles && window.heroFiles.length) new heroImage(heroFiles);
        $('.tabbed').each(function() { new tabbedArea($(this)) });

        var count = 0;
        var checkLoaded = function () {
            var val = true;
            $('img').each(function () {
                if (!this.complete) val = false;
            });
            count++;
            if (!val && count < 50) {
                setTimeout(checkLoaded, 100);
            } else {
                doLoadComplete();
            }
        }
        checkLoaded();
        if (headerFiles && headerFiles.length > 1) new heroImage(headerFiles);
        windowResize();
    }
});

function doLoadComplete() {
    $('.bordered').each(function() {
        var m = 0, tc = 1;
        $(this).find('.border>div.text').css({ height: 'auto' })
        $(this).find('.border').css({ height: 'auto' })
        $(this).find('.border').each(
            function() {
                var el = $(this), mm;
                if (el.find('div.text').length != 1) {
                    mm = el.height();
                    tc = 0;
                } else {
                    mm = el.find('div.text').height();
                }
                m = Math.max(m, mm);
            });
        if (tc == 1) {
            $(this).find('.border>div.text').height(m);
        } else {
            $(this).find('.border').height(m);
        }
    });
    windowResize();
    $(window).resize(windowResize);
}

function windowResize() {
    var content = $('#contentInner');
    content.height('auto');
    var oph = $('#outerpage').height();
    var winh = $(window).height();
    var dif = winh - oph;
    if (dif > 0) content.height(content.height() + dif);
}

function bannerText() {
    this.init();
}

function tabbedArea(tab) {
    this.init(tab);
}

tabbedArea.prototype = {
    tab: null,
    tabs: null,
    tabLinks: null,

    init: function(tab) {
        this.tab = tab;
        this.tabs = tab.find('.tab');

        this.tabLinks = $('<div />').addClass('tablinks').prependTo(this.tab);

        this.tabs.each($.proxy(this.createTab, this));
    },

    createTab: function(index, el) {
        el = $(el);
        var tab = el.children('h2').click($.proxy(this._click, this)).data('tab', el);
        this.tabLinks.append(tab);
        if (index > 0) {
            el.hide();
        } else {
            tab.addClass('tabactive');
        }
    },

    _click: function(e) {
        var el = $(e.currentTarget);
        this.tabs.hide();
        this.tab.find('.tabactive').removeClass('tabactive');
        if (el.data('tab')) {
            el.data('tab').show();
            el.data('tab');
            el.addClass('tabactive');
        }
    }
}

function heroImage(files) {
    this.init(files);
}

heroImage.prototype = {
    image: null,
    files: null,
    count: 0,
    maxTime: 10000,
    oldImage: null,

    init: function (files) {
        this.files = files;
        this.image = $('.headerlogo').find('img').first();
        this.startTimeout();
    },

    startTimeout: function () {
        setTimeout($.proxy(this.go, this), this.maxTime);
    },

    go: function () {
        if (++this.count == this.files.length) this.count = 0;
        src = this.files[this.count];

        if (this.oldImage) this.oldImage.remove();

        newImage = this.image.clone().appendTo('.headerlogo');
        newImage.attr('src', src).css({ opacity: 0 }).animate({ opacity: 1 }, 500);

        this.oldImage = this.image;
        this.image = newImage;

        this.startTimeout();
    }

}










