/// <reference path="jquery-1.7.1.js" />
/// <reference path="jquery-1.7-vsdoc.js" />
/// <reference path="jquery-ui-1.8.18.custom.js" />
/// <reference path="jquery.tooltip.js" />
/// <reference path="jquery.numberformatter-1.2.3.min.js" />
// DOM ready
$(function () {
    $('.my-alternate tr:even').addClass('alternate-row');

    $('.my-selectable tr').click(function (evt) {
        var radio = $('input[type="radio"]', this)
        if (radio.length > 0) {
            $(this).closest('.my-selectable').find('tr').removeClass('highlight-row')
            $(this).addClass('highlight-row');
            radio.attr('checked', true);
            return;
        }

        var checkbox = $('input[type="checkbox"]', this)
        if (checkbox.length > 0) {
            if (checkbox.attr('checked')) {
                $(this).removeClass('highlight-row')
                checkbox.attr('checked', false);
            } else {
                $(this).addClass('highlight-row');
                checkbox.attr('checked', true);
            }
        }
    });

    $('.my-selectable tr input[type="checkbox"]').click(function (evt) {
        $(this).attr('checked', !$(this).attr('checked'));
    });

    $('input[type="password"]').each(function () {
        if ($(this).attr('data-autocomplete') == 'true') return;
        $(this).attr('autocomplete', 'off');
    });

    $('.my-selectable tr label').click(function (evt) {
        var checkbox = $('#' + $(this).attr('for'));
        checkbox.attr('checked', !checkbox.attr('checked'));
    });

    $('.my-selectable tr input[type="radio"][checked]').closest('tr').addClass('highlight-row');
    $('.my-selectable tr input[type="checkbox"][checked]').closest('tr').addClass('highlight-row');

    $('#dialog_box').dialog({
        width: 600, height: 315, modal: true, autoOpen: false,
        resizable: false, draggable: false
    });

    $('[data-confirm="true"]').click(function () {
        if ($(this).is("input"))
            my.showConfirmDialog($(this).attr('data-confirm-title'), $(this).attr('data-confirm-message'), $(this).closest('form'));
        if ($(this).is("a"))
            my.showConfirmLinkDialog($(this).attr('data-confirm-title'), $(this).attr('data-confirm-message'), $(this).attr('href'));
        return false;
    });

    $('[data-dialog="true"]').click(function () {
        my.showDialog($(this).attr('data-dialog-title'), $(this).attr('data-dialog-message'), 300);
    });

    $('[title]').tooltip({ showURL: false, positionLeft: __res.isRightToLeft, track: true });

    $('.my-datepicker-value').each(function () {
        if ($(this).val().indexOf('1/1/0001') == 0)
            $(this).val('');
    });

    $(".my-datepicker").datepicker({
        dateFormat: "yy/mm/dd",
        onSelect: function (dateText, inst) {
            my.datePickerInput(dateText, inst, this);
        },
        onClose: function (dateText, inst) {
            my.datePickerInput(dateText, inst, this);
        }
    });

    $(".my-datepicker").blur(null, function () {
        var dateText = $(this).val();
        var inst = $.data(this, 'datepicker');
        my.datePickerInput(dateText, inst, this);
    });

    $('.my-farsi-input').keypress(function (evt) {
        return PersianKeyPress(evt);
    });

    $('.my-farsi-input').keydown(function (evt) {
        return PersianKeyDown(evt);
    });
});

