﻿//MultiView

/// <reference name="MicrosoftAjax.debug.js" />
/// <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._datasource = null;
    this._onpropertychanged = Function.createDelegate(this,this.onpropertychanged);

    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;
		this.raisePropertyChanged('value');
            } 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');
    },

    onpropertychanged: function(sender,args){
        var name = args.get_propertyName();
        
        if (name == 'value'){
            this._setactiveview(sender.get_value());
        }
    },

    get_datasource: function() {
        return this._datasource;
    },

    set_datasource: function(value) {
        if (!value) return;
        this._datasource = value;
        this._datasource.add_propertyChanged(this._onpropertychanged);
        this.add_propertyChanged(this._datasource._onpropertychanged);
        this._setactiveview(this._datasource.get_value());
    },

    add: function(aview) {

        if (!Array.contains(this._views, aview)) {
            Array.add(this._views, aview);
        }
    },

    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);
        }
    },

    get_value: function() {
        return this._activeview;
    },

    setactiveview: function(aview) {

        if (!aview) return;

        if (Array.contains(this._views, aview)) {
            this._setactiveview(Array.indexOf(this._views, aview));
        }
    }
}

Vamsoft.MultiView.registerClass('Vamsoft.MultiView', Sys.Component);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
