﻿73
/* equal heights */
$.fn.equalHeights = function(px) {
    $(this).each(function() {
        var currentTallest = 0;
        $(this).children().each(function(i) {
            if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
        });
        if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
        // for ie6, set height since min-height isn't supported

        $(this).children().css({ 'min-height': currentTallest });
    });
    return this;
};

/* px to em */
Number.prototype.pxToEm = String.prototype.pxToEm = function(settings) {
    settings = jQuery.extend({ scope: 'body', reverse: false }, settings);
    var pxVal = (this === '') ? 0 : parseFloat(this);
    var scopeVal;
    var getWindowWidth = function() {
        var de = document.documentElement;
        return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
    };
    if (settings.scope === 'body' && $.browser.msie && (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(1) > 0.0) {
        var calcFontSize = function() {
            return (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(3) * 16;
        };
        scopeVal = calcFontSize();
    }
    else { scopeVal = parseFloat(jQuery(settings.scope).css("font-size")); };

    var result = (settings.reverse === true) ? (pxVal * scopeVal).toFixed(2) + 'px' : (pxVal / scopeVal).toFixed(2) + 'em';
    return result;
};
/* share this */
var addthis_config = {
    ui_offset_top: 0,
    ui_offset_left: -130
}

var APP = (function($) {
    var app = {}, cur = 0, pages = 0, prev, next, key = '.body-copy';

    var types = [];
    types.id = 'By Id';
    types.jpg = 'JPG';
    types.gif = 'JPG';
    types.pdf = 'PDF';
    types.ppt = 'PowerPoint';
    types.pptx = 'PowerPoint';
    types.xls = 'Excel';
    types.xlsx = 'Excel';
    types.doc = 'Word Document';
    types.docx = 'Word Document';
    types.zip = 'Zip Files';

    app.showPage = function() {
        if (cur > pages) {
            cur = pages;
        } else if (cur < 0) {
            cur = 0;
        }
        $(key + ' .page').hide();
        $(key + ' #page-' + cur).show();

        prev.toggle(cur !== 0);
        next.toggle(cur !== pages - 1);

        var y = $(key).offset().top - 100;
        $('html,body').animate({ scrollTop: y }, 100);
        $('.container').equalHeights();
        next.attr('href', '#page-' + (cur + 1));
        prev.attr('href', '#page-' + (cur + 1));
        var h = $('.page').height();
        $('#columnMain').css({ 'min-height': h });
        $('#columnRight').css({ 'min-height': h });
        //console.log(h, $('#columnMain'), $('#columnRight'));
    };
    app.init = function() {
        app.trackDownloads();
        $('#searchButton').click(function() {
            $('#searchInputField').focus();
            $('#searchForm').submit();
        });
        app.searchInput('#searchInputField', 'Search site');
        app.paginate();
        app.twitter();
    };
    app.searchInput = function(id, text) {
        var el = $(id);
        var d = text;
        el.focus(function() {
            el.addClass('focusField');
            if ($(this).val() === d) { $(this).val(''); }
        }).blur(function() {
            el.removeClass('focusField');
            if ($.trim($(this).val()) === '') { el.val(d); }
        });
    };
    app.paginate = function() {
        if ($(key + ' a.page').length < 1) {
            return;
        }
        var hash = location.hash;
        if (hash !== "") {
            var m = hash.split(/^#page-/);
            cur = Number(m[1]) - 1;
        }
        $(key + ' a.page').unwrap();
        $(key).children().each(function(idx, el) {
            var $el = $(el);
            if ($el.attr('class') === 'page' && el.nodeName.toUpperCase() === 'A' && idx > 0) {
                wrap();
            }
            $el.addClass('page-' + pages);
        });
        wrap();
        $(key + ' a.page').hide();

        var html = '<p>&nbsp;</p><div id="keyBottomPagination"><div class="separator"></div>';
        html += '<div class="pagination inline">';
        html += '<a href=""  id="page-prev" title="Prev">PREV </a>';
        html += '<a href="" id="page-next" title="Next">NEXT </a>';
        html += '</div></div>';

        $(key).append(html);

        next = $('#page-next');
        next.click(function() {
            cur++;
            app.showPage();
        });
        prev = $('#page-prev');
        prev.click(function() {
            cur--;
            app.showPage();
        });
        app.showPage();
    };
    function wrap() {
        $('.page-' + pages).wrapAll('<div class="page" id="page-' + pages + '"></div>');
        pages++;
    }

    app.trackDownloads = function() {
        $("a[rel*='external']").click(function() { pageTracker._trackEvent('External link', '', $(this).attr('href')); });
        $('a[href^=/media/download/]').click(function() { app.track(this, 'id'); });
        $('a.print-btn').click(app.trackPrint);
        for (var ext in types) {
            $('a[href*=.' + ext + ']').click(function() { app.track(this); });
        }

    };
    app.track = function(o, ext) {
        var href = "", hrefa, i, filename, filetype;
        href = $(o).attr('href');
        hrefa = href.split('/');
        i = Math.max(0, hrefa.length - 1);
        filename = hrefa[i];
        if (ext !== 'id') {
            ext = filename.substr(filename.lastIndexOf('.') + 1, filename.length);
        }
        filetype = types[ext];

        _gaq.push(['_trackEvent', filetype + ' files', 'Download', filename]);
        return false;
    };
    app.trackPrint = function() {
        _gaq.push(['_trackEvent', 'Print', location.href]);
    };
    app.twitter = function() {
        var $t, hash, users, tweets, cnt = 3;
        $('.dynamic-twitter').each(function(idx, el) {
            $t = $(el);
            hash = $t.data('hash');
            users = 'from:' + $t.data('followers').split(' ').join('').split(',').join(' OR from:');
            cnt = Number($t.data('count'));
            if (cnt < 1) { cnt = 1; }
            tweets = $t.find('.tweets');
            $.getJSON('http://search.twitter.com/search.json?q=%23' + hash.split('#').join('') + ' ' + users + '&rpp=' + cnt + '&callback=?', function(r) {
                updateTweets(tweets, r);
            });
        });
    };
    function updateTweets(ul, data) {
        var tweet = '', html = '', url = '';
        if (data.results !== null && data.results.length < 1) {
            ul.html('<li>No tweets today.</li>');
            return;
        }
        $.each(data.results, function(i, el) {
            tweet = '';
            url = 'http://twitter.com/' + el.from_user;
            tweet += '<a href="' + url + '" class="thumb" target="_blank"><img src="' + el.profile_image_url + '" alt="" height="36" width="36" /></a>';
            tweet += '<a href="' + url + '" class="name" target="_blank">' + el.from_user + '</a><br/>';
            tweet += el.text + ' <br/><span class="time">' + relTime(el.created_at) + '</span>';
            html += '<li>' + tweet + '</li>';
        });
        ul.html(html);
    }

    function relTime(time) {
        time = time.replace(/(\+[0-9]{4}\s)/ig, "");
        var parsed_date = Date.parse(time);
        var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
        var ago = parseInt((relative_to.getTime() - parsed_date) / 1000);
        if (ago < 60) { return 'less than a minute ago'; }
        else if (ago < 120) { return 'about a minute ago'; }
        else if (ago < (45 * 60)) { return (parseInt(ago / 60)).toString() + ' minutes ago'; }
        else if (ago < (90 * 60)) { return 'about an hour ago'; }
        else if (ago < (24 * 60 * 60)) { return 'about ' + (parseInt(ago / 3600)).toString() + ' hours ago'; }
        else if (ago < (48 * 60 * 60)) { return '1 day ago'; }
        else { return (parseInt(ago / 86400)).toString() + ' days ago'; }
    }
    return app;
} (jQuery));

$(function() {
    APP.init();
     $('.container').equalHeights();
    $('.custom_button, .hover_menu').mouseenter(function() {
        $('.hover_menu').fadeIn('fast');
        $('.custom_button').addClass('active');
        $(this).data('in', true);
        $('.hover_menu').data('hidden', false);
    }).mouseleave(function() {
        $(this).data('in', false);
        setTimeout(hideMenu, delay);
    });

    var delay = 400;
    function hideMenu() {
        if (!$('.custom_button').data('in') && !$('.hover_menu').data('in') && !$('.hover_menu').data('hidden')) {
            $('.hover_menu').fadeOut('fast');
            $('.custom_button').removeClass('active');
            $('.hover_menu').data('hidden', true);
        }
    }
});


