
var LINK_NONE = 0;
var LINK_GO = 1;
var LINK_APPEND = 2;
var LINK_WINDOW = 4;
var LINK_ALL = 7;

function Link(params)
{
    this._params = params || {};
    
    this._modeTitles = [];
    this._modeVals = [];
    
    this._generateTargetParams();
    this._generateModes();

    var oLink = Rup.create('a');
    
    oLink.href = "#";
    
    oLink.innerHTML = this._params.text;
    
    oLink.oncontextmenu = function(){return false;}
    
    var instance = this;
    
    oLink.onmouseup = function(e){
        e = e || event;
        if( e.button == 2 )
        {
            showPopupMenu({
                x: e.clientX,
                y: e.clientY,
                items: instance._modeTitles,
                title: instance._params.menuTitle,
                resultfunc: function( val )
                { 
                    instance.rightClick(val); 
                }
            });
        }
    }
    
    oLink.onclick = function(){instance.leftClick(); return false;}
    
    this._setBaseElement(oLink);
}

Rup.extend(Link, Element_Abstract);

Link.prototype._generateTargetParams = function()
{
    this._targetParams = {
        typeId: this._params.typeId || 0,
        nodeId: this._params.nodeId || 0,
        mode: this._params.mode || ""
    };
}

Link.prototype._generateModes = function()
{	
	if( this._params.menuTitle == undefined)
    {
        this._params.menuTitle = 'Открыть объект:';
    }

    if( this._params.allowModes == undefined)
    {
        this._params.allowModes = LINK_ALL;
    }

    if( this._params.allowModes & LINK_GO )
    {
        this._modeTitles.push('В этом окне');
        this._modeVals.push(LINK_GO);
    }
    if( this._params.allowModes & LINK_APPEND )
    {
        this._modeTitles.push('Добавить в окно');
        this._modeVals.push(LINK_APPEND);
    }
    if( this._params.allowModes & LINK_WINDOW)
    {
        this._modeTitles.push('В новом всплывающем окне');
        this._modeVals.push(LINK_WINDOW);
    }
}

Link.prototype.leftClick = function()
{
    this.go( this._params.defaultMode );
}

Link.prototype.rightClick = function( val )
{
    var mode = this._modeVals[ val ];
    this.go( mode );
}

Link.prototype.go = function(mode)
{
    mode = mode || LINK_GO;
    
    if( mode & LINK_GO )
    {
        Rup.app.content.go(this._targetParams);
    }
    else if( mode & LINK_APPEND )
    {
        Rup.app.content.append(this._targetParams);
    }
    else if( mode & LINK_WINDOW )
    {
        Rup.app.content.goWindow(this._targetParams);
    }
}