/**
 * Class constructor
 * Initialisation of object is done by constructor
 */
var dynamic_popup_link = function(idPrefix, newConf, winWr)
{
    this.idPrefix = idPrefix;
    this.winWr    = winWr ? winWr : _wrapper;
    
    if (isDefined(newConf)) {
        var conf  = this.config;
        for (var k in newConf) {
            if (isDefined(conf, k)) {
                conf[k] = newConf[k];
            }
        }
    }

    if (!isDefined(this.winWr.doc, "body")) {
        this.onload();
    } else {
        this.winWr.setOnloadListener(this);
    }
    this._cIndex = this.__obj.length;
    this.__obj[this._cIndex] = this;
}

/**
 * Event processors
 */
dynamic_popup_link.prototype = {
    idPrefix : null,
    links    : null,
    inProgress : false,

    winWr    : null,
    popWinWr : null,

    __obj  : [null],
    _cIndex : 0,

    onload : function(evtWr)
    {
        var winWr, aLst, id, i;
        winWr = this.winWr;
        if (evtWr) {
            winWr.removeOnloadListener(this);
        }

        aLst = this.winWr.doc.getElementsByTagName("a");
        for (i=0; i<aLst.length; i++) {
            id = aLst[i].getAttribute("id");
            if (id && id.substr(0, this.idPrefix.length) == this.idPrefix) {
                winWr.getElmWrapper(aLst[i]).addListener(this, "onclick", "onClickTag");
            }
        }
        winWr.setOnUnloadListener(this, "onUnloadMain");
    },

    onClickTag : function(evtWr)
    {
        var conf, url, loc, pr, scr, x, y;
        evtWr.eventDrop();
        if (this.inProgress) {
            return;
        }

        conf = this.config;
        
        url = evtWr.elmWr.elm.getAttribute("href");
        url += (/\?/.test(url) ? "&" : "?") + conf.urlAdd + this._cIndex;

        if (this.popWinWr) {
            loc = this.popWinWr.win.location;
            if (loc.href == "http://" + loc.host + url) {
                this.popWinWr.win.focus();
                return;
            }
        }
        
        this.inProgress = true;
        this.winWr.setTimeout(conf.resTime,this,"resetProgress");
        
        if (this.popWinWr) {
            this.popWinWr.removeListener(this, "onunload", "onUnloadPopUp");
            loc.href = url;
        } else {
            scr = this.winWr.win.screen;
            x = (scr.availWidth - conf.popupWidth)/2;
            y = (scr.availHeight - conf.popupHeight)/2;
            pr  = "width=" + conf.popupWidth + ",height=" + conf.popupHeight;
            pr += ",screenX=" + x + ",screenY=" + y;
            pr += ",left=" + x + ",top=" + y;
            pr += "," + conf.popupAddProp;
            open(url, conf.popupName + this.idPrefix, pr);
        }
    },


    resetProgress : function()
    {
        this.inProgress = false;
    },

    iniPopUp : function(win)
    {
        this.popWinWr = new winWrapper(win);
        this.popWinWr.setOnloadListener(this, "onloadPopUp");
        this.inProgress = false;
    },

    onloadPopUp : function(evtWr)
    {
        this.popWinWr.setOnUnloadListener(this, "onUnloadPopUp");
        this.popWinWr.win.focus();
    },

    onUnloadMain : function(evtWr)
    {
        if (this.popWinWr){
            this.popWinWr.win.close();
        }
    },

    onUnloadPopUp : function(evtWr)
    {
        this.popWinWr = null;
    },

    __ini_popup : function(num, win)
    {
        if(num) {
            this.__obj[num].iniPopUp(win);
        }
    },

    config : {
        "urlAdd"       : "popup=",
        "popupName"    : "dynamicPopup_",
        "resTime"      : 8000,
        "popupWidth"   : 650,
        "popupHeight"  : 420,
        "popupAddProp" : "status=no,resizable=yes"
    }

}