var show_invite_link = 1;
var inv_userId = 0;

(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_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.properties=function(key)
{
	if(typeof Community.PROPS != "undefined" && typeof Community.PROPS[key]!="undefined")
	{
		key=Community.PROPS[key];
	}

	return key;
};


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


// ----------------------
// CHAT FUNCTIONALITY



function selectAll(mainbox, boxname)
{
    var elems = mainbox.form.elements;
    var elemnum = elems.length;

    for(i=0; i < elemnum; i++)
      if(elems[i].name.substr(0, boxname.length) == boxname)
	    elems[i].checked = mainbox.checked;
}


function transformButtonName(form, buttonname)
{
	var len = form.length;

	var eventsubmit_do = "button.";
	var _eventsubmit_doAction = "_" + buttonname.toLowerCase();

	for (i = 0; i < len; i++)
	{
		element = form.elements[i].name;
        if (startsWith(element.toLowerCase(), eventsubmit_do))
        {
            element = "_" + element;
            form.elements[i].name = element;
            form.elements[i].value = buttonname;
        }

        if (startsWith(element.toLowerCase(), _eventsubmit_doAction))
        {
//			alert(_eventsubmit_doAction.substring(1, _eventsubmit_doAction.length));
			form.elements[i].name = _eventsubmit_doAction.substring(1, _eventsubmit_doAction.length);
		}
	}
}

function startsWith(str, sub)
{
	return (str.indexOf(sub, 0) == 0);
}


function openForm(waitpage, form, buttonname, params)
{
	transformButtonName(form, buttonname);
	var date = new Date();
	var name = "" + date.getTime();
	var win = null;
	win = window.open(waitpage,name,params);

	win.moveTo(0,0);
	win.resizeTo(screen.availWidth, screen.availHeight);

	form.target = name;
	form.submit();
	form.target = "_self";
}

function openFormCentered(waitpage, form, buttonname, widthh, heightt, params)
{
	transformButtonName(form, buttonname);
	var date = new Date();
	var name = "" + date.getTime();
	var win = null;
	win = openWinCentered(waitpage, name, widthh, heightt, params);

	form.target = name;
	form.submit();
	form.target = "_self";
}

function openWinCentered(loc, winname, widthh, heightt, params)
{
    tp = Math.ceil((screen.height-heightt)/2);
    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 openWin(loc, winname, params)
{
	var win = null;
    win = window.open(loc,winname,params);
	win.moveTo(0,0);
	win.resizeTo(screen.availWidth, screen.availHeight);
}

function windowURLCentered(url, winwidth, winheight, param)
{
	var date = new Date();
	var name = date.getTime();

	var win = openWinCentered(url, name, winwidth, winheight,param);
}

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.photoTime=null;

SMenuFriend.generate=function(parent)
{
    if (!parent)
        return;

    if (SMenuFriend.div)
        return;

    var C=Community;
    SMenuFriend.div=C.CE("div",parent,{},{position:"absolute",visibility:"hidden",zIndex:"+1",border:"1px solid #f90",backgroundColor:"#fff",padding:"1px 1px 3px 3px"});
    SMenuFriend.div.onmouseover=function()
    {
        SMenuFriend.overDiv=1;
    }

    SMenuFriend.div.onmouseout=function()
    {
        SMenuFriend.overDiv=0;
        setTimeout('closeSMenuFriend()',500);
    }

    var body=C.CE("tbody",C.CE("table",SMenuFriend.div,{},{padding:"1px"}));
    var row=C.CE("tr",body);
    if (!is_mac)
        C.CE("td",row);
    SMenuFriend.userName=C.CE("td",row,{className:"smallnormalGray"});
    var td=C.CE("td",row,{},{padding:"5px"});
    if (!is_mac)
        C.CE("img",td,{src:"/img/div/close.gif"}).onclick=function()
        {
            SMenuFriend.overDiv=0;
            closeSMenuFriend();
        };

	function gen_menu(body,icon,label,clickHandler)
	{
        var row=C.CE("tr",body);
        if (!is_mac)
            C.CE("img",C.CE("td",row),{src:"/img/div/"+icon+".gif"});
        var td=C.CE("td",row,{className:"smallnormal"});
        C.CE("td",row);
        td.onmouseover=function(ev)
        {
            this.style.backgroundColor="#ffffd6";
            return false;
        };
        td.onmouseout=function(ev)
        {
            this.style.backgroundColor="#fff";
            return false;
        };

        var a=C.CE("a",td,{href:"#",innerHTML:C.i18n(label)});
        a.onclick=clickHandler;
    }

    
    gen_menu(body,"view","friend.menu.large-photo",function()
    {
        var url="/view_image.php"; 
        url = url+"?photoId="+SMenuFriend.photoId;
       
        openWinCentered(url,"view"+SMenuFriend.userId,700,590,'location=no,status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=no');
        return false;
    });

    gen_menu(body,"message","friend.menu.send-msg",function()
    {    	
        var url="/send_message.php" + "?id="+SMenuFriend.userId;                 
        openWinCentered(url, "send"+SMenuFriend.userId, 500, 500, 'location=no,status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=no');
        return false;
    });

       
    if( show_invite_link == 1 ){
    	
	    gen_menu(body,"message", "friend.menu.invite-to-friend",function()
	    {   
	        FiendAction(inv_userId, '0');
	        return false;
	    });
    } 
    
    
    if(Community.properties("friend.mmail") == "yes")
        gen_menu(body,"mmail","friend.menu.send-mmail",function()
        {
            var url="/sendMMail.do;jsessionid="+SMenuFriend.sessionId+
                "?forUserId="+SMenuFriend.userId;
            openWinCentered(url,"mmail"+SMenuFriend.userId,400,520,'location=no,status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=no');
            return false;
        });

        

    gen_menu(body,"photo","friend.menu.view-photos",function()
    {
    	location.href="/index.php?page=profile_photos&profile=" + SMenuFriend.userId;
        //window.location="/navigate.do;jsessionid="+SMenuFriend.sessionId+"?st.id=community.friendphotos.page&st.friend="+SMenuFriend.userId;
        return false;
    });    

   
    gen_menu(body,"friends","friend.menu.friends",function()
    {
        location.href="/index.php?page=profile_friends&profile="+SMenuFriend.userId;
        return false;
    });    
    

    gen_menu(body,"messaging","friend.menu.messaging",function()
    {
        location.href="/index.php?page=messages&profile="+SMenuFriend.userId;
        return false;
    });

}


SMenuFriend.popupDiv=function(e, sessionId, userId, userName, photoId, photoTime, friend_det)
{	
	
	inv_userId = userId;
	
	if( friend_det == 1 ){
		
		show_invite_link = 0;
	}
	
    e||(e=window.event);

    var x=e.clientX;
    var y=e.clientY;

    SMenuFriend.timer=setTimeout("showSMenuFriend("+x+","+y+",'"+sessionId+"','"+userId+"','"+userName+"','"+photoId+"','"+photoTime+"')",600);
}

showSMenuFriend=function(x,y,sessionId,userId,userName,photoId,photoTime)
{
    SMenuFriend.generate(document.body);
    var div=SMenuFriend.div;

    SMenuFriend.sessionId=sessionId;
    SMenuFriend.userId=userId;
    SMenuFriend.photoId=photoId;
    SMenuFriend.photoTime=photoTime;
    SMenuFriend.userName.innerHTML=userName;

    if(document.body.clientWidth && document.body.clientWidth-x<200)
        x=x-200;
    else
        x=x+15;

    if(document.body.clientHeight && document.body.clientHeight-y<230)
        y=y-230;

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

closeSMenuFriend=function()
{
    if (SMenuFriend.overDiv == "0")
    {
        SMenuFriend.div.style.visibility="hidden";
        SMenuFriend.showDiv=0;
    }
}
function SMenuPhoto()
{
}

SMenuPhoto.div=null;
SMenuPhoto.timer=null;
SMenuPhoto.showDiv=0;
SMenuPhoto.overDiv=0;

SMenuPhoto.sessionId=null;
SMenuPhoto.userId=null;
SMenuPhoto.userName=null;
SMenuPhoto.msg=null;
SMenuPhoto.image=null;

SMenuPhoto.generate=function(parent)
{
    if (!parent)
        return;

    if (SMenuPhoto.div)
        return;

    var C=Community;
    SMenuPhoto.div=C.CE("div",parent,{},{position:"absolute",visibility:"hidden",zIndex:"+1",border:"1px solid #f90",backgroundColor:"#fff",padding:"1px 1px 3px 3px"});
    SMenuPhoto.div.onmouseover=function()
    {
        SMenuPhoto.overDiv=1;
    }

    SMenuPhoto.div.onmouseout=function()
    {
        SMenuPhoto.overDiv=0;
        setTimeout('closeSMenuPhoto()',500);
    }

    var body=C.CE("tbody",C.CE("table",SMenuPhoto.div,{},{padding:"1px"}));
    var row=C.CE("tr",body);
    C.CE("td",row);
    var td=C.CE("td",row,{align:"right"},{padding:"5px"});
    if (!is_mac)
        C.CE("img",td,{src:"/img/div/close.gif"}).onclick=function()
        {
            SMenuPhoto.overDiv=0;
            closeSMenuPhoto();
        };

    row=C.CE("tr",body);
    td=C.CE("td",row,{align:"center",colSpan:2},{padding:"5px"});
    if (!is_mac)
    {
        var a=C.CE("a",td,{href:"#"});
        a.onclick=function()
        {
            window.location="/navigate.do;jsessionid="+SMenuPhoto.sessionId+"?st.id=community.friendmain.page&st.friend="+SMenuPhoto.userId;
            return false;
        };
        SMenuPhoto.image=C.CE("img",a,{border:0});
    }

    row=C.CE("tr",body);
    SMenuPhoto.userName=C.CE("td",row,{align:"center",colSpan:2, className:"smallnormalGray"});

    row=C.CE("tr",body);
    SMenuPhoto.msg=C.CE("td",row,{align:"center",colSpan:2, className:"smallnormal"});
}


SMenuPhoto.popupDiv=function(e,sessionId,userId,userName,gender,photoTime,serverUrl,msg)
{
    e||(e=window.event);

    var x=e.clientX;
    var y=e.clientY;

    SMenuPhoto.timer=setTimeout("showSMenuPhoto("+x+","+y+",'"+sessionId+"','"+userId+"','"+userName+"','"+gender+"','"+photoTime+"','"+serverUrl+"','"+msg+"')",600);
}

showSMenuPhoto=function(x,y,sessionId,userId,userName,gender,photoTime,serverUrl,msg)
{
    SMenuPhoto.generate(document.body);
    var div=SMenuPhoto.div;

    SMenuPhoto.sessionId=sessionId;
    SMenuPhoto.userId=userId;
    SMenuPhoto.userName.innerHTML=userName;
    SMenuPhoto.msg.innerHTML=msg;
    SMenuPhoto.image.src=serverUrl + '?userId=' + userId + '&photoType=2&gender=' + gender + '&photoModify=' + photoTime;

    if(document.body.clientWidth && document.body.clientWidth-x<200)
        x=x-200;
    else
        x=x+15;

    if(document.body.clientHeight && document.body.clientHeight-y<180)
        y=y-180;

    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";
    SMenuPhoto.showDiv=1;
}

SMenuPhoto.hideDiv=function()
{
    if(SMenuPhoto.timer)
    {
        clearTimeout(SMenuPhoto.timer);
        SMenuPhoto.timer=null;
    }

    if(SMenuPhoto.showDiv=="1")
    {
        SMenuPhoto.overDiv=0;
        setTimeout('closeSMenuPhoto()',500);
    }
}

closeSMenuPhoto=function()
{
    if (SMenuPhoto.overDiv == "0")
    {
        SMenuPhoto.div.style.visibility="hidden";
        SMenuPhoto.showDiv=0;
    }
}