﻿var XT = {};
XT.Domin = 'http://www.1419.cn';
XT.Local = 'home';
XT.State = function() {
    var cookiearray = document.cookie.split(';')
    for (var i = 0; i < cookiearray.length; i++) {
        if ($.trim(cookiearray[i].split('=')[0].toLowerCase()) == 'g1') {
            return isNaN(parseInt(cookiearray[i].split('=')[1])) ? 0 : parseInt(cookiearray[i].split('=')[1]);
        }
    }
    return 0;
}
XT.Menu = { 'item': [
        { 'name': 'home', 'page': XT.Domin, 'check': false },
        { 'name': 'game', 'page': XT.Domin + '/game/', 'check': true },
        //{ 'name': 'shop', 'page': XT.Domin + '/shop/', 'check': false },
        {'name': 'pay', 'page': XT.Domin + '/pay/', 'check': false },
        //{ 'name': 'exchange', 'page': XT.Domin + '/exchange/', 'check': false },
        {'name': 'rank', 'page': XT.Domin + '/rank/', 'check': false },
        { 'name': 'feedback', 'page': XT.Domin + '/feedback/', 'check': false },
        { 'name': 'bbs', 'page': XT.Domin + '/user/', 'check': false },
        { 'name': 'help', 'page': XT.Domin + '/help/', 'check': false }
    ]
    };
