//设置本模块分页部分
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 + "'})");
};

function setSelect(){
	var opetion = "<option value=''>请选择</option>";
	var v_all_tempModelData = jwAppPortletModel.jwAppsPortletDataAction.listAllPTemplate({});
	var i = 0;
	for (i = 0; i < v_all_tempModelData[0].rowSet.length; i++) {
		var rowData = v_all_tempModelData[0].rowSet[i];
		opetion = opetion + " <option value=\"" + rowData.PORTLET_ID + "\">"
				+ rowData.NAME + "</option>";
	}
	for(var i=1;i<10;i++){
		$("#set_"+i).html("<select id=\"s_"+i+"\" onChange=\"changeTemp(this.id,'h_"+i+"','c_"+i+"');\">"+opetion+"</select></div>");
	}
}

function removeSelect(){
	for(var i=1;i<10;i++){
		$("#set_"+i).html("");
	}
	$(".portlet-header span").remove();
}
var userDefaultModel =  "<div class=\"column\">"
                      + "<div class=\"portlet\">"
                      + "<div class=\"portlet-header\"><div id=\"h_1\">1</div></div>"
                      + "<div class=\"portlet-content\"><div id=\"c_1\">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div></div>"
                      + "<div id=\"set_1\" class=\"portlet-setup\"></div>"
                      + "</div>"
                      + "<div class=\"portlet\">"
                      + "<div class=\"portlet-header\"><div id=\"h_2\">2</div></div>"
                      + "<div class=\"portlet-content\"><div id=\"c_2\">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div></div>"
                      + "<div id=\"set_2\" class=\"portlet-setup\"></div>"
                      + "</div>"
                      + "<div class=\"portlet\">"
                      + "<div class=\"portlet-header\"><div id=\"h_3\">3</div></div>"
                      + "<div class=\"portlet-content\"><div id=\"c_3\">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div></div>"
                      + "<div id=\"set_3\" class=\"portlet-setup\"></div>"
                      + "</div>"
                      + "</div>"
                      + "<div class=\"column\">"
                      + "<div class=\"portlet\">"
                      + "<div class=\"portlet-header\"><div id=\"h_4\">4</div></div>"
                      + "<div class=\"portlet-content\"><div id=\"c_4\">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div></div>"
                      + "<div id=\"set_4\" class=\"portlet-setup\"></div>"
                      + "</div>"
                      + "<div class=\"portlet\">"
                      + "<div class=\"portlet-header\"><div id=\"h_5\">5</div></div>"
                      + "<div class=\"portlet-content\"><div id=\"c_5\">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div></div>"
                      + "<div id=\"set_5\" class=\"portlet-setup\"></div>"
                      + "</div>"
                      + "<div class=\"portlet\">"
                      + "<div class=\"portlet-header\"><div id=\"h_6\">6</div></div>"
                      + "<div class=\"portlet-content\"><div id=\"c_6\">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div></div>"
                      + "<div id=\"set_6\" class=\"portlet-setup\"></div>"
                      + "</div>"
                      + "</div>"
                      + "<div class=\"column\">"
                      + "<div class=\"portlet\">"
                      + "<div class=\"portlet-header\"><div id=\"h_7\">7</div></div>"
                      + "<div class=\"portlet-content\"><div id=\"c_7\">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div></div>"
                      + "<div id=\"set_7\" class=\"portlet-setup\"></div>"
                      + "</div>"
                      + "<div class=\"portlet\">"
                      + "<div class=\"portlet-header\"><div id=\"h_8\">8</div></div>"
                      + "<div class=\"portlet-content\"><div id=\"c_8\">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div></div>"
                      + "<div id=\"set_8\" class=\"portlet-setup\"></div>"
                      + "</div>"
                      + "<div class=\"portlet\">"
                      + "<div class=\"portlet-header\"><div id=\"h_9\">9</div></div>"
                      + "<div class=\"portlet-content\"><div id=\"c_9\">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div></div>"
                      + "<div id=\"set_9\" class=\"portlet-setup\"></div>"
                      + "</div>"
                      + "</div>";

