//------------------------------------------------------
// динамічний об'єкт - контролює завантаження інформації
// у заданий об'єкт DOM

DynObj.prototype.ctr = null; //

DynObj.prototype.src = "?"; // перша частина запиту
DynObj.prototype.provider = ""; // id об'єкта iframe через який відбувається завантаження

DynObj.prototype.obj = ""; // id цільового об'єкта
DynObj.prototype.key = ""; // назва змінної в запиті
DynObj.prototype.value = ""; // поточне значення змінної

// заглушка, що висвічується замість вмісту об'єкту під час завантаження
// може бути текстова змінна, або функція
DynObj.prototype.stub = null;

DynObj.prototype.ready = 0;

DynObj.prototype.add_child = function(x){
//     alert("add_child " + this.obj + " " + x.obj);
    this.child[this.child.length] = x;
};

DynObj.prototype.add_parent = function(p, r){
// p - parent object
// a - add current object to "parent" as "child"
//     alert("add_parent " + this.obj + " " + p.obj + " " + r);
    this.parent[this.parent.length] = p;
    if (r) p.add_child(this);
}

//повинно викликатися при кожній зміні значення об'єкту
DynObj.prototype.hit = function(v){
//     alert("hit " + this.obj);
    if (this.value==v) return;
    this.value=v;

    if (this.ctr != null && this.child.length > 0) this.ctr.start();
    for (var i=0; i < this.child.length; i++) { this.child[i].set_stub(); }
    for (var i=0; i < this.child.length; i++) { this.child[i].query(); }
};

// вставляє "заглушки" замість даного об'єкту і всіх дочірніх
DynObj.prototype.set_stub = function(){
//     alert("set_stub " + this.obj);
    var t = document.getElementById(this.obj);
    if (t==null) return;
    this.ready = 0;
    if (this.stub!==null)
        t.innerHTML = typeof(this.stub) == "function" ? this.stub() : this.stub;
    for (var i=0; i < this.child.length; i++) { this.child[i].set_stub(); }
};

// вставляє в об'єкт HTML код і виставляє значення
// визивається з iframe
DynObj.prototype.set_content = function(c, v){
//    alert("set_content " + this.obj);
    var t = document.getElementById(this.obj);
    if (t==null) return;
    t.innerHTML = c;
    this.value=v;
    this.ready = 1;
    if (this.ctr != null && this.child.length == 0) this.ctr.stop();
    for (var i=0; i < this.child.length; i++) { this.child[i].query(); }
};

DynObj.prototype.key_val = function(){
    var q="";
    for (var i=0; i < this.parent.length; i++)
        { q = q + this.parent[i].key_val(); }
    return this.key + "=" + encodeURIComponent(this.value) + "&" + q;
}

// формує iframe і викликає завантаження
DynObj.prototype.query = function(){
//     alert("query " + this.obj);

    var q="";
    for (var i=0; i < this.parent.length; i++)
        { q = q + this.parent[i].key_val(); }

    var p = document.getElementById(this.provider);
    if (p==null)
        {
        p = document.createElement("iframe");
        p.setAttribute("src", "");
        p.setAttribute("id", this.provider);
        p.setAttribute("width", "500");
        p.setAttribute("height", "300");
        p.setAttribute("frameborder", "0");
        p.style.display="none";
        p.style.visibility="hidden";
        document.body.appendChild(p);
        }

//     alert(q);

    p.src="";
    p.src=this.src + q;
};

//конструктор
function DynObj(t, k, v, s){
//t - target id
//k - key
//v - initial value
//s - url for query

//     alert("DynObj " + t);

    this.child = [];
    this.parent = [];

    this.obj = t + "_target";
    this.provider = t + "_iframe";
//    this.src = "?cxt=do." + t + "&";

    this.src = s;
    this.key = k;
    this.value = v;
    this.ready = 1;
};

//------------------------------------------------------
// об'єкт для виконання певних дій перед початком
// та по закінчинні ланцюжка завантажень групи динамічних об'єктів

DynCtr.prototype.obj = "";

// має визиватися об'єктом групи перед початком завантаження
DynCtr.prototype.start = function (){
    this.on_start();
};

// має визиватися об'єктом групи по закінченні завантаження
DynCtr.prototype.stop = function (){
    for (var i=0; i < this.parent.length; i++)
        {
        if (this.parent[i].ready == 0) return;
        }
    this.on_stop();
};

// виконується перед початком ланцюжка завантажень
DynCtr.prototype.on_start = function (){
    var t = document.getElementById(this.obj);
    if (t==null) return;
    t.disabled = true;
};

// виконується по закінчинні ланцюжка завантажень
DynCtr.prototype.on_stop = function (){
    var t = document.getElementById(this.obj);
    if (t==null) return;
    t.disabled = false;
};

//
DynCtr.prototype.add_parent = function(p, r){
//    this.parent[this.parent.length] = p;
    p.ctr = this;
};

// конструктор
function DynCtr(t){
//objects to control
    this.obj = t;
    this.parent = [];
};

