﻿// ---------------------------------------------------------------------------

if (!window.baseweb)
    window.baseweb = {};

if (!window.baseweb.url)
    window.baseweb.url = {}; 
    
var href = document.location.href;
var hash = document.location.hash;
    
var envs;
var GAME_ATV = "2xl-atv-offroad";
var GAME_SUPERCROSS = '2xl-supercross';
var GAME_SUPERCROSS_PC = '2xl-supercross-pc';
var GAME_SNOCROSS = '2xl-snocross';
var GAME_FLEETDEFENSE = '2xl-fleet-defense';
var GAME_RALLY = '2xl-rally';
var GAME_TROPHYLITE = '2xl-trophylite';

// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------

function _log(name, value) {
    try {
        if (console && console.info)
            console.info(name, value);
    }
    catch (e) {
    }
}
// ---------------------------------------------------------------------------

baseweb.url = function() {
    this.href = "";
    this.hash = "";
    this.dl = "";
    this.hashed = false;
    this.param_info = "#"; // can be # or ?
    this.param_type = "path"; // can be path, querystring or form
    this.param_separator = "/"; // can be / or &
    this.param_value_separator = "/"; // can be / or =
    this.fn_callback;
    xlreturn_url_obj = this;
}

baseweb.url.prototype = {
    //-------------------------------------------------
    set_url_state: function() {
        this.dl = document.location;
        this.href = document.location.href;
        this.hash = document.location.hash;
        if(xlwebapi != null)
            xlwebapi.init();
    }
    ,
    //-------------------------------------------------
    get_href: function() {
        return href;
    }
    ,
    //-------------------------------------------------
    get_hash: function() {
        return hash;
    }
    ,
    //-------------------------------------------------
    get_dl: function() {
        return this.dl;
    }
    ,
    //-------------------------------------------------
    is_hashed: function() {
        this.hashed = false;
        if (this.href.indexOf("#") > -1) {
            this.hashed = true;
        }
        return this.hashed;
    }
    ,
    //-------------------------------------------------
    get_url_param_value: function(key) {
        href = this.href; //document.location.href;
        var value = "";
        if (href.indexOf(key + "/") > -1) {
            var params = href.split("/");
            for (var i = 0; i < params.length; i++) {
                if (params[i] == key) {
                    if (params[i + 1] != null) {
                        value = params[i + 1];
                    }
                }
            }
        }
        return value;
    }
    ,
    //-------------------------------------------------
    change_url_to_state: function(href, param, paramvalue) {

        var _returnurl = "";

        if (href.indexOf(param) > -1) {
            // hash contains param, just update
            var _params = href.split(this.param_value_separator);
            for (var i = 0; i < _params.length; i++) {
                if (_params[i] == param) {
                    _params[i + 1] = paramvalue;
                }
            }
            _returnurl = _params.join(this.param_value_separator);
        }
        else {
            // has not present, append it
            if (href.indexOf(param + this.param_value_separator) == -1) {
                if (href.lastIndexOf(this.param_value_separator) == href.length - 1)
                    href = href.substr(0, href.length - 1);
                _returnurl = href + this.param_value_separator + param + this.param_value_separator + paramvalue;
            }
        }
        return _returnurl;
    }
}

xlweburl = new baseweb.url();

// ---------------------------------------------------------------------------

function is_section_login() {
    href = xlweburl.get_href();
    if (href.indexOf('#login') > -1 || href.indexOf('login/') > -1) {
        return true;
    }
    return false;
}

// ---------------------------------------------------------------------------

function is_section_admin() {
    href = xlweburl.get_href();
    if (href.indexOf('#admin') > -1 || href.indexOf('admin/') > -1) {
        return true;
    }
    return false;
}

// ---------------------------------------------------------------------------

function is_section_content() {
    href = xlweburl.get_href();
    if (href.indexOf('#content') > -1  || href.indexOf('content/') > -1) {
        return true;
    }
    return false;
}

// ---------------------------------------------------------------------------

function is_section_games() {
    href = xlweburl.get_href();
    if (href.indexOf('#games') > -1  || href.indexOf('games/') > -1) {
        return true;
    }
    return false;
}