var default_tab = function(){
	$("#tabshow").load(gaowj.WEB_APP_NAME+"/jwapp/pages/portlet/jwAppPortletSet.jsp",function(){
		var data = jwAppPortletModel.jwAppsPortletDataAction.listAllPSetup({});
		if(data[0].pageCount==0){
			resetQueryData();
			queryData.FIELD_TEMPLATE = userDefaultModel;
			queryData.FIELD_LOCATION = '0';
			jwAppPortletModel.jwAppsPortletActAction.insertPSetup(queryData);
			data = jwAppPortletModel.jwAppsPortletDataAction.listAllPSetup({});
		};
		$("#userPortlet").html(data[0].rowSet[0].TEMPLATE);
		setSelect();
		
		$(function() {
		    $(".column").sortable({
		      connectWith: ".column",
		      handle: ".portlet-header",
		      cancel: ".portlet-toggle",
		      placeholder: "portlet-placeholder ui-corner-all"
		    }); 
		    
		    $(".portlet")
		      .addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
		      .find(".portlet-header")
		      .addClass("ui-widget-header ui-corner-all")
		      .prepend( "<span class='ui-icon ui-icon-minusthick portlet-toggle'></span>");

		    $(".portlet-toggle").click(function(){
		      var icon = $( this );
		      icon.toggleClass( "ui-icon-minusthick ui-icon-plusthick" );
		      icon.closest( ".portlet" ).find( ".portlet-content" ).toggle();
		    });
		    
		    $("#getTemp").click(function(){
		    	removeSelect();
		 	    var userModel = $("#userPortlet").html();
		 	    resetQueryData();
			    queryData.FIELD_TEMPLATE = userModel;
				var data = jwAppPortletModel.jwAppsPortletActAction.updatePSetup(queryData);
				fancyAlert(data[0].VALUE,1);
	        	jQuery.fancybox.close();
	        	default_tab();
		    });
		  });
	});
};

function changeTemp(sid,hid,cid){
	var tid = $("#"+sid).val();
	var v_query = {};
    v_query.FIELD_PORTLET_ID = tid;
    var v_data = jwAppPortletModel.jwAppsPortletDataAction.listAllPTemplate(v_query);

    $("#"+hid).html(v_data[0].rowSet[0].TITLE);
    if(v_data[0].rowSet[0].P_TYPE == '0'){//手工录入
    	$("#"+cid).html(v_data[0].rowSet[0].CONTENT);
    }else if(v_data[0].rowSet[0].P_TYPE == '1'){//集成方Portlet
    	$("#"+cid).html("<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" allowtransparency=\"yes\" width=\"100%\" height=\"100%\" src=\""+v_data[0].rowSet[0].URL+"\"></iframe>");
    }else if(v_data[0].rowSet[0].P_TYPE == '2'){//URL抓取
    	var v_query = {};
    	v_query.FIELD_PORTLET_ID = v_data[0].rowSet[0].PORTLET_ID;
    	var v_data = jwAppPortletModel.jwAppsPortletDataAction.urlData(v_query);
    	$("#"+cid).html(v_data[0].rowSet);
    }else if(v_data[0].rowSet[0].P_TYPE == '3'){//RSS订阅
    	$("#"+cid).html("<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" allowtransparency=\"yes\" width=\"100%\" height=\"100%\" src=\""+v_data[0].rowSet[0].URL+"\"></iframe>");
    }
    
}

