// 楼层聚合数据高度处理
function FloorResizeHandle()
{
    $('.floor').each(function(k, v)
    {
        var height = $(this).find('.goods-list').height();
        $(this).find('.aggregation').css('height', ((window.innerWidth || $(window).width()) <= 640) ? 'auto' : height - 12 +'px');
    });
}

$(window).on('load', function() {
    FloorResizeHandle();
});

$(function() {
    //倒计时
	var nowTime = new Date(Date.parse($('.counters').attr('sc').replace(/-/g, "/")));
	$('.counters').each(function(){
		var g = $(this);
        var startTime = new Date(Date.parse(g.attr('sv').replace(/-/g, "/")));
        var endTime = new Date(Date.parse(g.attr('ev').replace(/-/g, "/")));
        if(startTime.getTime()<= nowTime && endTime.getTime() >=nowTime){
            var opts = {
	            nowTime: nowTime,
			    endTime: endTime,
			    callback: function(data){
			    	if(data.last>0){
				        g.find('.ctrP .aution_h').html(data.hour);
				        g.find('.ctrP .aution_i').html(data.mini);
				        g.find('.ctrP .aution_s').html(data.sec);
			    	}else{
			    		g.find('.counters').html(' ');
			    	}

			    }
			};
			FloorCountDown(opts);
        }else{
        	g.find('.counters').html(' ');
        }
	})    
    // 新闻轮播
    if((window.innerWidth || $(window).width()) <= 640) {
        function AutoScroll() {
            $('.banner-news').find("ul").animate({
                marginTop: "-30px"
            }, 500, function() {
                $(this).css({
                    marginTop: "0px"
                }).find("li:first").appendTo(this);
            });
        }
        setInterval(function() {
            AutoScroll();
        }, 3000);
    }

    // 浏览器窗口实时事件
    $(window).resize(function() {
        FloorResizeHandle();
    });
});

function FloorCountDown(opts){
	var f = {
		zero: function(n){
			var n = parseInt(n, 10);
			if(n > 0){
				if(n <= 9){
					n = n;
				}
				return String(n);
			}else{
				return "0";
			}
		},
		count: function(){
			if(opts.nowTime){
				var d = new Date();
				d.setTime(opts.nowTime.getTime()+1000);
				opts.nowTime = d;
				d = null;
			}else{
				opts.nowTime = new Date();
			}
			//现在将来秒差值
			var dur = Math.round((opts.endTime.getTime() - opts.nowTime.getTime()) / 1000), pms = {
				sec: "0",
				mini: "0",
				hour: "0",
				day: "0"
			};
			if(dur > 0){
				pms.sec = f.zero(dur % 60);
				pms.mini = Math.floor((dur / 60)) > 0? f.zero(Math.floor((dur / 60)) % 60) : "0";
				pms.hour = Math.floor((dur / 3600)) > 0? f.zero(Math.floor((dur / 3600)) % 24) : "0";
				pms.day = Math.floor((dur / 86400)) > 0? f.zero(Math.floor(dur / 86400)) : "0";
			}
			pms.last = dur;
			pms.nowTime = opts.nowTime;
			opts.callback(pms);
			if(pms.last>0)setTimeout(f.count, 1000);
		}
	};
	f.count();
};