/**
 * App下载
 */
$.registerNameSpace("cga.client.appclient");
$(function(){ //dom加载完再调用下载方法
$.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) {
	},
	//添加并下载App
	openApp : function(jsonData) {
		var isopen = window.top.hfweb.openSPG(jsonData);
		if(isopen)
			window.top.hfweb.showSpgWindow();
	},
	//客户端回调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 == 0 || status == 2 || status == 3 || status == 10)
				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"><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)
					$(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;
		}
		try{
			$("#app_"+app_id+" .down_status").find("a").removeClass().addClass("btn_download");
			window.top.hfweb.pauseSPG(app_id);
		}catch(e){};
	},
	//继续下载
	appContinue : function(app_id) {
		if(!app_id) {
			alert('继续游戏下载失败,游戏ID为空');
			return false;
		}
		try{
			$("#app_"+app_id+" .down_status").find("a").removeClass().addClass("btn_stop");
			window.top.hfweb.resumeSPG(app_id);
		}catch(e){};
	},
	//开始游戏
	appStart : function(app_id) {
		if(!app_id) {
			alert('开始游戏失败,游戏ID为空');
			return false;
		}
		window.top.hfweb.playSPG(app_id);
		//if(isplay)
			//$("#app_"+app_id).remove();
	},
	//打开下载窗体
	appWindow : function() {
		window.top.hfweb.showSpgWindow();
	},
	appStatusName : ["未下载","正在下载中...","已停止下载","下载失败","正在安装中...","正在安装中...","安装失败","安装完成","正在删除中...","删除完成","等待下载中..."]

}
});
