﻿(function ($) {

    // Toogle watermark on input fields
    $(':input[type=text][title]').each(function () {

        var $this = $(this);
        if ($this.val() !== '')
            return;

        $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'));
            }
        });
    });

    if (typeof initializeGoogleMaps == 'function') {
        initializeGoogleMaps();
    }

    if (typeof scrollToBottom == 'function') {
        scrollToBottom();
    }

    // Add css to every other tablerow
    $('table.default').each(function (index) {
        var stripingTable = $(this);
        $('tr:odd', stripingTable).addClass('odd');
    });

    // Tabs
    $(".jqTabs li a").each(function (index) {
        $(this).click(function () {
            var parent = $(this).parent();
            if (!($(parent).is(".active"))) {
                $(parent).addClass("active");
                $(parent).siblings().removeClass("active");
            }

            $(this).parents(".col1").find(".wrap").each(function () {
                if (!($(this).is(".hiddenTxt"))) {
                    $(this).addClass("hiddenTxt");
                } else {
                    $(this).removeClass("hiddenTxt");
                }
            });
            return false;
        });
    });

    $('.slideShow').cycle({
        fx: 'fade',
        speed: 2000,
        timeout: 7000,
        pause: 1,
        pager: '#nav',
        slideExpr: 'div.slide'
    });
    
    // Dictionary page
    $("#dictionaryWrapper div").each(function () {
        if (!($(this).is(".active"))) {
            $(this).find("p").addClass("hiddenTxt");
        }

        $(this).click(function () {
            if (($(this).is(".active"))) {
                $(this).removeClass("active");
                $(this).find("p").addClass("hiddenTxt");
            } else {
                $(this).addClass("active");
                $(this).find("p").removeClass("hiddenTxt");
            }
        });
    });

    //Fancybox
    if ($.fancybox) {
        $("a.iframe").fancybox({
            'width': 600,
            'height': 500,
            'autoScale': false,
            'scrolling': 'no',
            'transitionIn': 'none',
            'transitionOut': 'none',
            'type': 'iframe'
        });
    }

    // Font resizing
    $.fn.fontresizing = function (customOptions) {
        var options = $.extend({}, $.fn.fontresizing.defaultOptions, customOptions);
        var bodyClasses = '' + options.smallClass + ' ' + options.mediumClass + ' ' + options.largeClass + '';
        return this.each(function () {

            $('#resize ul li a').each(function () {
                $(this).click(function () {
                    var cssClass = $(this).attr('class');
                    if (cssClass == options.smallClass) {
                        $('#mainstylesheet').attr('href', options.smallCssPath);
                        $('#resize ul li a.normal').hide();
                        $('#resize ul li a.biggest').show();
                    }
                    else if (cssClass == options.largeClass) {
                        $('#mainstylesheet').attr('href', options.largeCssPath);
                        $('#resize ul li a.biggest').hide();
                        $('#resize ul li a.normal').show();
                    }
                    createCookie('Stylesheet', cssClass, options.cookieDuration);
                    return false;
                });
            });

            var fontresizingClass = readCookie('Stylesheet');
            if (fontresizingClass == options.smallClass) {
                $('#mainstylesheet').attr('href', options.smallCssPath);
                $('#resize ul li a.normal').hide();
            }
            else if (fontresizingClass == options.largeClass) {
                $('#mainstylesheet').attr('href', options.largeCssPath);
                $('#resize ul li a.biggest').hide();
            }
            else if (fontresizingClass == null) {
                $('#resize ul li a.normal').hide();
            }
        });
    };
    $.fn.fontresizing.defaultOptions = {
        smallClass: 'normal',
        largeClass: 'biggest',
        smallCssPath: '/Styles/styles.css',
        largeCssPath: '/Styles/large.css',
        fontresizingClass: '',
        cookieDuration: 365
    };

    $('#resize').fontresizing();
    $('#resize').toggle();

})(jQuery);

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;
}

// 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 displaysummary() {
    setTimeout("delayedShowValidation()", 1);
}

function delayedShowValidation() {
    if (Page_IsValid != null && Page_IsValid == false) {
        $('.errorValidation').attr('style', 'display:block');
        $(document).scrollTop($(document).height());
    }
}

