function load_profile_comments(div_id,u,idx) {
    $(div_id).update('<br><br><p align=center style="padding-left:3em;font-weight:bold;color:red;">... LOADING ...</p>');

    new Ajax.Request("/a_profile_comments.php", {
            method: 'get',
            parameters: { u: u, idx: idx, type: 'profile' },
            onSuccess: function(response) { 
                handle_comment_reformt(div_id,response);
            }
    });
}

function load_status_comments(div_id,u,i,idx,size) {
    $(div_id).update('<br><br><p align=center style="padding-left:3em;font-weight:bold;color:red;">... LOADING ...</p>');

    new Ajax.Request("/a_profile_comments.php", {
            method: 'get',
            parameters: { u: u, i: i, idx: idx, size: size, type: 'status' },
            onSuccess: function(response) {
                handle_comment_reformt(div_id,response);
            }
    });

    return true;
}

function load_content_aboutme(div_id,u) {
    $(div_id).innerHTML = '<br><br><p align=center style="padding-left:3em;font-weight:bold;color:red;">... LOADING ...</p>';
    
    var _url = "/a_profile_aboutme.php?u="+u;
    new Ajax.Updater(div_id, _url, {
            method: 'get'
   }); 

    return true;
}

function load_content_games(div_id,u) {
    $(div_id).innerHTML = '<br><br><p align=center style="padding-left:3em;font-weight:bold;color:red;">... LOADING ...</p>';

    var _url = "/a_profile_games.php?u="+u;
    new Ajax.Updater(div_id, _url, {
            method: 'get'
    });

    return true;
}

function load_content_music(div_id,u) {
    $(div_id).innerHTML = '<br><br><p align=center style="padding-left:3em;font-weight:bold;color:red;">... LOADING ...</p>';

    var _url = "/a_profile_music.php?u="+u;
    new Ajax.Updater(div_id, _url, {
            method: 'get'
    });

    return true;
}

function load_content_general(div_id,u) {
    $(div_id).innerHTML = '<br><br><p align=center style="padding-left:3em;font-weight:bold;color:red;">... LOADING ...</p>';

    var _url = "/a_profile_general.php?u="+u;
    new Ajax.Updater(div_id, _url, {
            method: 'get'
    });

    return true;
}

function load_content_idols(div_id,u) {
    $(div_id).innerHTML = '<br><br><p align=center style="padding-left:3em;font-weight:bold;color:red;">... LOADING ...</p>';

    var _url = "/a_profile_idols.php?u="+u;
    new Ajax.Updater(div_id, _url, {
            method: 'get'
    });

    return true;
}

function load_content_movies(div_id,u) {
    $(div_id).innerHTML = '<br><br><p align=center style="padding-left:3em;font-weight:bold;color:red;">... LOADING ...</p>';

    var _url = "/a_profile_movies.php?u="+u;
    new Ajax.Updater(div_id, _url, {
            method: 'get'
    });

    return true;
}

function toggleExpandCollapse(id) {
       identity = document.getElementById(id).className;
       identity2 = document.getElementById(id);
       if(identity == "userprofile_content_toggle_img"){
       identity2.className = "userprofile_content_toggle_img2";
       }else{
       identity2.className = "userprofile_content_toggle_img";
       }
}

function toggleText(id) {
       identity = document.getElementById(id).innerHTML;
       identity2 = document.getElementById(id);
       if(identity == "click to open"){
       identity2.innerHTML = "click to close";
       }else{
       identity2.innerHTML = "click to open";
       }
}

function add_user_status(formid,ver) {    
    var tmp = $(formid).serialize(true);
    var tmpargs = $H(tmp);

    var val = tmpargs.get('msg');
    val.strip();
    val.stripTags();

    if (val.blank()) {
        return;
    }

    tmpargs.set('msg',val);

    new Ajax.Request("/a_status.php", {
            method: 'post',
            parameters: tmpargs,
            onSuccess: function(response) {
                handle_add_user_status(response,ver);
            }
    }); 
}

function handle_add_user_status(transport,ver) {
    if (transport.status == 200) {
        var data = transport.responseJSON;
        if (data) {
            if (data['error']){
                $('set_online_status').update(data['html']);
            } else {
                if (ver == 0) {
                    $('set_online_status').update(data['html']);
                } else {
                    $('lite_status').hide();
                }
                if (event_helper) {
                    event_helper.trigget_event(STATUS_CHANGE);
                }
            }
        }
    }
}

