﻿(function ($) {
    Cufon.replace('ul#nav a', { textShadow: '#9f090d -1px 1px, #9f090d 1px 2px', fontFamily: 'myriad-pro', hover: true });
    Cufon.replace('.promo-side h2', { fontFamily: 'franklin-gothic-demi-cmpr' });
    Cufon.replace('.aside h2', { fontFamily: 'franklin-gothic-demi-cmpr' });
})(jQuery);

$(document).ready(function () {
    Skarpa.initHighslide();
    Skarpa.initExternalLinks();
    Skarpa.initPrint();
    Skarpa.initComments();
    Skarpa.initBlinds();
    Skarpa.initAccordion();
    Skarpa.initAutoScalingNav({
        menuId: "nav",
        sideClasses: true,
        minPaddings: 10
    });
});
var Skarpa = {
    initAccordion: function () {
        $('.accordion').accordion({ header: 'div.heading', collapsible: true, autoHeight: false });
    },
    initBlinds: function () {
        $('.btn-close, .btn-open').click(function () {
            $(this).toggleClass('btn-open btn-close');
            $(this).closest('li').find('ul').toggle('blind');
        });
    },
    initEmailObfuscation: function () {
        $('.contact-text a').each(function () {
            $(this).attr('href', Skarpa.rot13($(this).attr('href')));
            $(this).text(Skarpa.rot13($(this).text()));
        });
    },
    initComments: function () {
        $('.comment').click(function () {
            $('#disqus_thread').toggle();
        });
    },
    initPrint: function () {
        $('.print').click(function () {
            $('#main').printElement({
                printMode: 'popup',
                printBodyOptions: {
                    classNameToAdd: 'print'
                }
            });
        });
    },
    initExternalLinks: function () {
        $('a[rel*="external"]').attr('target', '_blank');
        $('a[rel*="external"], a[href^="http"]').click(function () {
            if (_gaq)
                _gaq.push(['_trackPageview', '/external/#!/' + $(this).attr('href')]);
        });
    },
    initRelatedDocumentsTracking: function () {
        $('a.file').click(function () {
            if (_gaq)
                _gaq.push(['_trackPageview', $(this).attr('rel')]);
        });
    },
    initAutoScalingNav: function (o) {
        if (!o.menuId) o.menuId = "nav";
        if (!o.tag) o.tag = "a";
        if (!o.spacing) o.spacing = 0;
        if (!o.constant) o.constant = 0;
        if (!o.minPaddings) o.minPaddings = 0;
        if (!o.liHovering) o.liHovering = false;
        if (!o.sideClasses) o.sideClasses = false;
        if (!o.equalLinks) o.equalLinks = false;
        if (!o.flexible) o.flexible = false;
        var nav = document.getElementById(o.menuId);
        if (nav) {
            nav.className += " scaling-active";
            var lis = nav.getElementsByTagName("li");
            var asFl = [];
            var lisFl = [];
            var width = 0;
            for (var i = 0, j = 0; i < lis.length; i++) {
                if (lis[i].parentNode == nav) {
                    var t = lis[i].getElementsByTagName(o.tag).item(0);
                    asFl.push(t);
                    asFl[j++].width = t.offsetWidth;
                    lisFl.push(lis[i]);
                    if (width < t.offsetWidth) width = t.offsetWidth;
                }
                if (o.liHovering) {
                    lis[i].onmouseover = function () {
                        this.className += " hover";
                    }
                    lis[i].onmouseout = function () {
                        this.className = this.className.replace("hover", "");
                    }
                }
            }
            var menuWidth = nav.clientWidth - asFl.length * o.spacing - o.constant;
            if (o.equalLinks && width * asFl.length < menuWidth) {
                for (var i = 0; i < asFl.length; i++) {
                    asFl[i].width = width;
                }
            }
            width = getItemsWidth(asFl);
            if (width < menuWidth) {
                var version = navigator.userAgent.toLowerCase();
                for (var i = 0; getItemsWidth(asFl) < menuWidth; i++) {
                    asFl[i].width++;
                    if (!o.flexible) {
                        asFl[i].style.width = asFl[i].width + "px";
                    }
                    if (i >= asFl.length - 1) i = -1;
                }
                if (o.flexible) {
                    for (var i = 0; i < asFl.length; i++) {
                        width = (asFl[i].width - o.spacing - o.constant / asFl.length) / menuWidth * 100;
                        if (i != asFl.length - 1) {
                            lisFl[i].style.width = width + "%";
                        }
                        else {
                            if (navigator.appName.indexOf("Microsoft Internet Explorer") == -1 || version.indexOf("msie 8") != -1 || version.indexOf("msie 9") != -1)
                                lisFl[i].style.width = width + "%";
                        }
                    }
                }
            }
            else if (o.minPaddings > 0) {
                for (var i = 0; i < asFl.length; i++) {
                    asFl[i].style.paddingLeft = o.minPaddings + "px";
                    asFl[i].style.paddingRight = o.minPaddings + "px";
                }
            }
            if (o.sideClasses) {
                lisFl[0].className += " first-child";
                lisFl[0].getElementsByTagName(o.tag).item(0).className += " first-child-a";
                lisFl[lisFl.length - 1].className += " last-child";
                lisFl[lisFl.length - 1].getElementsByTagName(o.tag).item(0).className += " last-child-a";
            }
            nav.className += " scaling-ready";
        }
        function getItemsWidth(a) {
            var w = 0;
            for (var q = 0; q < a.length; q++) {
                w += a[q].width;
            }
            return w;
        }
    },
    initHashcheck: function () {
        $(window).bind('hashchange', function (e) {
            var url = $.param.fragment();
            if (url == '')
                return;

            var id = parseInt(url.substring(2));
            if (isNaN(id))
                return;

            $('a.highslide[rel="img-' + id + '"]').trigger('click');
        });
        $(window).trigger('hashchange');
    },
    initHighslideGallery: function () {
        if (hs) {
            hs.captionOverlay.position = 'rightpanel';
            hs.captionOverlay.width = '240px';
            if (hs.addSlideshow) {
                hs.addSlideshow({
                    interval: 5000,
                    repeat: false,
                    useControls: true,
                    fixedControls: false,
                    overlayOptions: {
                        opacity: 0.65,
                        position: 'bottom center',
                        hideOnMouseOut: true
                    }
                });
            }
            hs.overlays[0].offsetX = 245;
        }
    },
    initHighslide: function () {
        if (hs) {
            hs.blockRightClick = true;
            hs.graphicsDir = '/UI/Scripts/highslide/graphics/';
            hs.align = 'center';
            hs.transitions = ['expand', 'crossfade'];
            hs.outlineType = 'rounded-white';
            hs.fadeInOut = true;
            hs.dimmingOpacity = 0.75;
            hs.showCredits = false;
            hs.registerOverlay({
                html: '<div class="closebutton" onclick="return hs.close(this);" title="' + hs.lang.closeText + '"></div>',
                position: 'top right',
                fade: 2
            });
        }
    }
}
