
App = {
    init: function() {
        var SEARCH = '#search-form input';
        $('#sub-nav li').mouseenter(App.onSubOver).mouseleave(App.onSubOut);
        // hack to remove padding/margin from the first element.  Move to the application
        $('#content :first').css('paddingTop', '0px').css('marginTop', '0px');

        this.blockQuotes();
        this.searchInput(SEARCH, 'Search');
        this.trackDownloads();
        stretch();
        $(window).resize(stretch);
    },
    onSubOver: function(e) {
        var $el = $(e.currentTarget);
        $el.find('ul:first').show();
    },
    onSubOut: function(e) {
        var $el = $(e.currentTarget);
        $el.find('ul:first').hide();
    },
    blockQuotes: function() {
    $('#content blockquote').each(function() {
        /*
            $(this).css('paddingTop', '24px').css('height', 'auto');

            if ($(this).width() > 520) $(this).width(472);
            var children = $(this).children();
            var first = $(children[0]);
            var text = first.text();
            var isQuote = text.indexOf('"') == 0;
            var lquote = '<span class="quote">&ldquo;</span>';
            var rquote = '<span class="quote">&rdquo;</span>';

            if (isQuote) {
            if (children.length == 1) {
            text = text.substring(1, text.length);
            if (text.substring(text.length - 1, text.length) == '"') {
            text = text.substring(0, text.length - 1);
            }
            first.text(text);
            first.prepend(lquote).append(rquote);
            return;
            }
            first.text(text.substring(1, text.length));
            first.prepend(lquote);
            }

            var last = children.length - 1;
            text = $(children[last]).text();
            var hasDash = text.indexOf('-') == 0 || text.indexOf('–') == 0 || text.indexOf('&ndash;') == 0;

            if (!hasDash) {
            target = children[last];
            } else {
            if (children.length == 2) {
            if (isQuote) {
            text = first.text();
            text = text.substring(1, text.length);
            if (text.substring(text.length - 1, text.length) == '"') {
            text = text.substring(0, text.length - 1);
            }
            first.text(text);
            first.prepend(lquote).append(rquote);
            }
            $(children[children.length - 1]).addClass('quote');
            return;
            }

                last--;
            target = children[last];
            if ($.trim($(target).text()).length <= 1) {
            while ($.trim($(target).text()).length <= 1 && last > 0) {
            target = children[last];
            last--;
            }
            }
            $(children[children.length - 1]).addClass('quote');
            }
            text = $(target).text();
            isQuote = isQuote && text.substring(text.length - 1, text.length) == '"';
            if (isQuote) {
            $(target).text(text.substring(0, text.length - 1));
            $(target).append(rquote);
            }
            */
        });
    },
    searchInput: function(id, text) {
        var d = text;
        $id = $(id);
        $id.focus(function() { if ($(this).val() == d) $(this).val(''); });
        $id.blur(function() { if ($.trim($(this).val()) == '') $(this).val(d); });
    },
    trackDownloads: function() {
        $("a[rel*='external']").click(function() { pageTracker._trackEvent('External link', '', $(this).attr('href')); });
        $('a[href^=/media/download/]').click(function() { App.track(this, 'id'); });
        for (var ext in types) {
            $('a[href*=.' + ext + ']').click(function() {
                App.track(this);
            });
        }

    },
    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];
        pageTracker._trackEvent(filetype + ' files', 'Download', filename);
        return false;
    }
}

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';

var w, b;
var maxW = 1000;
function stretch() {
    if (w == null) w = $(window);
    if (b == null) b = $('body');
    try {
        b.width(w.width() < maxW ? maxW : 'auto');
    } catch (e) { }
}
// window load
$().ready(function() {
    App.init();
});