// ---------------------------------------------------------------------------

function is_section_home() {
    href = xlweburl.get_href();
    if (href.indexOf('#home') > -1  || href.indexOf('home/') > -1) {
        return true;
    }
    return false;
}

// ---------------------------------------------------------------------------

function is_section_profile() {
    href = xlweburl.get_href();
    if (href.indexOf("#profile") > -1  || href.indexOf('profile/') > -1) {
        return true;
    }
    return false;
}

// ---------------------------------------------------------------------------

function is_section_live() {
    href = xlweburl.get_href();
    if (href.indexOf("#live") > -1  || href.indexOf('live/') > -1) {
        return true;
    }
    return false;
}

// ---------------------------------------------------------------------------

function is_section_statistics() {
    href = xlweburl.get_href();
    if (href.indexOf("#statistics") > -1  || href.indexOf('statistics/') > -1) {
        return true;
    }
    return false;
}

// ---------------------------------------------------------------------------

function is_section_achievements() {
    href = xlweburl.get_href();
    if (href.indexOf("#achievements") > -1  || href.indexOf('achievements/') > -1) {
        return true;
    }
    return false;
}

// ---------------------------------------------------------------------------

function is_section_leaderboards() {
    href = xlweburl.get_href();
    if (href.indexOf("#leaderboards") > -1  || href.indexOf('leaderboards/') > -1) {
        return true;
    }
    return false;
}

// ---------------------------------------------------------------------------

function is_game(game) {
    href = xlweburl.get_href();
    if (href.indexOf(game) > -1) {
        return true;
    }
    return false;
}

// ---------------------------------------------------------------------------

function is_game_trophylite() {
    return is_game(GAME_TROPHYLITE);
}

// ---------------------------------------------------------------------------

function is_game_fleetdefense() {
    return is_game(GAME_FLEETDEFENSE);
}

// ---------------------------------------------------------------------------

function is_game_supercross() {
    return is_game(GAME_SUPERCROSS);
}

// ---------------------------------------------------------------------------

function is_game_supercross_pc() {
    return is_game(GAME_SUPERCROSS_PC);
}

// ---------------------------------------------------------------------------

function is_game_atv() {
    return is_game(GAME_ATV);
}

// ---------------------------------------------------------------------------

function is_game_snocross() {
    return is_game(GAME_SNOCROSS);
}

// ---------------------------------------------------------------------------

function is_game_trophylite() {
    return is_game(GAME_TROPHYLITE);
}

// ---------------------------------------------------------------------------

function is_section_leaderboard() {
    href = xlweburl.get_href();
    if (href.indexOf('leaderboard') > -1) {
        return true;
    }
    return false;
}
// ---------------------------------------------------------------------------

function reset_matinee_header() {
    $("#matinee").removeClass("matinee-trophylite1");
    $("#matinee").removeClass("matinee-atv1");
    $("#matinee").removeClass("matinee-supercross-pc1");
    $("#matinee").removeClass("matinee-supercross1");
    $("#matinee").removeClass("matinee-fleetdefense1");
    $("#matinee").removeClass("matinee-snocross1");
    $("#matinee").removeClass("matinee-snocross");
    $("#matinee").removeClass("matinee-rally1");
    $("#matinee").removeClass("matinee-rally");
}

// ---------------------------------------------------------------------------

function set_matinee_header(){
    if (is_game_supercross_pc()) {
        reset_matinee_header();
        $("#matinee").addClass("matinee-supercross-pc1");
    }
    else if (is_game_supercross()) {
        reset_matinee_header();
        $("#matinee").addClass("matinee-supercross1");
    }
    else if (is_game_trophylite()) {
        reset_matinee_header();
        $("#matinee").addClass("matinee-trophylite1");
    }
    else if (is_game_fleetdefense()) {
        reset_matinee_header();
        $("#matinee").addClass("matinee-fleetdefense1");
    }
    else if (is_game_snocross()) {
        reset_matinee_header();
        $("#matinee").addClass("matinee-snocross1");
    }
    else {
        reset_matinee_header();
        $("#matinee").addClass("matinee-atv1");
    }
}

