﻿var zbTab = new function ($) {
    var $options = {
        namespace: 'Consumer'
    };

    function getContext(o) {
        var obj = $(o);
        var viewHolder = obj.parents("li:first");
        var editorHolder = viewHolder.next("li"); //$("#tabEditor"); 
        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 (event) {

            if (event.keyCode == 13) {
                add(context);
            }
        });

        context.viewHolder.bind("click.zbTab", function (event) {
            return false;
        });

        context.editorHolder.bind("click.zbTab", function (event) {
            return false;
        });

        $(document).bind("click.zbTab", function (event) {
            add(context);
        });

        $(document).bind("keydown.zbTab", function (event) {
            if (event.keyCode == 27) cancel(context);
        });

        //cancel event
        return false;
    };

    this.editTab = function (o, tabId) {
        var context = getContext(o);

        editMode(context);

        context.editor.bind("keyup.zbTab", function (event) {
            if (event.keyCode == 13) save(context, tabId);
        });

        context.viewHolder.bind("click.zbTab", function (event) {
            return false;
        });

        context.editorHolder.bind("click.zbTab", function (event) {
            return false;
        });

        $(document).bind("click.zbTab", function (event) {
            save(context, tabId);
        });

        $(document).bind("keydown.zbTab", function (event) {
            if (event.keyCode == 27) cancel(context);
        });

        //cancel event
        return false;
    };

    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; }
        unbind(context);
        var options = { async: true,
            postType: 'POST',
            assembly: 'CentrSource',
            methodClass: 'CentrSource.' + $options.namespace + '.Web.UI.WebControls.WebParts.WebPartManager',
            methodName: 'AddNewTab',
            args: [zwpm.space, lval],
            onOk: function (value) {
                if (!value) return;
                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; }
        unbind(context);
        var options = { async: true,
            postType: 'POST',
            assembly: 'CentrSource',
            methodClass: 'CentrSource.' + $options.namespace + '.Web.UI.WebControls.WebParts.WebPartManager',
            methodName: 'SaveTab',
            args: [zwpm.space, tabId, lval],
            onOk: function (value) {
                viewMode(context);
                context.sourceObject.html(lval);
                return;
            },
            onError: function () {
                cancel(context);
            }
        };
        zajaxm.call(options);
    };


} (jQuery);
