/* 
* @Author: anchen
* @Date:   2017-01-19 16:51:52
* @Last Modified by:   yubing
* @Last Modified time: 2017-02-09
*/
var contextPath="";
var visitPrefix="";
var currentPage=1;
var currentType="";
var userId="";

//var itemsData={    "items": [        {            "id": 1,            "type": "1",            "isTop": "1",            "title": "1",            "coverImageUrl": "",            "createTime": "2017-02-09"        },        {            "id": 2,            "type": "1",            "isTop": "0",            "title": "2",            "coverImageUrl": "",            "createTime": "2017-02-09"        },        {            "id": 3,            "type": "1",            "isTop": "0",            "title": "3",            "coverImageUrl": "",            "createTime": "2017-02-09"        },        {            "id": 4,            "type": "1",            "isTop": "0",            "title": "4",            "coverImageUrl": "",            "createTime": "2017-02-09"        },        {            "id": 5,            "type": "1",            "isTop": "0",            "title": "5",            "coverImageUrl": "",            "createTime": "2017-02-09"        },        {            "id": 6,            "type": "1",            "isTop": "0",            "title": "6",            "coverImageUrl": "",            "createTime": "2017-02-09"        },        {            "id": 7,            "type": "1",            "isTop": "0",            "title": "7",            "coverImageUrl": "",            "createTime": "2017-02-09"        },        {            "id": 8,            "type": "1",            "isTop": "0",            "title": "8",            "coverImageUrl": "",            "createTime": "2017-02-09"        },        {            "id": 9,            "type": "1",            "isTop": "0",            "title": "9",            "coverImageUrl": "",            "createTime": "2017-02-09"        },        {            "id": 10,            "type": "1",            "isTop": "0",            "title": "10",            "coverImageUrl": "",            "createTime": "2017-02-09"        },        {            "id": 11,            "type": "1",            "isTop": "0",            "title": "11",            "coverImageUrl": "",            "createTime": "2017-02-09"        },        {            "id": 12,            "type": "1",            "isTop": "0",            "title": "12",            "coverImageUrl": "",            "createTime": "2017-02-09"        }    ]};

$(document).ready(function(){
	contextPath=$("#contextPath").val();
	userId=$("#userId").val();
	visitPrefix=$("#visitPrefix").val();
	
	//初始化导航栏
	var swiper = new Swiper('.swiper-container', {
        loop: false,
        direction: "horizontal",
        slidesPerView: 'auto'
    });
    $(".nav-type").click(function(){
        var _this = $(this);
        _this.addClass("nav-sel").siblings(".nav-type").removeClass("nav-sel");
        query("0",_this.attr("ctype"), userId);
    })
	
    //初始化刷新控件
    refresher.init({
	    id:"wrapper",
	    pullDownAction:Refresh,                                                            
	    pullUpAction:Load                                                                           
    });                                                                                                                                                         
    //下拉刷新
	function Refresh() {                                                                
	    setTimeout(function () {    // <-- Simulate network congestion, remove setTimeout from production!
	        var el, li, i;                                                                      
	        el =document.querySelector("#wrapper ul");                  
	        //这里写你的刷新代码  
	        currentPage=1;
	        query("1",currentType, userId);
	        
	        document.getElementById("wrapper").querySelector(".pullDownIcon").style.display="none";     
	        document.getElementById("wrapper").querySelector(".pullDownLabel").innerHTML="<img class='refresh-img' src='"+contextPath+"/plugin/refresher/refresher_images/ok.png'/>刷新成功";                                                                                  
	        setTimeout(function () {
	            //wrapper.refresh();
	            document.getElementById("wrapper").querySelector(".pullDownLabel").innerHTML="";
	            },1000);//模拟qq下拉刷新显示成功效果
	        /****remember to refresh after  action completed！ ---yourId.refresh(); ----| ****/
	    }, 1000);
	}
	//上拉加载
	function Load() {
	    setTimeout(function () {// <-- Simulate network congestion, remove setTimeout from production!
	        var el, li, i;
	        query("2",currentType, userId);
	        //wrapper.refresh();/****remember to refresh after action completed！！！   ---id.refresh(); --- ****/
	    },2000);    
	}
	
	//搜索按钮点击
	$(".search-icon").click(function(){
		currentPage=1;
		query("1",currentType,userId);
	});
	
	//默认加载
	query("0","",userId);
});

/**
 * @param actType   操作类型，默认0，上拉刷新1，下拉加载2
 * @param ctype     通知公告类型
 * @param userId    用户ID
 */
function query(actType,ctype,userId){
	if(ctype!=currentType){
		currentPage=1;
	}
	var keyWord=$("#keyWord").val(); //关键字
	$.ajax({
		url :contextPath+"/wechat/announcement/getAnnoList",
		type : "POST",
		data : {ctype:ctype,userId:userId,currentPage:currentPage,keyWord:keyWord},
		dataType:"json",
		success : function(data) {
			var ulcontainer=$('#ulcontainer');
			if(ctype!=currentType||"1"==actType){
				ulcontainer.html("");
				currentType=ctype;
			}
			var items=data.items;
			if(items.length>0){
				currentPage=currentPage+1;
			}
			var strHTML="";
			$.each(items,function(index,item){
				var type=item.type;
				var isTop=item.isTop;
				var title=item.title;
				var coverImageUrl=item.coverImageUrl;
				var createTime=item.createTime;
				var id=item.id;
				
				var detailsUrl=contextPath+"/wechat/info/getDetail/1&"+id+"&"+userId;
				var labelHTML="";
				if(isTop=="1"){
					labelHTML+="<span class='black'>置顶</span>";
				}
				var typeName=$("#t_"+type).val();
				if(typeName!=""){
					if(type=="3"){
						labelHTML+="<span class='red'>"+typeName+"</span>";
					}
					else if(type=="2"){
						labelHTML+="<span class='blue'>"+typeName+"</span>";
					}
					else if(type=="1"){
						labelHTML+="<span class='green'>"+typeName+"</span>";
					}
					else{
						labelHTML+="<span class='yellow'>"+typeName+"</span>";
					}
				}
				if(coverImageUrl!=""&&coverImageUrl!=null){
					coverImageUrl=visitPrefix+coverImageUrl;
				}
				else{
					coverImageUrl=contextPath+"/images/common/defalut_notice.png";
				}
				strHTML+="<li><a href='"+detailsUrl+"' class='clearfix'><div class='tipLeft'>"
					   +"<div class='line'>"
					   +labelHTML
					   +title
					   +"</div>"
					   +"<p class='titleTime'>"
					   +"<span>2016年12月12日 17:20</span>"
					   +"</p></div>"
					   +"<div class='tipRight'>"
					   +"<img src='"+coverImageUrl+"' alt=''>"
					   +"</div></a></li>";
			});
			ulcontainer.append(strHTML);
			
			wrapper.refresh();
		},
		error : function(data) {
			// alert("系统繁忙，请稍后再试.");
		}
	});
}