var update=0;
var dossier;
var version;
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);
	//页面按钮地址初始化
	$("#modlistbutton").click(function(){
		modPage();
	});
	$("#tanknewsbutton").click(function(){
		newsPage();
	});
	$("#battlestatbutton").click(function(){
		battlePage();
        dossier.showRecentBattles(true);
	    setInterval(dossier.showRecentBattles, 30000);
	});
	$("#shipsstatbutton").click(function(){
		shipsPage();
	});
	$("#configbutton").click(function(){
		configPage();
		//customPath();
	});
	$("#startgamebutton").click(function(){
		startgame();
	});
	$("#msgbutton").click(function(){
		msgPage();
	});
    $("#replaybutton").click(function(){
        replayPage();
    });
	$('#search').click(function(){
		console.log($('#searchbar').val());
		console.log($('#searchzone').val());
		if ($('#searchbar').val().trim()!=''){
			var name=$('#searchbar').val();
			var zone=$(this).text();
            var url;
            if(zone=='南区'){
                url='http://rank.kongzhong.com/wows/index.html?name='+name+'&zone=south';
            }else if(zone=='北区'){
                url='http://rank.kongzhong.com/wows/index.html?name='+name+'&zone=north';
            }else if(zone=='亚服'){
				zone='.asia/zh-tw';
                url='http://worldofwarships'+zone+'/community/accounts/search/?search='+name;
			}else if(zone=='欧服'){
				zone='.eu/en';
                url='http://worldofwarships'+zone+'/community/accounts/search/?search='+name;
			}else if(zone=='美服'){
                zone='.com/en';
                url='http://worldofwarships'+zone+'/community/accounts/search/?search='+name;
            }else if(zone=='俄服'){
                zone='.ru/ru';
                url='http://worldofwarships'+zone+'/community/accounts/search/?search='+name;
            }else{
                return;
            }
			window.open(url,'_blank');
		}
	});
	$('.dropdown-menu').children('li').children('a').click(function(){
		$(this).parent().parent().parent().children('button:first').text($(this).text());
	});
	$("#minibutton").click(function(){min2tray();});
    $("#count").load('count.html #countindex');
    $("#begin").remove();
    setAccountActive();
    checkVersion();
    if(!checkERROR(config.get('account'))&&!checkERROR(config.get('account')['active'])){
        if(config.get('account')['active']>1500000000&&config.get('account')['active']<1900000000){
            dossier=new ChinaDossier();
        }else{
            dossier=new OtherDossier();
        }
        $("#battlestatbutton").click();
    }else{
        $("#configbutton").click();
    }
});
};
//启动游戏
function startgame(){
	if (checkERROR(config.get('path'))){
		customAlert("请先设置游戏路径",'warning');
		return;
	}
    if(fs.existsSync(config.get('path')+"\\WOWsLauncher.exe")) {
        cp.spawn(config.get('path') + "\\WOWsLauncher.exe");
    }else{
        customAlert("<font style='color:red'>启动文件"+config.get('path')+"\\WOWsLauncher.exe</font>不存在","danger");
    }
}

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

//插件页面
function modPage(){
    changePage();
    checkVersion(true);
    if ($("#modpage").html().length>0){
        $("#modpage").html("");
    }
    $("#modpage").load("mod.html #nav",function(data){
        $("#modassemblebutton").click(function(){
            //切换active
            $(this).parent().addClass("active").siblings().removeClass("active");
            assembleManager.initHtml();//assemble.js
        });
        $("#modinstallbutton").click(function(){
            //切换active
            $(this).parent().addClass("active").siblings().removeClass("active");
            refreshModList();//mod.js
        });
        $("#paintinstallbutton").click(function(){
            //切换active
            $(this).parent().addClass("active").siblings().removeClass("active");
            refreshPaintList();//paint.js
        });
        $("#moddeletebutton").click(function(){
            //切换active
            $(this).parent().addClass("active").siblings().removeClass("active");
            refreshModDelList();//mod.js
        });
        $("#modbakbutton").click(function(){
            //切换active
            $(this).parent().addClass("active").siblings().removeClass("active");
            modbakpage();
        })
        $("#modclever").click(function(){
            //切换active
            $(this).parent().addClass("active").siblings().removeClass("active");
            cleverinstallpage();
        })
        $("#modpage").append("<div id='modlist'></div>");
        $("#count").load('count.html #countmod');
        $("#modinstallbutton").click();
        $("#modpage").show();
        //统计页
    });
    $("#modlistbutton").parent().addClass("active");
}

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

//效率分析页面
function shipsPage(){
	changePage();
    dossier.refreshShipList();
	$("#shipsstatbutton").parent().addClass("active");
}

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

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

function replayPage(){
    changePage();
    showReplays();
    $("#replaybutton").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();
	$("#menu .active").removeClass("active");
    if(!checkERROR(dossier)){
        clearInterval(dossier.showRecentBattles);
    }
}

function checkNowPage(page,update){
    if (nowpage==page){
        return false;
    }else{
        if (update) {
            nowpage = page
        }
        return true;
    }
}
