//设置本模块分页部分
var local_pageNo=1;
var pageSize=15;
var queryData = eval("({'pageNo':'"+local_pageNo+"','pageSize':'"+pageSize+"'})");

function resetQueryData(){
	queryData = "";
	queryData = eval("({'pageNo':'"+local_pageNo+"','pageSize':'"+pageSize+"'})");
	queryData.pageSize=15;
};

function switchtab(url){
	switch(url){
    case 'ratingnav':
	    $("#ratingnav").attr("class","first_li checked");
  	    $("#categorynav").attr("class","");
  	    $("#installednav").attr("class","");
  	    $("#mng").attr("class","last_li nav_app_highlight");
  	    resetQueryData();
  	    loadCategory();
        break;
    case 'categorynav':
    	$("#ratingnav").attr("class","first_li");
    	$("#categorynav").attr("class","checked");
    	$("#installednav").attr("class","");
    	$("#mng").attr("class","last_li nav_app_highlight");
    	resetQueryData();
    	queryData.FIELD_INSTALL_STATUS = "uninstall";
    	loadCategory();
	    break;
    case 'installednav':
    	$("#ratingnav").attr("class","first_li");
    	$("#categorynav").attr("class","");
    	$("#installednav").attr("class","checked");
    	$("#mng").attr("class","last_li nav_app_highlight");
    	resetQueryData();
    	queryData.FIELD_INSTALL_STATUS = "install";
    	loadCategory();
	    break;
    case 'mng':
    	$("#ratingnav").attr("class","first_li");
    	$("#categorynav").attr("class","");
    	$("#installednav").attr("class","");
    	$("#mng").attr("class","last_li checked nav_app_highlight");
    	loadManegePage(1);
	    break;
  }
}

/**---------------------------应用查看----------------------------------------**/
/**
 * 装载app类型tab
 *
 */
function loadCategory(){
	var v_data = appstoreModel.dataAction.listAllCategory({})[0].rowSet;
	var target_category = "";
	for(var i=0;i<v_data.length;i++){
		target_category = target_category + "<a href=\"javascript:void(0)\" id=\""+v_data[i].CATEGORY_ID+"\" onclick=\"loadPage(1,'"+v_data[i].CATEGORY_ID+"',false)\">"+v_data[i].NAME+"</a> ";
	}
	var target = "<div class=\"category-content-box\"> "
		       + "<div class=\"category-content-tab\"><div class=\"category-content-nav\">"
		       + "<a href=\"javascript:void(0)\" id=\"all\" onclick=\"loadPage(1,'',false)\" class=\"category-content-active\">全部应用</a>"
		       + target_category
		       + "<a href=\"javascript:void(0)\" id=\"default\" onclick=\"loadPage(1,'',true)\">默认应用</a>"
               + "</div></div>"
               + "<div class=\"category-content-container\"> "
               + "<div id=\"categoryInfo\" class=\"category-content-sub-con cur-category-content-sub-con\">"
               + "<div id=\"categoryInfoContent\" class=\"category-content-sub-con-content\">单位信息</div></div> "
               + "</div>"
               + "</div> ";
	$("#yingyzxright").html(target);
	
	var curLi=0;
	$(".category-content-nav a").mouseover(function(){ 
	 curLi=$(this);
	});
	
	$(".category-content-nav a").click(function(){
	  $(".category-content-active").removeClass("category-content-active"); 
	  curLi.addClass("category-content-active"); 
	}); 
	
	loadPage(1,'',false);
	
}

/**
 * 全部应用翻页
 */
