﻿/// <reference path="jquery-1.4.4-vsdoc.js" />
/// <reference path="jquery.validate-vsdoc.js" />

var stopEvent = function (e) {
    if (!e) { e = window.event; }
    //e.cancelBubble is supported by IE - this will kill the bubbling process.
    e.cancelBubble = true;
    e.returnValue = false;
    //e.stopPropagation works only in Firefox.
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
    return false;
};
var alertDebug = function (message) {
    if (!$('#debugAlert')[0]) {
        $('body').append('<div id="debugAlert" style="position:fixed;top:100px;left:5px;z-index:1000;border:1px solid #010085;background:#fbfbff;padding:2px 5px;color:#000;-moz-box-shadow: 2px 2px 10px #383795;-webkit-box-shadow: 2px 2px 10px #383795;"><span style="color:#363636;display:block;padding:2px 0;">Debug Window</span><a href="#" onclick="$(this).parent().hide().find(\'#debugContent\').html(\'\');return false;" style="color:#f00;font-weight:bold;position:absolute;right:6px;text-decoration:none;top:3px;">X</a><div id="debugContent" style="border-top:1px solid #ccc;font-size:12px !important;max-height:400px;max-width:600px;min-height:100px;min-width:200px;overflow:auto;padding:2px;"></div></div>');
        if ($.ui && $.ui.draggable) {
            $('#debugAlert').draggable({ handle: 'span' }).find('span').css({ cursor: 'move' });
        }
    }
    $('#debugAlert').show().find('#debugContent').append(message + '<br />\n');
    return false;
};
window.alert = function (message) {
    alertDebug(message);
};
var printDmp = function (object) {
    var test = '';
    for (var key in object) {
        test += '<b>' + key + ':</b> ' + object[key] + '<br />\n';
    }
    alertDebug(test);
};


$.extend($.fn, {
    loader: function (css) {
        var self = $(this).html('<div class="loader" style="display:block;"></div>');
        if (css) {
            self.find('.loader').css(css);
        }
        return self;
    }
});
$.extend($.fn, {
    loaderAppend: function (css) {

        var self = $(this).append('<div class="loader" style="display:block;"></div>');

        if (css) {
            self.find('.loader').css(css);
        }

        return self;
    }
});

var loader = function (open) {
    open = typeof (open) == 'undefined' ? true : open;

    if (open) {
        $('#loader').show();
    }
    else {
        $('#loader').hide();
    }
};

$(document)
    .ajaxStart(function () {
        loader(true);
        $('#message.error').fadeOut();
    })
    .ajaxComplete(function () {
        loader(false);
    });


$.fn.onlyNumbers = function () {
    return $(this).keypress(function (e) {
        return !(e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57) && !(e.ctrlKey && (e.which == 99 || e.which == 118 || e.which == 120 || e.which == 121 || e.which == 122)));
    });
};
$.fn.closeOutsideDialog = function () {
    var self = this;
    $('.ui-widget-overlay').click(function () {
        self.dialog('close');
        $(this).unbind('click');
        return false;
    });
};
$.fn.resetForm = function () {
    return $(this).each(function () {
        var form = this;
        if (form.nodeName !== 'FORM')
            form = $(this).closest('form');

        form.find('input[type=text],select,input[type=password]').val('');
        form.find('select').val('0');
    });
};


if ($.ui && $.ui.dialog) {
    $.extend($.ui.dialog.prototype.options, {
        modal: true,
        autoOpen: false,
        open: function (event, ui) {
            $(this).closeOutsideDialog();
        }
    });
}

if ($.datepicker) {
    $.datepicker.setDefaults({
        dateFormat: 'yy-mm-dd',
        showButtonPanel: true,
        showWeek: true,
        numberOfMonths: 1,
        firstDay: 1,
        changeMonth: true,
        changeYear: true

    });
}

$.ajaxSetup({
    cache: false
});

var flash = function (message, className) {
    $('#flash div').parent().click(function (e) {
        $(this).slideUp();
    });
};


$(document).ready(function () {
    $('.number').onlyNumbers();
    if ($.datepicker) {
        $('.date').datepicker();
    }
});


$.fn.resetForm = function (callback) {

    function reset(el) {
        var form = el;
        if (form.nodeName !== 'FORM') {
            form = $(el).closest('form');
        }
        form.find('input[type=text],select,input[type=password]').val('');
        form.find('select').val('0');

        if (callback) {
            callback(form);
        }
    }

    return $(this).each(function () {
        if (this.nodeName !== 'FORM') {
            $(this).click(function (e) {
                stopEvent(e);
                reset(this);
            });
        } else {
            reset(this);
        }
    });
};



