var isClick = common.isMove, page = 1, bOk2 = true,

	// 数据交互
	data = {
		popularity: function()
		{
		    //  人气数据
		    condition = 1;
		    page = 1;
		    //$(".commodity_list_box").empty();
		    GetJsonData(0, 1, "", 2, 1, 0, true);
		    page = 2;
		},

		news: function()
		{
		    // 最新数据
		    condition = 2;
		    page = 1;
		    //$(".commodity_list_box").empty();
		    GetJsonData(0, 2, "", 2, 1, 0, true);
		    page = 2;
		},

		status: function()
		{
		    // 进度数据
		    condition = 3;
		    page = 1;
		   // $(".commodity_list_box").empty();
		    GetJsonData(0, 3, "", 2, 1, 0, true);
		    page = 2;
		},

		minimumReq: function()
		{
		    // 最低需求
		    condition = 4;
		    //$(".commodity_list_box").empty();
		    GetJsonData(0, 4, "", 1, 1, 0, true);
		},

		highestReq: function()
		{
		    // 最高需求
		    condition = 4;
		    //$(".commodity_list_box").empty();
		    GetJsonData(0, 4, "", 2, 1, 0, true);
		},

		defualt: function()
		{
			// 默认加载
			this.popularity();
		},

		bannerData: function()
		{
			// banner
			common.swiper(); //放到成功函数里执行
		}
	},

	// ui 交互
	interactive = {
		init: function(){
			this.broadcast();
			this.switchList();
			data.defualt();
			data.bannerData();
			this.returnTop();
		},

		// 广播
		broadcast: function () {
		    $('.broadcast ul').html($('.broadcast ul').html() + $('.broadcast ul').html());
		    var distance = 0, timer = null;
		    clearInterval(timer);

		    $('.broadcast ul').css(
			{
			    'transition': 'all .3s ease',
			    'transition': 'all .3s ease',
			});
		    timer = setInterval(function () {
		        var offset = $('.broadcast_list_box li')[0].offsetHeight;
		        distance++;

		        $('.broadcast ul').css(
				{
				    'transition': 'all .3s ease',
				    'transition': 'all .3s ease',
				    'transform': 'translate(0,' + -distance * offset + 'px)',
				    '-webkit-transform': 'translate(0,' + -distance * offset + 'px)'
				});

		        if (distance * offset >= $('.broadcast ul')[0].offsetHeight / 2) {
		            distance = 0;
		            $('.broadcast ul').on('transitionend', function () {
		                $('.broadcast ul').css({
		                    'transition': 'none',
		                    'transition': 'none',
		                    'transform': 'translate(0,' + 0 + 'px)',
		                    '-webkit-transform': 'translate(0,' + 0 + 'px)',
		                });
		                $('.broadcast ul').off('transitionend');
		            });
		        }

		    }, 3000);
		},

		// 菜单栏吸顶
		menuBarScroll: function()
		{
			var menuTop = $('.select_list_box').offset().top;
			
		    // 判断是否吸顶
			function isTop() {
			    if ($(window).scrollTop() >= menuTop - $('.nav_bar')[0].offsetHeight) {
			        $('.select_list_box').addClass('fixed');
			        if (!$('.return_top').hasClass('show')) {
			            $('.return_top').addClass('show');
			        }
			    }
			    else {
			        $('.select_list_box').removeClass('fixed');
			        $('.return_top').removeClass('show');
			    }
			}

			// 重新计算
			function setOpa(){
				// setTimeout(function(){
					isTop();
				// },30);
			}

			// $(document).on('touchmove',function(){
			// 	isTop();
			// });

			$(window).on('scroll',function(){
				setOpa();
			});
		},

		switchList: function()
		{
			var iNum = 0;
			isClick($('.select_list li'),function(_this){
				$('.select_list .slide_bar').css({
				    '-webkit-transform': 'translate3d(' + $(_this).index() * $(_this)[0].offsetWidth + 'px,0,0)',
				    'transform': 'translate3d(' + $(_this).index() * $(_this)[0].offsetWidth + 'px,0,0)'
				});

				// 加载对应数据即可
				switch($(_this).text()) 
				{
					case '人气':
						data.popularity();
					break;
					case '最新':
						data.news();
					break;
					case '进度':
						data.status();
					break;
					case '总需人次':
						if(iNum > 0)
						{
							if($(_this).hasClass('minimum'))
							{
								// 最高人次
								$(_this).removeClass('minimum').addClass('highest');
								data.highestReq();
							}
							else if($(_this).hasClass('highest'))
							{
								// 最低人次
								$(_this).removeClass('highest').addClass('minimum');
								data.minimumReq();
							}
						}
						else if(iNum == 0)
						{
							data.highestReq();
						}
						iNum++;
					break;
				};
			});
		},

		returnTop: function () {
		    var timer = null;
		    common.isMove($('.return_top'), function () {
		        clearInterval(timer);
		        var count = Math.floor(1000 / 30);
		        var start = document.documentElement.scrollTop || document.body.scrollTop;
		        var dis = 0 - start;
		        var n = 0;
		        timer = setInterval(function () {
		            bSys = false;
		            n++;

		            var a = 1 - n / count;
		            var cur = start + dis * (1 - Math.pow(a, 3));

		            document.documentElement.scrollTop = document.body.scrollTop = cur;

		            if (n == count) {
		                clearInterval(timer);
		            }
		        }, 30);
		    });
		}
	};

interactive.init();

window.onload = function()
{
	interactive.menuBarScroll();
}

//请求服务器
function GetJsonData(categoryid, condition, sname, order, page, isresult,isclick) {
    $.get("/Order/List", { "categoryid": categoryid, "condition": condition, "sname": sname, "order": order, "page": page, "isresult": isresult },
        function (data) {
            if (data=="") {
                $(".loading_list").text("已经到底");
            } else {
                if (isclick) {
                    $(".commodity_list_box").empty();
                    $(".commodity_list_box").append(data);
                } else {
                    $(".commodity_list_box").append(data);
                }
                //延时加载
                $("ul[page=page" + page + "]").find('img').lazyload({
                    effect: "fadeIn",
                    threshold: 50
                });
                bOk2 = true;
            }
        });
}