/**
 * Control of member's icons in block
 * @version:  01.01.01 Alpha
 * @modified: 2009-07-06 15:58:00
 */
var iconsBlock = newClass({
    sMenuId   : null,
    sIconsId  : null,
    aElm      : null,

    limit  : 0,
    format : 0,

    sCurKey  : null,
    sBakKey  : null,

    moreLink : null,
    liElm    : null,

    listLoader : null,

    init : function(sMenuId, sIconsId, sFormCnt, aKeys, sCurKey, limit, format)
    {
        this.sMenuId  = sMenuId;  // Tab list id
        this.sIconsId = sIconsId; // Icons list id
        this.sFormCnt = sFormCnt; // Show (form) link id

        this.aKeys = aKeys;

        this.aElm    = {};

        for (var i in aKeys) {
            this.aElm[aKeys[i]] = null;
        }

        this.sCurKey = sCurKey;

        this.limit  = limit;
        this.format = format;

        this.$w0.createStyle(this.config.form_sex_orient + ' button', "display: none;");

        // Loader initialisation
        this.listLoader = this.getLoadWrapper(this.config.loader, "get", false);
        this.listLoader.addListener(this, "ondataload", "onListLoad");
        this.listLoader.addListener(this, "ondataerror", "onLoadError");
    },
    onready : function()
    {
        var aMenu, i, k, dlr;
        aMenu = this.$$("#" + this.sMenuId + " a");
        i = 0;
        for (k in this.aElm) {
            this.aElm[k] = {
                "menu"  : aMenu[i].getParent(), // Tag (li) of menu elements
                "icons" : (k == this.sCurKey ? this.$("#" + this.sIconsId) : null), // Tag (ul) containing list of avatars
                "ready" : k == this.sCurKey, // Flag of curent list is loaded
                "list"  : [],
                "qtt"   : 0
            };
            aMenu[i].addListener(this, "onclick", "onClickMenu", k);
            i++;
        }

        this.moreLink = this.$("#" + this.sMenuId).getNextSibling();
        this.liElm    = this.$("#" + this.sIconsId + " li.avatar_prototype");

       /*======= show form for condition of show ==========*/
        this.show_link  = this.$('#' + this.sFormCnt);
        this.form = this.$(this.config.form_sex_orient);
        if (this.show_link && this.form) {
            dlr = this.getDelayer(this.config.m_out_delay, 'hideSelForm');
            dlr.setBreakMethod('showForm');

            dlr.addStart(this.show_link, 'onmouseout');
            dlr.addBreak(this.show_link, 'onmouseover', true);

            dlr.addStart(this.form, 'onmouseout');
            dlr.addBreak(this.form, 'onmouseover', false);

            this.$w0.addListener(this, 'onclick', 'onBodyClick', dlr);
            this.form.addListener(this, 'onclick', 'onFormClick');
            this.$$(this.config.form_sex_orient + this.config.depend_sex).addListener(this, "onchange", "changeSexDepend");
        }
        /*=================================*/
    },

    onload : function()
    {
        var conf = this.config;
        if (this._checkVal(this.aKeys, conf.online_key)) {
            this._makeIconList(conf.online_key, false);
            this.$w0.setInterval(conf.req_interval, this, "send_request", [conf.online_key]);
        }
    },

    onClickMenu : function(evtWr, key)
    {
        evtWr.eventDrop();
        evtWr.elmWr.elm.blur();

        this._selectList(key);
    },

    /* ----- Loader events ----- */
    onListLoad : function(json, dom, txt)
    {
        var curSel, ul, elm, li, a, img, memb, i;
        if (txt == "ok") {

            if (json.mem_qtt) {
                for (i in this.aElm) {
                    this.aElm[i].menu.setDisplay(json.mem_qtt[i] > 0);
                    this.aElm[i].qtt = json.mem_qtt[i];
                }
                this.hideSelForm();
            }

            curSel = this.aElm[json.key];
            if (!this._compareObj(json.list, curSel.list)) {
                curSel.ready = true;
                elm = curSel.icons.getChildren();
                for (i = 0; i < elm.length; i++ ) {
                     elm[i].remove();
                }
                ul = curSel.icons.elm;
                if (json.list) {
                    curSel.qtt = json.list.length;
                    for (i in json.list) {
                        memb = json.list[i];
                        li = this.liElm.getClone();
                        li.removeClass("avatar_prototype");
                        a = li.getChild(0);
                        a.elm.href = memb.profile_url;
                        a.setClass("imail_contact imail_memb_" + memb.memb_id);
                        img = a.getChild(0);
                        img.elm.src = memb.avatar_url;
                        img.elm.alt = memb.firstname;
                        img.elm.title = memb.firstname;
                        if (this.format != 1) {
                            a.getChild(1).write(memb.firstname);
                            li.getChild(1).write(memb.gender + ", " + memb.age);
                        }
                        ul.appendChild(li.elm);
                    }
                    
                    curSel.menu.show();
                } else {
                    curSel.qtt = 0;
                    for (i in this.aElm) {
                        if (this.aElm[i].qtt > 0) {
                            this._selectList(i);
                            break;
                        }
                    }
                    curSel.menu.hide();
                    li = this.$w0.makeElement("li", null, this.config.no_result_msg);
                    li.addClass("no_result");
                    ul.appendChild(li.elm);
                }
            }
            curSel.list = json.list;
        } else {
            alert(txt);
            this._selectList(this.sBakKey);
        }
    },
    onLoadError : function(txt)
    {
        this._selectList(this.sBakKey);
    },

    changeSexDepend : function(evtWr)
    {
        for (var i in this.aElm) {
            this.aElm[i].ready = false;
        }
        this.send_request(this.sCurKey, evtWr.elmWr.elm.value, this.aKeys);
    },

    send_request : function(key, filter)
    {
        var param = {"key" : key, "limit" : this.limit, "format" : this.format}
        if (isDefined(filter)) {
            param["keys"] = this.aKeys
            param["filter"] = filter;
        }
        this.listLoader.send(param);
    },

    /*========== hide radio script =============*/
    showForm : function(evtWr, pos)
    {
        this.form.show();
        if (pos) {
            this.form.setAbsTop(evtWr.absY - evtWr.relY + this.config.offsetY);
            this.form.setAbsLeft(evtWr.absX - evtWr.relX + this.config.offsetX);
        }
    },

    onBodyClick : function (evtWr, dlr)
    {
        dlr.clearTimer();
        this.hideSelForm(evtWr);
    },

    hideSelForm : function(evtWr)
    {
       this.form.hide();
    },

    onFormClick : function (evtWr)
    {
        evtWr.stopBubbling();
    },

    /*========= end of hide radio script =======*/
    
    /* ======== Protected methods ======= */
    _compareObj : function(obj1, obj2)
    {
        if (obj1 == null && obj2 == null) {
            return true;
        }
        if (obj1 != null || obj2 != null) {
            return false;
        }
        return obj1.toSource() == obj2.toSource();
    },
    _checkVal : function(obj, val)
    {
        for (var i in obj) {
            if (obj[i] == val) {
                return true;
            }
        }
        return false;
    },
    _selectList : function(key)
    {
        var oldSel, newSel, li;

        if (key == this.sCurKey) return;
        
        oldSel = this.aElm[this.sCurKey];
        newSel = this.aElm[key];
        oldSel.menu.removeClass("current");
        newSel.menu.addClass("current");
        
        this.moreLink.elm.href = newSel.menu.getChild(0).elm.href;

        this._makeIconList(key, true);

        if (!newSel.ready || key == this.config.online_key) {
            this.send_request(key);
        }

        this.sBakKey = this.sCurKey;
        this.sCurKey = key;

        oldSel.icons.hide();
        newSel.icons.show();
    },

    _makeIconList : function(key, display)
    {
        var newSel, li;
        newSel = this.aElm[key];
        if (!newSel.icons) {
            li = this.$w0.makeElement("li");
            li.addClass("is_loading");
            newSel.icons = this.$w0.makeElement("ul", null, li);
            newSel.icons.addClass("avatar_list");
            newSel.icons.setDisplay(display);
            this.aElm[this.sCurKey].icons.getParent().elm.appendChild(newSel.icons.elm)
        }
    },

    config : {
        "loader" : "/extra/member_short_list.php",
        "no_result_msg" : "There aren't members",
        "online_key"    : "online_members",
        "form_sex_orient" : "#select_sex_orient",
        "depend_sex"      : ' input[name=sex_depend]',
        "req_interval" : 30000,
        "m_out_delay"  : 1000,
        "offsetX" : -490,
        "offsetY" : -30
    }
});
