﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("OS.Web");

OS.Web.HtmlArea = function (element) {
    OS.Web.HtmlArea.initializeBase(this, [element]);

    this._element = element;
    this._btnEdit = null;
    this._pnlContent = null;
    this._pnlLoading = null;
    this._key = null;
    this._localizable = null;
    this._reloadAfterSave = false;
    this._editMode = false;
}

OS.Web.HtmlArea.prototype = {
    initialize: function () {
        OS.Web.HtmlArea.callBaseMethod(this, 'initialize');

        $addHandler(this._element, "mouseover", Function.createDelegate(this, this.ShowEditButton));
        $addHandler(this._element, "mouseout", Function.createDelegate(this, this.HideEditButton));
        $addHandler(this._btnEdit, "click", Function.createDelegate(this, this.EditContent));
    },

    dispose: function () {
        //Add custom dispose actions here
        OS.Web.HtmlArea.callBaseMethod(this, 'dispose');
    },

    EditContent: function () {
        ShowPageModalDialog(Resources.Content,
            SITE_BASE_URL + "Modules/Core/IFrames/IHtmlAreaEditor.aspx?key=" + this._key, 685, 520,
            Function.createDelegate(this, this.OnContentChanged));
    },

    OnContentChanged: function (e, data) {
        if (e == OS.Web.DialogResult.Ok) {
            this._reloadAfterSave = true;
            this.SaveContent(data);
        }
    },

    SaveContent: function (data) {

        this.ShowLoading();

        var serviceProxy = new OS.TC.Portal.WebSite.Modules.Core.WebServices.Core();

        serviceProxy.set_defaultSucceededCallback(Function.createDelegate(this, this.saveSucceededCallback));
        serviceProxy.set_defaultFailedCallback(this.failedCallback);

        serviceProxy.UpdateHtmlArea(this._key, data);
    },

    SaveResultedContent: function (languages) {

        if (!this._editMode)
            alert("Saving resulted content possible in edit mode only!");

        this.ShowLoading();

        var serviceProxy = new OS.TC.Portal.WebSite.Modules.Core.WebServices.Core();

        serviceProxy.set_defaultSucceededCallback(Function.createDelegate(this, this.saveResultedHtmlSucceededCallback));
        serviceProxy.set_defaultFailedCallback(this.failedCallback);

        var data = this._pnlContent.innerHTML;

        if (this._localizable && languages) {
            serviceProxy.SaveResultedLocalizedHtmlArea(this._key, languages);
        }
        else {
            serviceProxy.SaveResultedHtmlArea(this._key);
        }
    },

    saveSucceededCallback: function (result) {
        this.HideLoading();

        this._pnlContent.innerHTML = result;
        if (this._reloadAfterSave) {
            location.reload();
        }
    },

    saveResultedHtmlSucceededCallback: function () {
        this.HideLoading();
        if (this._reloadAfterSave) {
            location.reload();
        }
    },

    Reset: function () {
        this.ShowLoading();

        var serviceProxy = new OS.TC.Portal.WebSite.Modules.Core.WebServices.Core();

        serviceProxy.set_defaultSucceededCallback(Function.createDelegate(this, this.resetSucceededCallback));
        serviceProxy.set_defaultFailedCallback(this.failedCallback);

        serviceProxy.ResetHtmlArea(this._key);
    },

    resetSucceededCallback: function (result) {
        this.HideLoading();
        this._pnlContent.innerHTML = result;
    },

    failedCallback: function (error) {
        //alert(error.get_message());
        //alert("Во время сохранения произошла ошибка.");
    },

    ShowEditButton: function () {
        Sys.UI.DomElement.addCssClass(this._btnEdit, "HtmlAreaEditButtonActive");
    },

    HideEditButton: function () {
        Sys.UI.DomElement.removeCssClass(this._btnEdit, "HtmlAreaEditButtonActive");
    },

    ShowLoading: function () {
        $(this._pnlLoading).show();
    },

    HideLoading: function () {
        $(this._pnlLoading).hide();
    },

    get_BtnEdit: function () {
        return this._btnEdit;
    },
    set_BtnEdit: function (value) {
        this._btnEdit = value;
    },

    get_PnlContent: function () {
        return this._pnlContent;
    },
    set_PnlContent: function (value) {
        this._pnlContent = value;
    },

    get_PnlLoading: function () {
        return this._pnlLoading;
    },
    set_PnlLoading: function (value) {
        this._pnlLoading = value;
    },

    get_Key: function () {
        return this._key;
    },
    set_Key: function (value) {
        this._key = value;
    },

    get_Localizable: function () {
        return this._localizable;
    },
    set_Localizable: function (value) {
        this._localizable = value;
    },

    get_EditMode: function () {
        return this._editMode
    },
    set_EditMode: function (value) {
        this._editMode = value;
    }
}
OS.Web.HtmlArea.registerClass('OS.Web.HtmlArea', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();





