
var fun_stuff = {
    'sub_categories': ['Style Studio', 'Videos', 'Music', 'Screenshots', 'Wallpaper', 'Polls'],
    'urls': ['/funStuff/BeautySalon.aspx', '/FunStuff/VideoList.aspx', '/FunStuff/MusicList.aspx', '/FunStuff/Screenshots.aspx', '/FunStuff/WallPaper.aspx', '/FunStuff/Poll.aspx']
};
var site_map = [
	{
	    'main_category': 'News',
	    'sub_categories': ['Notices', 'Events', 'Updates', 'Maintenance'],
	    'urls': ['/News/List.aspx?boardNo=101&amp;pageIndex=1', '/News/List.aspx?boardNo=200&amp;pageIndex=1', '/News/List.aspx?boardNo=102&amp;pageIndex=1', '/News/List.aspx?boardNo=103&amp;pageIndex=1']
	},
	{
	    'main_category': 'Guide',
	    'sub_categories': ['Game Intro', 'Getting Started', 'Game Info', 'Beginner\'s Guide', 'Lorna & Pan', 'Housing Guide', 'Strategy Guide', 'Strategy FAQ'],
	    'urls': ['/Guide/Intro.aspx', '/Guide/GettingStarted.aspx', '/Guide/GameInfo.aspx', 'javascript:void window.open(\'/Guide/Beginner.aspx\',\'popup\',\'width=768,height=600\');', '/Guide/LornaPan.aspx', 'javascript:void window.open(\'/Guide/Housing/housingC2.html\',\'popup\',\'width=1000,height=700\');', '/Guide/StrategyGuide.aspx?boardNo=700&pageIndex=1', '/Guide/StrategyFAQ.aspx']
	},
	{
	    'main_category': 'Shop',
	    'sub_categories': ['Web Shop', 'Cash Shop Guide', 'NX Guide', 'Review Purchases'],
	    'urls': ['/Shop/', '/Shop/Guide_Premium.aspx', 'javascript:void window.open(\'http://www.nexon.net/nx/guide\',\'popup\');', 'javascript:void window.open(\'http://www.nexon.net/nx/review-purchases\',\'popup\');']
	},
	{
	    'main_category': 'Community',
	    'sub_categories': ['Forums', 'Guild', 'Erinn Walker', 'Regulation Policy'],
	    'urls': ['/Community/Default.aspx', '/Community/GuildCommunity.aspx', 'javascript:void window.open(\'/ErinnWalker/08/index.htm\',\'popup\',\'width=1000,height=700\');', '/Forums/Regulations.aspx']
	},
	{
	    'main_category': 'Support',
	    'sub_categories': ['Client &amp; <br/> Driver Downloads', 'FAQ', 'User Abuse', 'Security', 'Customer Support'],
	    'urls': ['/Support/client.aspx', '/Support/FAQList.aspx?boardNo=410&amp;pageIndex=1', '/Support/UserAbuse.aspx', '/Support/Security.aspx', 'javascript:void window.open(\'http://support.nexon.net\',\'popup\');']
	}
];

//returns a string in lowercase, without apostrophes, and with underscores replacing whitespace and hyphens
var formatImageName = function (string) {
    var regExp = /\s|[-]/g;
    var regExp2 = /[^\w_]/g;
    var str = string.toLowerCase().replace(regExp, "_").replace(regExp2, "");
    return str;
};
//relies on cookie plugin
function getCookie(c_name) {
    return $.cookie(c_name) !== null ? $.cookie(c_name) : "";
}
function getDownloadLinks(callback) {
    var downloadUrl = 'http://nxcache.nexon.net/mabinogi/web/js/download_data.js';
    $.ajaxSetup({ cache: false });
    return $.getScript(downloadUrl, function () {
        if (typeof dl == 'undefined') {
            dl = { pandoLink: "#", manualLink: "#", npcMusic: "#", noMusic: "#", patch: "#" };
        }
        callback.apply(dl);
    });
}
//formats numbers with commas (e.g. 2000 becomes 2,000)
function commaFormat(s) {
    var t = s.length;
    if (t < 4) return s;
    var m = t % 3;
    var n = Math.floor((t - 1) / 3);
    var st = s.split('');
    for (var i = 0; i < n; i++) {
        if (m == 0 && i == 0) {
            st.splice(0, 0, ' ');
        } else {
            st.splice((i * 3 + m + i), 0, ',');
        }
    }
    s = st.join('');
    return s;
}
function startGameCheck() {
    $.getScript('http://nxcache.nexon.net/publisher/mabinogi/publisher_startGame.js', function () {
        if (typeof Data_startGame == 'undefined') { return false; }
        var d = Data_startGame;
        if (d[0].content_category == '3' || (d[0].content_category == '1' && d[0].progress_type == '1')) {
            $('#m-ticker').text(d[0].content_title).removeClass('dn');
            $("#m-play").addClass('maintenance').unbind('click').css('cursor', 'default');
        }
    });
}

