var i, j, anim = false, l1, l2, l3, l4, l5, l6, l7, c;
var t = new Array();
t[1] = new Array();
t[2] = new Array();
t[3] = new Array();
t[4] = new Array();
function start(event, target){
    i = event.clientX+15;
    j = event.clientY-25;
    c = target;
    if(!anim){
        for(k in t){
            t[k][1] = document.getElementById("t"+k+"1");
            t[k][2] = document.getElementById("t"+k+"2");
            t[k][3] = document.getElementById("t"+k+"3");
            t[k][4] = document.getElementById("t"+k+"4");
        }
        l1 = 0;
        l2 = 0;
        l3 = 0;
        l4 = 0;
        l5 = 0;
        l6 = 0;
        l7 = 0;
        anim = true;
        setTimeout(mozgat, 15);
    }
    document.getElementById(c).style.top = j+"px";
    document.getElementById(c).style.left = i+"px";
    pos();
    anim = true;
}
function mozgat(){
    if(!anim)
        return;
    t[1][1].style.width = (l1*2)+"px";
    t[1][1].style.height = (l1)+"px";
    t[1][2].style.width = (l2*2)+"px";
    t[1][2].style.height = (l2)+"px";
    t[1][3].style.width = (l3*2)+"px";
    t[1][3].style.height = (l3)+"px";
    t[1][4].style.width = (l4*2)+"px";
    t[1][4].style.height = (l4)+"px";
    t[2][1].style.width = (l2*2)+"px";
    t[2][1].style.height = (l2)+"px";
    t[2][2].style.width = (l3*2)+"px";
    t[2][2].style.height = (l3)+"px";
    t[2][3].style.width = (l4*2)+"px";
    t[2][3].style.height = (l4)+"px";
    t[2][4].style.width = (l5*2)+"px";
    t[2][4].style.height = (l5)+"px";
    t[3][1].style.width = (l3*2)+"px";
    t[3][1].style.height = (l3)+"px";
    t[3][2].style.width = (l4*2)+"px";
    t[3][2].style.height = (l4)+"px";
    t[3][3].style.width = (l5*2)+"px";
    t[3][3].style.height = (l5)+"px";
    t[3][4].style.width = (l6*2)+"px";
    t[3][4].style.height = (l6)+"px";
    t[4][1].style.width = (l4*2)+"px";
    t[4][1].style.height = (l4)+"px";
    t[4][2].style.width = (l5*2)+"px";
    t[4][2].style.height = (l5)+"px";
    t[4][3].style.width = (l6*2)+"px";
    t[4][3].style.height = (l6)+"px";
    t[4][4].style.width = (l7*2)+"px";
    t[4][4].style.height = (l7)+"px";
    if(l1 < 25)
        l1 += 2;
    if(l1 > 20 && l2 < 25)
        l2 += 2;
    if(l2 > 20 && l3 < 25)
        l3 += 2;
    if(l3 > 20 && l4 < 25)
        l4 += 2;
    if(l4 > 20 && l5 < 25)
        l5 += 2;
    if(l5 > 20 && l6 < 25)
        l6 += 2;
    if(l6 > 20)
        l7 += 2;
    if(anim && l7 < 25)
        setTimeout(mozgat, 15);
    else{
        document.getElementById(c).style.display = "inline";
        setTimeout(megjel, 15);
    }
}
function megjel(){
    if(!anim)
        return;
    t[1][1].style.width = (l1*2)+"px";
    t[1][1].style.height = (l1)+"px";
    t[1][2].style.width = (l2*2)+"px";
    t[1][2].style.height = (l2)+"px";
    t[1][3].style.width = (l3*2)+"px";
    t[1][3].style.height = (l3)+"px";
    t[1][4].style.width = (l4*2)+"px";
    t[1][4].style.height = (l4)+"px";
    t[2][1].style.width = (l2*2)+"px";
    t[2][1].style.height = (l2)+"px";
    t[2][2].style.width = (l3*2)+"px";
    t[2][2].style.height = (l3)+"px";
    t[2][3].style.width = (l4*2)+"px";
    t[2][3].style.height = (l4)+"px";
    t[2][4].style.width = (l5*2)+"px";
    t[2][4].style.height = (l5)+"px";
    t[3][1].style.width = (l3*2)+"px";
    t[3][1].style.height = (l3)+"px";
    t[3][2].style.width = (l4*2)+"px";
    t[3][2].style.height = (l4)+"px";
    t[3][3].style.width = (l5*2)+"px";
    t[3][3].style.height = (l5)+"px";
    t[3][4].style.width = (l6*2)+"px";
    t[3][4].style.height = (l6)+"px";
    t[4][1].style.width = (l4*2)+"px";
    t[4][1].style.height = (l4)+"px";
    t[4][2].style.width = (l5*2)+"px";
    t[4][2].style.height = (l5)+"px";
    t[4][3].style.width = (l6*2)+"px";
    t[4][3].style.height = (l6)+"px";
    t[4][4].style.width = (l7*2)+"px";
    t[4][4].style.height = (l7)+"px";
    pos2();
    if(l1 > 0){
        l1 -= 2;
    }
    if(l1 < 10 && l2 > 0){
        l2 -= 2;
    }
    if(l2 < 10 && l3 > 0){
        l3 -= 2;
    }
    if(l3 < 10 && l4 > 0){
        l4 -= 2;
    }
    if(l4 < 10 && l5 > 0){
        l5 -= 2;
    }
    if(l5 < 10 && l6 > 0){
        l6 -= 2;
    }
    if(l6 < 10){
        l7 -= 2;
    }
    if(anim && l7 > -1)
        setTimeout(megjel, 15);
    else
        _gaq.push(["_trackEvent", "User", "Megtekintett"]);
}
function pos(){
    t[1][1].style.left = (i)+"px";
    t[1][1].style.top = (j)+"px";
    t[1][2].style.left = (i+50)+"px";
    t[1][2].style.top = (j)+"px";
    t[1][3].style.left = (i+100)+"px";
    t[1][3].style.top = (j)+"px";
    t[1][4].style.left = (i+150)+"px";
    t[1][4].style.top = (j)+"px";
    t[2][1].style.left = (i)+"px";
    t[2][1].style.top = (j+25)+"px";
    t[2][2].style.left = (i+50)+"px";
    t[2][2].style.top = (j+25)+"px";
    t[2][3].style.left = (i+100)+"px";
    t[2][3].style.top = (j+25)+"px";
    t[2][4].style.left = (i+150)+"px";
    t[2][4].style.top = (j+25)+"px";
    t[3][1].style.left = (i)+"px";
    t[3][1].style.top = (j+50)+"px";
    t[3][2].style.left = (i+50)+"px";
    t[3][2].style.top = (j+50)+"px";
    t[3][3].style.left = (i+100)+"px";
    t[3][3].style.top = (j+50)+"px";
    t[3][4].style.left = (i+150)+"px";
    t[3][4].style.top = (j+50)+"px";
    t[4][1].style.left = (i)+"px";
    t[4][1].style.top = (j+75)+"px";
    t[4][2].style.left = (i+50)+"px";
    t[4][2].style.top = (j+75)+"px";
    t[4][3].style.left = (i+100)+"px";
    t[4][3].style.top = (j+75)+"px";
    t[4][4].style.left = (i+150)+"px";
    t[4][4].style.top = (j+75)+"px";
}
function pos2(){
    t[1][1].style.left = (i+50-l1*2)+"px";
    t[1][1].style.top = (j+(25-l1))+"px";
    t[1][2].style.left = (i+100-l2*2)+"px";
    t[1][2].style.top = (j+(25-l2))+"px";
    t[1][3].style.left = (i+150-l3*2)+"px";
    t[1][3].style.top = (j+(25-l3))+"px";
    t[1][4].style.left = (i+200-l4*2)+"px";
    t[1][4].style.top = (j+(25-l4))+"px";
    t[2][1].style.left = (i+50-l2*2)+"px";
    t[2][1].style.top = (j+25+(25-l2))+"px";
    t[2][2].style.left = (i+100-l3*2)+"px";
    t[2][2].style.top = (j+25+(25-l3))+"px";
    t[2][3].style.left = (i+150-l4*2)+"px";
    t[2][3].style.top = (j+25+(25-l4))+"px";
    t[2][4].style.left = (i+200-l5*2)+"px";
    t[2][4].style.top = (j+25+(25-l5))+"px";
    t[3][1].style.left = (i+50-l3*2)+"px";
    t[3][1].style.top = (j+50+(25-l3))+"px";
    t[3][2].style.left = (i+100-l4*2)+"px";
    t[3][2].style.top = (j+50+(25-l4))+"px";
    t[3][3].style.left = (i+150-l5*2)+"px";
    t[3][3].style.top = (j+50+(25-l5))+"px";
    t[3][4].style.left = (i+200-l6*2)+"px";
    t[3][4].style.top = (j+50+(25-l6))+"px";
    t[4][1].style.left = (i+50-l4*2)+"px";
    t[4][1].style.top = (j+75+(25-l4))+"px";
    t[4][2].style.left = (i+100-l5*2)+"px";
    t[4][2].style.top = (j+75+(25-l5))+"px";
    t[4][3].style.left = (i+150-l6*2)+"px";
    t[4][3].style.top = (j+75+(25-l6))+"px";
    t[4][4].style.left = (i+200-l7*2)+"px";
    t[4][4].style.top = (j+75+(25-l7))+"px";
}
function reset(){
    anim = false;
    document.getElementById(c).style.display = "none";
    t[1][1].style.width = "0px";
    t[1][1].style.height = "0px";
    t[1][2].style.width = "0px";
    t[1][2].style.height = "0px";
    t[1][3].style.width = "0px";
    t[1][3].style.height = "0px";
    t[1][4].style.width = "0px";
    t[1][4].style.height = "0px";
    t[2][1].style.width = "0px";
    t[2][1].style.height = "0px";
    t[2][2].style.width = "0px";
    t[2][2].style.height = "0px";
    t[2][3].style.width = "0px";
    t[2][3].style.height = "0px";
    t[2][4].style.width = "0px";
    t[2][4].style.height = "0px";
    t[3][1].style.width = "0px";
    t[3][1].style.height = "0px";
    t[3][2].style.width = "0px";
    t[3][2].style.height = "0px";
    t[3][3].style.width = "0px";
    t[3][3].style.height = "0px";
    t[3][4].style.width = "0px";
    t[3][4].style.height = "0px";
    t[4][1].style.width = "0px";
    t[4][1].style.height = "0px";
    t[4][2].style.width = "0px";
    t[4][2].style.height = "0px";
    t[4][3].style.width = "0px";
    t[4][3].style.height = "0px";
    t[4][4].style.width = "0px";
    t[4][4].style.height = "0px";
}

