var update=0;
var baseurl="apps\\";
var res=new Array();
var version;
var tanksjson;
var mapsjson;
var gui = require('nw.gui'); 
var win = gui.Window.get();
//win.maximize();
gui.App.setCrashDumpDir("dump\\");
var nowpage="";
window.onload = function() {
$(document).ready(function() {

	//检测更新补丁
	getPatchList(examinePatch);
	//启动监控程序
    monitorStart();
    wowpStart();
    replayStart();
	//显示新闻
	newsPage();
    //获取插件更新
    modManager.loadRemoteList(function(){
        //对当前列表进行对比
        var canupdate=0;
        var unread=0;
        var lt=modManager.db.get("lastTime");
        for(var i in modManager.jsonlist.modlist){
            if(i=="0"){
                continue;
            }
            var mod=modManager.jsonlist.modlist[i];
            if(!checkERROR(modManager.db.json.modlist))
                var localmod=modManager.getLocalMod(i);
                if(!checkERROR(localmod))
                    if(localmod.time<mod.time) {
                        canupdate++;
                    }
            if(!checkERROR(lt)&&lt<mod.time){
                unread++;
            }
        }
        if(unread>0) {
            var ur = '&nbsp;<span class="badge text-info" title="'+unread+'个新插件" id="unread">' + unread + '</span>';
            $("#modlistbutton").append(ur);
        }
        if(canupdate>0) {
            var cu = '&nbsp;<span class="badge" title="'+canupdate+'个可更新插件" id="canupdate">' + canupdate + '</span>';
            $("#modlistbutton").append(cu);
        }
    });
	$("#begin").remove();
	$("#tanks").load("..\\dossier\\tanks.json",function(data){
        try {
            tanksjson = JSON.parse(data);
        }catch(e){
            customAlert("加载坦克失败","warning");
        }
	});
	
	$("#maps").load("..\\dossier\\maps.json",function(data){
        try {
            mapsjson = JSON.parse(data);
        }catch(e){
            customAlert("加载坦克失败","warning");
        }
	});
	//页面按钮地址初始化
	$("#modlistbutton").click(function(){
		modPage();
	});
	$("#tanknewsbutton").click(function(){
		newsPage();
	});
	$("#battlestatbutton").click(function(){
		battlePage();
		showRecentBattles();
		setInterval(showRecentBattles, 10000);
	});
	$("#playerbutton").click(function(){
		playerPage();
	});
	$("#configbutton").click(function(){
		configPage();
		//customPath();
	});
	$("#startgamebutton").click(function(){
		startgame();
	});
	$("#replaySimbutton").click(function() {
        if (fs.existsSync("apps/replaySim/index.html")) {
            window.open("../replaySim/index.html");
        }
        else {
            customAlert("请安装录像模拟器补丁", 'warning');
        }
    });
	$("#replaybutton").click(function(){
		changePage();
		$("#replaybutton").parent().addClass("active");
		replayList();
	});
	$("#msgbutton").click(function(){
		msgPage();
	});
	$('#search').click(function(){
		console.log($('#searchbar').val());
		console.log($('#searchzone').val());
		if ($('#searchbar').val().trim()!=''){
			var name=$('#searchbar').val();
			var zone='';
			if($(this).text()=='北区'){
				zone='north';
			}else {
				zone='south';
			}
			window.open('http://rank.kongzhong.com/index.html?name='+name+'&area='+zone,'_blank');
		}
	});	
	window.setTimeout(function() {
		if($("#tanknewsbutton").parent().hasClass("active")){
			battlePage();
			showRecentBattles();
			setInterval(showRecentBattles, 10000);
		}
	}, 5000);
	$('.dropdown-menu').children('li').children('a').click(function(){
		$(this).parent().parent().parent().children('button:first').text($(this).text());
	});
	$("#minibutton").click(function(){min2tray();});
	$("#wowpicon").click(function(){
		changeApp("wowp\\index.html",'wowp',false);
	});

    $("#count").load('count.html #countindex');
});
}

//启动游戏
function startgame(){
	if (checkERROR(config.get('path'))){
		customAlert("请先设置游戏路径",'warning');
		return;
	}
    if(fs.existsSync(config.get('path')+"\\WOTLauncher.exe")) {
        cp.exec("\"" + config.get('path') + "\\WOTLauncher.exe\"");
    }else{
        customAlert("<font style='color:red'>启动文件"+config.get('path')+"\\WOTLauncher.exe</font>不存在","danger");
    }
}

function newsPage(){
	changePage();
	newsList();
	$("#tanknewsbutton").parent().addClass("active");
}

//实时效率页面
function battlePage(){
	changePage();
	$("#battlepage").show();
	$("#battlestatbutton").parent().addClass("active");
}

//效率分析页面
function playerPage(){
	changePage();
	playerlist();
	$("#playerbutton").parent().addClass("active");
}

//配置页面
function configPage(){
	changePage();
	refreshConfig();
	$("#configbutton").parent().addClass("active");
}

//消息盒子页面
function msgPage(){
	changePage();
	$("#messagepage").show();
	message.read();
}

//插件页面
function modPage(){
	changePage();
	if ($("#modpage").html().length>0){
		$("#modpage").html("");
	}
	$("#modpage").load("mod.html #nav",function(data){
		$("#modinstallbutton").click(function(){
			refreshModList();//mod.js
			//切换active
			$(this).parent().parent().children(".active").removeClass("active");
			$(this).parent().addClass("active");
		});
		$("#moddeletebutton").click(function(){
			refreshModDelList();//mod.js
			//切换active
			$(this).parent().parent().children(".active").removeClass("active");
			$(this).parent().addClass("active");
		});
		$("#modclever").click(function(){
			cleverinstallpage();
			//切换active
			$(this).parent().parent().children(".active").removeClass("active");
			$(this).parent().addClass("active");
		})
		$("#modpage").append("<div id='modlist'></div>");
        $("#count").load('count.html #countmod');
		refreshModList();//mod.js
        $("#modpage").show();
		//统计页
	});
	$("#modlistbutton").parent().addClass("active");
}
function togglemenu(){
	if($('#menu').css("display")=="none"){
		$('#wrapper').css('padding-left',225);
		$("#togglemenu").html('<i class="fa fa-caret-square-o-left fa-fw"></i>')
	}else{
		$('#wrapper').css('padding-left',0);
		$("#togglemenu").html('<i class="fa fa-caret-square-o-right fa-fw"></i>')
	}
	$('#menu').toggle();

}
//切换页面的预动作
function changePage(){
	$(".index").hide();
	$(".active").removeClass("active");
	clearInterval(showRecentBattles);
}
