/// <reference name="MicrosoftAjax.js"/>



/*
Description:
Contains javascript code logic that provides features to the Tips Community website or 
complements script that is emitted by certain server controls.
Dependency on the jQuery library.
*/



/* TODO: Import to document this method properly. */
function scheduleMethodInvocation(context, methodName, milliSeconds)
{
    abortMethodInvocation(context);
    
    var invocationArguments = '';
    if (arguments.length > 3)
    {
        for (var i = 3; i < arguments.length; i++)
        {
            invocationArguments += (typeof(arguments[i]) == 'string' ? "'" + arguments[i] + "'" : arguments[i].toString()) + ",";
        }
        if (invocationArguments.endsWith(","))
        {
            invocationArguments = invocationArguments.substr(0, invocationArguments.length - 1);
        }
    }
    context.timeoutID = window.setTimeout(String.format("{0}({1})", methodName, invocationArguments), milliSeconds);
}


function abortMethodInvocation(context)
{
    if (context.timeoutID)
    {
        window.clearTimeout(context.timeoutID);
    }
}


/* jQuery extensions */
/* TODO: Should be moved to a globally shared js library in the future. */
jQuery.fn.ensureMaxLength = function(maxLength, suffix)
{
    return this.each
    (
        function() 
        {
            var
                $this = $(this),
                textBefore = $this.text(),
                textAfter = jQuery.ensureMaxLength(textBefore, maxLength, suffix);
            
            if (textBefore != textAfter)
            {
                $this.text(textAfter);
            }
        } 
    );
};
jQuery.ensureMaxLength = function(subject, maxLength, suffix)
{
    if (typeof suffix == "undefined")
    {
        suffix = "";
    }

    if (subject.length > maxLength)
    {
        var truncationIndex = subject.lastIndexOf(" ", maxLength);

        if (truncationIndex == -1)
        {
            truncationIndex = maxLength;
        }
        
        return subject.substring(0, truncationIndex) + suffix;
    }
    
    return subject;
}
$(document).ready(
    function () {
        $("a.facebook-connect-link").click(function (e) {
            var connectUrl = $(this).attr("href");
            window.open(connectUrl, "connectWindow", "width=666,height=400,top=100,left=100,scrollbars=no,toolbar=no", false);
            e.returnValue = false;
            if (e.preventDefault)
                e.preventDefault();
        });
    }
);

