﻿//MultiView

/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />

var idprefix = "ctl00_Content_form_";

function $$get(id) {
    var elem = $get(id);

    if (!elem) {
        elem = $get(idprefix + id);
    }

    return elem;
}

Type.registerNamespace("Vamsoft");

Vamsoft.MultiView = function() {
    Vamsoft.MultiView.initializeBase(this);

    this._views = [];
    this._activeview = null;

    this.next = function(index) {
        if (index == null || index == "undefined" || index < 0 || index > this._views.length - 1) {
            return null;
        }

        if (index == this._views.length - 1) {
            return 0;
        } else {
            return ++index;
        }
    }

    this._setactiveview = function(index) {

        if (index == null || index == "undefined" || index < 0 || index > this._views.length) return;

        var prevview = this._activeview;

        if (prevview != null && prevview != index) {
            this._views[prevview].deactivate();
        }

        if (prevview != index) {
            try {
                this._views[index].activate();
                this._activeview = index;
            } catch (e) {
                if (prevview != null) this._views[prevview].activate();
                throw (e);
            }
        }
    }
}

Vamsoft.MultiView.prototype = {
    initialize: function() {
        Vamsoft.MultiView.callBaseMethod(this, 'initialize');
    },

    dispose: function() {
        Vamsoft.MultiView.callBaseMethod(this, 'dispose');
    },

    add: function(aview) {

        if (!Array.contains(this._views, aview)) {
            Array.add(this._views, aview);
            /*
            if (this._views.length == 1) {
                this._setactiveview(0);
            }
            */
        }
    },

    remove: function(aview) {
        if (Array.contains(this._views, aview)) {
            var index = Array.indexOf(this._views, aview);

            if (index == this._activeview) {
                this._setactiveview(this.next(index));
            }

            Array.remove(this._views, aview);
        }
    },

    setactiveview: function(aview) {

        if (!aview) return;

        if (Array.contains(this._views, aview)) {
            this._setactiveview(Array.indexOf(this._views, aview));
        }
    },

    flipview: function(aview) {
        
        if (!aview) return;
        var index = Array.indexOf(this._views, aview);
        
        if (index == this._activeview) {
            this._views[index].deactivate();
            this._activeview = null;
        } else this.setactiveview(aview);

    }

}

Vamsoft.MultiView.registerClass('Vamsoft.MultiView', Sys.Component);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

