
//var enumChallengeStatus = function()
//{
//    this.initiated = 0;
//    this.playing = 1;
//    this.closed = 2;
//    this.cancelled = 255;
//}

//var enumChallengePlayerStatus = function()
//{
//    this.initiated = 0;
//    this.acknowledged = 1;
//    this.accepted = 2;
//    this.playing = 3;
//    this.declined = 4;
//    this.cancelled = 255;
//}


function cancelTournament(instanceUid, playerId) {

    $.facebox(function() {
        var wsGameService = "/ws/game/cancel.aspx";
        $.get(wsGameService, { instanceUid: instanceUid, playerId: playerId},
              function(data) {
        cancelTournamentCallBack(data);
              });
    });

    return false;
}
function cancelTournamentCallBack(httpRequest) {

    var rootNode = httpRequest.documentElement;
    var html = "";

    if (!rootNode) {
        popupDialog.close();
        return false;
    }
    var xNode = null;

    if (rootNode.getAttribute("status") != null) {
        html = getCancelStatusDescription(rootNode.getAttribute("status"));
        popupDialog.showForm('Cancel Game', html, '446', popupDialog.ok, showMyGames);
    }
    else {
        html = "<p id=\"screenNameError\">Unable to cancel for the game. Please try later.</p><p class=\"errorF\"></p>";
        popupDialog.showForm('Cancel Game', html, '446', popupDialog.cancel);
    }

    return false;
}
function onPreRegisterTournamentSubmit(gameId, playerId, startTime) {
  
    $.facebox(function() {
        var wsGameService = "/ws/game/register.aspx";
        $.get(wsGameService, { gameId: gameId, playerId: playerId, startTime: startTime },
              function(data) {
                  registerTournamentCallBack(data);
              });
    });

    return false;
}

function registerTournamentCallBack(httpRequest) {
    
    var rootNode = httpRequest.documentElement;
    var html = "";

    if (!rootNode) {
        popupDialog.close();
        return false;
    }
    var xNode = null;

    if (rootNode.getAttribute("status") != null) {
        html = getRegisterStatusDescription(rootNode.getAttribute("status"), rootNode.getAttribute("errorCode"));

        popupDialog.showForm('Game Registration Result', html, '446', popupDialog.ok, showMyGames);
    }
    else {
        html = "<p id=\"screenNameError\">Unable to register for the game. Please try later.</p><p class=\"errorF\"></p>";
        popupDialog.showForm('Game Registration Result', html, '446', popupDialog.cancel);
    }

    return false;
}

function getRegisterStatusDescription(status, errorCode) {
    var description = "";
    switch (status) {
        case "0":
            description = "You have successfully registered to this tournament game. You would be able to play once the game started. You can start playing using the links on your profile.";
            break;
        case "-1":
            description = "Registration opens 24 hours before this game is due to start. Why not play a free game and hone your skills?";
            break;
        case "-2":
            description = "Unsuccessful registration. Please try again later.";
            break;
        case "-3":
            description = "Too many players are registered and the no place available for new registeration.";
            break;
        case "-4":
            if (errorCode != null) {
                switch (errorCode) {
                    case "5":
                        description = 'You don\'t have sufficient funds to join this game. You need to <a href="/wallet/depositFunds.aspx"> add funds </a> to your account and then register to the game.';
                        break;
                    default:
                        description = "Error occured while processing your request, Please try again later.";                        
                }
            }
            else {
                description = "Error occured while processing your request, Please try again later.";
            }
            break;
    }
    return description;
}
function getCancelStatusDescription(status) {
    var description = "";
    switch (status) {
        case "0":
            description = "You have successfully cancled your registration.";
            break;
        case "-1":
            description = "This game has already started and it's too late to cancel now.";
            break;
        case "-2":
            description = "Unsuccessful cancellation. Please try again.";
            break;
        case "-4":
            description = "Error occured while processing your request, Please try again later.";
            break;
    }
    return description;
}

