﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("OS.Web");

OS.Web.AjaxLabel = function(element) {
    OS.Web.AjaxLabel.initializeBase(this, [element]);

    this._pnlEditable = null;
    this._pnlNoneEditable = null;
    this._txtBox = null;
    this._saveButton = null;
    this._cancelButton = null;
    this._btnDelete = null;
    this._btnEdit = null;
    this._spnNoneEditableText = null;
    this._emptyText = "";
    this._entityTypeId = "";
    this._entityId = "";
    this._propertyName = "";
    this._editOnMouseClick = true;
    this._editButton = false;
    this._deleteButton = false;
    this._isNew = false;
    this._deleteOnEmpty = false;
    this._isTemplate = false;
}

OS.Web.AjaxLabel.prototype = {
    initialize: function() {
        OS.Web.AjaxLabel.callBaseMethod(this, 'initialize');
        if (this._isTemplate) {
            return;
        }

        if (this._editOnMouseClick) {
            $addHandler(this._pnlNoneEditable, "click", Function.createDelegate(this, this.ShowEditableView));
        }

        $addHandler(this._pnlNoneEditable, "mouseover", Function.createDelegate(this, this.MouseOver));
        $addHandler(this._pnlNoneEditable, "mouseout", Function.createDelegate(this, this.MouseOut));
        $addHandler(this._saveButton, "click", Function.createDelegate(this, this.Save));
        $addHandler(this._cancelButton, "click", Function.createDelegate(this, this.Cancel));

        if (this._editButton)
            $addHandler(this._btnEdit, "click", Function.createDelegate(this, this.ShowEditableView));

        if (this._deleteButton)
            $addHandler(this._btnDelete, "click", Function.createDelegate(this, this.Delete));
    },

    dispose: function() {
        //Add custom dispose actions here
        OS.Web.AjaxLabel.callBaseMethod(this, 'dispose');
    },

    hideElement: function() {
        this.addCssClass("Invisible");
    },

    FailedCallback: function(error) {
        this._txtBox.value = this._spnNoneEditableText.innerHTML.trim() == this._emptyText ? "" : this._spnNoneEditableText.innerHTML;
        this.ShowNonEditableView();
        alert(error.get_message());
    },

    SaveSucceededCallback: function(result) {
        var value = result.Value;

        if (this._isNew) {
            this._entityId = result.ID;
        }
        this._isNew = false;

        this.ShowNonEditableView();
        this._spnNoneEditableText.innerHTML = value == "" ? this._emptyText : value;
    },

    DeleteSucceededCallback: function(result) {
        this.hideElement();
    },

    ShowEditableView: function() {
        this._pnlEditable.style.display = "block";
        this._txtBox.focus();
        this._pnlNoneEditable.style.display = "none";
    },

    ShowNonEditableView: function() {
        this._pnlEditable.style.display = "none";
        this._pnlNoneEditable.style.display = "block";
    },

    Save: function() {
        var serviceProxy = new OS.TC.Portal.WebSite.Modules.Core.WebServices.Core();

        serviceProxy.set_defaultSucceededCallback(Function.createDelegate(this, this.SaveSucceededCallback));
        serviceProxy.set_defaultFailedCallback(Function.createDelegate(this, this.FailedCallback));

        var value = this._txtBox.value.trim();
        var isEmpty = value == "";

        if (this._isNew) {
            if (isEmpty && this._deleteOnEmpty) {
                this.hideElement();
                return;
            }
            serviceProxy.AddItem(this._entityTypeId, this._propertyName, value);
            return;
        }
        if (isEmpty && this._deleteOnEmpty) {
            this.hideElement();
            serviceProxy.DeleteItem(this._entityTypeId, this._entityId);
            return;
        }
        serviceProxy.ChangeProperty(this._entityTypeId, this._entityId, this._propertyName, value);
    },

    Cancel: function() {
        if (this._isNew) {
            this.hideElement();
        }
        this.ShowNonEditableView();
    },

    Delete: function() {
        if (!confirm(Resources.AreYouSure))
            return;

        if (this._isNew) {
            this.hideElement();
            return;
        }

        var serviceProxy = new OS.TC.Portal.WebSite.Modules.Core.WebServices.Core();

        serviceProxy.set_defaultSucceededCallback(Function.createDelegate(this, this.DeleteSucceededCallback));
        serviceProxy.set_defaultFailedCallback(Function.createDelegate(this, this.FailedCallback));

        serviceProxy.DeleteItem(this._entityTypeId, this._entityId);
    },

    MouseOver: function() {
        this._pnlNoneEditable.className = "AjaxLabel_TextOnOver AjaxLabel_Text";
        if (this._spnNoneEditableText.innerHTML.trim() == this._emptyText) {
            this._spnNoneEditableText.innerHTML = this._emptyText;
        }

        this.toggleEditButtons();
    },

    MouseOut: function() {
        this._pnlNoneEditable.className = "AjaxLabel_Text";

        this.toggleEditButtons();
    },

    toggleEditButtons: function() {
        if (this._deleteButton) {
            Sys.UI.DomElement.toggleCssClass(this._btnDelete, "AjaxLabel_Delete");
        }

        if (this._editButton) {
            Sys.UI.DomElement.toggleCssClass(this._btnEdit, "AjaxLabel_Edit");
        }
    },

    get_PnlEditable: function() {
        return this._pnlEditable;
    },
    set_PnlEditable: function(value) {
        this._pnlEditable = value;
    },

    get_PnlNoneEditable: function() {
        return this._pnlNoneEditable;
    },
    set_PnlNoneEditable: function(value) {
        this._pnlNoneEditable = value;
    },

    get_SpnNoneEditableText: function() {
        return this._spnNoneEditableText;
    },
    set_SpnNoneEditableText: function(value) {
        this._spnNoneEditableText = value;
    },

    get_BtnDelete: function() {
        return this._btnDelete;
    },
    set_BtnDelete: function(value) {
        this._btnDelete = value;
    },

    get_BtnEdit: function() {
        return this._btnEdit;
    },
    set_BtnEdit: function(value) {
        this._btnEdit = value;
    },

    get_TxtBox: function() {
        return this._txtBox;
    },
    set_TxtBox: function(value) {
        this._txtBox = value;
    },

    get_EmptyText: function() {
        return this._emptyText;
    },
    set_EmptyText: function(value) {
        this._emptyText = value;
    },

    get_EntityTypeId: function() {
        return this._entityTypeId;
    },
    set_EntityTypeId: function(value) {
        this._entityTypeId = value;
    },

    get_EntityId: function() {
        return this._entityId;
    },
    set_EntityId: function(value) {
        this._entityId = value;
    },

    get_PropertyName: function() {
        return this._propertyName;
    },
    set_PropertyName: function(value) {
        this._propertyName = value;
    },

    get_SaveButton: function() {
        return this._saveButton;
    },
    set_SaveButton: function(value) {
        this._saveButton = value;
    },

    get_CancelButton: function() {
        return this._cancelButton;
    },
    set_CancelButton: function(value) {
        this._cancelButton = value;
    },

    get_EditOnMouseClick: function() {
        return this._editOnMouseClick;
    },
    set_EditOnMouseClick: function(value) {
        this._editOnMouseClick = value;
    },

    get_EditButton: function() {
        return this._editButton;
    },
    set_EditButton: function(value) {
        this._editButton = value;
    },

    get_DeleteButton: function() {
        return this._deleteButton;
    },
    set_DeleteButton: function(value) {
        this._deleteButton = value;
    },

    get_IsNew: function() {
        return this._isNew;
    },
    set_IsNew: function(value) {
        this._isNew = value;
    },

    get_DeleteOnEmpty: function() {
        return this._deleteOnEmpty;
    },
    set_DeleteOnEmpty: function(value) {
        this._deleteOnEmpty = value;
    },

    get_IsTemplate: function() {
        return this._isTemplate;
    },
    set_IsTemplate: function(value) {
        this._isTemplate = value;
    }
}
OS.Web.AjaxLabel.registerClass('OS.Web.AjaxLabel', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();