// ---------------------------------------------------------------------------

function set_leaderboard_initial_keys()
{
    if (is_game_supercross_pc()) {
        reset_matinee_header();
        $("#matinee").addClass("matinee-supercross-pc1");

        if (href.indexOf("key1") == -1) {
            loadContentIntoPath(320, 101, 0, 0, 0, 0, 0, 0);
        }
    }
    else if (is_game_supercross()) {
        reset_matinee_header();
        $("#matinee").addClass("matinee-supercross1");

        if (href.indexOf("key1") == -1) {
            loadContentIntoPath(320, 100, 0, 0, 0, 0, 0, 0);
        }
    }
    else if (is_game_trophylite()) {
        reset_matinee_header();
        $("#matinee").addClass("matinee-trophylite1");

        if (href.indexOf("key1") == -1) {
            loadContentIntoPath(302, 101, 0, 0, 0, 0, 0, 0);
        }
    }
    else if (is_game_fleetdefense()) {
        reset_matinee_header();
        $("#matinee").addClass("matinee-fleetdefense1");

        if (href.indexOf("key1") == -1) {
            loadContentIntoPath(210, 101, 0, 0, 0, 0, 0, 0);
        }
    }
    else if (is_game_snocross()) {
        reset_matinee_header();
        $("#matinee").addClass("matinee-snocross1");

        if (href.indexOf("key1") == -1) {
            loadContentIntoPath(320, 101, 0, 0, 0, 0, 0, 0);
        }
    }
    else {
        reset_matinee_header();
        $("#matinee").addClass("matinee-atv1");

        if (href.indexOf("key1") == -1) {
            loadContentIntoPath(128, 101, 0, 0, 0, 0, 0, 0);
        }
    }
}

// ---------------------------------------------------------------------------

function switch_to_dynamic() {
    
    if (href.indexOf("#") == -1) {

        var arrpaths = href.split('/');
        arrpaths.splice(0, 3);
        
        var path = arrpaths.join("/");        

        _log("path:", path);

        //document.location.replace('/#' + path);
   }
}

if(is_section_leaderboards())
{
    $(function() {
        if (is_game_supercross_pc()) {
            if (href.indexOf("key1") == -1) {
                loadContentIntoPath(320, 101, 0, 0, 0, 0, 0, 0);
            }
        }
        else if (is_game_supercross()) {
            if (href.indexOf("key1") == -1) {
                loadContentIntoPath(320, 100, 0, 0, 0, 0, 0, 0);
            }
        }
        else if (is_game_trophylite()) {
            if (href.indexOf("key1") == -1) {
                loadContentIntoPath(302, 101, 0, 0, 0, 0, 0, 0);
            }
        }
        else if (is_game_fleetdefense()) {
            if (href.indexOf("key1") == -1) {
                loadContentIntoPath(210, 101, 0, 0, 0, 0, 0, 0);
            }
        }
        else if (is_game_snocross()) {
            if (href.indexOf("key1") == -1) {
                loadContentIntoPath(320, 101, 0, 0, 0, 0, 0, 0);
            }
        }
        else {
            if (href.indexOf("key1") == -1) {
                loadContentIntoPath(128, 101, 0, 0, 0, 0, 0, 0);
            }
        }

        
    });
}

switch_to_dynamic();

// ---------------------------------------------------------------------------

function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

// ---------------------------------------------------------------------------

function setSelectByValue(sel, val)
{
	for( var i=0; i < sel.options.length; ++i )
	{
		if( sel.options[i].value==val )
			sel.options[i].selected=true;
	}
}

// ---------------------------------------------------------------------------

function setSelectByIndex(sel, val)
{
	for( var i=0; i < sel.options.length; ++i )
	{
	    if(i == val)
			sel.options[i].selected=true;
	}
}

// ---------------------------------------------------------------------------

