// application wide javascript

;(function($){$.fn.superfish=function(f){var g=$.fn.superfish,c=g.c,$arrow=$(['<span class="',c.arrowClass,'"></span>'].join('')),over=function(){var a=$(this),menu=getMenu(a);clearTimeout(menu.sfTimer);a.showSuperfishUl().siblings().hideSuperfishUl()},out=function(){var a=$(this),menu=getMenu(a),o=g.op;clearTimeout(menu.sfTimer);menu.sfTimer=setTimeout(function(){o.retainPath=($.inArray(a[0],o.$path)>-1);a.hideSuperfishUl();if(o.$path.length&&a.parents(['li.',o.hoverClass].join('')).length<1){over.call(o.$path)}},o.delay)},getMenu=function(a){var b=a.parents(['ul.',c.menuClass,':first'].join(''))[0];g.op=g.o[b.serial];return b},addArrow=function(a){a.addClass(c.anchorClass).append($arrow.clone())};return this.each(function(){var s=this.serial=g.o.length;var o=$.extend({},g.defaults,f);o.$path=$('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){$(this).addClass([o.hoverClass,c.bcClass].join(' ')).filter('li:has(ul)').removeClass(o.pathClass)});g.o[s]=g.op=o;$('li:has(ul)',this)[($.fn.hoverIntent&&!o.disableHI)?'hoverIntent':'hover'](over,out).each(function(){if(o.autoArrows)addArrow($('>a:first-child',this))}).not('.'+c.bcClass).hideSuperfishUl();var b=$('li:last a',this);b.height('auto');var d=0;var w=b.parent().width();while(b.height()>40&&d<100){++w;b.parent().width(w);++d}b.height(40);var e=$('a',this);e.each(function(i){var a=e.eq(i).parents('li');e.eq(i).focus(function(){over.call(a)}).blur(function(){out.call(a)})});o.onInit.call(this)}).each(function(){var a=[c.menuClass];if(g.op.dropShadows&&!($.browser.msie&&$.browser.version<7))a.push(c.shadowClass);$(this).addClass(a.join(' '))})};var h=$.fn.superfish;h.o=[];h.op={};h.IE7fix=function(){var o=h.op;if($.browser.msie&&$.browser.version>6&&o.dropShadows&&o.animation.opacity!=undefined)this.toggleClass(h.c.shadowClass+'-off')};h.c={bcClass:'sf-breadcrumb',menuClass:'sf-js-enabled',anchorClass:'sf-with-ul',arrowClass:'sf-sub-indicator',shadowClass:'sf-shadow'};h.defaults={hoverClass:'sfHover',pathClass:'overideThisToUse',pathLevels:1,delay:800,animation:{opacity:'show'},speed:'normal',autoArrows:true,dropShadows:true,disableHI:false,onInit:function(){},onBeforeShow:function(){},onShow:function(){},onHide:function(){}};$.fn.extend({hideSuperfishUl:function(){var o=h.op,not=(o.retainPath===true)?o.$path:'';o.retainPath=false;var a=$(['li.',o.hoverClass].join(''),this).add(this).not(not).removeClass(o.hoverClass).find('>ul').hide().css('visibility','hidden');o.onHide.call(a);return this},showSuperfishUl:function(){var o=h.op,sh=h.c.shadowClass+'-off',$ul=this.addClass(o.hoverClass).find('>ul:hidden').css('visibility','visible');h.IE7fix.call($ul);o.onBeforeShow.call($ul);$ul.show();o.onShow.call($ul);return this}})})(jQuery);

App = {
    init: function() {
        var SEARCH = '#searchInputBox';
        $('#subNav').superfish({ delay: 100, speed: '0', dropShadows: false });
        // 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);
    },
    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();
});
