﻿
/* 20100831 > FCFO > PLUGINS
--------------------------------- */

$(function() {

    $.fn.rollhover = function(settings) {

        var _config = $.fn.rollhover.defaults;
        this.each(function() {

            var $this = $(this);

            $this.hover(
                function() {
                    if ($this.attr("longdesc").length > 0) {
                        $this.attr("hover", $this.attr("src"))
                        $this.attr("src", $this.attr("longdesc"))
                    }
                },
                function() {
                    if ($this.attr("hover").length > 0) {
                        $this.attr("src", $this.attr("hover"))
                        $this.attr("hover", null)
                    }
                }
            );
        });

    }
    $.fn.rollhover.defaults = {};







    /* 20100909 > FCFO > Paginação
    ------------------------------------------------------------------------- */
    $.fn.jPager = function(settings) {

        var _config = $.fn.jPager.defaults;
        if (settings) $.extend(_config, settings);

        return this.each(function() {

            var _Container = $(this);
            _Container.unbind();
            _Container.empty();

            //control vars
            var _totalPages = Math.ceil(_config.totalRows / _config.maxRowsPerPage);
            var _firstVPage = (_config.currentPage - 1) - _config.sidePages;
            var _lastVPage = (_config.currentPage - 1) + _config.sidePages;

            if (_config.currentPage > _totalPages) { _config.currentPage = _totalPages }
            if (_config.currentPage < 1) { _config.currentPage = 1 }

            //transporta o excedente para a posição oposta
            if (_firstVPage < 0) {
                _lastVPage = _lastVPage + (-_firstVPage);
                _firstVPage = 0;
            }
            if (_lastVPage > _totalPages) {
                _firstVPage = _firstVPage - (_lastVPage - _totalPages);
                _lastVPage = _totalPages;
            }

            //Verifica os Limites das Páginas
            if (_firstVPage < 0) { _firstVPage = 0 };
            if (_lastVPage > _totalPages) { _lastVPage = _totalPages };

            var _jPagerMainContainer = $("<div class='jPager'></div>");

            var _RowsLabel = $("<div class='rowsLabel'><strong>" + _config.totalRows + "</strong> registos, página <strong>" + _config.currentPage + "</strong> de <strong>" + _totalPages + "</strong></div>");
            _jPagerMainContainer.append(_RowsLabel);

            //Cria o Layout da Página
            var _pageContainer = $("<div class='pageContainer'></div>");

            if (_firstVPage > 0) { _pageContainer.append("<a class='page' page='0'>" + _config.lblFirst + "</a>"); }
            if (_config.currentPage > 1) { _pageContainer.append("<a class='page' page='" + (_config.currentPage - 2) + "'>" + _config.lblPrevious + "</a>"); }

            for (i = _firstVPage; i < _lastVPage; i++) {
                var _classes = ((i + 1) == _config.currentPage) ? "cPage" : "page";
                var _link = $("<a class='" + _classes + "' page='" + i + "'>" + (i + 1) + "</a>");
                _pageContainer.append(_link);
            }

            if (_config.currentPage < _totalPages) { _pageContainer.append("<a class='page' page='" + _config.currentPage + "'>" + _config.lblNext + "</a>"); }
            if (_lastVPage < _totalPages) { _pageContainer.append("<a class='page' page='" + (_totalPages - 1) + "'>" + _config.lblLast + "</a>"); }

            _jPagerMainContainer.append(_pageContainer);
            _jPagerMainContainer.append("<div style='clear: both; height: 0px;'><div>")

            _Container.append(_jPagerMainContainer);
            _Container.find("a.page").click(function() {
                onChangePage($(this).attr("page"));
            });

            var _CurrentPage = 0;
            function onChangePage(nPage) {

                var retval = $(this).createDataPage();
                retval.rowIndex = nPage * _config.maxRowsPerPage;
                retval.pageLen = _config.maxRowsPerPage;
                retval.nPage = +nPage + 1;

                _CurrentPage = retval;
                _Container.trigger("onChangePage", retval);
            }

        });

    }
    $.fn.jPager.defaults = {
        totalRows: 0, // Numero total de rows
        maxRowsPerPage: 25, //Numero maximo de Registos por pagina
        sidePages: 5, //numero maximo de paginas visiveis antes e depois da pag. actual
        currentPage: 1, //Pagina actual
        lblFirst: '|<',
        lblLast: '>|',
        lblPrevious: '<',
        lblNext: '>'
    };






    $.fn.msgBox = function(settings) {

        var _config = $.fn.msgBox.defaults;
        if (settings) $.extend(_config, settings);
        var _container = $(this);

        this.each(function() {

            var _sDialog = $("<div title='" + _container.attr("title") + "'></div>");
            _sDialog.append("<div>" + _container.attr("message") + "</div>");

            $(_sDialog).dialog({
                bgiframe: true,
                zIndex: 9999,
                modal: true,
                resizable: false,
                buttons: {
                    OK: function() {
                        _config.callBack(this, null);
                        $(this).dialog('close').remove();
                    }
                }

            });

        });

        return this;
    }
    // default Settings
    $.fn.msgBox.defaults = {
        callBack: function(sender, eventArgs) { }
    };

});



jQuery.fn.extend({
    createDataPage: function() {
        return {
            rowIndex: 0,
            pageLen: 0,
            orderByField: "",
            orderByDir: "Ascending"
        };
    }
});

jQuery.extend({ alert: function(t, m) {
    var _div = $("<div title='" + t + "'>" + m + "</div>");
    _div.dialog({
        modal: true,
        resizable: false,
        buttons: {
            "Ok": function() {
                $(this).dialog('close');

            }
        }
    });
}
});





jQuery.extend({
    admin: function(s, m, d) {
        var retval = null;

        var oReqData = { "s": s, "m": m };
        jQuery.extend(oReqData, d);

        jQuery.ajax({
            url: "jsonAdmin.ashx",
            async: false,
            data: oReqData,
            type: "POST",
            dataType: "json",
            success: function(data) {
                retval = data;
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                retval = { rType: "error", jsonVal: XMLHttpRequest.statusText + ': ' + textStatus };
            }
        });

        return retval;
    }
});

jQuery.extend({
    accounts: {
        authenticate: function(u, p) {
            return $.admin("account", "authenticate", { "u": u, "p": p });
        }
    }
});

