﻿function toggleClientValidation() {
    Page_ValidationActive = !Page_ValidationActive;
}

function validationEmailLink() {
    var $validationSummaryContainer = $('.validationSummaryContainer');

    if ($validationSummaryContainer.length > 0) {
        var $link;
        $link = $('.validationEmailLink', $validationSummaryContainer);
        if ($link.length == 0) {
            $link = $('<a class="button validationEmailLink">Email Errors</a>').click(function (eventObject) {
                var body = '';
                $.each(Page_Validators, function (index, validator) {
                    if (!validator.isvalid) {
                        body += encodeURI(validator.errormessage) + '%0D';  //validator.errormessage.replace(' ', '%20') + '%0D';
                    }
                });

                $(this).attr('href', 'mailto:' + __validationEmailTo + '?subject=Validation%20Errors&body=' + body);
            });
            $validationSummaryContainer.append($link);
            $link.button();
        }
    }
}

function mainmenu() {
    $("ul.subnav").css({ display: "none" }); // Opera Fix

    $("ul.topnav li").hover(function () { //When trigger is clicked...

        var $this = $(this);
        //Following events are applied to the subnav itself (moving subnav up and down)
        $('.item .ui-icon-circle-triangle-e', $this).removeClass('ui-icon-circle-triangle-e').addClass('ui-icon-circle-triangle-s');
        $this.addClass("subhover"); //On hover over, add class "subhover"
        $this.find("ul.subnav").stop(true, true).slideDown('fast'); //Drop down the subnav on click

    },
        function () {
            var $this = $(this);
            $('.item .ui-icon-circle-triangle-s', $this).removeClass('ui-icon-circle-triangle-s').addClass('ui-icon-circle-triangle-e');
            $this.removeClass("subhover"); //On hover out, remove class "subhover"
            $this.find("ul.subnav").stop(true, true).slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
        });

}

$(function () {
    mainmenu();

    $(".btn").button();
    $(".datePicker").datepicker({ showAnim: jQuery.support.boxModel ? 'slide' : null, duration: 'fast',
        onSelect: function (dateText, inst) {

            // todo: close date picker on select 
            if ($.browser.msie && $.browser.version <= 9) {
                // MSIE 7 triggers focus event twice, forcing the datepicker to re-open
                // to get around this, we move the focus to the next form element
                var $next = $(this).parent().next("input[type='text']");
                if ($next != null) $next.focus();
            } else {
                // give focus back to the input element to preserve tabbing
                $(this).trigger('focus');
            }
        }
    });
    $('.deleteLink').click(function (eventObject) {
        $.post(this.href, function (data, status, xhr) {
            window.location.reload();
        });
        eventObject.preventDefault();

    });

    $(".expand").toggler();

    $tables = $('table.dataTable');
    if ($tables.length > 0) {
        $("table.dataTable").dataTable({ bJQueryUI: true,
            bFilter: true,
            bInfo: true,
            bPaginate: true,
            bSort: false

        });
    }
    if (typeof (Page_Validators) !== "undefined") {
        $.each(Page_Validators, function (index, validator) {
            if (!validator.isvalid) {
                validationEmailLink();
                return false;
            }
        });
    }

});

$.ajaxSetup({ error: function (XMLHttpRequest, textStatus, errorThrown) {

    return; 
    $.ajax
            ({
                url: __siteRoot + '/Logging.mvc/Error',
                type: 'POST',
                data: { HttpRequest: XMLHttpRequest, TextStatus: textStatus, ErrorThrown: errorThrown },
                dataType: 'json',
                complete: function (xhr, status) { },
                error: function (error) {
                    // error in handler, ignore
                }
            });
}
});
