﻿
/* Voer een ajax request uit voor een menu link */
function linkAjaxFadeIn(event) {
    event.preventDefault();

    $('#gameContent').html('');
    showProgressIndicator();
    
    $.ajax({ url: this.href,
        type: 'get',
        dataType: 'html',
        complete: hideProgressIndicator,
        success: setGameContentFadeIn, 
        error: ajaxError
    });
}

function showProgressIndicator() {
    $('#progressIndicator').show();
}

function hideProgressIndicator() {
    $("#progressIndicator").hide();
}

function setGameContentFadeIn(newContent) {
    $('#gameContent').hide();
    $('#gameContent').html(newContent);

    // add ajaxfadeIn behaviour to ajaxLink class in new content
    $('a.ajaxlink').click(linkAjaxFadeIn);
    
    $('#gameContent').fadeIn('slow');
}

function ajaxError(XMLHttpRequest, textStatus, errorThrown) {
    window.location.replace('/error/unhandled');
}



/* 
 * On startup off page 
 */
$(document).ready(function() {
    /* Alter stylesheet

    /* Set menu effects */
    $('#mainmenu li.topmenu').hover(
            function() { $('ul', this).fadeIn('slow'); },
            function() { $('ul', this).fadeOut('slow'); });

    /* add functinality to class menulink to do this in an ajax fashion */ 
    $('a.menulink').click(linkAjaxFadeIn);

    /* add ajax functionality to start game, this works without fadeIn, silvlight does this for us */
    $('a.menulinkstartgame').click(function(event) { 
        event.preventDefault();
        $('#gameContent').load(this.href);
    });
    
});

/* 
 * Silverlight Error Handling 
 */
function onSilverlightError(sender, args) {

    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    }
    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;

    var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n";

    errMsg += "Code: " + iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError") {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError") {
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }

    throw new Error(errMsg);
}


