function openWinCentered(loc, winname, widthh, heightt, params)
{
    var tp = Math.ceil((screen.height - heightt) / 2);
    var lf = Math.ceil((screen.width - widthh) / 2);
    if (params.length > 0)
	    params = "," + params;

    var win = window.open(loc, winname, "width=" + widthh + ",height=" + heightt + ",top=" + tp + ",left=" + lf + params);
    win.focus();
    return win;
}


(function()
{
    var UA = navigator.userAgent;
    is_gecko = /gecko/i.test(UA);
    is_opera = /opera/i.test(UA);
    is_mac = /mac_powerpc/i.test(UA);
    is_ie = /msie/i.test(UA) && !is_opera && !is_gecko && !is_mac;
    is_ie5 = is_ie && /msie 5\.[^5]/i.test(UA);
    is_nn4 = document.layers ? true : false;
})();

function Community()
{
}

Community.i18n = function(text, arg1, arg2)
{
    if (typeof Community.I18N != "undefined" && typeof Community.I18N[text] != "undefined")
        text = Community.I18N[text];

    if (arg1)
    {
        text = text.replace(/(\(1\))/g, arg1);

        if (arg2)
            text = text.replace(/(\(2\))/g, arg2);
    }

    return text;
};


Community.CE = function(type, parent, props, style)
{
    var el = null,i;

    if (document.createElementNS)
        el = document.createElementNS("http://www.w3.org/1999/xhtml", type);
    else
        el = document.createElement(type);

    if (parent)
        parent.appendChild(el);

    if (props)
        for (i in props)
            el[i] = props[i];

    if (style)
        for (i in style)
            el.style[i] = style[i];

    return el;
};

Community.I18N = {
    "friend.menu.large-photo"       : "збільшити фото",
    "friend.menu.send-msg"          : "написати повідомлення",
    "friend.menu.friends"           : "друзі друга",
    "friend.menu.photos"            : "фотографії",
    "friend.menu.self"              : "нотатки"
    
};

function SMenuFriend()
{
}

SMenuFriend.div = null;
SMenuFriend.timer = null;
SMenuFriend.showDiv = 0;
SMenuFriend.overDiv = 0;

SMenuFriend.sessionId = null;
SMenuFriend.userId = null;
SMenuFriend.userName = null;
SMenuFriend.photoId = null;

SMenuFriend.generate = function(parent)
{
    if (!parent)
        return;

    if (SMenuFriend.div)
        return;

    var C = Community;
    SMenuFriend.div = C.CE("div", parent, {id:"shortcutMenuFriend"}, {});
    SMenuFriend.div.onmouseover = function()
    {
        SMenuFriend.overDiv = 1;
    }

    SMenuFriend.div.onmouseout = function()
    {
        SMenuFriend.overDiv = 0;
        setTimeout('closeSMenuFriend()', 500);
    }

    SMenuFriend.userName = C.CE("h4", SMenuFriend.div, {});

    var body = C.CE("ul", SMenuFriend.div, {}, {});

    function gen_menu(body, icon, label, clickHandler)
    {
        var a = C.CE("a", C.CE("li", body, {className:"shortcutMenuItem-" + icon}), {href:"#",innerHTML:C.i18n(label)});
        a.onclick = clickHandler;
    }

    gen_menu(body, "view", "friend.menu.large-photo", function()
    {
        var url = "./" + SMenuFriend.sessionId;

        openWinCentered(url, "view" + SMenuFriend.userId, 700, 670, 'location=no,status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=no');
        return false;
    });

    gen_menu(body, "message", "friend.menu.send-msg", function()
    {
        window.location = "/user_messages_new.php?to=" + SMenuFriend.userName.innerHTML;
        return false;
    });



    gen_menu(body, "friends", "friend.menu.friends", function()
    {
        window.location = "/profile_friends.php?user=" + SMenuFriend.userName.innerHTML;
        return false;
    });



    gen_menu(body, "photo", "friend.menu.photos", function()
    {
        window.location = "/album.php?user=" + SMenuFriend.userName.innerHTML;
        return false;
    });
    
    

    gen_menu(body, "self", "friend.menu.self", function()
    {
        window.location = "/blog.php?user=" + SMenuFriend.userName.innerHTML;
        return false;
    });


}

SMenuFriend.popupDiv = function(e, sessionId, userId, userName, photoId)
{
    if (!e)
        e = window.event;

    var x = e.clientX;
    var y = e.clientY;

    SMenuFriend.timer = setTimeout("showSMenuFriend(" + x + "," + y + ",'" + sessionId + "','" + userId + "','" + userName + "','" + photoId + "')", 600);
}

var showSMenuFriend = function(x, y, sessionId, userId, userName, photoId)
{
    SMenuFriend.generate(document.body);
    var div = SMenuFriend.div;

    SMenuFriend.sessionId = sessionId;
    SMenuFriend.userId = userId;
    SMenuFriend.photoId = photoId;
    SMenuFriend.userName.innerHTML = userName;

    if (document.body.clientWidth && document.body.clientWidth - x < 200)
        x = x - 200;
    else
        x = x + 5;

    if (document.body.clientHeight && document.body.clientHeight - y < 170)
        y = y - 170;

    if (document.body.scrollLeft)
        x = x + document.body.scrollLeft;

    if (document.body.scrollTop)
        y = y + document.body.scrollTop;

    if (is_nn4)
    {
        div.left = x;
        div.top = y;
    }
    else if (is_ie)
    {
        div.style.pixelLeft = x;
        div.style.pixelTop = y;
    }
    else
    {
        div.style.left = x + "px";
        div.style.top = y + "px";
    }
    div.style.visibility = "visible";
    SMenuFriend.showDiv = 1;
}

SMenuFriend.hideDiv = function()
{
    if (SMenuFriend.timer)
    {
        clearTimeout(SMenuFriend.timer);
        SMenuFriend.timer = null;
    }

    if (SMenuFriend.showDiv == "1")
    {
        SMenuFriend.overDiv = 0;
        setTimeout('closeSMenuFriend()', 500);
    }
}

var closeSMenuFriend = function()
{
    if (SMenuFriend.overDiv == "0")
    {
        SMenuFriend.div.style.visibility = "hidden";
        SMenuFriend.showDiv = 0;
    }
}




