﻿(function ($) {
    // Cookie functions
    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }

        return null;
    }

    // Font sizing
    $.fn.fontresizing = function (customOptions) {
        var options = $.extend({}, $.fn.fontresizing.defaultOptions, customOptions);
        var bodyClasses = '' + options.smallClass + ' ' + options.mediumClass + ' ' + options.largeClass + '';
        return this.each(function () {
            $(this).append('<ol class="' + options.fontresizingClass + '"><li><a href="" class="' + options.smallClass + '">A</a></li><li><a href="" class="' + options.mediumClass + '">A</a></li><li><a href="" class="' + options.largeClass + '">A</a></li></ol>');

            $('ol.' + options.fontresizingClass + ' a').click(function () {
                var cssClass = $(this).attr('class');
                $('body').removeClass(bodyClasses).addClass(cssClass);
                createCookie('fontresizingClass', cssClass, options.cookieDuration);

                return false;
            });

            var fontresizingClass = readCookie('fontresizingClass');
            if (fontresizingClass == options.smallClass || fontresizingClass == options.mediumClass || fontresizingClass == options.largeClass) {
                $('body').removeClass(bodyClasses).addClass(fontresizingClass);
            }
        });
    };

    $.fn.fontresizing.defaultOptions = {
        smallClass: 'small',
        mediumClass: 'normal',
        largeClass: 'large',
        fontresizingClass: 'font-resizing',
        cookieDuration: 365
    };

    $('#font-sizing').fontresizing();

    // Contrast styleswitcher
    $('#contrast .styleswitch').click(function () {
        switchStylestyle(this.getAttribute("rel"));
        return false;
    });
    var c = readCookie('style');
    if (c) switchStylestyle(c);

    function switchStylestyle(styleName) {
        $('link[rel*=style][title]').each(function (i) {
            this.disabled = true;
            if (this.getAttribute('title') == styleName) {
                this.disabled = false;
                $('.styleswitch').hide();
                if (styleName === "main") {
                    $('.styleswitch[rel=contrast]').show();
                }
                if (styleName === "contrast") {
                    $('.styleswitch[rel=main]').show();
                }
            }
        });
        createCookie('style', styleName, 365);
    }

    // Toogle watermark on input fields
    $(':input[title]').each(function () {
        var $this = $(this);
        if ($this.val() === '') {
            $this.val($this.attr('title'));
        }
        $this.focus(function () {
            if ($this.val() === $this.attr('title')) {
                $this.val('');
            }
        });
        $this.blur(function () {
            if ($this.val() === '') {
                $this.val($this.attr('title'));
            }
        });
    });

    // Xform default button and enter key fix
    $('#id_matrix input').keypress(function (e) {
        if (e.keyCode && e.keyCode == 13) {
            $('#id_matrix :submit:first').click();
            return false;
        } else {
            return true;
        }
    });
    // Lightbox
    if (jQuery().fancybox) {
        $('a.featured').fancybox();
    }


})(jQuery);

//Employee list
$().ready(function () {


    var prevUrl = '';
    $('#peopleWrapper .peopleList li a').hover(function () {

        var dynamicContact = $('div.dynamicContact');
        $('#peopleWrapper .peopleList li a').stop();
        dynamicContact.stop();


        $(this).animate({ opacity: 1 }, 500, function () {
            var url = $(this).attr('href');
            if (url != prevUrl) {

                $.get('/Templates/Services/PersonHandler.ashx?personUrl=' + escape(url), function (data) {
                    dynamicContact.animate(
                { opacity: 0 }, 300, function () {

                    dynamicContact.html(data);
                    dynamicContact.animate({ opacity: 1 }, 300, function () { active = false; })
                });
                });
            }
            prevUrl = url;
        });
    },
        function () { $('#peopleWrapper .peopleList li a').stop(); }
    );
});

// Toggle fadein
jQuery.fn.fadeToggle = function (speed, easing, callback) {
    return this.animate({ opacity: 'toggle' }, speed, easing, callback);

};

$("#selectortoggler").click(function () {
    $('.innerSelector').fadeToggle();
});


$().ready(function () {
    var selectortoggler = $("#selectortoggler");
    if (selectortoggler.length > 0) {
        $(document.body).click(function () {
            var innerSelector = $('.innerSelector');
            if (innerSelector.css('opacity') == 1) {
                $('.innerSelector').fadeOut();
            }
        });
    }
}); 