function delete_user_all_status(uid,action) {
    if (confirm("Delete all statuses?")) {
        new Ajax.Request("/a_status.php", {
            method: 'post',
            parameters: { action: 'deleteall', uid: uid },
            onSuccess: function(response) {
                handle_delete_user_status(response,action);
            }
        });
    }
}


function delete_user_status(uid,id,action) {
    if (confirm("Delete this status?")) {
        new Ajax.Request("/a_status.php", {
            method: 'post',
            parameters: { action: 'delete', uid: uid, id: id },
            onSuccess: function(response) {
                handle_delete_user_status(response,action);
            }
        });
    }
}

function handle_delete_user_status(transport,action) {
    if (transport.status == 200) {
        var data = transport.responseJSON;
        if (data) {
            if (data['error']){
                alert(data['html']);
            } else {
                if (data['option']) {
                    var div = $('current_online_status');
                    if (div) {
                        div.update(data['option']);
                    }
                }
                if (action) {
                    window.location.reload();    
                }
            }
        }
    }
}

function change_online_icon(selectObj) {
    var idx = selectObj.selectedIndex; 
    var which = selectObj.options[idx].value;

    new Ajax.Request("/a_status.php", {
            method: 'post',
            parameters: { action: 'icon', icon: which },
            onSuccess: function(response) {
                handle_change_online_icon(response);
            }
    });
}

function handle_change_online_icon(transport,form) {
    if (transport.status == 200) {
        var data = transport.responseJSON;
        if (data) {
            if (data['error']){
                alert(data['html']);
            } else {
                if (data['html']) {
                    $('set_online_icon_img').src = data['html'];
                }
            }
        }
    }
}

function jump_to_leave_comment(uid) {
    //check if empty
    var divid = 'comments_'+uid+'_0';
    var div = $(divid);
    if (div) {
        myDivLL.remove(divid);    
    }
    $('comment_frm_0').scrollTo();
}

function add_new_friend(event) {
    if (!event.stopped) {
        var form = $(Event.element(event));
        if (form) {
            new Ajax.Request('/a_friend_actions.php', {
                parameters: form.serialize(true),
                onSuccess: function(resp) {
                    add_new_friend_response(resp,'friend');
                }
            }); 
        }
    
        Event.stop(event);
    } else {
        Modalbox.resizeToContent();
    }
}

function hidealluserfriendactions() {
    var divs = $$('tr.userfriendactions');
    for (var i = 0; i < divs.length; ++i) {
        divs[i].hide();
    }
}

function add_new_friend_response(response,captchaid) {
    if (response.status == 200) {
        var data = response.responseJSON;
        if (data) {
            if (data.option && data.option.token) {
                reload_captcha_done(response,captchaid);
                Modalbox.resizeToContent();
            }

            if (data.error){
                if (!data.html.empty()) {
                    Modalbox.insert_error_bottom(1,data.html);
                }
            } else {
                if (pageTracker && (typeof data['to_life'] != 'undefined')) {
                    pageTracker._trackEvent('friendreq', 'recv', String(data['to_life']));
                }
                if (pageTracker && (typeof data['from_life'] != 'undefined')) {
                    pageTracker._trackEvent('friendreq', 'sent', String(data['from_life']));
                }

                Modalbox.notification('Successfully, sent user a new friend request.', { autoClose: 3000} );
                hidealluserfriendactions();
            }
        }
    }
}

function remove_friend(params) {
    new Ajax.Request('/a_friend_actions.php', {
        parameters: { action: 'remove', uid: params.uid },
        onSuccess: function(resp) {
            remove_friend_response(resp,params);
        }
    });    

    return false; //do not remove dialog, will remove after ajax
}

function remove_friend_response(response,params) {
    if (response.status == 200) {
        var data = response.responseJSON;
        if (data) {
            if (data.error){
                if (!data.html.empty()) {
                    Modalbox.insert_error_bottom(1,data.html);
                }
            } else {
                Modalbox.notification('Successfully, removed this user from friends.', { autoClose: 3000} );
                hidealluserfriendactions();
            }
        }
    }
}

function confirmModal(msg,moptions,yesfunc,nofunc) { 
    if(typeof yesfunc != 'undefined') {
        var tmp = { OnYes: yesfunc };
        Object.extend(moptions, tmp);
    }    
    if(typeof nofunc != 'undefined') {
        var tmp = { OnNo: nofunc };
        Object.extend(moptions, tmp);
    }

    Modalbox.showConfirm(msg,moptions); 
}


