Type.registerNamespace('WindowsSite.Ajax.Controls');

WindowsSite.Ajax.Controls.ContentPanelBehavior = function(element)
{
    WindowsSite.Ajax.Controls.ContentPanelBehavior.initializeBase(this, [element]);
    this.contentRecievedDelegate = Function.createDelegate(this, this._onContentRecieved);
    this.errorDelegate = Function.createDelegate(this, this._onError);

    this.currentItemSelectedIndex = 1; //a one based index of the currently selected item;
}

WindowsSite.Ajax.Controls.ContentPanelBehavior.prototype =
{
    initialize: function()
    {
        WindowsSite.Ajax.Controls.ContentPanelBehavior.callBaseMethod(this, 'initialize');
    },

    dispose: function()
    {
        WindowsSite.Ajax.Controls.ContentPanelBehavior.callBaseMethod(this, 'dispose');
    },

    get_ContentFilePath : function() {
        return this._contentFilePath;
    },
    set_ContentFilePath : function(value) {
        if (this._contentFilePath != value) {
            this._contentFilePath = value;
            this.raisePropertyChanged('ContentFilePath');
        }
    },

    get_TransformFilePath : function() {
        return this._transformFilePath;
    },

    set_TransformFilePath : function(value) {
        if (this._transformFilePath != value) {
            this._transformFilePath = value;
            this.raisePropertyChanged('TransformFilePath');
        }
    },

    _onControlCommand: function(sender, args)
    {
        if (args != null)
        {
            this._commandName = args.get_CommandName();
            this._commandArgument = args.get_CommandArgument();
        }
        switch (this._commandName)
        {
            case "GetHtml":
                WindowsSite.Services.DataService.GetHtmlSection(this.get_ContentFilePath(), this._commandArgument, this.contentRecievedDelegate, this.errorDelegate);
                break;
            case "GetContentSection":
                this.getContentSection(this._commandArgument);
                break;
            case "GetCustomContentSection":
                this.getCustomContentSection(this._commandArgument);
                break;
            default:
                WindowsSite.Ajax.Controls.ContentPanelBehavior.callBaseMethod(this, '_onControlCommand');
                break;
        }
    },

    getContentSection: function(sectionID)
    {
        WindowsSite.Services.DataService.GetContentSection(this.get_ContentFilePath(), sectionID, this.contentRecievedDelegate, this.errorDelegate);
    },

    getCustomContentSection: function(sectionID)
    {
        WindowsSite.Services.DataService.GetCustomContentSection(this.get_ContentFilePath(), sectionID, this.get_TransformFilePath(), this.contentRecievedDelegate, this.errorDelegate);
    },

    _onContentRecieved: function(result, userContext)
    {
        this.get_element().innerHTML = result;
    },

    _onError: function(result)
    {
        this.get_element().innerHTML = "<div class='contentPanelError'>There was an error retrieving data.</div>";
    }
}

WindowsSite.Ajax.Controls.ContentPanelBehavior.registerClass('WindowsSite.Ajax.Controls.ContentPanelBehavior', WindowsSite.Ajax.Controls.CommandControlBehavior);