XT.InitHead = function() {
    XT.State() == 0 ? $('.head_top').html('<div onclick="XT.Browser.addBookMark(\'http://www.1419.cn\',\'心跳1419\')">加入收藏</div><div onclick="XT.Browser.addHomePage(\'http://www.1419.cn\')">设为首页</div><div onclick="XT.Reg()">用户注册</div>') : $('.head_top').html('<div onclick="XT.Browser.addBookMark(\'http://www.1419.cn\',\'心跳1419\')">加入收藏</div><div onclick="XT.Browser.addHomePage(\'http://www.1419.cn\')">设为首页</div><div onclick="XT.Out()">退出游戏</div>' + $('.head_top').html());
}
XT.InitHead();
$('.head_main').html('<div>首页</div><div>游戏</div><div>帐户充值</div><div>排行榜</div><div>问题反馈</div><div>个人账户</div><div class="last">帮助</div>');
XT.Init = function(name) {
    name = name.toLowerCase();
    var li = XT.GetIndex(name);
    XT.Local = name;
    if (li > 0) {
        $('.head_main div:nth-child(' + li + ')').addClass('slt');
    }
    var mitem = $('.head_main div');
    for (var i = 0; i < mitem.length; i++) {
        if (li - 1 != i) {
            if (XT.Menu.item[i]['check'] == true && XT.State() == 0) {
                $(mitem[i]).html('<a href="javascript:void(0)" onclick="XT.Into(\'' + XT.Menu.item[i]['page'] + '\')">' + $(mitem[i]).html() + '</a>');
            } else {
                $(mitem[i]).html('<a href="' + XT.Menu.item[i]['page'] + '">' + $(mitem[i]).html() + '</a>');
            }
        }
    }
}
XT.SetInit = function() {
    $('.head_top').html('');
    XT.InitHead();
    for (var i = 0; i < XT.Menu.item.length; i++) {
        if (XT.Menu.item[i]['check'] == true || XT.Menu.item[i]['name'] != XT.Local) {
            var im = $('.head_main div:nth-child(' + (i + 1) + ')');
            im.html('<a href="' + XT.Menu.item[i]['page'] + '">' + im.text() + '</a>');
        }
    }
}
XT.GetIndex = function(name) {
    for (var i = 0; i < XT.Menu.item.length; i++) {
        if (XT.Menu.item[i]['name'] == name) {
            return i + 1;
        }
    }
    return 0;
}
XT.Into = function(page) {
    location.hash = 'login';
    XT.Alert('你还没有登录，请登录后再进入游戏');
}
XT.Browser = {
    addHomePage: function(url) {
        if (!!(window.attachEvent && !window.opera)) {
            document.body.style.behavior = 'url(#default#homepage)';
            document.body.setHomePage(url);
        } else {
            if (window.clipboardData && clipboardData.setData) {
                clipboardData.setData('text', url);
            } else {
                XT.Alert('您的浏览器不允许脚本访问剪切板');
                return;
            }
            XT.Alert('网址已经拷贝到剪切板');
        }
        return true;
    },
    addBookMark: function(url, title) {
        var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL';
        try {
            window.external.addFavorite(url, title || document.title);
        } catch (e) {
            try {
                window.sidebar.addPanel(url, title || document.title);
            } catch (e) {
                XT.Alert('您尝试通过快捷键' + ctrl + ' + D添加书签');
            }
        }
    }
};
XT.IntoGame = function() {
    XT.GotoPage('game');
};
XT.Help = function() {
    XT.GotoPage('help');
}
XT.Detail = function() {
    if (XT.State() == 0) {
        XT.Into(XT.Domin + '/game/');
    } else {
        location.href = XT.Domin + '/user/';
    }
}
XT.Pay = function() {
    XT.GotoPage('pay');
}
XT.Msnlive = function() {
   location.href = XT.Domin + '/Live/';
}
XT.ExChange = function() {
    return false;
    XT.GotoPage('exchange');
}
XT.Reg = function() {
    location.href = XT.Domin + "/reg/";
}
XT.Out = function() {    
    location.href = XT.Domin + '/loginout.aspx';
}
XT.GotoPage = function(page, para) {
    var i = XT.GetIndex(page) - 1;
    if (XT.Menu.item[i]['check'] == true && XT.State() == 0) {
        XT.Into(XT.Menu.item[i]['page'] + (para ? ('?' + para) : ''));
    } else {
        location.href = XT.Menu.item[i]['page'] + (para ? ('?' + para) : '');
    }
}
XT.DoLogin = function() {
    if ($.trim($('#txt_username').val()).length == 0) {
        XT.Alert('请输入用户名');        
        return false;
    }
    if ($.trim($('#txt_password').val()).length == 0) {
        XT.Alert('请输入密码');        
        return false;
    }
    
    $('.login .show').append('<div class="load"></div>');
    $.ajax({
        url: XT.Domin + "/ajax/dologin.aspx?un=" + XT.EncodeURI($.trim($('#txt_username').val())) + "&ps=" + XT.EncodeURI($("#txt_password").val()) + "&vc=" + $.trim($("#txt_vc").val()),
        success: function(v) {
            $('.login .show .load').remove();
            if (v == 1) {
                XT.Alert('验证码错误');
            } else if (v == 2) {
                XT.Alert('密码错误');              
            } else {
                $('.login .show .main').html(v);
                XT.SetInit();
            }
        }
    });
}
XT.InitLogin = function() {
    $('.txt').keypress(function() {
        var e = window.event ? window.event : arguments[0];
        if (e.keyCode == 13) {
            $(this).blur();
            $('#btn_login').click();
        }
    });
    $('#btn_login').click(function() {
        XT.DoLogin();
    });
    $('.login .lt img').click(function() {
        $(this).attr('src', 'ValidateCode.aspx?tmp=' + Math.random());
    });
}
XT.Move = function(mt) {
    $('#' + mt + '1 div:lt(3)').addClass('top');
    $('#' + mt + '2').html($('#' + mt + '1').html());
    function marquee() {
        if ($('#' + mt + '2')[0].offsetTop - $('#' + mt)[0].scrollTop - $('#' + mt + '1')[0].offsetHeight <= 0) {
            $('#' + mt)[0].scrollTop -= $('#' + mt + '1')[0].offsetHeight;
        } else {
            $('#' + mt)[0].scrollTop++;
        }
    }
    var mymar = setInterval(marquee, 30);
    $('#' + mt).mouseover(function() {
        clearInterval(mymar);
    });
    $('#' + mt).mouseout(function() {
        mymar = setInterval(marquee, 30);
    });
}
XT.EncodeURI = function(str) {
    if (encodeURIComponent)
        return encodeURIComponent(str);
    else
        return escape(str);
}
XT.GetStar = function(pi) {
    location.hash = 'login';
    $('.starlist').append('<div class="loading"></div>');
    $.ajax({
        url: XT.Domin + "/ajax/getstarlist.aspx?pageindex=" + pi + "&" + Math.random(),
        success: function(c) {
            $('.starmain').html(c);
        }
    });
}
XT.Alert = function(msg) {
    $('.boxbg').remove();
    $('.alertfrom').remove();
    $('body').append('<div class="boxbg"></div><div class="alertfrom"><div class="title">温馨提示</div><div class="content"><div class="tips"><div style="height:24px; line-height:24px; font-weight:bold; font-size:14px;">' + msg + '</div><div style="height:24px; line-height:24px; font-size:12px; color:#aaa;">以上提示会给您带来帮助！</div><div style="height:4px; line-height:4px; margin-bottom:4px; font-size:0px; border-bottom:1px dotted #aaa; width:90%;"></div><div style="height:20px; line-height:20px;">如果在游戏中遇到问题,请联系游戏官方客服。</div><div  style="height:20px; line-height:20px;">客服电话：400-688-6609</div></div><div class="controls"><input type="button" onclick="$(\'.alertfrom\').remove();$(\'.boxbg\').remove();" class="btn4" value="确定" /></div></div></div>');
    $('.btn4').focus();
}
XT.PayAlert = function(msg) {
    $('.boxbg').remove();
    $('.alertfrom').remove();
    $('body').append('<div class="boxbg"></div><div class="alertfrom"><div class="title">温馨提示</div><div class="content"><div class="tips"><div style="height:24px; line-height:24px; font-weight:bold; font-size:14px;">' + msg + '</div><div style="height:24px; line-height:24px; font-size:12px; color:#aaa;">以上提示会给您带来帮助！</div><div style="height:4px; line-height:4px; margin-bottom:4px; font-size:0px; border-bottom:1px dotted #aaa; width:90%;"></div><div style="height:20px; line-height:20px;">如果在游戏中遇到问题,请联系游戏官方客服。</div><div  style="height:20px; line-height:20px;">客服电话：400-688-6609</div></div><div class="controls"><input type="button" onclick="location.href=location.href" class="btn4" value="已完成充值" /></div></div></div>');
}
XT.GetTop20 = function(ta, pid) {
    $('.show').removeClass();
    $(ta).find('div').addClass('show');
    $.ajax({
        url: XT.Domin + '/ajax/getpttop.aspx?pid=' + pid + "&" + Math.random(),
        success: function(c) {
            $('.top20').html(c);
        }
    });
}
