var cacheInfo = new Array();

function userInfo(userId, postId) {
    
    //ak je uzivatel zaregistrovany vyberieme jeho informacie, ktore chce zobrazit
    if(userId > 0) {
        $("user-info-popup").appear({duration: 0.3});
        
        //ak nie su informacie v cache tak ich vyziadame zo serveru
        if(cacheInfo[userId] == null){
            $("user-info-popup").innerHTML = $("user-info-loading").innerHTML;
            
            //ajax vrati informacie o uzivatelovy z databaze, ktore chce publikovat
            new Ajax.Request(
                "sk/user/info/ajax",{
                    method:   "post",
                    parameters: {
                        action:  "user-info",
                        id:      userId
                    },
                    
                    //informacie sa ulozia do cache premennej a zobrazia sa v user-info-popup dive
                    onSuccess: function(transport) {
                        cacheInfo[userId] = transport.responseText;
                        $("user-info-popup").innerHTML = transport.responseText;
                    },
                    
                    onFailure: function(response) {
                        $("user-info-popup").fade({duration: 0.2});
                        $("user-info-popup").innerHTML = '';
                    }
                }
            );
        
        //ak uz udaj existuje v docasnej pamati tak ho len zobrazime a nenahravame z databazi    
        } else {
            $("user-info-popup").innerHTML = cacheInfo[userId];
        }
    
    
    //ak nie je uzivatel registrovany zobrazime informacie ktore vyplnil vo formulari    
    } else if(postId > 0){
        $("user-info-popup").appear({duration: 0.3});
        
        //ak nie su informacie v cache tak ich vyziadame zo serveru 
        if(cacheInfo["post"+postId] == null){
            $("user-info-popup").innerHTML = $("user-info-loading").innerHTML;
            
            //ajax vrati informacie ktore anonym vyplnil vo formulari a jeho IP
            new Ajax.Request(
                "sk/user/info/ajax",{
                    method:   "post",
                    parameters: {
                        action:  "post-info",
                        id:      postId
                    },
                    
                    //informacie sa ulozia do cache premennej a zobrazia sa v user-info-popup dive
                    onSuccess: function(transport) {
                        cacheInfo["post"+postId] = transport.responseText;
                        $("user-info-popup").innerHTML = transport.responseText;
                    },
                    
                    onFailure: function(response) {
                        $("user-info-popup").fade({duration: 0.2});
                        $("user-info-popup").innerHTML = '';
                    }
                }
            );
        
        //ak uz udaj existuje v docasnej pamati tak ho len zobrazime a nenahravame z databazi
        } else {
            $("user-info-popup").innerHTML = cacheInfo["post"+postId];
        }
        
    }
}

function removeUserInfo(){
    $("user-info-popup").fade({duration: 0.05});
}






// Simple follow the mouse script

var divName = 'user-info-popup'; // div that is to follow the mouse
                       // (must be position:absolute)
var offX = 10;          // X offset from mouse position
var offY = 10;          // Y offset from mouse position

function mouseX(evt) {
    if (!evt) evt = window.event; 
    if (evt.pageX) return evt.pageX; 
    else if (evt.clientX) return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); 
    else return 0;
}

function mouseY(evt) {
    if (!evt) evt = window.event; 
    if (evt.pageY) return evt.pageY; 
    else if (evt.clientY) return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); 
    else return 0;
}

function follow(evt) {
    if (document.getElementById(divName)) {
        var obj = document.getElementById(divName).style; obj.visibility = 'visible';
        obj.left = (parseInt(mouseX(evt))+offX) + 'px';
        obj.top = (parseInt(mouseY(evt))+offY) + 'px';
    }
}

document.onmousemove = follow;
                    