﻿// jQuery iBuilder 2.0 Grid
//
// Version 1.0
//
// Created by Marcin Gielewski
//
// Usage:
// 
// History:
//
//		1.00 - Released (15.06.2010)
//
(function($) {

    $.jGrid = {

        // These properties can be read/written by accessing $.jGrid.propertyName from your scripts at any time

        ajaxUrl: '/Ajax/ControlsWrapper.aspx',        // form wrapper ajax url
        width: 400,                                   // popup window width
        height: 400,                                  // popup window hieght

        // Public methods

        advancedfilter: function(title, suid, ctrl, askey, w, h) {
            jPopup(title, $.jGrid.ajaxUrl + '?suid=' + suid + '&ctrl=' + ctrl + '&askey=' + askey, null, w != null ? w : width, h != null ? h : height);
        },
        addRow: function(title, suid, ctrl, w, h) {
            jPopup(title, $.jGrid.ajaxUrl + '?suid=' + suid + '&ctrl=' + ctrl, null, w != null ? w : width, h != null ? h : height);
        },
        editRow: function(title, suid, ctrl, id, w, h) {
            jPopup(title, $.jGrid.ajaxUrl + '?suid=' + suid + '&ctrl=' + ctrl + '&id=' + ctrl, null, w != null ? w : width, h != null ? h : height);
        },        
        deleteRows: function(title, message) {
            return jConfirm(message, title, null);
        },
        publishRows: function(title, message) {
            return jConfirm(message, title, null);
        },
        exportRows: function() {
            jAlert('Not implemented yet.');
        },
        publishImgClick: function(img) {
            $('input', $('td:last-child', $(img).parent().parent())).attr('checked', true);
        },

        // Private methods

        updateGrid: function(gid, suid, ctrl, pi, ps, sort, skey, askey) {
            $.alerts._hide();

            var over = $('<div>');
            over.addClass('clip');
            over.css({ width: $(gid).outerWidth() + 'px', height: $(gid).outerHeight() + 'px' });
            over.css($(gid).offset());
            over.html('&nbsp;');
            $(gid).after(over);

            $(gid).load($.jGrid.ajaxUrl + ' #formWrapperContent', { gid: gid, suid: suid, ctrl: ctrl, pi: pi, ps: ps, sort: sort, skey: skey, askey: askey }, function(response, status, xhr) {
                if (status == "error") {
                    var msg = "Sorry but there was an error: ";
                    $(gid).html(msg + xhr.status + " " + xhr.statusText);
                }
                over.remove();

                $("tr", $(gid)).hover(
                function() {
                    if (!$(this).hasClass('header')) $('td', $(this)).addClass("highlight");
                },
                function() {
                    if (!$(this).hasClass('header')) $('td', $(this)).removeClass("highlight");
                });
            });
        }
    }

    // Shortuct functions

    jGridUpdate = function(gid, suid, ctrl, pi, ps, sort, skey, askey) {
        $.jGrid.updateGrid(gid, suid, ctrl, pi, ps, sort, skey, askey);
    };
    jGridAdd = function(title, suid, ctrl, w, h) {
        $.jGrid.addRow(title, suid, ctrl, w, h);
    };
    jGridEdit = function(title, suid, ctrl, id, w, h) {
        $.jGrid.editRow(title, suid, ctrl, id, w, h);
    };
    jGridTranslate = function(title, suid, ctrl, id, w, h) {
        $.jGrid.editRow(title, suid, ctrl, id, w, h);
    };
    jGridSearch = function(title, suid, ctrl, askey, w, h) {
        $.jGrid.advancedfilter(title, suid, ctrl, askey, w, h)
    };
})(jQuery);