// Profil cuccok

function regellenorzes(){
    FB.getLoginStatus(function(response) {
        if (response.session) {
            var query = FB.Data.query('SELECT name, uid FROM user WHERE uid = me()'); 
            query.wait(function(rows) {
                var kliens = nyit();
                if(kliens!=null){
                    kliens.open('POST', 'http://'+location.hostname+'/profil/regelt', true);
                    kliens.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                    kliens.send("id="+rows[0].uid);
                    kliens.onreadystatechange = function(){
                        if(kliens.readyState==4) {
                            if(kliens.status == 200) {
                                if(kliens.responseText == "OK")
                                    belep(rows);
                            }
                        }
                    }
                }
            });
        }
    });
}

function beleptet(){
    FB.login(function(response) {
        if (response.session) {
            var query = FB.Data.query('SELECT name, uid FROM user WHERE uid = me()'); 
            query.wait(function(rows) {
                var kliens = nyit();
                if(kliens!=null){
                    kliens.open('POST', 'http://'+location.hostname+'/profil/regelt', true);
                    kliens.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                    kliens.send("id="+rows[0].uid);
                    kliens.onreadystatechange = function(){
                        if(kliens.readyState==4){
                            if(kliens.status == 200){
                                if(kliens.responseText == "OK"){
                                    belep(rows);
                                } else {
                                    kliens.open('POST', 'http://'+location.hostname+'/profil/regisztral', true);
                                    kliens.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                                    kliens.send("id="+rows[0].uid+"&nev="+rows[0].name);
                                    kliens.onreadystatechange = function(){
                                        if(kliens.readyState==4){
                                            if(kliens.status == 200){
                                                if(kliens.responseText == "OK")
                                                    belep(rows);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
    });
}

function kilep(){
    //FB.logout();
    belepett = false;
    var kliens = nyit();
    if(kliens!=null){
        kliens.open('POST', 'http://'+location.hostname+'/profil/kilep', true);
        kliens.send();
        kliens.onreadystatechange = function(){
            if(kliens.readyState==4) {
                if(kliens.status == 200) {
                    if(kliens.responseText == "OK"){
                        if(nyelv == "Magyar"){
                            szoveg = '<br>A kilépés sikeresen megtörtént';
                        } else {
                            szoveg = '<br>You succesfully logged out';
                        }
                        document.getElementById("lib").innerHTML = szoveg;
                        document.getElementById("lib").onclick = beleptet;
                    }
                }
            }
        }
    }
}

function belep(sor){
    var szoveg;
    if(nyelv == "Magyar"){
        szoveg = '<br>Sikeresen beléptél!';
    } else {
        szoveg = '<br>Successfully logged in!';
    }
    belepett = true;
    document.getElementById("lib").innerHTML = szoveg;
    document.getElementById("lib").onclick = null;
    var query = FB.Data.query('SELECT uid2 FROM friend WHERE uid1 = me()'); 
    query.wait(function(rows) {
        var post = '';
        for(j in rows)
            post += "fid[]="+rows[j].uid2+"&";
        var kliens = nyit();
        if(kliens!=null){
            kliens.open('POST', 'http://'+location.hostname+'/profil/baratok', true);
            kliens.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            kliens.send(post);
        }
    });
    _gaq.push(["_trackEvent", "User", "Belépett"]);
}

function pontmentes(adat, jatekid){
    var kliens = nyit();
    if(kliens!=null){
        if(adat.submission == "medal") {
            if (adat.kind == "leaderboards") {
                adatok = "mi=m&jatekid="+jatekid+"&medal="+adat.name+"&cim=erem&kep="+adat.thumbnail;
            } else {
                adatok = "mi=a&jatekid="+jatekid+"&cim="+adat.name+"&kep="+adat.thumbnail+"&leiras="+adat.description;
            }
        } else {
            adatok = "mi=p&sorrend="+adat.sortOrder+"&pont="+adat.score+"&cim="+adat.title+"&jatekid="+jatekid;
        }
        kliens.open('POST', 'http://'+location.hostname+'/profil/pontment', true);
        kliens.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        kliens.send(adatok);
        kliens.onreadystatechange = function(){
            if(kliens.readyState==4) {
                if(kliens.status == 200) {
                    var div = document.getElementById("mo");
                    div.innerHTML = kliens.responseText;
                    div.style.display = "inline";
                    div.style.top = "200px";
                }
            }
        }
    }
}

function eltuntet(){
    var div = document.getElementById("mo");
    div.innerHTML = "";
    div.style.display = "none";
}