default_tab();
$("#searchportlettab").click(function(){
	default_tab();
});
$("#addportlettab").click(function(){
	$("#tabshow").load(gaowj.WEB_APP_NAME+"/jwapp/pages/portlet/jwAppPortletUpdate.jsp",function(){
		$(document).ready(function(){
			loadPage(local_pageNo);
		});
	});
});


    
    /**
     * 翻页
     */
    function loadPage(pageNo){
    	local_pageNo = pageNo;
    	queryData.pageNo=local_pageNo;
    	queryData.pageSize=pageSize;
		var portletData = jwAppPortletModel.jwAppsPortletDataAction.listPTemplate(queryData);
		$("#portletListData").load(gaowj.WEB_APP_NAME+"/jwapp/pages/portlet/portletManageTemp.jsp",function(){
		   renderSPAN(portletData[0].rowSet,$("#row"),"resizabletable");
		   var target = "<div style=\"\"><div>"
			           +"<div style=\"border:solid #ccc 1px;width:50px;height:25px;float:left;margin-left:5px;\"><a href=\"#\" style=\"text-decoration:none;line-height:25px;\" onclick=\"loadPage(1);\">首页</a></div>"
			           +"<div style=\"border:solid #ccc 1px;width:50px;height:25px;float:left;margin-left:5px;\"><a href=\"#\" style=\"text-decoration:none;line-height:25px;\" onclick=\"loadPage("+ (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;line-height:25px;\" onclick=\"loadPage("+ (pageNo<portletData[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;line-height:25px;\" onclick=\"loadPage("+portletData[0].pageCount+");\">末页</a></div>"
			           +"<div style=\"border:solid #ccc 1px;width:50px;height:26px;float:left;margin-left:5px;\"><span style=\"line-height:25px;\">"+portletData[0].pageNo+"/"+portletData[0].pageCount+"</span></div>"
			           +"<div style=\"float:left;margin-left:5px;\">到第<input type=\"text\" id=\"toPage\" value=\""+pageNo+"\" onblur=\"if(this.value>"+portletData[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));\" class=\"input_btn_12 page_define_btn\"/></div>"
			           +"<div></div>";
			 
		   $("#myphotopage").html(target);
		   $("#toPage").numeral();
		   
		   // 绑定开户日期框日历插件
		   $("#search_begin_time, #search_end_time").loadJqueryDateMinMax(true,"search_begin_time","search_end_time");

		   //
		   renderOPE();
		});
    }
    
    function renderOPE(){
      $("tr[id='row']").each(function(i,row){
    	  $(row).find("#checkboxportletinfoid").val($(row).find("#PORTLET_ID").text());
    	  
    	  $(row).find("#portletUpdate").click(function(){
    		  var portletID = $(row).find("#PORTLET_ID").text();
    		  portletInfoModify(portletID);
    	  });
    	  $(row).find("#portletDelete").click(function(){
    		  fancyConfirm("确定删除?",function(){
    			  var portletID = $(row).find("#PORTLET_ID").text();
    			  var v_query = {};
    			  v_query.deletekey = portletID;
    			  var data = jwAppPortletModel.jwAppsPortletActAction.deletePTemplate(v_query);
	        	  fancyAlert(data[0].VALUE,1);
		          jQuery.fancybox.close();
		          loadPage(local_pageNo);
    	    	},"提示",1);
    		  
    	  });
      });
      
      $("#portletinsert").click(function(){
    	  portletInsert();
      });
      
      $("#portletbatchdelete").click(function(){
    	  var str=getCheckBoxValue("checkboxportletinfo");
		  if(str==""){
			 fancyAlert("请选中要删除的选项!",2); 
			 return;
		  }
		  fancyConfirm("确定删除?",function(){
		     var v_query = {};
		     v_query.deletekey = str;
		     var data = jwAppPortletModel.jwAppsPortletActAction.deletePTemplate(v_query);
    	     fancyAlert(data[0].VALUE,1);
             jQuery.fancybox.close();
             loadPage(local_pageNo);
		  },"提示",1);
      });
    	
    }
    
    var portletInfoModify = function(portletID){
    	var v_query = {};
	    v_query.FIELD_PORTLET_ID = portletID;
	    var v_data = jwAppPortletModel.jwAppsPortletDataAction.listAllPTemplate(v_query);
    	jQuery.fancybox({
			"modal" : false,
			"content" : ""
				+ "<div class=\"popup_header_icon_450\"><div class=\"popup_header_text\">门户插件修改</div></div>"
		        + "<form id=\"portletInfoForm\" action=\"\">"
		        + "<div id=\"portlet_content_row\">"
		        + "<input type=\"hidden\" id=\"PORTLET_ID\" name=\"FIELD_PORTLET_ID\"/>"
		        + "<div style=\"width:450px;\">"
		        + "<div>"
		        + "<span style=\"line-height:30px;height:33px;\">插件名称：</span><input type=\"text\" id=\"NAME\"  name=\"FIELD_NAME\" validate=\"required,maxlength\" maxlengthvalue=\"10\"/>"
		        + "<span style=\"line-height:30px;height:33px;margin-left:5px;\">插件主题：</span><input type=\"text\" id=\"TITLE\" name=\"FIELD_TITLE\"  validate=\"required,maxlength\" maxlengthvalue=\"50\"/>"
		        + "</div>"
		        + "<div>"
		        + "<span style=\"line-height:30px;height:33px;\">数据来源：</span>"
		        + "<select id=\"P_TYPE\" name=\"FIELD_P_TYPE\" style=\"width:130px;\" validate=\"required\">"
		        + "<option value=\"\">请选择</option>"
		        + "<option value=\"0\">手工录入</option>"
		        + "<option value=\"1\">集成方Portlet</option>"
		        + "<option value=\"2\">URL抓取</option>"
		        + "<option value=\"3\">RSS订阅</option>"
		        + "</select>"
		        + "<span style=\"line-height:30px;height:33px;margin-left:5px;\">插件图片：</span><input type=\"text\" id=\"ICON\" name=\"FIELD_ICON\"/>"
		        + "</div>"
		        + "<div>"
		        + "<span style=\"line-height:30px;height:33px;\">插件  URL：</span><input type=\"text\" id=\"URL\"  name=\"FIELD_URL\" style=\"width:325px;\"/>"
		        + "</div>"
		        + "<div>"
		        + "<span style=\"line-height:30px;height:33px;\">开始取值：</span><input type=\"text\" id=\"TAKE_START\"  name=\"FIELD_TAKE_START\" />"
		        + "<span style=\"line-height:30px;height:33px;margin-left:5px;\">结束取值：</span><input type=\"text\" id=\"TAKE_END\" name=\"FIELD_TAKE_END\"/>"
		        + "</div>"
		        + "<div>"
		        + "<span style=\"line-height:30px;height:33px;\">取值编码：</span><input type=\"text\" id=\"URL_ENCODED\" name=\"FIELD_URL_ENCODED\"/>"
		        + "<span style=\"line-height:30px;height:33px;margin-left:5px;\">点击动作：</span>"
		        + "<select id=\"IS_POP\" name=\"FIELD_IS_POP\" style=\"width:130px;\" validate=\"required\">"
		        + "<option value=\"\">请选择</option>"
		        + "<option value=\"0\">弹出层</option>"
		        + "<option value=\"1\">页面跳转</option>"
		        + "</select>"
		        + "</div>"
		        + "<div>"
		        + "<span style=\"height:125px;\">手工录入：</span>﻿﻿﻿﻿﻿﻿<textarea id=\"CONTENT\" name=\"FIELD_CONTENT\" rows=\"8\" cols=\"65\"></textarea>"
		        + "</div>"
		        + "<div style=\"float:right;margin-right:25px;\"><input type=\"button\" class=\"input_btn right\" id=\"portletInfoForm_ok\"  value=\"确定\" /></div>"
		        + "</div>"
		        + "</div>"
			    + "</form>",
			        onComplete : function() {
			        	// 校验
//			        	$("#portletInfoForm").validateFormBlur();
			        	renderDATAFORM(v_data[0].rowSet[0],$("#portlet_content_row"));
			        	setFocus($("#NAME"));
			        	
			        	$("#portletInfoForm_ok").click(function() {
			        		if(!($("#standardInfoForm").validateSubmit())) return;
			        		var data = jwAppPortletModel.jwAppsPortletActAction.updatePTemplate($("#portletInfoForm").getFormVal());
			        		fancyAlert(data[0].VALUE,1);
        		        	jQuery.fancybox.close();
        		        	loadPage(local_pageNo);
			        	});
			        	
			        	
			        	
			        }
		});
    };
    
    
    var portletInsert = function(){
    	jQuery.fancybox({
			"modal" : false,
			"content" : ""
			        + "<div class=\"popup_header_icon_450\"><div class=\"popup_header_text\">门户插件增加</div></div>"
			        + "<form id=\"portletInfoForm\" action=\"\">"
			        + "<input type=\"hidden\" id=\"FIELD_tableName\" name=\"FIELD_PORTLET_ID\"/>"
			        + "<div style=\"width:450px;\">"
			        + "<div>"
			        + "<span style=\"line-height:30px;height:33px;\">插件名称：</span><input type=\"text\" id=\"FIELD_NAME\"  name=\"FIELD_NAME\" validate=\"required,maxlength\" maxlengthvalue=\"10\"/>"
			        + "<span style=\"line-height:30px;height:33px;margin-left:5px;\">插件主题：</span><input type=\"text\" id=\"FIELD_TITLE\" name=\"FIELD_TITLE\"  validate=\"required,maxlength\" maxlengthvalue=\"50\"/>"
			        + "</div>"
			        + "<div>"
			        + "<span style=\"line-height:30px;height:33px;\">数据来源：</span>"
			        + "<select id=\"P_TYPE\" name=\"FIELD_P_TYPE\" style=\"width:130px;\" validate=\"required\">"
			        + "<option value=\"\">请选择</option>"
			        + "<option value=\"0\">手工录入</option>"
			        + "<option value=\"1\">集成方Portlet</option>"
			        + "<option value=\"2\">URL抓取</option>"
			        + "<option value=\"3\">RSS订阅</option>"
			        + "</select>"
			        + "<span style=\"line-height:30px;height:33px;margin-left:5px;\">插件图片：</span><input type=\"text\" id=\"ICON\" name=\"FIELD_ICON\"/>"
			        + "</div>"
			        + "<div>"
			        + "<span style=\"line-height:30px;height:33px;\">插件  URL：</span><input type=\"text\" id=\"URL\"  name=\"FIELD_URL\" style=\"width:325px;\"/>"
			        + "</div>"
			        + "<div>"
			        + "<span style=\"line-height:30px;height:33px;\">开始取值：</span><input type=\"text\" id=\"TAKE_START\"  name=\"FIELD_TAKE_START\" />"
			        + "<span style=\"line-height:30px;height:33px;margin-left:5px;\">结束取值：</span><input type=\"text\" id=\"TAKE_END\" name=\"FIELD_TAKE_END\"/>"
			        + "</div>"
			        + "<div>"
			        + "<span style=\"line-height:30px;height:33px;\">取值编码：</span><input type=\"text\" id=\"URL_ENCODED\" name=\"FIELD_URL_ENCODED\"/>"
			        + "<span style=\"line-height:30px;height:33px;margin-left:5px;\">点击动作：</span>"
			        + "<select id=\"IS_POP\" name=\"FIELD_IS_POP\" style=\"width:130px;\" validate=\"required\">"
			        + "<option value=\"\">请选择</option>"
			        + "<option value=\"0\">弹出层</option>"
			        + "<option value=\"1\">页面跳转</option>"
			        + "</select>"
			        + "</div>"
			        + "<div>"
			        + "<span style=\"height:125px;\">手工录入：</span>﻿﻿﻿﻿﻿﻿<textarea id=\"FIELD_CONTENT\" name=\"FIELD_CONTENT\" rows=\"8\" cols=\"65\"></textarea>"
			        + "</div>"
			        + "<div style=\"float:right;margin-right:25px;\"><input type=\"button\" class=\"input_btn right\" id=\"portletInfoForm_ok\"  value=\"确定\" /></div>"
			        + "</div>"
			        + "</form>",
			        onComplete : function() {
			        	// 校验
//			        	$("#portletInfoForm").validateFormBlur();
			        	setFocus($("#FIELD_NAME"));
			        	var v_model =  "<div class=\"portlet\">"
                                     + "<div class=\"portlet-header\"></div>"
                                     + "<div class=\"portlet-content\"></div>"
                                     + "</div>";
			        	$("#FIELD_TEMPLATE").val(v_model);
			        	$("#portletInfoForm_ok").click(function() {
			        		if(!($("#portletInfoForm").validateSubmit())) return;
			        		var data = jwAppPortletModel.jwAppsPortletActAction.insertPTemplate($("#portletInfoForm").getFormVal());
			        		if(data[0].NAME.indexOf("-")>-1){
			        			fancyAlert(data[0].VALUE,0);
			        		}else{
			        			fancyAlert(data[0].VALUE,1);
			        		}
        		        	jQuery.fancybox.close();
        		        	loadPage(local_pageNo);
			        	});
			        	
			        	
			        	
			        }
		});
    };