function ChallengeHandler(recipientId, screenName)
{

    this.recipientId = recipientId;
    this.screenName = screenName;
    
    this.templateFramework = "<div class=\"div446\"><div class=\"grayBoxH\">Challenge Player</div> \
                        <div class=\"grayBoxC\" style=\"display:block;\"> \
                            <ul class=\"feed\"> \
                                <li> \
                                    <img id=\"dialogImage\" alt=\"Challenge %screenName%\" src=\"/gdi/avatar.aspx?sizeType=s&playerId=%recipientId%\" /> \
                                    <H3>Challenge %screenName%</H3> \
                                    <span>You have challenged this player. Please wait while they respond...</span> \
                                 </li> \
                            </ul> \
                            <ul id=\"myChallengeNotice\" class=\"feed\"> \
                                %content% \
                            </ul> \
                        </div><div class=\"grayBoxF\"></div></div>";
    
    this.templateChallenge = "  <li> \
                                    <p class=\"action\">Sending challenge. Please wait...</p> \
                                </li> \
                                <li class=\"feedLink\"> \
                                    <BR/> \
                                    <a href=\"javascript:void(0);\" onclick=\"javascript:return challengeHandler.cancel();\" title=\"Cancel Challenge\">Cancel Challenge &#187;</a> \
                                </li>";
    
    this.templateSent = "       <li> \
                                    <p class=\"action\">Challenge sent. Waiting for response...</p> \
                                </li> \
                                <li class=\"feedLink\"> \
                                    <BR/> \
                                    <a href=\"javascript:void(0);\" onclick=\"javascript:return challengeHandler.cancel();\" title=\"Cancel Challenge\">Cancel Challenge &#187;</a> \
                                </li>";
    
    this.templateError = "      <li> \
                                    <p class=\"error\">%error%</p> \
                                </li> \
                                <li class=\"feedLink\"> \
                                    <br/> \
                                    <a href=\"javascript:void(0);\" onclick=\"javascript:$(document).trigger('close.facebox');return(false);\" title=\"Continue\">Continue &#187;</a> \
                                </li>";
    
    this.templateAccept = "     <li> \
                                    <p class=\"action\">Player has accepted your challenge. Automatically transferring to game arena in 5 seconds...</p> \
                                </li> \
                                <li class=\"feedLink\"> \
                                    <BR/> \
                                    <a href=\"javascript:void(0);\" onclick=\"javascript:return challengeHandler.loadGame();\" title=\"Go to Arena\">Go to Arena &#187;</a> \
                                </li>";
                  
    this.templateDecline = "   <li> \
                                    <p class=\"error\">Player has declined your challenge. Click the link below to find another friend to challenge now...</p> \
                                </li> \
                                <li class=\"feedLink\"> \
                                    <BR/> \
                                    <a href=\"/buddy/findFriend.aspx?search=&online=on\" title=\"Find Another Friend\">Find Another Friend &#187;</a> \
                                </li>";
                  
    this.templateCancel = "     <li> \
                                    <p class=\"error\">You have cancelled the challenge. Click the links below to continue find another friend to challenge now...</p> \
                                </li> \
                                <li class=\"feedLink\"> \
                                    <BR/> \
                                    <a href=\"/buddy/findFriend.aspx?search=&online=on\" title=\"Find Another Friend\">Find Another Friend &#187;</a> \
                                </li> \
                                <li class=\"feedLink\"> \
                                    <a href=\"javascript:void(0);\" onclick=\"javascript:$(document).trigger('close.facebox');return(false);\" title=\"Continue\">Continue &#187;</a> \
                                </li>";
                  
    this.gameId = 0;
    this.categoryId = 0;
    this.subjectId = 0;
                  
    this.challengeId = 0;
    this.isCancelled = false;                 

    this.init = function(gameId, categoryId, subjectId) 
    {
        if (playerId != 0)
        {
        
            challengeHandler.gameId = gameId;
            challengeHandler.categoryId = categoryId;
            challengeHandler.subjectId = subjectId;
            challengeHandler.isCancelled = false;
            
            var content = challengeHandler.templateFramework;
            while (content.match("%content%") != null) content = content.replace(/%content%/,challengeHandler.templateChallenge);
            while (content.match("%recipientId%") != null) content = content.replace(/%recipientId%/,challengeHandler.recipientId.toString());
            while (content.match("%screenName%") != null) content = content.replace(/%screenName%/,challengeHandler.screenName);
            $.facebox(content);
            
            var t = new Date().getTime();        
            var post =  { playerId: playerId, recipientId : recipientId, gameId:gameId, t: t };
            $.get('/ws/challenge/initiate.aspx', post, function(data, status){challengeHandler.initAction(data, status);});
            
        }
        return false;

    };
    
    this.initAction = function(data, status)
    {
        
        var challenge = $(data).find("challenge");
        if (challenge.length == 0)
            challengeHandler.error("Unexpected Error");
        else 
        {
            challengeHandler.challengeId = parseInt(challenge.attr("challengeId")); 
            
            if (challengeHandler.isCancelled)
            {
                challengeHandler.isCancelled = false;
                var t = new Date().getTime();
                $.get('/ws/challenge/cancel.aspx', { playerId: playerId, challengeId: challengeHandler.challengeId, t: t});
            }
            else
            {
                var challengeStatus = parseInt(challenge.attr("status")) ;
                if (challengeStatus == 0)
                {          
                    var content = challengeHandler.templateSent;
                    while (content.match("%recipientId%") != null) content = content.replace(/%recipientId%/,challengeHandler.recipientId.toString());
                    while (content.match("%screenName%") != null) content = content.replace(/%screenName%/,challengeHandler.screenName);
                    $('#myChallengeNotice').html(content);

                    //check for response in 2 seconds
                    $.timer(2000, function (timer) {challengeHandler.wait(); timer.stop();});
                }
                else{
                    var maxPlayer = challenge.attr("maxPlayer") ;
                    var msg;
                    switch (challengeStatus)
                    {
                        case 1:
                            msg = 'This player is offline at the moment.' ;
                            break;
                        case 2:
                            msg = 'This player is busy at the moment.' ;
                            break;
                        case 3:
                            msg = 'Player not found.' ;
                            break;                    
                        case 4:
                            msg = 'You cannot challenge more than ' + maxPlayer + ' players to play this game.';
                            break;
                            
                        default:
                        msg = 'There was an error while trying to send your challenge request. Please try again.' ;             
                            break;                
                    }
                    challengeHandler.error(msg);
                }
            }
        }
        
    };
    
    this.error = function(msg)
    {
        var content = challengeHandler.templateError;
        while (content.match("%error%") != null) content = content.replace(/%error%/,msg);
        while (content.match("%recipientId%") != null) content = content.replace(/%recipientId%/,challengeHandler.recipientId.toString());
        while (content.match("%screenName%") != null) content = content.replace(/%screenName%/,challengeHandler.screenName);
        $('#myChallengeNotice').html(content);
    };
    
    this.wait = function()
    {
        if (!challengeHandler.isCancelled)
        {
            var t = new Date().getTime();
            $.get('/ws/challenge/check.aspx', { playerId: playerId, challengeId: challengeHandler.challengeId, t: t}, 
              function(data, status){
                challengeHandler.waitAction(data, status);        
                });
        }
        return false;
    };
    
    this.waitAction = function(data, status)
    {
        if (!challengeHandler.isCancelled)
        {
            var challenge = $(data).find("challenge[@challengeId='" + challengeHandler.challengeId.toString() + "']");
            var playersInitiated = challenge.find("player[@status='0']").length + challenge.find("player[@status='1']").length;
            var playersAccepted = challenge.find("player[@status='2']").length + challenge.find("player[@status='3']").length;
            var playersRejected = challenge.find("player[@status='4']").length;
            var playersCancelled = challenge.find("player[@status='255']").length;
            
            if (playersInitiated != 0)
            {
                //check for response in another 2 seconds
                $.timer(2000, function (timer) {challengeHandler.wait(); timer.stop();});
            }
            else if ((playersAccepted > 0) && (playersRejected + playersCancelled == 0))
            {
                //all accepted - go to arena
                var content = challengeHandler.templateAccept;
                while (content.match("%recipientId%") != null) content = content.replace(/%recipientId%/,challengeHandler.recipientId.toString());
                while (content.match("%screenName%") != null) content = content.replace(/%screenName%/,challengeHandler.screenName);
                $('#myChallengeNotice').html(content);

                //notify other players of acceptance
                var t = new Date().getTime();        
                $.get('/ws/challenge/respond.aspx', { challengeId: challengeHandler.challengeId , playerId: playerId, action:"accept", t:t  });

                //automatically transfer to arena                    
                $.timer(5000, function (timer) {challengeHandler.loadGame(); timer.stop();});
                
            }
            else 
            {
                //any rejected or cancelled 
                var content = challengeHandler.templateDecline;
                while (content.match("%recipientId%") != null) content = content.replace(/%recipientId%/,challengeHandler.recipientId.toString());
                while (content.match("%screenName%") != null) content = content.replace(/%screenName%/,challengeHandler.screenName);
                $('#myChallengeNotice').html(content);
                
                //acknowledge the result
                var t = new Date().getTime();
                $.get('/ws/challenge/acknowledge.aspx', { playerId: playerId, challengeId: challengeHandler.challengeId, t: t});
            }
                
        }
    };
    
    this.cancel = function()
    {
        if (challengeHandler.challengeId == 0)
        {
            this.isCancelled = true;
            $(document).trigger('close.facebox');    
        }
        else
        {
            if (!this.isCancelled)
            {
                this.isCancelled = true;
                var t = new Date().getTime();
                $.get('/ws/challenge/cancel.aspx', { playerId: playerId, challengeId: challengeHandler.challengeId, t: t}, 
                  function(data, status){
                    challengeHandler.cancelAction(data, status);        
                    });
            }   
        } 
        return false;
    };
    
    this.cancelAction = function(data, status)
    {
        $(document).trigger('close.facebox');
    };

    this.loadGame = function()
    {
        if (!challengeHandler.isCancelled)
        {
            var clientId = Math.ceil(32767*Math.random());
            var gameUrl = '/gameLoader.aspx?gameId=' + challengeHandler.gameId + '&categoryId=' + challengeHandler.categoryId + '&subjectId=' + challengeHandler.subjectId + '&gameType=4&clientId=' + clientId + '&challengeId=' + challengeHandler.challengeId;
            document.location.replace(gameUrl) ;
        }
        return false;
    };
    
}

