﻿function FavorableContent(varName, containerId, valueId, linkId) {
    this.VariableName = varName;
    this.ValueHandle = document.getElementById(valueId);
    this.ContainerHandle = document.getElementById(containerId);
    this.LinkHandle = document.getElementById(linkId);
    this.IsInitialized = false;

    this.SetValue = function(value) {
        if (!this.IsInitialized)
            return;
        this.ValueHandle.value = value;
        eval(this.VariableName + "_ajax").Favoring(value, new Function('value', 'window.' + this.VariableName + '.ValueSaved(value);'), this.OnError);
    }
    this.ValueSaved = function(result) {
        result = eval(result);
        if (result[3]) {
            document.location = result[3];
            return;
        }
        this.ValueHandle.value = result[0];
        this.ContainerHandle.innerHTML = result[1];
        this.LinkHandle = document.getElementById(result[2]);
        var control = $("#" + this.VariableName);
        control.attr("class", "");
        control.addClass(result[4]);
        this.AttachEvents();
    }
    this.OnError = function(er) {
        alert('При сохранении возникла ошибка!');
    }
    this.Initialize = function() {
        this.AttachEvents();
        this.IsInitialized = true;
    }
    this.AttachEvents = function() {
        if (this.ValueHandle.value == "1") {
            this.LinkHandle.onclick = new Function("window." + this.VariableName + ".SetValue(0); return false;");
        }
        else {
            this.LinkHandle.onclick = new Function("window." + this.VariableName + ".SetValue(1); return false;");
        }
    }
    this.Initialize();
}