function loadPage(pageNo,categoryID,isDefault){
	local_pageNo = pageNo;
	queryData.pageNo=local_pageNo;
	queryData.pageSize=pageSize;
	if(isDefault == true){
	   queryData.FIELD_APPIS_DEFAULT = '1';
	   queryData.FIELD_APPCATEGORY = '';
	}else{
	   queryData.FIELD_APPCATEGORY = categoryID;
	   queryData.FIELD_APPIS_DEFAULT = '';
	}
	var v_data = appstoreModel.dataAction.listUserAllApp(queryData);
	if(v_data[0].rowSet.length>0){
   	   $("#categoryInfoContent").load(gaowj.WEB_APP_NAME+"/jwapp/pages/apps/appsAllTemp.jsp",function(){
   		   renderDATA(v_data[0].rowSet,$("#row"),"resizabletable");
   		   var target = "<div style=\"border-bottom:solid #ccc 1px;width:760px;height:40px;\"><div style=\"margin:10px 0 0 150px;\">"
   			           +"<div style=\"border:solid #ccc 1px;width:50px;height:25px;float:left;margin-left:5px;\"><a href=\"#\" style=\"text-decoration:none;\" onclick=\"loadPage(1,'"+categoryID+"',"+isDefault+");\">首页</a></div>"
   			           +"<div style=\"border:solid #ccc 1px;width:50px;height:25px;float:left;margin-left:5px;\"><a href=\"#\" style=\"text-decoration:none;\" onclick=\"loadPage("+ (pageNo>1?(pageNo -1):pageNo) +",'"+categoryID+"',"+isDefault+");\">上一页</a></div>"
   			           +"<div style=\"border:solid #ccc 1px;width:50px;height:25px;float:left;margin-left:5px;\"><a href=\"#\" style=\"text-decoration:none;\" onclick=\"loadPage("+ (pageNo<v_data[0].pageCount?(pageNo + 1):pageNo) +",'"+categoryID+"',"+isDefault+");\">下一页</a></div>"
   			           +"<div style=\"border:solid #ccc 1px;width:50px;height:25px;float:left;margin-left:5px;\"><a href=\"#\" style=\"text-decoration:none;\" onclick=\"loadPage("+v_data[0].pageCount+",'"+categoryID+"',"+isDefault+");\">末页</a></div>"
   			           +"<div style=\"border:solid #ccc 1px;width:50px;height:25px;float:left;margin-left:5px;\">"+v_data[0].pageNo+"/"+v_data[0].pageCount+"</div>"
   			           +"<div style=\"float:left;margin-left:5px;\">到第<input type=\"text\" id=\"toPage\" value=\""+pageNo+"\" onblur=\"if(this.value>"+v_data[0].pageCount+" || this.value<0) this.value=1\" style=\"width:30px;height:25px;line-height:25px;\"/>页</div>"
   			           +"<div style=\"float:left;margin-left:5px;\"><input type=\"button\" value=\"确定\" onclick=\"loadPage(parseInt(toPage.value),'"+categoryID+"',"+isDefault+");\" class=\"input_btn_12 page_define_btn\"/></div>"
   			           +"<div></div>";
   			 
   		  $("#myapppage").html(target);
   		  $("#toPage").numeral();
   		 
   		  renderOPE();
   	 });
   	}else{
   		$("#categoryInfoContent").html("");
   	}
	
}
/**
 * 添加管理动作
 * @param rows
 * @param pkid
 * @param target
 */
function renderOPE(){
	$("div[id='row']").each(function(i,row){
	   var v_id = $(row).find("#RESOURCE_ID").text();
	   var v_name = $(row).find("#APPNAME").text();
	   var v_install_status = $(row).find("#INSTALL_STATUS").text();
	   var v_isdefault = $(row).find("#APPIS_DEFAULT").text();
	   var v_album_photo = $(row).find("#ALBUM_PHOTO_LASTTIME").text();
	   
	   var v_isdefault_name="";
	   if(v_isdefault == "1"){
		   v_isdefault_name = "(默认安装)";
	   }else{
		   v_isdefault_name = "(用户安装)";
	   }
	   
	   var v_install_status_name="";
	   if(v_install_status == "install"){
		   v_install_status_name = "<div align=\"center\" style=\"background:#b20000 url("+gaowj.WEB_APP_NAME+"/jwapp/pages/apps/image/white_delete_icon.png) no-repeat 18px -1px;border:1px solid #b20000;width:100px;height:20px;margin-left:-1px;\"><a href=\"javascript:void(0);\" style=\"color:#fff;\">卸载</a></div>";
	   }else if(v_install_status == "uninstall"){
		   v_install_status_name = "<div align=\"center\" style=\"background:#0099CC url("+gaowj.WEB_APP_NAME+"/jwapp/pages/apps/image/new_apps_add.png.png) no-repeat 9px 3px;border:1px solid #0099CC;width:100px;height:20px;margin-left:-1px;\"><a href=\"javascript:void(0);\" style=\"color:#fff;\">安装应用</a></div>";
	   }

	   var img_html = "<img src=\""+gaowj.WEB_APP_NAME+"/jwapp/pages/apps/photo.jsp?ls_photo="+v_album_photo+"\" width=\"50\" height=\"50\" style=\"cursor:pointer;margin:5px 0 0 22px;\" onclick=\"viewPhoto();\"/>"
	                + "<div align=\"center\"><span>"+CutString(v_name,4)+"</span></div>"
	                + "<div align=\"center\"><span>"+v_isdefault_name+"</span></div>"
	                + v_install_status_name;

	   $(row).find("#target3_app").html(img_html);
	});
  }