function loadGame(gameId, categoryId, subjectId, gameType, playerId, startTime) {
    if (gameType == 1) {

        if (playerId == 0) {
            document.location.replace(gameUrl)
            window.location = "/login/register.aspx?retUrl=" + escape(window.location.pathname);
        }
        else {
            onPreRegisterTournamentSubmit(gameId, playerId, startTime);
        }
        return false;
    }
    else if (gameType == 4) {
        challengeHandler.init(gameId, categoryId, subjectId);
        return false;
    }
    else {
        var clientId = Math.ceil(32767*Math.random());
        var gameUrl = '/gameLoader.aspx?gameId=' + gameId + '&categoryId=' + categoryId + '&subjectId=' + subjectId + '&gameType=' + gameType + '&clientId=' + clientId;
        document.location.replace(gameUrl) ;
        return false;
    }

}


function NotificationHandler()
{
    this.templateGrowl = "<ul class=\"feed\"> \
                            <li class=\"feedTitle\">New Challenger!</li> \
                            <li> \
                                <img id=\"dialogImage\" alt=\"New Challenger: %screenName%\" src=\"/gdi/avatar.aspx?sizeType=s&playerId=%challengerId%\" /> \
                                <H3>New Challenger: %screenName%</H3> \
                                <span>You have been challenged by this player. Please respond...</span> \
                            </li> \
                            <li class=\"feedLink\"><a href=\"javascript:void(0);\" onclick=\"javascript:$.growl.dismiss(this);return notificationHandler.dialog(%challengeId%);\">Reply to Challenge...</a></li> \
                        </ul>";

    this.templateDialog = "<div class=\"div446\"><div class=\"grayBoxH\">New Challenger</div> \
                        <div id=\"challengeNotification\" class=\"grayBoxC\" style=\"display:block;\"> \
                            <ul class=\"feed\"> \
                                <li> \
                                    <img id=\"dialogImage\" alt=\"New Challenger: %screenName%\" src=\"/gdi/avatar.aspx?sizeType=s&playerId=%challengerId%\" /> \
                                    <H3>New Challenger: %screenName%</H3> \
                                    <span>You have been challenged by this player. Please respond...</span> \
                                </li> \
                                <li class=\"feedTitle\">Game Details</li> \
                                <li>%gameDetail%<br/><br/></li> \
                             </ul> \
                             <ul id=\"myChallengeNotification\" class=\"feed\"> \
                                <li class=\"feedTitle\">Accept</li> \
                                <li>Think you're up to the challenge? Click the link to start playing;<br/><br/></li> \
                                <li class=\"feedLink\"><a href=\"javascript:void(0);\" onclick=\"javascript:return notificationHandler.accept(%challengeId%);\">I accept the challenge. Let's play &#187;</a></li> \
                                <li class=\"feedTitle\">Decline</li> \
                                <li>Not ready to play right now? Choose an option from the following;<br/><br/></li> \
                                <li class=\"feedLink\"><a href=\"javascript:void(0);\" onclick=\"javascript:return notificationHandler.decline(%challengeId%, 0);\">I don't want to play. &#187;</a></li> \
                                <li class=\"feedLink\"><a href=\"javascript:void(0);\" onclick=\"javascript:return notificationHandler.decline(%challengeId%, 30);\">I'm busy. Ignore challenges for the next 30 minutes. &#187;</a></li> \
                                <li class=\"feedLink\"><a href=\"javascript:void(0);\" onclick=\"javascript:return notificationHandler.decline(%challengeId%, 43200);\">Ignore all challenges until I change my status. &#187;</a></li> \
                            </ul> \
                        </div><div class=\"grayBoxF\"></div></div>";

    this.templateAccept = "<li class=\"feedTitle\">Challenge Accepted</li> \
                            <li>Waiting for other players...</li> \
                            <li class=\"feedLink\"><a href=\"javascript:void(0);\" onclick=\"javascript:return notificationHandler.cancel(%challengeId%);\">Cancel &#187;</a></li> \
                            ";
                            
    this.templateReady = "<li class=\"feedTitle\">Game Ready</li> \
                            <li>All players are ready. Automatically transferring to game arena in 5 seconds...</li> \
                            <li class=\"feedLink\"><a href=\"javascript:void(0);\" onclick=\"javascript:return notificationHandler.loadGame(%challengeId%);\">Go to Arena &#187;</a></li> \
                            ";

    this.templateDecline = "<li class=\"feedTitle\">Challenge Not Accepted</li> \
                            <li>You have declined this challenge.</li> \
                            <li class=\"feedLink\"><a href=\"javascript:void(0);\" onclick=\"javascript:$(document).trigger('close.facebox');return(false);\" title=\"Continue\">Continue &#187;</a></li> \
                            ";

    this.templateCancel = "<li class=\"feedTitle\">Game Cancelled</li> \
                            <li>You have cancelled this challenge game.</li> \
                            <li class=\"feedLink\"><a href=\"javascript:void(0);\" onclick=\"javascript:$(document).trigger('close.facebox');return(false);\" title=\"Continue\">Continue &#187;</a></li> \
                            ";

    this.templateCancelled = "<li class=\"feedTitle\">Game Cancelled</li> \
                            <li>The other player has cancelled this challenge game.</li> \
                            <li class=\"feedLink\"><a href=\"javascript:void(0);\" onclick=\"javascript:$(document).trigger('close.facebox');return(false);\" title=\"Continue\">Continue &#187;</a></li> \
                            ";

    this.check = function()
    {
        var t = new Date().getTime();
        var post =  { playerId: playerId, t: t };
        $.get('/ws/challenge/check.aspx', post, function(data, status){notificationHandler.checkAction(data, status);});
        
        return false;
    };
    
    this.checkAction = function(data, status)
    {
    
        var challenge = $(data).find("challenge");
        if (challenge.length > 0)
            $(challenge).each(function(i){notificationHandler.process(this);});
    
        //check for challenges again in another 16 seconds
        $.timer(16000, function (timer) {notificationHandler.check(); timer.stop();});

    };
    
    this.challengeArray = new Array();
    
    this.process = function(data)
    {

        //processes a challenge node
        challengeId = parseInt($(data).attr("challengeId"));

        if (notificationHandler.challengeArray[challengeId.toString()] == null)
        {

            var gameId = parseInt($(data).attr("gameId"));
            var categoryId = parseInt($(data).attr("categoryId"));
            var subjectId = parseInt($(data).attr("subjectId"));

            challengerId = parseInt($(data).find("player[@isChallenger]").attr("playerId"));
            screenName = $(data).find("player[@isChallenger]").attr("screenName");
            
            notificationHandler.challengeArray[challengeId.toString()] = { challengeId: challengeId, challengerId: challengerId, gameId: gameId, categoryId: categoryId, subjectId: subjectId, screenName: screenName, isCancelled: false };
            
            var content = notificationHandler.templateGrowl;
            while (content.match("%challengeId%") != null) content = content.replace(/%challengeId%/,challengeId.toString());
            while (content.match("%challengerId%") != null) content = content.replace(/%challengerId%/,challengerId.toString());
            while (content.match("%screenName%") != null) content = content.replace(/%screenName%/,screenName);

            var title = screenName;
            $.growl(title, content, {sticky: true}); 

        }

    };
    
    this.dialog = function(challengeId)
    {

        var challenge = notificationHandler.challengeArray[challengeId.toString()]

        var content = notificationHandler.templateDialog;
        while (content.match("%challengeId%") != null) content = content.replace(/%challengeId%/,challengeId.toString());
        while (content.match("%challengerId%") != null) content = content.replace(/%challengerId%/,challenge.challengerId.toString());
        while (content.match("%screenName%") != null) content = content.replace(/%screenName%/,challenge.screenName);
        $.facebox(content);
        
        //acknowledge the result
        var t = new Date().getTime();
        $.get('/ws/challenge/acknowledge.aspx', { playerId: playerId, challengeId: challenge.challengeId, t: t});
        
        return false;
    };
    
    this.accept = function(challengeId)
    {
        var t = new Date().getTime();        
        $.get('/ws/challenge/respond.aspx', { challengeId: challengeId , playerId: playerId, action:"accept", t:t  });

        var challenge = notificationHandler.challengeArray[challengeId.toString()];

        var content = notificationHandler.templateAccept;
        while (content.match("%challengeId%") != null) content = content.replace(/%challengeId%/,challengeId.toString());
        while (content.match("%challengerId%") != null) content = content.replace(/%challengerId%/,challenge.challengerId.toString());
        while (content.match("%screenName%") != null) content = content.replace(/%screenName%/,challenge.screenName);
        $('#myChallengeNotification').html(content);
        
        notificationHandler.wait(challengeId);
        
        return false;

    };

    this.wait = function(challengeId)
    {
        var challenge = notificationHandler.challengeArray[challengeId.toString()];
        if (!challenge.isCancelled)
        {
            var t = new Date().getTime();
            $.get('/ws/challenge/check.aspx', { playerId: playerId, challengeId: challengeId, t: t}, 
              function(data, status){
                notificationHandler.waitAction(data, status);        
                });
        }
        return false;
    };
    
    this.waitAction = function(data, status)
    {
    
        var node = $(data).find("challenge");
        var challengeId = parseInt(node.attr("challengeId"));
        
        var challenge = notificationHandler.challengeArray[challengeId.toString()];
        if (!challenge.isCancelled)
        {
            
            var playersInitiated = node.find("player[@status='0']").length + node.find("player[@status='1']").length;
            var playersAccepted = node.find("player[@status='2']").length + node.find("player[@status='3']").length;
            var playersRejected = node.find("player[@status='4']").length;
            var playersCancelled = node.find("player[@status='255']").length;
            
            if (playersInitiated != 0)
            {
                //check for response in another 2 seconds
                eval("$.timer(2000, function (timer) {notificationHandler.wait(" + challengeId.toString() + "); timer.stop();});");
            }
            else if ((playersAccepted > 0) && (playersRejected + playersCancelled == 0))
            {
                //all accepted - go to arena
                var content = notificationHandler.templateReady;
                while (content.match("%challengeId%") != null) content = content.replace(/%challengeId%/,challengeId.toString());
                while (content.match("%challengerId%") != null) content = content.replace(/%challengerId%/,challenge.challengerId.toString());
                while (content.match("%screenName%") != null) content = content.replace(/%screenName%/,challenge.screenName);
                $('#myChallengeNotification').html(content);
        
                //automatically transfer to arena                    
                eval("$.timer(5000, function (timer) {notificationHandler.loadGame(" + challengeId.toString() + "); timer.stop();});");
                
            }
            else 
            {
                //any rejected or cancelled 
                var content = notificationHandler.templateCancelled;
                while (content.match("%challengeId%") != null) content = content.replace(/%challengeId%/,challengeId.toString());
                while (content.match("%challengerId%") != null) content = content.replace(/%challengerId%/,challenge.challengerId.toString());
                while (content.match("%screenName%") != null) content = content.replace(/%screenName%/,challenge.screenName);
                $('#myChallengeNotification').html(content);
                
                //acknowledge the result
                var t = new Date().getTime();
                $.get('/ws/challenge/acknowledge.aspx', { playerId: playerId, challengeId: challengeId, t: t});
                
            }
        }
    };
    
    this.decline = function(challengeId, busy)
    {
        var action = (busy==0)? "reject" : "busy";
        var t = new Date().getTime();        
        $.get('/ws/challenge/respond.aspx', { challengeId: challengeId , playerId: playerId, action:action, busy:busy, t:t  });
            
        $(document).trigger('close.facebox');
        
        return false;
    };
    
    this.cancel = function(challengeId)
    {
        
        var challenge = notificationHandler.challengeArray[challengeId.toString()];
        
        if (!challenge.isCancelled)
        {
            challenge.isCancelled = true;
            var t = new Date().getTime();
            $.get('/ws/challenge/cancel.aspx', { playerId: playerId, challengeId: challengeId, t: t}, 
              function(data, status){
                notificationHandler.cancelAction(data, status);        
                });
        }   

        return false;
    };
    
    this.cancelAction = function(data, status)
    {
        $(document).trigger('close.facebox');
    };

    this.loadGame = function(challengeId)
    {
        var challenge = notificationHandler.challengeArray[challengeId.toString()];

        var clientId = Math.ceil(32767*Math.random());
        var gameUrl = '/gameLoader.aspx?gameId=' + challenge.gameId + '&categoryId=' + challenge.categoryId + '&subjectId=' + challenge.subjectId + '&gameType=4&clientId=' + clientId + '&challengeId=' + challengeId;
        document.location.replace(gameUrl) ;
        
        return false;
    };
}


function showMyGames() {
    $(document).ready(
        function() {
            if (document.getElementById('divMyGames') == null) return;
            
            if (playerId != 0) $('#divMyGames').load('/games/myGames.aspx', null,
            function() {
                $('#divMyGames').find("div.grayBoxH").each(
                function(i) { grayBoxHandler.load(this); }
            );
            }
        );
        }
    );
    }

function evenGameAsset(categoryId, subjectId, gameTypeId, imageId) {
    this.imageId = imageId;
    this.categoryId = categoryId;
    this.subjectId = subjectId;
    this.gameTypeId = gameTypeId;
    var me = this;

    this.loadAsset = function() {
        $(document).ready(
                function() {
                    var t = new Date().getTime();
                    $.get("/games/gameAsset.aspx", { categoryId: categoryId, subjectId: subjectId, gameTypeId: gameTypeId, t: t }, me.loadAssetCallback);
                }
                );
    };
    this.loadAssetCallback = function(data) {

        var rootNode = data.documentElement;

        var img = document.getElementById(me.imageId);
        if (img != null)
            img.src = rootNode.getAttribute("uri");
    };    
}