function filterAllEnvironments()
{
    if(is_game_supercross())
    {
        if ($("#key1").val() == 100) {
            setSelectByIndex(document.getElementById('key2'), 2);
        }
    }
    else if(is_game_trophylite())
    {
        if ($("#key1").val() == 101) {
            setSelectByIndex(document.getElementById('key2'), 2);
        }
    }
    else if(is_game_snocross())
    {
        if ($("#key1").val() == 101) {
            setSelectByIndex(document.getElementById('key2'), 2);
        }
    }
    else if(is_game_atv())
    {
        if ($("#key1").val() == 101) {
            setSelectByIndex(document.getElementById('key2'), 2);
        }
    }
}

// ---------------------------------------------------------------------------

function loadEnvs() {
    var localhref = document.location.href;
    envs = new Array();
    // TODO change out the tracks array with a collection get...
    if (localhref.indexOf(GAME_ATV) > -1) {
        envs.push("ALL");
        envs.push("Anaheim");
        envs.push("Toronto");
        envs.push("Daytona");
        envs.push("St Louis");
        envs.push("Dallas");
        envs.push("Philadelphia");
        envs.push("Boston");
        envs.push("Black Hawk");
        envs.push("Diablo Pass");
        envs.push("Castlerock");
        envs.push("Pine Knob");
        envs.push("Shadow Ridge");
        envs.push("Seneca");
        envs.push("Little Scout");
        envs.push("Rubicon");
    }

    if (localhref.indexOf(GAME_SUPERCROSS_PC) > -1) {
        envs.push("ALL");
        //envs.push("Training Grounds");
        envs.push("Anaheim");
        envs.push("Phoenix");
        envs.push("San Diego");
        envs.push("San Francisco");
        envs.push("Jackson");
        envs.push("Houston");
        envs.push("Indianapolis");
        envs.push("Seattle");
        envs.push("Atlanta");
        envs.push("New Orleans");
        envs.push("Dallas");
        envs.push("Las Vegas");
        envs.push("Toronto");
        envs.push("Montreal");
    }
    else if (localhref.indexOf(GAME_SUPERCROSS) > -1) {
        envs.push("ALL");
        //envs.push("Training Grounds");
        envs.push("Anaheim");
        envs.push("Phoenix");
        envs.push("San Diego");
        envs.push("San Francisco");
        envs.push("Jackson");
        envs.push("Houston");
        envs.push("Indianapolis");
        envs.push("Seattle");
        envs.push("Atlanta");
        envs.push("New Orleans");
        envs.push("Dallas");
        envs.push("Las Vegas");
        envs.push("Toronto");
        envs.push("Montreal");
    }   


    if (localhref.indexOf(GAME_RALLY) > -1) {
        envs.push("ALL");
        envs.push("Track 1");
        envs.push("Track 2");
        envs.push("Track 3");
        envs.push("Track 4");
        envs.push("Track 5");
    }
    
    if (localhref.indexOf(GAME_TROPHYLITE) > -1) {
        envs.push("ALL");
        envs.push("KC HiLiTES Midnight Special");
        envs.push("BITD Parker 425");
        envs.push("General Tire Mint 400");
        envs.push("BITD Desert Challenge");
        envs.push("BITD Bilek Silver State 300");
    }
    

    if (localhref.indexOf(GAME_FLEETDEFENSE) > -1) {
        envs.push("ALL");
    }

    if (localhref.indexOf(GAME_SNOCROSS) > -1) {
        envs.push("ALL");
        envs.push("Aspen");
        envs.push("Telluride");
        envs.push("Breckenridge");
        envs.push("Echo Mountain");
        envs.push("Powderhorn");
        envs.push("White River");
    }
}


// ---------------------------------------------------------------------------
// TODO filter out the non leaderboard stats
/* if (id == "TOT_MILES_DRIVEN"
|| id == "FASTEST_LAP"
|| id == "FASTEST_3_LAPS"
|| id == "FASTEST_5_LAPS"
|| id == "FASTEST_10_LAPS"
|| id == "FINISHED_FIRST"
|| id == "SINGLE_JUMP_POINTS"
|| id == "STUNTS_ATTEMPTED"
|| id == "STUNTS_LANDED"
|| id == "STUNTS_ATTEMPTED"
|| id == "STUNTS_CRASHED"
|| id == "STUNT_TOTAL_POINTS"
|| id == "MOST_STUNT_EVENT_POINTS"
|| id == "SINGLE_JUMP_AIRTIME"
|| id == "SINGLE_JUMP_DISTANCE"
|| id == "HIGHEST_SINGLE_JUMP"
|| id == "TOT_GOLD_FINISHES"
|| id == "TOT_SILVER_FINISHES"
|| id == "TOT_BRONZE_FINISHES"
) {*/