my = new function () {

    this.showHelpDialog = function (title) {
        $('.ui-dialog-titlebar').append('<span class="title_icon title_icon_help"></span>');
        $('#dialog_box').dialog('option', 'title', __res.helpTitlePrefix + title);
        $('#dialog_box').dialog('option', 'height', 315);
        $('#dialog_box').dialog('option', 'buttons', null);
        $('#dialog_box').empty();
        $('#dialog_box').append('<div class="loading"></div>')
        $('#dialog_box').dialog('open');
    }

    this.showHelpDialogError = function (ajaxContext) {
        $('#dialog_box').dialog('option', 'title', __res.error);
        $('#dialog_box').empty();
        $('#dialog_box').append(__res.helpErrorMessage)
        $('#dialog_box').dialog('open');
    }

    this.showPopupDialog = function (title) {
        $('.ui-dialog-titlebar').append('<span class="title_icon title_icon_popup"></span>');
        $('#dialog_box').dialog('option', 'title', title);
        $('#dialog_box').dialog('option', 'height', 250);
        $('#dialog_box').dialog('option', 'buttons', null);
        $('#dialog_box').empty();
        $('#dialog_box').append('<div class="loading"></div>')
        $('#dialog_box').dialog('open');
    }

    this.showDialogError = function (ajaxContext) {
        $('#dialog_box').dialog('option', 'title', __res.error);
        $('#dialog_box').empty();
        $('#dialog_box').append(__res.DialogErrorMessage)
        $('#dialog_box').dialog('open');
    }

    this.showDialog = function (title, message, size) {
        $('.ui-dialog-titlebar').append('<span class="title_icon title_icon_info"></span>');
        $('#dialog_box').dialog('option', {
            title: title,
            height: size == undefined ? 150 : size,
            buttons: [
                {
                    text: __res.close,
                    click: function () { $(this).dialog("close"); }
                }
            ]
        });
        $('#dialog_box').empty();
        $('#dialog_box').append(message);
        $('#dialog_box').dialog('open');
    }

    this.showConfirmLinkDialog = function (title, message, href) {
        $('.ui-dialog-titlebar').append('<span class="title_icon title_icon_info"></span>');
        $('#dialog_box').dialog('option', {
            title: title,
            height: 150,
            buttons: [
            {
                text: __res.confirm,
                click: function () {
                    $(this).dialog("close");
                    $(location).attr('href', href);
                }
            },
                {
                    text: __res.cancel,
                    click: function () { $(this).dialog("close"); }
                }
            ]
        });
        $('#dialog_box').empty();
        $('#dialog_box').append(message);
        $('#dialog_box').dialog('open');
    }

    this.showConfirmDialog = function (title, message, submitForm, onConfirm) {
        if (submitForm != undefined && !submitForm.valid())
            return;
        $('.ui-dialog-titlebar').append('<span class="title_icon title_icon_info"></span>');
        $('#dialog_box').dialog('option', {
            title: title,
            height: 150,
            buttons: [
                {
                    text: __res.confirm,
                    click: function () {
                        $(this).dialog("close");
                        if (submitForm != undefined)
                            submitForm.submit();
                        if (onConfirm != undefined)
                            onConfirm();
                    }
                },
                {
                    text: __res.cancel,
                    click: function () { $(this).dialog("close"); }
                }
            ]
        });
        $('#dialog_box').empty();
        $('#dialog_box').append(message);
        $('#dialog_box').dialog('open');
    }

    this.showErrorMessage = function (selector, message) {
        $(selector).empty();
        $(selector).append(message);
    }

    this.setOnlinePaymentAmount = function (amount) {
        $('#my_onlinePaymentAmount').empty();
        var formatedAmount = this.formatMoney(amount);
        $('#my_onlinePaymentAmount').append(formatedAmount);
    }

    this.formatMoney = function (amount) {
        if (amount == 0)
            return '-';
        var number = $.formatNumber(amount, { format: '#,###', locale: 'us' });
        return number + ' ' + __res.currency;
    }

    this.showLoading = function (selector) {
        $(selector).empty();
        $(selector).append('<div class="loading"></div>')
    }

    this.showMiniLoading = function (selector) {
        $(selector).empty();
        $(selector).append('<div class="loading-mini"></div>')
    }

    this.clearValidationSummaryErrors = function () {
        $('li', $('.validation-summary-errors')).empty();
    }

    this.datePickerInput = function (dateText, inst, obj) {
        try {
            if (dateText.match('^1[34][0-9][0-9]\/((1[0-2])|([0]?[1-9]))\/(([12][0-9])|(3[01])|([0]?[1-9]))$') == null) {
                $('.my-datepicker-value', $(obj).parent()).val('');
                return;
            }
            if (dateText == null || dateText == '') {
                $('.my-datepicker-value', $(obj).parent()).val('');
            }
            else {
                var d = new JalaliDate(inst['selectedYear'], inst['selectedMonth'], inst['selectedDay']).getGregorianDate();
                $('.my-datepicker-value', $(obj).parent()).val((d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear());
            }
        }
        catch (ex) {
            $('.my-datepicker-value', $(obj).parent()).val('');
        }
    }

    this.alternateRows = function () {
        $('.my-alternate tr:even').addClass('alternate-row');
    }

    this.debug = function (ajaxContext) {
        alert(ajaxContext.responseText);
    }
} ();
