var host = "http://www.videoklass.ru/user/";

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;
};


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", "увеличить фото", function()
    {
        var url = host + "photobig.php?uid=" + SMenuFriend.photoId;

        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", "написать сообщение", function()
    {
        //var url = host + "sendmess.php?tid="+SMenuFriend.userId;

        // dublicate change dimension in TCreateMessageLink class
        //openWinCentered(url, "msg" + SMenuFriend.userId, 520, 650, 'location=no,status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=yes');
        //var url = host + "sendmess2.php?tid="+SMenuFriend.userId;
        //SqueezeBox.open(url, {handler: 'iframe'});
        window.location = host + "message.php?tid=" + SMenuFriend.userId;


        return false;
    });

    gen_menu(body, "messaging", "показать переписку", function()
    {
        window.location = host + "?pr=13&u=" + SMenuFriend.userId;
        return false;
    });


    gen_menu(body, "video", "видео чат", function()
    {
        window.location = host + "startVideo.php?friendId=" + SMenuFriend.userId;
        return false;
    });

    gen_menu(body, "friends", "друзья друзей", function()
    {
        window.location = host + "?pr=2&u=" + SMenuFriend.userId;
        return false;
    });

    gen_menu(body, "groups", "сообщества", function()
    {
        window.location = host + "?pr=3&u=" + SMenuFriend.userId;
        return false;
    });

        gen_menu(body, "altGroups", "группы", function()
        {
        window.location = host + "?pr=11&u=" + SMenuFriend.userId;
        return false;
    });

        gen_menu(body, "altGroupInvite", "пригласить в группу", function()
        {
        window.location = host + "?pr=11";
        return false;
    });

    gen_menu(body, "photo", "фотографии", function()
    {
        window.location = host + "?pr=4&u=" + SMenuFriend.userId;
        return false;
    });
    gen_menu(body, "photoalbums", "Фотоальбомы", function()
    {
        window.location = host + "?pr=14&u=" + SMenuFriend.userId;
        return false;
    });

    gen_menu(body, "self", "о себе", function()
    {
        window.location = host + "?pr=5&u=" + SMenuFriend.userId;
        return false;
    });

    gen_menu(body, "forum", "форум", function()
    {
        window.location = host + "?pr=6&u=" + SMenuFriend.userId;
        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.documentElement.clientWidth && document.documentElement.clientWidth - x < 200)
        x -= 200;
    else
        x += 5;

    if (document.documentElement.clientHeight && document.documentElement.clientHeight - y < 240)
        y -= 240;

    if (document.documentElement.scrollLeft)
        x += document.documentElement.scrollLeft;
    else if (document.body.scrollLeft)  // for safari
        x += document.body.scrollLeft;

    if (document.documentElement.scrollTop)
        y += document.documentElement.scrollTop;
    else if (document.body.scrollTop)  // for safari
        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;
    }
}