function filterStatsFromDisplay(id) {
    var show = true;
    var localhref = document.location.href;

    if (localhref.indexOf(GAME_ATV) > -1) {
        if (id == 373 // Total Multiplayer Time
            || id == 762 // Total Multiplayer Sessions Hosted
            || id == 592 // Total Multiplayer Sessions Joined
            || id == 794 // Finished 2nd
            || id == 949 // Finished 3rd
            || id == 733 // Finished 4th
            || id == 358 // Finished 5th
            || id == 827 // Number of times played
            ) 
        {
            show = false;
        }
    }

    else if (localhref.indexOf(GAME_SNOCROSS) > -1) {
        if (id == 315 // Total Multiplayer Time
            || id == 340 // Total Multiplayer Sessions Hosted
            || id == 345 // Total Multiplayer Sessions Joined
            || id == 381 // Finished 2nd
            || id == 382 // Finished 3rd
            || id == 383 // Finished 4th
            || id == 384 // Finished 5th
            || id == 310 // Number of times played
            ) {
            show = false;
        }
    }   
    else if (localhref.indexOf(GAME_TROPHYLITE) > -1) {
        if (id == 321 // Finished 2nd
            || id == 322 // Finished 3rd
            || id == 323 // Finished 4th
            || id == 324 // Finished 5th
            || id == 301 // Number of times played
            ) {
            show = false;
        }
    } 
    else if (localhref.indexOf(GAME_SUPERCROSS_PC) > -1) {
        if (id == 410 // Finished 2nd
            || id == 420 // Finished 3rd
            || id == 430 // Finished 4th
            || id == 440 // Finished 5th
            || id == 310 // Number of times played
            ) {
            show = false;
        }
    }
    else if (localhref.indexOf(GAME_SUPERCROSS) > -1) {
        if (id == 410 // Finished 2nd
            || id == 420 // Finished 3rd
            || id == 430 // Finished 4th
            || id == 440 // Finished 5th
            || id == 310 // Number of times played
            ) {
            show = false;
        }
    } 

    return show;
}

// ---------------------------------------------------------------------------

function checkForStuntStats(uniqueID) {
    // This method determines if we should switch to stunt stats.
    var doStunt = false;
    var localhref = document.location.href;

    if (localhref.indexOf(GAME_ATV) > -1) {
        // TODO clean up the key detection
        if (uniqueID == "898") {
            doStunt = true;
        }
    }

    else if (localhref.indexOf(GAME_SNOCROSS) > -1) {
        // TODO clean up the key detection
        if (uniqueID == "400") {
            doStunt = true;
        }
    }
    return doStunt;
}

// ---------------------------------------------------------------------------