switchtab("ratingnav");

/**---------------------------应用搜索----------------------------------------**/
    var v_all_app_data = appstoreModel.dataAction.listUserAllApp({})[0].rowSet;
    var v_search_data = getSearchData(v_all_app_data,"APPNAME");
    $("#search_app").autocomplete({
       source: v_search_data
    });
    
    $("#search_result").click(function(){
    	queryData.FIELD_APPNAME = $("#search_app").val();
    	loadCategory();
    });
    
/**---------------------------应用管理----------------------------------------**/
    /**
     * 全部应用翻页
     */
    function loadManegePage(pageNo){
    	local_pageNo = pageNo;
    	queryData.pageNo=local_pageNo;
    	queryData.pageSize=21;
    	var v_data = appstoreModel.dataAction.listApp(queryData);
    	if(v_data[0].rowSet.length>0){
       	   $("#yingyzxright").load(gaowj.WEB_APP_NAME+"/jwapp/pages/apps/appsManageTemp.jsp",function(){
       		   renderDATA(v_data[0].rowSet,$("#row"),"resizabletable");
       		   var target = "<div style=\"border-bottom:solid #ccc 1px;width:760px;height:40px;\"><div style=\"margin:10px 0 0 150px;\">"
       			           +"<div style=\"border:solid #ccc 1px;width:50px;height:25px;float:left;margin-left:5px;\"><a href=\"#\" style=\"text-decoration:none;\" onclick=\"loadManegePage(1);\">首页</a></div>"
       			           +"<div style=\"border:solid #ccc 1px;width:50px;height:25px;float:left;margin-left:5px;\"><a href=\"#\" style=\"text-decoration:none;\" onclick=\"loadManegePage("+ (pageNo>1?(pageNo -1):pageNo) +");\">上一页</a></div>"
       			           +"<div style=\"border:solid #ccc 1px;width:50px;height:25px;float:left;margin-left:5px;\"><a href=\"#\" style=\"text-decoration:none;\" onclick=\"loadManegePage("+ (pageNo<v_data[0].pageCount?(pageNo + 1):pageNo) +");\">下一页</a></div>"
       			           +"<div style=\"border:solid #ccc 1px;width:50px;height:25px;float:left;margin-left:5px;\"><a href=\"#\" style=\"text-decoration:none;\" onclick=\"loadManegePage("+v_data[0].pageCount+");\">末页</a></div>"
       			           +"<div style=\"border:solid #ccc 1px;width:50px;height:25px;float:left;margin-left:5px;\">"+v_data[0].pageNo+"/"+v_data[0].pageCount+"</div>"
       			           +"<div style=\"float:left;margin-left:5px;\">到第<input type=\"text\" id=\"toPage\" value=\""+pageNo+"\" onblur=\"if(this.value>"+v_data[0].pageCount+" || this.value<0) this.value=1\" style=\"width:30px;height:25px;line-height:25px;\"/>页</div>"
       			           +"<div style=\"float:left;margin-left:5px;\"><input type=\"button\" value=\"确定\" onclick=\"loadManegePage(parseInt(toPage.value));\" class=\"input_btn_12 page_define_btn\"/></div>"
       			           +"<div></div>";
       			 
       		  $("#myapppage").html(target);
       		  $("#toPage").numeral();
       		 
       		  renderManageOPE();
       		  
       		  $("#appmanageadd").click(function(){
       			appInsert();
 			  });
       	 });
       	}else{
       		$("#yingyzxright").html("");
       	}
    	
    }
    /**
     * 添加管理动作
     * @param rows
     * @param pkid
     * @param target
     */
    function renderManageOPE(){
    	$("div[id='row']").each(function(i,row){
    	   var v_app_id = $(row).find("#APP_ID").text();
    	   var v_name = $(row).find("#NAME").text();
    	   var v_install_status = $(row).find("#INSTALL_STATUS").text();
    	   var v_isdefault = $(row).find("#IS_DEFAULT").text();
    	   var v_album_photo = $(row).find("#ALBUM_PHOTO_LASTTIME").text();

    	   var img_html = "<div class=\"app_back_ico\">"
    		            + "<a title=\"删除\" class=\"app_back_ico_delete\" onclick=\"appDelete('"+v_app_id+"')\" href=\"javascript:void(0);\"></a>"
    		            + "<a title=\"编辑\" class=\"app_back_ico_edit\" onclick=\"appUpdate('"+v_app_id+"')\" href=\"javascript:void(0);\"></a>"
    		            + "<a href=\"javascript:void(0);\">"
    		            + "<img src=\""+gaowj.WEB_APP_NAME+"/jwapp/pages/apps/photo.jsp?ls_photo="+v_album_photo+"\" width=\"50\" height=\"50\" style=\"cursor:pointer;margin:5px 0 0 22px;\" onclick=\"viewPhoto();\"/>"
    		            + "</a>"
    		            + "<a class=\"highlight\" href=\"javascript:void(0);\"><span class=\"highlight\">"+CutString(v_name,5)+"</span></a>"
    	                + "</div>";

    	   $(row).find("#target3_app").html(img_html);
    	});
      }
    
    /**
     * 应用新增
     */
   function appInsert(){
	 $("#yingyzxright").load(gaowj.WEB_APP_NAME+"/jwapp/pages/apps/appsInsertTemp.jsp",function(){
		 $("#app_content_image").hide();
		 $("#app_content_resource").hide();
		 $("#PUB_DATE").loadMy97Date(true);
		 $("#OPEN_TIME").loadMy97Date(true);
		 $("#app_content_form").validateFormBlur();
		 $("#do_ok").click(function() {
     		if(!($("#app_content_form").validateSubmit())) return;
     		var v_entity = $("#app_content_form").getFormVal();
     		var data = {};
     		data = appstoreModel.actAction.insertApp(v_entity);
		    fancyAlert(data[0].VALUE,1);
	        jQuery.fancybox.close();
	        appUpdate(data[0].RETURNUUID);  
		 });
		 $("#do_cancel").click(function() {
			 resetQueryData();
		     loadManegePage(local_pageNo);
		 });
		 $(":radio[name=FIELD_NEED_RIGHT][value='1']").click(function (){
			 $("#app_content_resource").show();
		 });
         $(":radio[name=FIELD_NEED_RIGHT][value='0']").click(function (){
        	 $("#app_content_resource").hide();
		 });
   	 });
   }
   
   /**
    * 应用编辑
    */
   function appUpdate(app_id){
	   $("#yingyzxright").load(gaowj.WEB_APP_NAME+"/jwapp/pages/apps/appsInsertTemp.jsp",function(){
		     queryData.FIELD_APP_ID = app_id;
			 var v_data = appstoreModel.dataAction.listAllApp(queryData);
			 queryData.FIELD_APP_ID = "";
			 $("#app_content_image").show();
			 $("#PUB_DATE").loadMy97Date(true);
			 $("#OPEN_TIME").loadMy97Date(true);
			 $("#app_content_form").validateFormBlur();
			 renderDATAFORM(v_data[0].rowSet[0],$("#app_content_row"));
			 $("#do_ok").click(function() {
	     		if(!($("#app_content_form").validateSubmit())) return;
	     		var v_entity = $("#app_content_form").getFormVal();
	     		var data = {};
	     		data = appstoreModel.actAction.updateApp(v_entity);
			    fancyAlert(data[0].VALUE,1);
		        jQuery.fancybox.close();
			 });
			 $("#do_cancel").click(function() {
				 resetQueryData();
			     loadManegePage(local_pageNo);
			 });
			 if(v_data[0].rowSet[0].NEED_RIGHT == '1'){
				 $("#app_content_resource").show();
			 }else{
				 $("#app_content_resource").hide(); 
			  }
			 $(":radio[name=FIELD_NEED_RIGHT][value='1']").click(function (){
				 $("#app_content_resource").show();
			 });
	         $(":radio[name=FIELD_NEED_RIGHT][value='0']").click(function (){
	        	 $("#app_content_resource").hide();
			 });
	   	 });
	   
   }
   
   /**
    * 应用删除
    */
   function appDelete(app_id){
	   queryData.deletekey = app_id;
   	   fancyConfirm("确定删除此?",function(){
   		appstoreModel.actAction.deleteApp(queryData);
   		loadManegePage(local_pageNo);
   	   },"提示",1);
   }