var PlayerInfo = {

    lang: 'nl',
    tooltip: null,
    playersData: new Array(),
    ajaxLoadingInProcess: 0,

    init: function()
    {
        this.lang = siteLang !== null ? siteLang : 'nl';
        this.tooltip = new Control.ToolTip($('playerinfo'), '', {
            className: 'playerinfo',
            offsetLeft: -246,
            offsetTop: -136
        });
    },

    position: function(event)
    {
        this.tooltip.position(event);
    },

    show: function( playerId, event )
    {
        this.tooltip.container.innerHTML = 'Loading...';
        this.tooltip.position(event);
        this.tooltip.open(event);
        if((data = this.checkDataLoaded( playerId )) != 0) {
            this.tooltip.container.innerHTML = data;
        } else {
            if(!this.ajaxLoadingInProcess) {
                this._ajaxLoadData( playerId );
            }
        }
    },

    checkDataLoaded: function( playerId )
    {
        var html = 0;
        for(var i=0; i < this.playersData.length; i++) {
            if(this.playersData[i].id == playerId) {
                html = this.playersData[i].html;
                break;
            }
        }
        return html;
    },

    _ajaxLoadData: function( playerId )
    {
        this.ajaxLoadingInProcess = 1;
        var url = '/' + this.lang + '/player/ajaxLoadPlayerData';
        new Ajax.Request(url,
        {
            parameters: { playerId: playerId },
            onComplete: PlayerInfo._ajaxLoadData_callback
        });
    },

    _ajaxLoadData_callback: function( response )
    {
        eval('data=' + response.responseText);
        if(data.error) {
            PlayerInfo.ajaxLoadingInProcess = 0;
            PlayerInfo.tooltip.container.innerHTML = 'Error';
        } else {
            var obj = new Object();
            obj.id = data.data.id;
            obj.html = data.data.html;
            PlayerInfo.playersData.push(obj);
            PlayerInfo.ajaxLoadingInProcess = 0;
            PlayerInfo.tooltip.container.innerHTML = data.data.html;
        }
    },

    hide: function( event )
    {
		this.tooltip.close(event);
    }
};

document.observe( 'dom:loaded', function() {
	PlayerInfo.init();
});