//初始化全局变量
var waitShowObjs = new Array();
//var httpHeadUrl="http://localhost:84/api.fxhelper.cn";
var BUBUJS_VERSION="2.0";
var APP_VERSION="3.0";
var httpHeadUrl="http://api.fxhelper.cn";

//初始化日志参数MACHINE_ID
var MACHINE_ID="";
	if(bubujs.readRegistry("Software\\外汇助手\\MACHINE_ID")==false){
		MACHINE_ID=generateMixed(16);
			bubujs.writeRegistry("Software\\外汇助手\\BUBUJS_VERSION", BUBUJS_VERSION);
			bubujs.writeRegistry("Software\\外汇助手\\APP_VERSION", APP_VERSION);
			bubujs.writeRegistry("Software\\外汇助手\\MACHINE_ID", MACHINE_ID);
			$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/4/machine_id/"+MACHINE_ID+"/category/2");
	 }else{
		MACHINE_ID=bubujs.readRegistry("Software\\外汇助手\\MACHINE_ID");
	 }

	if("undefined" == typeof(bubujs)){
		//alert("没有bubujs对象");
	
	}else{
		bubujs.setTrayIcon("外汇助手");
		bubujs.addContextMenuItem("打开外汇助手", "openmainwindow");
		bubujs.addContextMenuItem("开机启动", "starWithSystem");
		bubujs.addContextMenuItem("退出", "exit");
	}


$('#fxad1').on('click', function(){
		$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/15/machine_id/"+MACHINE_ID+"/category/2");
});
$('#fxad2').on('click', function(){
		$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/16/machine_id/"+MACHINE_ID+"/category/2");
});
$('#fxad3').on('click', function(){
		$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/17/machine_id/"+MACHINE_ID+"/category/2");
});
$('#fxad4').on('click', function(){
		$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/18/machine_id/"+MACHINE_ID+"/category/2");
});

function openmainwindow() {
	bubujs.restore('mainwindow');
}

function starWithSystem(){
	if (bubujs.isStartWithSystem())
	{
		//ui.setting.startWithSystem.set(false);
		//ui.setting.setinit();
		
		bubujs.cancelStartWithSystem();
		bubujs.showBalloon("开机不启动");
		bubujs.checkMenuItem("开机启动", false);
		$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/10/machine_id/"+MACHINE_ID+"/category/2");

	}
	else
	{
		//ui.setting.startWithSystem.set(true);
		//ui.setting.setinit();
		
		bubujs.setStartWithSystem();
		bubujs.showBalloon("开机启动");
		bubujs.checkMenuItem("开机启动", true);
		$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/9/machine_id/"+MACHINE_ID+"/category/2");

	}
}


function exit() {
	$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/6/machine_id/"+MACHINE_ID+"/category/2");
	bubujs.quit();
}

var onTabCtrlClick = function(e) {
	//console.log(e.detail.id, e.detail.index, typeof(e.detail.index), Number(e.detail.index), typeof(Number(e.detail.index)));
		if(e.detail.id=="tabctrl1"){
			$("#id_index").show();
			$("#id_calendar").hide();
			$("#id_feedback").hide();
			$("#id_tools").hide();
			$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/11/machine_id/"+MACHINE_ID+"/category/2");

		}else if(e.detail.id=="tabctrl2"){
			$("#id_index").hide();
			$("#id_calendar").show();
			$("#id_feedback").hide();
			$("#id_tools").hide();
			$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/12/machine_id/"+MACHINE_ID+"/category/2");

		}
		else if(e.detail.id=="tabctrl4"){
			$("#id_index").hide();
			$("#id_calendar").hide();
			$("#id_feedback").show();
			$("#id_tools").hide();
			$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/14/machine_id/"+MACHINE_ID+"/category/2");

		}
		else if(e.detail.id=="tabctrl3"){
			$("#id_index").hide();
			$("#id_calendar").hide();
			$("#id_feedback").hide();
			$("#id_tools").show();
			$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/13/machine_id/"+MACHINE_ID+"/category/2");

		}
}

var OnSysButtonCloseClick = function(e) {
	if (e.detail.id == 'mainwindow')
	{
			bubujs.hide(e.detail.id)
			//bubujs.quit();
			$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/5/machine_id/"+MACHINE_ID+"/category/2");

	}
	else{
		bubujs.close(e.detail.id);
	}
}

addEventListener("OnSysButtonCloseClick", OnSysButtonCloseClick);
addEventListener("onTabCtrlClick", onTabCtrlClick);

window.onload=function(){
	//载入财经日历
	//alert("载入财经日历");
	//修复今日BUG
	var today = new Date();
	var strtoday=today.getFullYear()+"-"+(today.getMonth()+1)+"-"+today.getDate();
			
	setFilter("date",strtoday);
	
	$("#id_index").show();
	$("#id_calendar").hide();
	$("#id_tools").hide();
	$("#id_feedbackBody").hide();
	
}

	function setFilter(field, value){ 
		var params = {}; params["type"] = "calendar"; params[field] = value;
		$('#calendar_content').load("http://www.dailyfx.com.hk/inc/process.php", params, function(data){ 
			//$(this).html(gbBig5(data)); 
			//alert("c");
			fixbug_today();
		});	
		
		}
		
	// function gbBig5(text){ text = StranText(text,"gb2312"); return text;	 }
	// function StranText(txt,chg){ if(txt==""||txt==null)
	// return "";
	// if(chg=="big-5")
	// {return TraditionalizedPath(TraditionalizedFix(Traditionalized(txt)),chg)} else{return SimplizedPath(Simplized(txt),chg)} }
	
	function gbBig5(text){ return text; }

	function fixbug_today(){
			var today = new Date();
			var strtoday=today.getFullYear()+"-"+(today.getMonth()+1)+"-"+today.getDate();

			items=$('.bottom15 a');
			for(var i=0;i< items.length;i++){
				if (items[i].href.indexOf("index.html")>0){
					//console.log( items[i] );
					items[i].href="javascript:setFilter('date','" + strtoday + "')";
					//alert(items[i].href);
					//items[i].href="#";
				}
				
				if (items[i].href.indexOf("historical")>0){
					//console.log( items[i] );
					var href=items[i].href;
					var path=href.substring(items[i].href.indexOf("historical"));
					console.log(path);
					items[i].href='http://www.dailyfx.com.hk/' + path;         //添加http头，由弹窗改为_blank
					items[i].target='_blank';
					items.eq(i).removeClass().addClass("cc");        //删除历史数据弹窗
					//alert(items[i].href);
					//items[i].href="#";
				}

			}
			items=$('.bottom15 img');
			for(var i=0;i< items.length;i++){
				if (items[i].src.indexOf("img")>0){
					//console.log( items[i] );
					var src=items[i].src;
					var path=src.substring(items[i].src.indexOf("img"));
					console.log(path);
					items[i].src='http://www.dailyfx.com.hk/' + path;         //添加http头，由弹窗改为_blank
					//alert(items[i].href);
					//items[i].href="#";
				}
				
			}
				
			$('.article').eq(0).hide();         //删除中间的财经日历标题
			$('.article').eq(1).hide();         //删除中间的财经日历标题
			$('.article').eq(2).hide();         //删除中间的财经日历标题

			$('.bottom15 td').eq(0).html('财经日历');    //删除财经日历按钮
			
			var bottom15=$('.bottom15').eq(0);
			bottom15.css("border-bottom","0");
	}
	

	