// add follow
function follow(following_id, real_name){
    var url = "/friend/add_follow";
    var elem = $(this);
    var form = $("followRequest");
    showCentralNoticer(DIS_10);
    new Ajax.Request(url, {
        method: 'post',
        parameters:{
            id:following_id,
            lw:true
        },
        onComplete: function(originalRequest){
            var a= originalRequest.responseText.evalJSON();
            if(originalRequest.status==200 && a.status == 1){
                if(elem){
                    elem.className = "followingMark";
                    elem.innerHTML = "<span><strong>Following</strong></span>";
                    elem.onclick = function(){
                        unfollow_confirm(a.following_id, real_name)
                    };
                    elem.title = "You are following "+real_name+". Click to un-follow.";
                }
            }
            follow_window(a.following_id,a.whtml);
            hideCentralNoticer();
            if(AvatarPopup){
                AvatarPopup.followCallback(originalRequest.status, 'yes');
            }
        }
    }
    );
}

function follow_window(friend_id, whtml){
    var left = 150;
    if($("followLink_"+friend_id).offsetLeft>800){
        left=-280;
    }
    var pw = new PopWindow({
        optEnabled: true,
        left:left,
        top:0,
        title:"",
        root:"followLink_"+friend_id
    });
    pw.submit = add_following_to_list;
    PopWindow.instance.noticer.hide();
    PopWindow.instance.content.show();
    PopWindow.instance.submitBtn.value = "Add Username to this friend list";
    PopWindow.instance.opt.hide();
    PopWindow.instance.appendNode(new Element("div", {
        id:"followForm",
        style:"padding:5px 10px 20px 5px;"
    }));
    $("followForm").innerHTML=whtml;
}

function add_following_to_list(){
    var url = "/friend/add_friend_to_list";
    var form = $("followRequest");
    PopWindow.instance.hide();
    showCentralNoticer(DIS_10);
    new Ajax.Request(url, {
        method: 'post',
        parameters: Form.serialize(form),
        onComplete: function(originalRequest){
            var a= originalRequest.responseText.evalJSON();
            if(originalRequest.status==200 && a.status == 1){
            }
            hideCentralNoticer();
        }
    }
    );
}
            
/*--------------------------------------
 *used in friend_select_create_list
 * by leinte 
 * 2008-04-05
 ----------------------------------------*/
function changeAddToList(index){
    var value = $(this).value;
    if(value==0){
        return;
    }
    else if(value==-1){           
        showCreateNewList(index);
    }else{
        $("show_contact_name_"+index).innerHTML = value;
        $("list_name_"+index).value = value;
        $(this).hide();
        $("show_add_to_contact_"+index).show();
        PopWindow.instance.opt.show();
    }
}
function showCreateNewList(index){
    $("list_creator_"+index).show();
    $("list_selector_"+index).hide();
    $("new_list_name_"+index).focus();
}
function removeAddToList(index){
    $("list_selector_"+index).show();
    $("list_selector_"+index).value=0;
    $("show_add_to_contact_"+index).hide();
    PopWindow.instance.opt.hide();
    $("show_contact_name_"+index).innerHTML = "";
    $("list_name_"+index).value = "";
}
function createNewList(index){
    var new_list_name = $("new_list_name_"+index).value
    $("list_name_"+index).value = new_list_name;
    $("show_contact_name_"+index).innerHTML = new_list_name;
    $("list_creator_"+index).hide();
    $("show_add_to_contact_"+index).show();
    PopWindow.instance.opt.show();
}
function cancelNewList(index){
    $("list_creator_"+index).hide();
    $("list_selector_"+index).show();
    $("list_selector_"+index).value=0;
}


function unfollow_confirm(following_id, real_name){
    var left = 150;
    if($("followLink_"+following_id).offsetLeft>800){
        left=-280;
    }
    var pw = new PopWindow({
        'optEnabled': true,
        left:left,
        top:0,
        'title':"",
        'root':"followLink_"+following_id
    });
    $('wContent').innerHTML = 'Un-follow <strong>'+real_name+'</strong>?';
    pw.submit = function(){
        unfollow(following_id, real_name)
    };
    $("wSubmit").value="Yes";
    $("wCancel").value = "No";
    pw.cancel = function(){
        PopWindow.instance.hide();
    }
}

function unfollow(following_id, real_name){
    var url = "/friend/remove_follow";
    PopWindow.instance.hide();
    showCentralNoticer(DIS_10);
    new Ajax.Request(url, {
        method: 'post',
        parameters: {
            id:following_id
        },
        onComplete: function(originalRequest){
            var a= originalRequest.responseText.evalJSON();
            if(originalRequest.status==200  && a.status == 1){
                var elem = $("followLink_"+following_id);
                if(elem){
                    elem.className = "followMe";
                    elem.innerHTML = "<span><strong>Follow Me!</strong></span>";
                    elem.onclick = function(){
                        follow.bind(this)(following_id, real_name)
                    };
                    elem.title = "Follow " + real_name;
                }
				//delete item after unfollown in firend page
				var item_user = $("user_"+following_id);
				if(item_user){item_user.remove()};
            }
            hideCentralNoticer();
            if(AvatarPopup){
                AvatarPopup.followCallback(originalRequest.status, 'no');
            }
        }
    }
    );
}