function doDrawTables(key, data) {
    var localhref = document.location.href;
    if (localhref.indexOf(GAME_ATV) > -1) {
        if (key == "637-945-657-905") {
            // FASTEST LAP
            drawFastestLapsStatTable(key, data);
        }
        else if (key == "830-794-949-733-358") {
            // TIMES FINISHED
            drawTimesFinishedStatTable(key, data);
        }
        else {
            drawDefaultStatTable(key, data);
        }
    }
    else if (localhref.indexOf(GAME_SUPERCROSS_PC) > -1) {
        if (key == "360-370-380-390") {
            // FASTEST LAP
            drawFastestLapsStatTable(key, data);
        }
        else if (key == "400-410-420-430-440") {
            // TIMES FINISHED
            drawTimesFinishedStatTable(key, data);
        }
        else {
            drawDefaultStatTable(key, data);
        }
    }
    else if (localhref.indexOf(GAME_SUPERCROSS) > -1) {
        if (key == "360-370-380-390") {
            // FASTEST LAP
            drawFastestLapsStatTable(key, data);
        }
        else if (key == "400-410-420-430-440") {
            // TIMES FINISHED
            drawTimesFinishedStatTable(key, data);
        }
        else {
            drawDefaultStatTable(key, data);
        }
    }
    
    else if (localhref.indexOf(GAME_TROPHYLITE) > -1) {
        if (key == "310-311-312-313") {
            // FASTEST LAP
            drawFastestLapsStatTable(key, data);
        }
        else if (key == "320-321-322-323-324") {
            // TIMES FINISHED
            drawTimesFinishedStatTable(key, data);
        }
        else {
            drawDefaultStatTable(key, data);
        }
    }
    else if (localhref.indexOf(GAME_FLEETDEFENSE) > -1) {
        drawSingleStatTableFD(key, data);
    }

    else if (localhref.indexOf(GAME_SNOCROSS) > -1) {
        if (key == "355-365-370-375") {
            // FASTEST LAP
            drawFastestLapsStatTable(key, data);
        }
        else if (key == "380-381-382-383-384") {
            // TIMES FINISHED
            drawTimesFinishedStatTable(key, data);
        }
        else {
            drawDefaultStatTable(key, data);
        }
    }

    else if (localhref.indexOf(GAME_RALLY) > -1) {
        drawDefaultStatTable(key, data);
    }
}
// ---------------------------------------------------------------------------
// SPECIFIC GAME CODE
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// FLEET DEFENSE

function convertFDCurrentRankToRank(val) {
    if (val < 250000)
        return "Ensign";
    if (val < 500000)
        return "Lieutenant Junior Grade";
    if (val < 950000)
        return "Lieutenant";
    if (val < 1710000)
        return "Lieutenant Commander";
    if (val < 2907000)
        return "Commander";
    if (val < 4651000)
        return "Captain";
    if (val < 6976800)
        return "Rear Admiral Lower Half";
    if (val < 10465200)
        return "Rear Admiral Upper Half";
    if (val < 15697800)
        return "Vice Admiral";
    if (val < 23546700)
        return "Admiral";
    if (val < 35320050)
        return "Fleet Admiral";
    if (val >= 35320050)
        return "Joint Chiefs of Staff";
}


// ---------------------------------------------------------------------------

function drawSingleStatTableIndividualFD(data) {
    var size = data.items.length;
    shtml = "<div><table width=\"100%\">";
    for (var i = 0; i < size; i++) {

        var col = 2;

        if (classitem == "") {
            //classitem = "row-alt";
        } else {
            classitem = "";
        }
        var _displayName = data.displayName;
        var _displayValue = data.items[i].value1;
        var _rank = "";
        var _output = _displayValue;
        var _value = _displayValue;
        if (!isNaN(_value)) {
            _value = addCommas(_value);
        }
        if (_displayName.toLowerCase() == "current rank") {
            _rank = convertFDCurrentRankToRank(_displayValue);
            _output = _rank + " (" + _value + ")";
            _displayValue = _output;
        }
        else {
            if (!isNaN(_displayValue)) {
                _displayValue = addCommas(_displayValue);
            }
        }
        shtml += "<tr class='" + classitem + "'>";
        shtml += "<td align='left'>";
        shtml += _displayName;
        shtml += "</td>";
        shtml += "<td align='right'>";
        shtml += _displayValue;
        shtml += "</td>";
        shtml += "</tr>";
    }
    shtml += "</table></div>";
    $("#" + data.statID).replaceWith(shtml);
}

// ---------------------------------------------------------------------------

function drawSingleStatTableFD(key, data) {

    for (var i = 0; i < data.length; i++) {
        shtml += "<div ><table width=\"100%\">";
        for (var j = 0; j < data[i].data[0].items.length; j++) {

            shtml += "<tr>";
            shtml += "<td>";
            shtml += data[i].data[0].displayName;
            shtml += "</td>";
            shtml += "<td  align='right'>";
            var _value = data[i].data[0].items[j].value1;
            if (!isNaN(_value)) {
                _value = addCommas(_value);
            }
            shtml += _value;
            shtml += "</td>";
            shtml += "</tr>";

        }
        shtml += "</table></div>";
    }
    $("#" + key).replaceWith(shtml);
}


