﻿var zbTab = new function($) {
    var $options = {
        namespace: 'Consumer'
    };

    function getContext(o) {
        var obj = $(o);
        var viewHolder = obj.parents("li:first");
        var editorHolder = $("#tabEditor"); // viewHolder.next("li");
        var editor = editorHolder.find("input");
        return { viewHolder: viewHolder, editorHolder: editorHolder, editor: editor, defaultText: editor.val(), sourceObject: obj };

    };

    this.init = function(options) {
        $.extend($options, options);
    };

    this.addTab = function(o) {
        var context = getContext(o);

        editMode(context);
        context.editor.bind("keyup.zbTab", function(e) { if (e.keyCode == 13) add(context); });
        context.viewHolder.bind("click.zbTab", function() { return false; })
        context.editorHolder.bind("click.zbTab", function() { return false; })
        $(document).bind("click.zbTab", function() { add(context); });
        $(document).bind("keydown.zbTab", function(e) { if (e.which == 27) cancel(context); });

    };

    this.editTab = function(o, tabId) {
        var context = getContext(o);


        editMode(context);
        context.editor.bind("keyup.zbTab", function(e) { if (e.keyCode == 13) save(context, tabId); });
        context.viewHolder.bind("click.zbTab", function() { return false; })
        context.editorHolder.bind("click.zbTab", function() { return false; })
        $(document).bind("click.zbTab", function() { save(context, tabId); });
        $(document).bind("keydown.zbTab", function(e) { if (e.which == 27) cancel(context); });
    };

    this.deleteTab = function(tabId, confMessage) {
        if (!tabId) return

        if (!confirm(confMessage)) return;
        var options = { async: true,
            postType: 'GET',
            assembly: 'CentrSource',
            methodClass: 'CentrSource.' + $options.namespace + '.Web.UI.WebControls.WebParts.WebPartManager',
            methodName: 'DeleteTab',
            args: [zwpm.space, tabId],
            onOk: function(value) {
                if (!value) return;
                window.location.href = value;
                return;
            }

        };
        zajaxm.call(options);
    };

    function editMode(context) {
        context.viewHolder.hide();
        context.editorHolder.fadeIn("fast");
        context.editor.focus().select();
    };

    function viewMode(context) {
        context.editorHolder.hide();
        context.viewHolder.fadeIn("fast");
    };

    function unbind(context) {
        $(document).unbind("keydown.zbTab");
        context.editor.unbind("keyup.zbTab");
        $(document).unbind("click.zbTab");

    }

    function cancel(context) {
        unbind(context);
        viewMode(context);
        context.editor.val(context.defaultText);
    };

    function add(context) {
        var lval = $.trim(context.editor.val());
        if (!lval || lval.length == -1) { cancel(context); return; }
        var options = { async: true,
            postType: 'GET',
            assembly: 'CentrSource',
            methodClass: 'CentrSource.' + $options.namespace + '.Web.UI.WebControls.WebParts.WebPartManager',
            methodName: 'AddNewTab',
            args: [zwpm.space, escape(lval)],
            onOk: function(value) {
                if (!value) return;
                unbind(context);
                window.location.href = value;
                return;
            },
            onError: function(e, o, q) {
                cancel(context);
            }
        };
        zajaxm.call(options);
    };

    function save(context, tabId) {
        var lval = $.trim(context.editor.val());
        if (!lval || lval.length == -1) { cancel(context); return; }
        var options = { async: true,
            postType: 'GET',
            assembly: 'CentrSource',
            methodClass: 'CentrSource.' + $options.namespace + '.Web.UI.WebControls.WebParts.WebPartManager',
            methodName: 'SaveTab',
            args: [zwpm.space, tabId, escape(lval)],
            onOk: function(value) {
                unbind(context);
                viewMode(context);
                context.sourceObject.html(lval);
                return;
            },
            onError: function() {
                cancel(context);
            }
        };
        zajaxm.call(options);
    };


} (jQuery);