﻿// Friends
var dh;
function addasfriend() {
    // Check for a comment
    document.getElementById("tools").className = "hidden";
    document.getElementById("addfriend").className = "contain";
    dh = document.getElementById("addfriend").offsetHeight;
    document.getElementById("addfriend").style.height = "20px"
    window.setTimeout(fadein, 5);
}

function fadein() {
/*
    if (parseInt(document.getElementById("addfriend").className.replace("contain fade", "")) < 100) {
        document.getElementById("addfriend").className = "contain fade" + (parseInt(document.getElementById("addfriend").className.replace("contain fade", "")) + 10);
        window.setTimeout(fadein, 5);
    }
    else {
        document.getElementById("addfriend").className = "contain";
    }
*/
    if (parseInt(document.getElementById("addfriend").style.height.replace("px", "")) < dh) {
        document.getElementById("addfriend").style.height = (parseInt(document.getElementById("addfriend").style.height.replace("px", "")) + 20) + "px";
        window.setTimeout(fadein, 5);
    }
    else {
        document.getElementById("addfriend").style.height = "";
    }
}

function canceladdasfriend() {
    // Check for a comment
    document.getElementById("addfriend").className = "contain";
    document.getElementById("addfriend").style.height = document.getElementById("addfriend").offsetHeight - 20 + "px";
    window.setTimeout(fadeout, 5);
}

function fadeout() {
/*
    if (parseInt(document.getElementById("addfriend").className.replace("contain fade", "")) > 0) {
        document.getElementById("addfriend").className = "contain fade" + (parseInt(document.getElementById("addfriend").className.replace("contain fade", "")) - 10);
        window.setTimeout(fadeout, 5);
    }
    else {
        document.getElementById("addfriend").className = "hidden contain";
        document.getElementById("tools").className = "tools";
    }
*/
    if (parseInt(document.getElementById("addfriend").style.height.replace("px", "")) > 0) {
        if (parseInt(document.getElementById("addfriend").style.height.replace("px", "")) - 20 > 0) {
            document.getElementById("addfriend").style.height = (parseInt(document.getElementById("addfriend").style.height.replace("px", "")) - 20) + "px";
        }
        else {
            document.getElementById("addfriend").style.height = "0px";
        }
        window.setTimeout(fadeout, 5);
    }
    else {
        document.getElementById("addfriend").className = "hidden contain";
        document.getElementById("addfriend").style.height = "";
        document.getElementById("tools").className = "tools";
    }
}

function initialisemessage(o, active) {
    if (active && o.value == "Personal Message (Optional)") {
        o.value = "";
    }
    if (!active && o.value == "") {
        o.value = "Personal Message (Optional)";
    }
}

function addtofriends(sname) {
    // Call ajax function
    var pcomm = "";
    if (document.getElementById("txtSPMessage").value != "Personal Message (Optional)" && document.getElementById("txtSPMessage").value != "") {
        pcomm = document.getElementById("txtSPMessage").value;
    }
    // Pass name
    var ctx = {
        greyout: "greyoutfriend",
        sname: sname
    }
    // Grey out
    greyOut(true, "greyoutfriend")
    PageMethods.Add_Friend(pcomm, addtofriendssuccess, ajaxfailure, ctx);
}

function addtofriendssuccess(value, ctx, methodName) {
    // Check for errors
    var fdivs = document.getElementById("addfriend").getElementsByTagName("div")
    if (value == "error timeout") {
    	fdivs[1].innerHTML = "<p>Your session has timed out. Please sign in again.</p>";
	}
	else if (value == "current") {
    	fdivs[1].innerHTML = "<p>You are already pals with " + ctx.sname + ".</p>";
	}
	else if (value == "pending") {
    	fdivs[1].innerHTML = "<p>You already have an active pal request with " + ctx.sname + ".</p>";
	}
	else {
    	fdivs[1].innerHTML = "<p>You pal request has been sent. " + ctx.sname + " will need to confirm you as a pal.</p>";
	}

    // Show close button
    fdivs[2].innerHTML = "<input type=\"button\" value=\"Close\" onclick=\"canceladdasfriend();\" />"

    if (ctx.greyout) {
        greyOut(false,ctx.greyout);
    }
}


