﻿
// execute your scripts when DOM is ready. this is a good habit
$(function () {
    $.reject({
        imagePath: '/!includes/js/images/',
        closeCookie: true
    });  

    $('.dropdown dd').each(function () {
        var minWidth = 12;
        var maxWidth = 25;
        var fontsize = $('<dd id="menu-fontsize">&#8212;</dd>').css({
            'padding': 0,
            'position': 'absolute',
            'top': '-999em',
            'width': 'auto'
        }).appendTo($(this)).width();
        $('#menu-fontsize').remove();

        $(this).children('ul').each(function () {
            var $$ = $(this);
            $$.css({ 'display': 'block', 'visibility': 'hidden' });
            $$.css('width', 'auto');
            $$.children().css('white-space', 'nowrap')

            var emWidth = $$.outerWidth() / fontsize; ;

            if (emWidth > maxWidth) emWidth = maxWidth;
            if (emWidth < minWidth) emWidth = minWidth;

            $$.css({ 'width': emWidth + 'em' });

            var count = 0 - $$.outerWidth();
            while ($$.offset().left + $$.outerWidth() > $(document.forms[0]).offset().left + $(document.forms[0]).innerWidth()) {
                $$.css('left', 'auto');
                $$.css('right', count + 'px');
                count++;
            }

            $$.css({ 'display': '', 'visibility': '' })
            $$.children().css({ 'white-space': 'normal', 'width': '100%' });

            $$.prev('a').addClass('haschildren');
        });
    });

    $('.dropdown dd').hover(function () {
        $('.dropdown dd').find('ul').stop(true, true).parent().removeClass('over').css("z-index", "");

        if ($(this).children('ul').length > 0) {
            $(this).children('ul')
            .animate({ "height": "hide" }, 2000)
            .animate({
                "height": "show",
                "opacity": "show"
            }, "slow", "swing", function () {
                $(this).parent().addClass('over');
                $(this).css({ "height": "", "opacity": "", "display": "" });
            });
        } else {
            $(this).addClass('over');
        }

    }, function () {
        $(this).children('ul').stop(true, false);
        if ($(this).children('ul').length > 0) {
            $(this).children('ul')
                .animate({ "height": "show" }, 5000)
                .animate({
                    "height": "hide",
                    "opacity": "hide"
                }, "slow", "swing", function () {
                    $(this).parent().removeClass('over');
                    $(this).css({ "height": "", "opacity": "", "display": "" });
                });
        } else {
            $(this).removeClass('over');
        }
    });


    $("a[href^=http://]").each(function () {
        var rexcludedomains = new RegExp(window.location.hostname, "i");
        if (this.hostname.search(rexcludedomains) == -1)
            $(this).attr("target", "_blank");
    });

    $("a[rel*=external], a[href$=.pdf]").each(function () {
        $(this).attr("target", "_blank");
    });

    $("a[rel*=external], a[href^=http://]").one("click", function () {
        var rexcludedomains = new RegExp(window.location.hostname, "i");
        if (this.hostname.search(rexcludedomains) == -1)
            $.getScript('/track.axd' + '?key=js&action=5&data=' + escape($(this).attr("href")));
        //        window.open($(this).attr("href"));
        //        return false;
    });
});

$(document).ready(function() {
    var sizer = $('#tb_fontsize').FontSizer('main', 'fsUp', 'fsDown', 'fsNormal');
});

var Cookies = {
    add: function(name, value, days) {
        var expires = '';
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = '; expires=' + date.toGMTString(); ;
        }
        document.cookie = name + "=" + escape(value) + expires + "; path=/";
    },
    remove: function(name) {
        this.add(name, '', -1);
    },
    get: function(name) {
        var results = document.cookie.match(name + '=([^;]*?)(;|$)');
        if (results)
            return (unescape(results[1]));
        else
            return null;
    }
};

(function($) {
    jQuery.fn.FontSizer = function(type, upid, downid, resetid) {
        $.extend(this, {
            theconstructor: function(type, upid, downid, resetid) {
                this.type = type;
                this.baseSize = 14;
                this.minSize = 9;
                this.maxSize = 20;
                this.stepSize = 2;
                this.cookieName = type + 'FontSize';
                var x = Cookies.get(this.cookieName);
                this.currentSize = x ? parseInt(x) : this.baseSize;
                if (this.currentSize != this.baseSize) {
                    this.update();
                }
                var sizer = this;
                $('#' + upid).click(function(e) {
                    sizer.increase(e, this);
                });
                $('#' + downid).click(function(e) {
                    sizer.decrease(e, this);
                });
                $('#' + resetid).click(function(e) {
                    sizer.reset(e, this);
                });
                return this;
            },
            reset: function(e) {
                e.preventDefault();
                Cookies.remove(this.cookieName);
                this.currentSize = this.baseSize;
                this.update();
            },
            update: function() {
                Cookies.add(this.cookieName, this.currentSize, 7);
                cont = this.type;
                $('#' + cont).css('font-size', (this.currentSize / this.baseSize) + 'em');
            },
            increase: function(e) {
                e.preventDefault();
                this.currentSize = Math.min(this.currentSize + this.stepSize, this.maxSize);
                this.update();
            },
            decrease: function(e) {
                e.preventDefault();
                this.currentSize = Math.max(this.currentSize - this.stepSize, this.minSize);
                this.update();
            }
        });
        this.theconstructor(type, upid, downid, resetid);
        return this;
    };
})(jQuery);

function printWin(url) {
    popWin(url, "printWin", 700, 500, true);
}

var popWin = function(url, name, width, height, center) {
    var win,
    opt = [];
    if (width || height) {
        height = height || 570;
        width = width || 770;
        opt.push('height=' + height, 'width=' + width);
        if (center === false) {
            opt.push('top=' + ((screen.width) ? (screen.width - width) / 2 : 0));
            opt.push('left=' + ((screen.height) ? (screen.height - height) / 2 : 0));
        }
        opt.push('scrollbars=yes', 'resizable', 'menubar=1');
    }
    win = window.open(url, name, opt.join(','));
    if (win) win.focus();
    return win;
}
