/**
 * App下载
 */
$.registerNameSpace("cga.client.appclient");
$.cga.client.appclient = {
	//app状态
	initApp : function(app_id) {
		if(!app_id) {
			alert('初始化游戏失败,游戏ID为空');
			return false;
		}
		return window.top.hfweb.getSpgState(app_id);
	},
	//客户端回调app状态
	appStatus : function(jsonData) {
		if(typeof jsonData == 'string')
			jsonData = eval("("+jsonData+")");
		var _thisg = $("#mainIframe")[0].contentWindow.$("a.addRes_g[resid="+jsonData.app_id+"]");
		var _thism = $("#mainIframe")[0].contentWindow.$("a.addRes_m[resid="+jsonData.app_id+"]");
		//var _thisg = $("a.addRes_g[resid="+jsonData.app_id+"]");
		//var _thism = $("a.addRes_m[resid="+jsonData.app_id+"]");
		if(jsonData.app_status == 1 || jsonData.app_status == 4 || jsonData.app_status == 5) {
			_thisg.removeClass().addClass("addRes_g loading_g");
			_thism.removeClass().addClass("addRes_m loading_m");
		} else if(jsonData.app_status == 2) {
			_thisg.removeClass().addClass("addRes_g restart_g");
			_thism.removeClass().addClass("addRes_m restart_m");
		} else if(jsonData.app_status == 7) {
			_thisg.removeClass().addClass("addRes_g downloadOpen_g");
			_thism.removeClass().addClass("addRes_m downloadOpen_m");
		} else {
			_thisg.removeClass().addClass("addRes_g download_g");
			_thism.removeClass().addClass("addRes_m download_m");
		}
		
		if(jsonData.app_id && jsonData.app_status == 7) {
			$.post('/app/initApp',{"app_id":jsonData.app_id,"app_status":jsonData.app_status},function(ref){
				if(!ref.errno) {
					window.frames.barIframe.$.cga.apps.index.gameBar.openGameAddMenu(ref.data);
				}
			},'json');
		}
	},
	//添加并下载App
	openApp : function(jsonData, operate) {
		var isopen = window.top.hfweb.openSPG(jsonData);
		if(isopen && operate != 'downloadOpen')
			this.appWindow();
		return isopen;
	},
	//客户端回调app下载状态
	appsDownloadStatus : function(jsonData, resizeDiv) {
		var str = new Array();
		var  total_size = 0, speed = 0, status = 0, n = 0;
		var total ='', dspeed ='', html ='';
		if(typeof jsonData == 'string')
			jsonData = eval(jsonData);
		jsonData = jsonData[0];
		
		var app_id = jsonData.app_id;
		status = jsonData.app_status ? parseInt(jsonData.app_status) : 0;
		if(status != 9) {
			if(status == 7) {
				total_size = isNaN(jsonData.app_size) ? 0.00 : parseFloat(jsonData.app_size/1024);
				if(total_size > 1048576) {
					total = new Number(total_size/1048576).toFixed(2) + "GB";
				} else if (total_size>1024) {
					total = new Number(total_size/1024).toFixed(2) + "MB";
				} else {
					total = total_size.toFixed(2) + "KB";
				}
			}
			var speed = isNaN(jsonData.down_speed) ? 0.00 : parseFloat(jsonData.down_speed/1024);
			if (speed > 1024){
				dspeed = new Number(speed/1024).toFixed(2) + "MB/S";
			} else {
				dspeed = speed.toFixed(2)+ " KB/S";
			}
			str[n++] = '<span class="down_status">';
			if(status == 2 || status == 3)
				str[n++] ='<a href="javascript:;" class="btn_download"><span class="hide">下载</span></a>';
			else if(status == 7)
				str[n++] = '<a href="javascript:;" class="btn_ok"><span class="hide">下载</span></a>';
			else
				str[n++] = '<a href="javascript:;" class="btn_stop" hidefocus="true"><span class="hide">下载</span></a>';
			str[n++] =	'</span>'+
				'<div class="down_files">'+
					'<p class="down_info">'+
						'<strong>'+jsonData.app_name+'</strong>';
			if(status ==1)
				str[n++] ='<span class="size">'+dspeed+'</span>';
			else if(status != 7)
				str[n++] ='<span class="done">'+eval('this.appStatusName['+status+']')+'</span>';
			if(status == 7) {
				str[n++] = '</p>'+
				'<p class="down_info2">'+
					'<span class="size">'+total+'</span>'+
					'<span class="done">下载完成</span>'+
				'</p>'+
				'</div>'+
				'<span class="down_tools">'+
					'<a href="javascript:;" class="btn_play"><span class="hide">开始游戏</span></a>'+
				'</span>';
			} else {
				str[n++] = '</p>'+
					'<p class="progress">'+
						'<span class="progress_bar">'+
							'<em class="percent" style="width:'+jsonData.app_progress+'%"><i></i></em>'+
						'</span>'+
					'</p>'+
				'</div>'+
				'<span class="down_tools">'+
					'<a href="javascript:;" class="btn_delete"><span class="hide">删除</span></a>'+
				'</span>';
			}
			html = str.join("");
			if(!$("#app_"+app_id).length) {
				html = '<li id="app_'+app_id+'" '+(status == 7 ? 'class="down_ok"' : '')+'>' +html+ '</li>';
				$(".download_list ul").prepend(html);
				if(resizeDiv && $(".download_list ul li").length > 4) //dom加载完再重新初始化div大小
					$(function(){
						$(resizeDiv).data('jsp').reinitialise();
					});
			} else {
				$("#app_"+app_id).removeClass();
				if(status == 7) 
					$("#app_"+app_id).addClass('down_ok');
				$("#app_"+app_id).html(html);
			}
		} else {
			$("#app_"+app_id).remove();
		}
	},
	//取消下载
	appDelete : function(app_id) {
		if(!app_id) {
			alert('取消游戏下载失败,游戏ID为空');
			return false;
		}
		window.top.hfweb.deleteSPG(app_id);
	},
	//暂停下载
	appStop : function(app_id) {
		if(!app_id) {
			alert('暂停游戏下载失败,游戏ID为空');
			return false;
		}
		window.top.hfweb.pauseSPG(app_id);
	},
	//继续下载
	appContinue : function(app_id) {
		if(!app_id) {
			alert('继续游戏下载失败,游戏ID为空');
			return false;
		}
		var iscontinue = window.top.hfweb.resumeSPG(app_id);
		if(iscontinue)
			this.appWindow();
	},
	//开始游戏
	appStart : function(app_id) {
		if(!app_id) {
			alert('开始游戏失败,游戏ID为空');
			return false;
		}
		return window.top.hfweb.playSPG(app_id);
		/*if(isplay)
			$("#app_"+app_id).remove();*/
	},
	//打开下载窗体
	appWindow : function() {
		window.top.hfweb.showSpgWindow();
	},
	appStatusName : ["未下载","正在下载中...","已停止下载","下载失败","正在安装中...","正在安装中...","安装失败","安装完成","正在删除中...","删除完成","等待下载中..."]

}

