﻿// JScript File

function FriendRequestPage()
{
    
}

FriendRequestPage.Show = ShowFriendRequest;
FriendRequestPage.Hide = HideFriendRequest;


function ShowFriendRequest(profileimage, profileid, screenname)
{
    var n = document.getElementById('friend_request_box');
    
    if(n == null)
    {
        var n = document.createElement('div', 'ajax_box_holder');
        var holder = document.createElement('div', 'fr_main');
        var img_div = document.createElement('div', 'fr_image');
        var info_div = document.createElement('div', 'fr_info');
        var name_div = document.createElement('div', 'fr_name');
        var buttons_div = document.createElement('div', 'fr_buttons');
        var clear_div = document.createElement('div', 'fr_clear');
        
        var b_ok = null;
        var b_cancel = null;
        
        b_ok = document.createElement('input', 'fr_b_ok');
        b_cancel = document.createElement('input', 'fr_b_cancel');
                
        n.id = 'ajax_box_holder';
        holder.id = 'friend_request_box';
        img_div.id = 'fr_image';
        info_div.id = 'fr_info';
        name_div.id = 'fr_name';
        buttons_div.id = 'fr_buttons';
        clear_div.id = 'fr_clear';
        clear_div.className = 'clear';
        
        b_ok.id = 'fr_b_ok';
        b_cancel.id = 'fr_b_cancel';
        
        b_ok.type = 'button';
        b_cancel.type = 'button';
        
        b_ok.value = 'Send';
        b_cancel.value = 'Cancel';
        
        b_ok.className = 'ajax-send-button';
        b_cancel.className = 'ajax-cancel-button';
        
        b_ok.profileid = profileid;
        
        b_ok.onclick = function(ev)
        {
            var params = new Array();
            
            params[0] = 'friend-id=' + this.profileid;
            
            var a = new AjaxCall('friend.request', params);
            a.sessionid = 'xx-xx';
            a.resulttype = 'text';
            
            a.Send(function(result)
            {
                if(result.toLowerCase() == 'alreadyrequestedx')
                {
                    name_div.innerHTML = 'You already have requested ' + screenname + ' as a friend. You\'ll have wait until it is approved.'
                    buttons_div.removeChild(b_ok);
                    b_cancel.value = 'OK';
                }
                else
                {
                    name_div.innerHTML = 'Request sent.<br/>You\'ll have wait until it is approved.'
                    buttons_div.removeChild(b_ok);
                    b_cancel.value = 'OK';
                }
                
                b_cancel.style.width = '75px';
                setTimeout("FriendRequestPage.Hide()", 4000);
            });
        };
        
        b_cancel.onclick = function(ev)
        {
            FriendRequestPage.Hide();
        };
        
        name_div.innerHTML = 'You are about to add ' + screenname + ' as a friend.<br/>Continue?';
        img_div.innerHTML = '<img src="' + profileimage + '" />';

        buttons_div.appendChild(b_ok);
        buttons_div.appendChild(b_cancel);
        
        info_div.appendChild(name_div);
        info_div.appendChild(buttons_div);
        
        holder.appendChild(img_div);
        holder.appendChild(info_div);
        holder.appendChild(clear_div);
        n.appendChild(holder);
        
        document.body.appendChild(n);
        
        n.style.left = ((ScreenWidth() / 2) - 200).toString() + 'px';
        n.style.top = (ScrollY() + ((ScreenHeight() / 2) - 120)).toString() + 'px';
        
        $('#' + n.id).fadeIn(200);
    }
}

function HideFriendRequest()
{
    var n = document.getElementById('ajax_box_holder');
    if(n != null)
    {
        $('#' + n.id).fadeOut(200, function()
        {
            document.body.removeChild(n);
        });
    }    
}

