﻿function lunTopFn(obj,time){
	time = time ? time: 30;
	var len = ($(obj).children('ul').height()/2) + 100;
	var times = null;
	if($(obj)[0]){
		times = window.setInterval(function(){
			var t = $(obj).children('ul').css('top');
			t = t.replace('px','');
			if(t > -len){
				t--;
				$(obj).children('ul').css({top: t})
			}else{
				$(obj).children('ul').css({top: 10})
			}
		},time)
		$(obj).children('ul').hover(function(){
			window.clearInterval(times);
		},function(){
			times = window.setInterval(function(){
				var t = $(obj).children('ul').css('top');
				t = t.replace('px','');
				if(t > -len){
					t--;
					$(obj).children('ul').css({top: t})
				}else{
					$(obj).children('ul').css({top: 0})
				}
			},time)
		})
	}
}
//加载公告
function loadAnnounce(callback) {
	callback();
}
/**
 * 抢红包
 */
var sid = 0,stitle,sbeginDatetime,sendDatetime;
var redBagUtil = {
	   flag : false,
	   initRedBag : function(){
    	   if(this.flag){
    		   $("#hb-message").html("暂无红包活动!");
    		   	swal({
				  title: "暂无红包活动，请及时关注！",
				  confirmButtonColor: "#e22000",
				  confirmButtonText: "确定", 
				  html: true 
				});
    		   return;
    	   }
    	   flag = true;
        	$.ajax({
        		url : base + "/userCenter/redpacket/curNew.do",
        		dataType  : "json",
        		success : function(j){
        			if(j && j.id){
        				sid = j.id;
        				stitle = j.title;
        				sbeginDatetime = j.beginDatetime;
        				sendDatetime = j.endDatetime;
        				$('#red-bag-title').html(stitle);
        				if(nowTimeStamp < new Date(sbeginDatetime).getTime()){
        					sbeginDatetime = nowTime;
        					sendDatetime = j.beginDatetime;
        					$('#redBagDescTitle').html('离活动开始还剩');
        				}
        				$(".count_down").countDown({
        					startTimeStr:sbeginDatetime,//开始时间
        		        	endTimeStr:sendDatetime,//结束时间
        		        	daySelector:".day_num",
        		            hourSelector:".hour_num",
        		            minSelector:".min_num",
        		            secSelector:".sec_num"
        				});
        				redBagUtil.getRedBagRecord(sid);
        			}else{
        				$("#hb-message").html("暂无红包活动!");
        				swal({ 
        					  title: '暂无红包活动，请及时关注！', 
        					  showCancelButton: false, 
        					  confirmButtonColor: '#d63030',
        					  confirmButtonText: '确定', 
        					}).then(function(){
        					  location.href= base + '/index.do';
        					})
        			}
        		}
        	})
        },
        getPacket : function(){
        	if(sid == 0){
				swal({ 
					  title: '暂无红包活动，请及时关注！', 
					  showCancelButton: false, 
					  confirmButtonColor: '#d63030',
					  confirmButtonText: '确定', 
					}).then(function(){
					  location.href= base + '/index.do';
					})
        		return;
        	}
        	$.ajax({
        		url : base + "/userCenter/redpacket/grab.do",
        		dataType  : "json",
        		data : {
        			redPacketId : sid
        		},
        		success : function(j){
        			if(j.msg){
      					 swal({
        					  title: j.msg, 
        					  showCancelButton: false, 
        					  confirmButtonColor: '#d63030',
        					  confirmButtonText: '确定', 
        					}).then(function(){
        					  location.href= base + '/index.do';
        					})
        			}else{
        				sweetAlert(
        						  "恭喜您",
        						  "获得红包 <font class='red-bag-font-size20'>"+j+"</font> 元！"
        						)
        			}
        		}
        	})
        },
        getRedBagRecord : function(sid){
        	$.ajax({
        		url : base + "/userCenter/redpacket/record.do",
        		dataType  : "json",
        		data : {
        			redPacketId : sid,
					limit:5000,
        		},
        		success : function(j){
        			if(!j){
        				return;
        			}
        			var html = template('red_bag_recore_tpl', {list: j});
    				$('#announce').html(html);
    				 loadAnnounce(function() {
	    			 	lunTopFn('.luntop2',120);
	    			 });
        		}
        	})
        }
}

$(document).ready(function(){
     $(".cha").click(function(){
        $(".kef").hide();
    });
    $(".chag").click(function(){
        $(".gonggao").hide();
    });
	$(".xiao").click(function(){
        $(".gonggao").toggleClass("cur");
    });

    $(window).scroll(function(){
        var sc = $(window).scrollTop();
        $('.kef').stop().animate({
            top: 80+sc
        },150)
        
    })


});