function gaTrackEvent(group, title) {
    _gaq = (typeof _gaq != "undefined") ? _gaq : [];
    _gaq.push(['cdt._trackEvent', group, title]);
    _gaq.push(['sdt._trackEvent', group, title]);
}
function NxLaunchGame() {
    gaTrackEvent("MBWebLaunch", "StartGame");
    var game = "33563141";
    nexon.play(game);
}

function NxLaunchGameMain() {
    $.getScript('http://nxcache.nexon.net/publisher/mabinogi/publisher_startGame.js', function () {
        if (typeof Data_startGame == 'undefined') { return false; }
        var d = Data_startGame;
        switch (d[0].content_category) {
            case '1':
                if (d[0].progress_type != '1') {
                    NxLaunchGame();
                }
                else {
                    alert(d[0].content_title);
                }
                break;
            case '2':
                NxLaunchGame();
                break;
            case '3':
                alert(d[0].content_title);
                break;
        }
    });
}

function IsLogin() {
    var _loggedIn = false;
    $.ajax({
        async: false,
        type: "POST",
        url: "/WebService/Billing.asmx/CheckAuth",
        dateType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (response) {
            var json = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
            switch (json.response_code) {
                case 0: _loggedIn = json.data.IsAuthenticated;
                    break;
                default: alert(json.response_message);
            }
        }
    });
    return _loggedIn;
}

// global page load
function globalLoad() {
    $('#popuptrigger').popup({ height: 256, width: 455, top: 100, contents: redir });
    startGameCheck();
    $('#nav').navBuilder(site_map);
    $('#fun').navBuilder(fun_stuff, { listID: "fun-links", buildMenu: false });

    // start cdd plugins //
    $('#fun-exp').funexpand({
        oHeight: 158,
        eHeight: 44,
        timeout: 200,
        expandThese: '#fun'
    });
    $('#main, #drop').hoverdropdown({
        oHeight: 0,
        eHeight: 188,
        expandThese: '#drop'
    });
    $('#drop > li').pseudohover({
        hover: '#drop > li',
        pseudo: '#main li',
        className: 'active'
    });
    $("#backtotop").backtotop({
        tophide: 400,
        speed: 500,
        easing: 'easeOut',
        offset: 100
    });
    // end cdd plugins

    $("#m-play a").bind('click', function (e) { e.preventDefault(); NxLaunchGameMain(); });
    //$("#m-start-button").flash({ swf: 'http://nxcache.nexon.net/mabinogi/swf/btn-start-game.swf', id: 'startbtn', width: 300, height: 155, wmode: "transparent", allowScriptAccess: "always" });
    $("#m-nxlinks-cards").flash({ swf: 'http://nxcache.nexon.net/mabinogi/swf/banner_nx_card.swf', width: 278, height: 59, wmode: "transparent", allowScriptAccess: "always" });
    $("#m-nxlinks-games").flash({ swf: 'http://nxcache.nexon.net/mabinogi/swf/banner_nx_games.swf', width: 278, height: 59, wmode: "transparent", allowScriptAccess: "always" });
    $('#legal .legal-popup').click(function (e) {
        e.preventDefault();
        var newWin = window.open($(this).attr('href'), 'legal', 'width=600, height=400, resizable=no, scrollbars=yes');
        if (!newWin)
            alert("Your browser's pop-up blocker is currently enabled.  If you wish to view the legal information, please disable the pop-up blocker temporarily and try again.  Thank you.");
    });
}

