//<!--
/************
This file is part of Genwi JS Lib.
All Rights Reserved.

Desc: people. Controls for people.aspx page..

************/

function People()
{
    this.oDialog = null;
    this.oAddFriend = null;
    this.iReqFriendId = 0;
    this.sReqElem = null;
    this.sPostReqHTML = this.sPostRatingHTML = null;
    this.sItemId = null;
    this.bDelRow = false;
    this.bAddFriendReq = false;
    this.sFriendReqName = null;
    this.oProgDialog = null;
    this.sBaseUrl = _GL.getBaseUrl();
    this.sAjaxBaseUrl = typeof sAjaxBaseUrl != 'undefined' ? sAjaxBaseUrl : this.sBaseUrl;
	this.sAjaxUtilsUrl = this.sAjaxBaseUrl + 'AjaxServices/utils.asmx/';
	this.sAjaxMyStuffUrl = this.sAjaxBaseUrl + 'AjaxServices/MyStuff.asmx/';
    this.sAddFriendUrl = this.sAjaxUtilsUrl + 'AddUserAsFriend?friendid=<#1#>&subscribefeeds=<#2#>&sharefolders=<#3#>';
    this.sAcceptFriendUrl = this.sAjaxMyStuffUrl + 'AjaxUserFriendsAcceptRequest?friendid=<#1#>&accSubscribeFeeds=<#2#>&accShareFolders=<#3#>';
    this.sImportContactsUrl = this.sAjaxUtilsUrl + 'importEmailAddress?username=<#1#>&password=<#2#>&type=<#3#>';
    this.sCheckFriendUrl = this.sAjaxUtilsUrl + 'checkIfFriendIsFollowing?friendid=<#1#>';
    this.aInlineAjaxUrls = {'deny':{'url':this.sAjaxMyStuffUrl+'AjaxUserFriendsDenyRequest?FriendID=<#1#>','postListner':'processReloadPage'},'remFriend':{'url':this.sAjaxMyStuffUrl+'AjaxUserFriendsDeleteFriend?FriendID=<#1#>'},'subscribe':{'url':this.sAjaxUtilsUrl+'addFavoriteItem?itemId=<#1#>','postHTML':'Subscribed'},'subscribechannel':{'url':this.sAjaxUtilsUrl+'addFavoriteChannel?channelId=<#1#>','postHTML':'Subscribed'},'deleteMsg':{'url':this.sAjaxMyStuffUrl+'AjaxUserMessagesDeleteMessage?MessageID=<#1#>','postListner':'processReloadPage'},'deleteComment':{'url':this.sAjaxMyStuffUrl+'AjaxUserCommentsRemoveComment?CommentID=<#1#>','postListner':'processReloadPage'},'addToWire':{'url':this.sAjaxUtilsUrl+'subsToAllFeedsByUserId?friendId=<#1#>','postListner':'processAddToUser'}};
    this.saveShareUrls ={'save':_GL.getBaseUrl() + 'addpage.aspx?addtopage=existing&itemId=','share':_GL.getBaseUrl() + 'share.aspx?itemId='};
    this.sLoginUrl = _GL.getBaseUrl() + 'login.aspx';
    this.sSubToAllUserFeed = this.sAjaxUtilsUrl + 'subsToAllFeedsByUserId?userId=<#1#>&friendId=<#2#>';
    this.sImportFeedUrl = _GL.getBaseUrl() + 'importfeeds.aspx';
    
    this.isAthenticated = function()
    {
        return (typeof username != 'undefined' && username.length > 0);
    }
    
    this.addFriend = function(pFriendId, pFriendName, pObj)
    {
        with(this)
        {
            if (!isAthenticated())
            {
                _GL.redirect(sLoginUrl + '?refer=' + encodeURIComponent(location.href));
                return;
            }
        
            iReqFriendId = pFriendId;
            sFriendReqName = pFriendName;
            if (!pObj)
                pObj = _GL.getUIElem('btnSpan_'+pFriendId);
            oAddFriend = pObj;
                     
            var oAjax = new Ajax('checkFriend', this.sCheckFriendUrl.replaceTokens(pFriendId), true,  ['oPeople.processCheckFriend']);
            oAjax.iResponseType = oAjax.RESPONSE_HTML;
            oAjax.bShowProgress = true;
            oAjax.send();
        }
    }
    
    this.processCheckFriend = function(pResp)
    {
        var v = oXmlHelper.getValue(pResp.oResponseXml,'boolean');
        this.bAddFriendReq = !this.oAddFriend.innerHTML.has('Accept');
        if (v.is('false'))
        {
            var s = '', sFN = this.sFriendReqName;
            s += sMBHeader.replaceTokens('Add Friend', 'oPeople');
            s += sMBContent.replaceTokens(addFriendHTML.replaceTokens(sFN, sFN +"'s"));
            this.openDialog(s, 600, 220);
            
            if (!this.bAddFriendReq)
            {
                var oFootNote = _GL.getUIElem('footNoteId')
                if (oFootNote)
                    oFootNote.style.display = 'none';
            }
        }
        else
            this.sendAddFriendRequest();
    }
    
    this.sendAddFriendRequest = function()
    {
        var oSubFeed = _GL.getUIElem('subFeed'),
            u = this.bAddFriendReq ? this.sAddFriendUrl : this.sAcceptFriendUrl;
        
        var v = oSubFeed && oSubFeed.checked ? '1' : '0', sUrl = u.replaceTokens(this.iReqFriendId, v, v);
        var oAjax = new Ajax('addFriendReq', sUrl, true,  ['oPeople.processAddFriendReq']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.bShowProgress = true;
        oAjax.sShowProgressId = this.oAddFriend.id;
        if (this.oDialog)
            this.oDialog.display(false);
        oAjax.send();   
    }
    
    this.processAddFriendReq = function()
    {
        with(this)
        {
            if (oDialog)
                oDialog.display(false);
            var pn = oAddFriend.parentNode ? oAddFriend.parentNode : oAddFriend.parentElement;
            pn.innerHTML = '<span class="text4">Request ' + (this.bAddFriendReq ? 'Submitted' : 'Accepted') + '</span>';
        }
    }
    
    this.subscribeToProfile = function(pProfile,pScreenName)
    {
        if (typeof pProfile != 'undefined')
            profilename = pProfile;
            
        var s = '', sFN = this.sFriendReqName;
            s += sMBHeader.replaceTokens('Follow ' + pScreenName, 'oPeople');
            s += sMBContent.replaceTokens(followProfileHTML.replaceTokens(pScreenName));
        
        this.openDialog(s, 600, 220);
    }
    
    this.sendFollowRequest = function()
    {
        var bFollowPost = _GL.getUIElem('followPost').checked,
            bFriendReq = _GL.getUIElem('friendReq').checked;
        
        this.oDialog.display(false);
        var u = _GL.getBaseUrl() + 'json/jutils.aspx?action=subscribe';
        
        if (bFollowPost)
            u += '&profile=' + profilename;
        
        if (bFriendReq)
            u += '&friend=true';
        else
            u += '&friend=false';
             
        if (!bFollowPost && bFriendReq)
            var u = _GL.getBaseUrl() + 'json/jutils.aspx?action=friend&profile='+ profilename;            

        //_GL.redirect(this.isAthenticated() ? u : (this.sLoginUrl + '?refer=' + u));
        _GL.redirect(u,false,(typeof sPageName != 'undefined' && sPageName.is('follow'))); 

        return;
    }
    
    this.postInlineAjax = function(pId, pObj, pType)
    {
        var b = this.isAthenticated();
        if (pType.is('subscribe'))
        {
            if (!b)
            {
                var u = _GL.getBaseUrl() + 'json/jutils.aspx?action=subscribe&itemid=' + pId;
                _GL.redirect(u);
            }
            else
                this.postInlineChannelAjax(pId,pObj,'subscribe');
                //this.postInlineChannelAjax(pId,pObj,'subscribechannel');
                
            return;
        }
    
        if (!b)
        {
            _GL.redirect(this.sLoginUrl + '?refer=' + encodeURIComponent(location.href));
            return;
        }
    
        var a = this.aInlineAjaxUrls[pType], reqObj;
        if (typeof(pObj) == 'string')
            reqObj = _GL.getUIElem(pObj);
        else
            reqObj = pObj;
        this.sReqElem = reqObj;
        this.sPostReqHTML = a.postHTML;
        this.sItemId = pId;
        this.bDelRow = a.delRow ? a.delRow : false;
        var oAjax = new Ajax('inlineAjaxReq', a.url.replaceTokens(pId), true,  [a.postListner ? 'oPeople.'+a.postListner : 'oPeople.processInlineAjaxReq']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.bShowProgress = true;
        oAjax.sShowProgressId = reqObj.id;
        //reqObj.style.position = 'relative';
        oAjax.send();
    }
    
    this.postInlineChannelAjax = function(pId, pObj, pType)
    {
        if (!this.isAthenticated())
        {
            var u = pType.is('subscribechannel') ? (_GL.getBaseUrl() + 'json/jutils.aspx?action=subscribe&channelid=' + pId) :(this.sLoginUrl + '?refer=' + encodeURIComponent(location.href));
            
            _GL.redirect(u);
            return;
        }
    
        var a = this.aInlineAjaxUrls[pType], reqObj;
        if (typeof(pObj) == 'string')
            reqObj = _GL.getUIElem(pObj);
        else
            reqObj = pObj;
        this.sReqElem = reqObj;
        this.sPostReqHTML = a.postHTML;
        this.sItemId = pId;
        this.bDelRow = a.delRow ? a.delRow : false;
        var oAjax = new Ajax('inlineAjaxReq', a.url.replaceTokens(pId), true,  [a.postListner ? 'oPeople.'+a.postListner : 'oPeople.processInlineAjaxReq']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.bShowProgress = true;
        oAjax.sShowProgressId = reqObj.id;
        //reqObj.style.position = 'relative';
        oAjax.send();
    }
    
    this.processInlineAjaxReq = function(pResp)
    {//debugger;
        this.bDelRow = false;
        if (this.bDelRow)
        {
            var oDelRow = _GL.getUIElem('row_' + this.sItemId);
            if (oDelRow)
                oDelRow.style.display = 'none';
            
            this.bDelRow = false;
            
            //display message
            var oMsgSec = _GL.getUIElem('pageMsgSec'),
                oMsgTxt = _GL.getUIElem('pageMsgTxt');
            
            if (oMsgSec && oMsgTxt)
            {
                oMsgSec.style.display = 'block';
                oMsgTxt.innerHTML = this.sPostReqHTML
            }
        }
        else
            this.sReqElem.innerHTML = this.sPostReqHTML;
    }
    
    this.processReloadPage = function()
    {
        _GL.redirect(location.href);
    }
    
    this.processAddToUser = function(pResp)
    {
        var oMsg = _GL.getUIElem('msgDiv'), s = '';
        s += 'Successfully added ';
        s += (typeof userScreenName != 'undefied' ? userScreenName + '\'s posts' : '');
        s += ' to your wire';
        oMsg.innerHTML = s;
        oMsg.style.display = 'block';
        this.sReqElem.innerHTML = "";   
    }
    
    this.addComment = function(pItemId, pCommentsId)
    {
        var sUrl = this.sAjaxUtilsUrl + 'addItemComment?itemid=';
        sUrl += pItemId;
        sUrl += '&comment=' + _GL.getUIElem(pCommentsId).value;
        
        var oCommentBody = _GL.getUIElem('commentsInput');
        oCommentBody.innerHTML = 'Submitting Comments...';
        
        var oAjax = new Ajax('submitComments', sUrl, true,  ['oPeople.processSendComments']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.send();
        
    }
    
    this.processSendComments = function()
    {
        var oCommentBody = _GL.getUIElem('commentsInput');
        oCommentBody.innerHTML = 'Comments posted successfully';
    }
    
    this.addUserComment = function(pUserTo, pCommentsElem, pItemId)
    {
        this.sCommentsUserId = pUserTo;
        
        var sUrl = this.sAjaxBaseUrl + 'json/jcomments.aspx?action=add&itemid='+pItemId+'&text='+_GL.getUIElem(pCommentsElem).value;
        
        //var sUrl = this.sAjaxMyStuffUrl + 'AjaxUserCommentsPostComment?UserTo='+pUserTo;
        //sUrl += '&CommentText=' + _GL.getUIElem(pCommentsElem).value;
        
        var oAjax = new Ajax('submitComments', sUrl, true,  ['oPeople.processUserComment']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.bShowProgress = true;
        oAjax.sShowProgressId = 'postCommentBtn';
        oAjax.send();
    }
    
    this.processUserComment = function(pResp)
    {
        _GL.redirect(_GL.getBaseUrl() + 'people/mystuffcomments.aspx?username=' + this.sCommentsUserId);
    }
    
    this.removeFriend = function(pFriendId)
    {
        var b = confirm("Are you sure you want to delete this friend?");
        if (b)
        {
            this.sItemId = pFriendId;
            var sUrl = this.aInlineAjaxUrls['remFriend'].url.replaceTokens(pFriendId);
            var oAjax = new Ajax('removeAjaxReq', sUrl, true,  ['oPeople.processRemoveFriend']);
            oAjax.iResponseType = oAjax.RESPONSE_HTML;
            oAjax.bShowProgress = true;
            oAjax.sShowProgressId = 'rem_'+pFriendId;
            oAjax.send();
        }
    }
    
    this.processRemoveFriend = function(pResp)
    {
        var oLi = _GL.getUIElem('li_'+ this.sItemId);
        if (oLi)
        {
            oPrt = oLi.parentNode ? oLi.parentNode : oLi.parentElement;
            oPrt.removeChild(oLi);
        }
    }
    
    this.ssFeed = function(pAction, pItemId)
    {
        if (this.isAthenticated())
        {
            var sUrl = this.saveShareUrls[pAction];
            if (!oOverlay)
                //oOverlay = new OverlayPopup('dialog', 500, 400, true, sUrl + pItemId);
                oOverlay = new OverlayPopup('mb-overlay', '500', (pAction.is('save')?'370':'400'), true, sUrl + pItemId, 'mb');
            else
                oOverlay.sUrl = sUrl + pItemId;
            //oOverlay.setClass('overlayDialog');
            oOverlay.display();
            return;
        }
        else
        {
            alert('Please login to ' + pAction + ' feed.');
            return false;
        }
    }
    
    this.addComments = function(pItemId)
    {
        if (this.isAthenticated())
        {
            this.sCommentId = pItemId;
            with(this)
            {
                oDialog = null;
                oDialog = new Overlay('dialog', 600, 350, true);
                oDialog.setClass('overlayDialog');
                oDialog.setContent(commentsHTML.replaceTokens('oPeople'));
                oDialog.display(true);
                oDialog.setPosition();
            }
        }
        else
        {
            alert('Please login to post a comment.');
            return false;
        }
    }
    
    this.sendComments = function()
    {
        var v = _GL.getUIElem('postComments').value,
            oErrMsg = _GL.getUIElem('errorMsg');
        oErrMsg.style.display = 'none';
        
        if (v.isEmpty())
        {
            oErrMsg.innerHTML = sCommentsErrMsg;
            oErrMsg.style.display = 'block';
            return;
        }
        var sUrl = this.sAjaxUtilsUrl + 'addItemComment?itemid=';
        sUrl += this.sCommentId;
        sUrl += '&comment=' + v;
        
        var oAjax = new Ajax('submitComments', sUrl, true,  ['oPeople.processSendComments']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.send();
        
    }
    
    this.processSendComments = function()
    {
        var oCommentBody = _GL.getUIElem('commentsBody'), oCL = _GL.getUIElem('comments'),
            v = _GL.getUIElem('postComments').value, s;
        if (typeof aUserImage != 'undefined' && oCL)
        {
            s = commentsDisplayHTML.replaceTokens(username, aUserImage,aDateTime, v);
            var oTable = oCL.getElementsByTagName('TABLE'), oTr, oTd;
            if (oTable && oTable[0])
            {
                oTr = oTable[0].insertRow(0), oTd = oTr.insertCell(-1);
                oTd.innerHTML = s;
            }
            else
              oCL.innerHTML = s;
                
            this.oDialog.display(false);
        }
        else
            oCommentBody.innerHTML = commentsSuccHtml.replaceTokens('oPeople');
    }
    
    this.inviteFriends = function(pEmails, pMessage)
    {   
        var oMessageDiv = _GL.getUIElem('pageDivInviteAFriendStatus');
        if (pEmails.isEmpty())
        {
            oMessageDiv.innerHTML = 'Please provide friend\'s email address.';
            return;   
        }
        var oContent = _GL.getUIElem('inviteForm');
        if (oContent)
            oContent.style.visibility = 'hidden';
        oMessageDiv.innerHTML = sLoaderHtml + '<div style="margin-top:10px;">Sending Invitation...</div>';
        var sUrl = this.sAjaxMyStuffUrl + 'AjaxUserInviteEmailInvite?FriendEmail=' + pEmails + '&Message=' + encodeURIComponent(pMessage);
        
        var oAjax = new Ajax('submitComments', sUrl, true,  ['oPeople.processInviteFriends']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.send();
    }
    
    this.processInviteFriends = function()
    {
        var oMessageDiv = _GL.getUIElem('pageDivInviteAFriendStatus');
        if (oMessageDiv)
            oMessageDiv.innerHTML = 'Invite sent! Send more invitations';
        
        var oContent = _GL.getUIElem('inviteForm');
        if (oContent)
            oContent.style.visibility = 'visible';
            
        var oEmail = _GL.getUIElem('pageTextBoxInviteFriendAddress');
        if (oEmail)
            oEmail.value = '';
        
        /*var oMessage = _GL.getUIElem('pageTextBoxInviteFriendMessage');
        if (oMessage)
            oMessage.value = '';
        */
    }
    
    this.checkUsername = function(username)
    {   
        var oMessageDiv = _GL.getUIElem('usernameavailibility');
        if (username.isEmpty())
            return;   
        oMessageDiv.innerHTML = sLoaderSmallHtml + 'Checking..';
        var sUrl = this.sAjaxUtilsUrl + 'checkUsernameExists?username=' + encodeURIComponent(username);
        var oAjax = new Ajax('checkUsername', sUrl, true,  ['oPeople.processCheckUsername']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.send();
    }
    
    this.processCheckUsername = function(pResp)
    {
        var oMessageDiv = _GL.getUIElem('usernameavailibility');
        if((!pResp)||(pResp.oResponseXml.documentElement.childNodes[0].nodeValue == "true"))
        {
           if (oMessageDiv)
                oMessageDiv.innerHTML = "<span class='text3'>Available</span>";
        }
        else
        {
            if (oMessageDiv)
                oMessageDiv.innerHTML = "<span class='red3'>Taken</span>";
        }
    }
    
    
    this.sendUserMessage = function(pToUser, pTitleId, pMsgTxt, pMsgId)
    {
        var userTo = _GL.getUIElem(pToUser).value,
            sUrl = this.sAjaxMyStuffUrl + 'AjaxUserMessagesSendMessage?UserTo=';
            
            
        userTo = userTo.replace(/\[.*?\]/gi, "");
        var users = userTo.split(';');
        for(var i=0; i<users.length; i++){
        var auserTo = users[i];
            if(auserTo.replace(/\s*/, "") != "")
            {
                sUrl += auserTo;
                sUrl += '&MessageTitle=' + encodeURIComponent(_GL.getUIElem(pTitleId).value);
                sUrl += '&MessageText=' + encodeURIComponent(_GL.getUIElem(pMsgTxt).value);
                
                this.sMsgDisplayId = typeof sMsgPlaceHolder != 'undefined' ? sMsgPlaceHolder : pMsgId;
                
                var oMessageDiv = _GL.getUIElem(this.sMsgDisplayId);
                var oContent = _GL.getUIElem(sContent);
                if (oContent)
                    oContent.style.visibility = 'hidden';
                if(i == 0)
                oMessageDiv.innerHTML = sLoaderHtml + '&nbsp;<div style="margin-left:10px;float:left;">Sending Message to ' + auserTo + '</div>';
                var oAjax = new Ajax('sendEmail', sUrl, true,  ['oPeople.processSendMessage']);
                oAjax.iResponseType = oAjax.RESPONSE_HTML;
                oAjax.send();
            }
        }
    
    }
    
    this.processSendMessage = function()
    {
        var oMsgDiv = _GL.getUIElem(this.sMsgDisplayId);
        if (oMsgDiv)
            oMsgDiv.innerHTML = '<span>Message Sent Successfully.</span>';
    }
    
    this.openImportContacts = function()
    {
        s += sMBHeader.replaceTokens('Iport Contacts', 'oPeople');
        s += sMBContent.replaceTokens(importContactsHTML);
        this.openDialog(s, 600, 600);
    }
    
    this.importContacts = function()
    {
        var oErrDiv = _GL.getUIElem('ErrorMessage');
        if (oErrDiv)
            oErrDiv.style.display = 'none';
        
        _GL.showShadow(true);
        if (!this.oProgDialog)
            this.oProgDialog = new Overlay('progW',null,null,false);
        
        this.oProgDialog.display(true);
        this.oProgDialog.setPosition();
    }
    
    this.transferContacts = function(pStr, pError)
    {
        this.oProgDialog.display(false);
        if (!pError)
        {
            var s = '';
            s += sMBHeader.replaceTokens('Tell a Friend', 'oPeople');
            s += sMBContent.replaceTokens(importContactsHTML);
            this.openDialog(s, 500, 500);
            var oContacts = _GL.getUIElem('contactDetails');
            oContacts.innerHTML = pStr;
        }
        else
        {
            _GL.showShadow(false);
            var oErrDiv = _GL.getUIElem('ErrorMessage');
            if (oErrDiv)
            {
                oErrDiv.style.display = 'none';
                oErrDiv.innerHTML = pStr;
                var oInvite = _GL.getUIElem('inviteaddresses');
                oInvite.className = "";
                oErrDiv.style.display = 'inline';
            }
        }
    }
    
    this.processImportContact = function(pResp)
    {
        var oXml = pResp.oResponseXml,
            emails = oXmlHelper.getNodes(oXml,'string'), 
            len = emails ? emails.length : 0, s = '', 
            oLyr = _GL.createElement('div'),
            oContacts = _GL.getUIElem('contactDetails'),
            oCopButton = _GL.getUIElem('copyContactBtn');
            
            
        if (len > 0)
        {
            oLyr.className = 'contactList';
            for (var i=0; i<len; i++)
            {
                var v = oXmlHelper.getNodeValue(emails[i]);
                s += '<input type="checkbox" name="emailContact" value="' + v + '" checked />&nbsp;' + v + '<br />';
            }
            oLyr.innerHTML = s;
            
            oContacts.innerHTML = "";
            oContacts.appendChild(oLyr);
            oCopButton.style.display = 'inline';
        }
        else
        {
            oContacts.innerHTML = "<br />The username/password you entered is wrong or your address book is empty. Please try again."
            //oImpContactBtn.style.display = 'inline';
        }
        
    }
    
    this.copyContacts = function()
    {
        var emails = document.forms['frmEmail'].elements,
            len = emails ? emails.length : 0,
            pEmail = document.getElementById('pageTextBoxInviteFriendAddress'),
            s = '';

        for (var i=0; i<len; i++)
        {
            var email = emails[i];
            if (email.type == 'checkbox' && email.checked)
            {
                if (s.length > 0)
                    s += ';';
                s += email.value;
            }
        }
        
        if (pEmail)
        {
            if (pEmail.value.length > 0)
                pEmail.value = pEmail.value + ';' + s;
            else
                pEmail.value = s;
        }
        this.oDialog.display(false);
    }
    
    this.openDialog = function(pHtml, pWidth, pHeight)
    {
        with(this)
        {
            oDialog = new Overlay('mb-overlay', pWidth, pHeight, true,'mb');
            oDialog.sWidth = pWidth;
            oDialog.sHeight = pHeight;
            oDialog.setContent(pHtml);  
            oDialog.display(true);
            oDialog.setPosition(); 
        }
    }
    
    this.emailFeed = function(pItemId)
    {
        this.sItemId = pItemId;
        var s = '';
        s += sMBHeader.replaceTokens('Tell a Friend', 'oPeople');
        s += sMBContent.replaceTokens(emailHTML.replaceTokens('oPeople'));
        this.openDialog(s, 600, 420);
    }
    
    this.sendEmail = function()
    {
        var oErrMsg = _GL.getUIElem('errorMsg');
        oErrMsg.style.display = 'none';
        var toEmailVal = _GL.getUIElem('toEmails').value,
            fromEmailVal = _GL.getUIElem('fromEmail').value,
            nameVal =  _GL.getUIElem('fromName').value,
            _1 = toEmailVal.isEmpty() ? 'Friend\'s Email Address' : '',
            _2 = fromEmailVal.isEmpty() ? ', Your Email Address' : '', 
            _3 = nameVal.isEmpty() ? ', Your name' : '';
        
        if (_1 || _2 || _3)
        {
            oErrMsg.innerHTML = sEmailErrMsg.replaceTokens(_1, _2, _3);
            oErrMsg.style.display = 'block';
            return;   
        }
    
        var sUrl = this.sAjaxUtilsUrl + 'mailItem2?to=' + toEmailVal;
        sUrl += '&from=' + fromEmailVal;
        sUrl += '&name=' + nameVal;
        sUrl += '&emailmessage=' + _GL.getUIElem('emailMessage').value;
        sUrl += '&itemid=' + this.sItemId,
        oEmailBody = _GL.getUIElem('emailBody'),
        oLoaderDiv = _GL.createElement('div');
        
        var oAjax = new Ajax('emailFeed', sUrl, true,  ['oPeople.processSendEmail']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oEmailBody.innerHTML = "";
        oEmailBody.appendChild(oLoaderDiv);
        oLoaderDiv.innerHTML = sLoaderHtml;
        oAjax.send();
    }
    
    this.processSendEmail = function(pResp)
    {
        var oEmailBody = _GL.getUIElem('emailBody');
        oEmailBody.innerHTML = emailSuccHtml.replaceTokens('oPeople');
    }
    
   this.onMouseOverStar = function(pObj, pOver)
   {
        var pn = _GL.getParentNode(pObj), cl = pObj.className;
        if (pn)
            pn.className = pOver ? cl + 's' : '';
    
    }
    
    this.rateItem = function(pItemId, pRating)
    {
        if (!this.isAthenticated())
        {
            alert('Please login to rate feed.');
            return false;
        }
       
        var sUrl = this.sAjaxUtilsUrl + 'addItemRating?itemId=' + pItemId + '&rating=' + pRating;
        var oAjax = new Ajax('rateItem', sUrl, true, ['oPeople.processRatingResponse']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        var oRating = _GL.getUIElem('rateFeed');
        this.sPostRatingHTML = oRating.innerHTML;
        oRating.onmouseover = null;
        oRating.onMouseout = null;
        oAjax.send();
    }
    
    this.processRatingResponse = function(pResp)
    {
        // show the saved rating.
        var v = oXmlHelper.getValue(pResp.oResponseXml,'string'),
            oRating = _GL.getUIElem('rateFeed');
        oRating.innerHTML = this.sPostRatingHTML ? this.sPostRatingHTML : 'Rating Added';
        var oRO = _GL.getUIElem('rateOuter'), 
            oCR = _GL.getUIElem('curRating');
        
        if (oRO)
            oRO.className = 'rating';
        
        if (oCR && v)
            oCR.style.width = v + '%';
        
    }

	this.sendItemClick = function(pItemId, pUrl)
	{
		var sUrl = this.sAjaxUtilsUrl + 'addItemClicks?itemId=' + pItemId + '&referUrl=' + pUrl;

	    var oAjax = new Ajax('itemClick', sUrl, true, ['oPeople.processNoResponse']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.send();
    }
    
    this.processNoResponse = function()
    {
       return;
    }
    
    this.subscribeToAllUserFeeds = function(pUserId, pFriendId, pObj)
    {
        this.sReqElem = pObj;
        this.sPostReqHTML = 'Subscribed';
        var oAjax = new Ajax('inlineAjaxReq', this.sSubToAllUserFeed.replaceTokens(pUserId,pFriendId), true,['oPeople.processInlineAjaxReq']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.bShowProgress = true;
        oAjax.sShowProgressId = pObj.id;
        oAjax.send();
    }
    
    this.importFeeds = function()
    {
        if (!oOverlay)
            oOverlay = new OverlayPopup('mb-overlay', '500', '225', true, this.sImportFeedUrl, 'mb');
        else
            oOverlay.sUrl = this.sImportFeedUrl;
        
        oOverlay.display();
        return;
    }
}

var oPeople = new People();
var oOverlay = null;
