yohood.js 6.3 KB
define('yohood', function(require, exports) {
	var swipe = require("plugins/swipe");
	var $ = require("jquery");
	var freetile = require("lib/ui/jquery.freetile");
	var goodsSwiper;
	require("plugins/pagination");
	//设置瀑布流
	function setFlowLayout() {
		$('.fluid-list-inner').freetile({
			animate: false,
			elementDelay: 0
		});
	}
	
	//设置商品滚动条
	function setGoodsSwiper() {
		var limitedPrev,
		limitedNext,
		swiperLen;
		//限量商品滑动
		goodsSwiper = swipe.init({
			slideBox: '.limited-slide-box',
			prev: '.limited-slide-prev',
			next: '.limited-slide-next',
			auto: false,
		});
		limitedPrev = $('.limited-slide-prev');
		limitedNext = $('.limited-slide-next');
		limitedPrev.addClass('disable');
		limitedNext.on('click', function() {
			limitedPrev.removeClass('disable');
			swiperLen = $('.limited-slide-box').find('.box').size();
			if (goodsSwiper.getPos() === swiperLen - 1) {
			   $(this).addClass('disable');
			};
		});
		limitedPrev.on('click', function() {
			limitedNext.removeClass('disable');
			if (goodsSwiper.getPos() === 0) {
				$(this).addClass('disable');
			};
		});
	}
	//获取推荐商品
	function getRecommendProduct(obj,  limit) {
		var template = '<div class="box"><ul class="goods-list clearfix">{li}</ul></div>';
		var offset = parseInt(obj.attr("offset"));
		var temp = '';
		var html = '';
		$.ajax
		({
			url:"/product/getrecommend",
			type:"post",
			data:{offset:offset, limit:limit},
			success:function(data)
			{
				var product = data.data, pic = '', pos = offset , part, topClass = '';
				if(data.code == 200 && product !="")
				{
					$.each(product, function(i, v)
					{
						pic = v.pic.split('|')[0].replace(/-(\w+)-/g, '-0150x0200-');
						parts = v.price.split('.');
						topClass = pos>2 ? '':'red';
						html +='<li><a href="'+v.url+'" target="_blank" title="'+v.name+'">' +
	                    '<div class="img-box"><img src="'+ pic +'" alt=""><span class="'+topClass+'">TOP '+(++pos)+'</span></div>'+
	                    '<p class="goods-name">'+v.name+'</p>'+
	                    '<p class="price">¥'+( parts[0] + '.'+ ( parts[1] ? parts[1] : '00' ) )+'</p></a></li>';
						if(pos %5 == 0)
						{
							temp += template.replace('{li}', html);
							html = '';
						}
					});
					if(pos % 5 != 0 )
					{
						temp += template.replace('{li}', html);
					}
					obj.append(temp);
					obj.attr("offset", pos);
					if(offset<=0)
					{
						setGoodsSwiper();
					}
					else
					{
						//重新绘画
						goodsSwiper.setup();
						$('.limited-slide-next').removeClass('disable');
					}
				}
			}
		});
		return html;
	}
	
    //yohood展会页面
	exports.exhibition = function() {
			swipe.init({
				slideBox: '.slide-box-exhibition',
				prev: '.slide-prev-exhibition',
				next: '.slide-next-exhibition',
				pagination: '.dib a',
				activeClass: 'on',
				auto: 3000,
				continuous: true
			});
		}
    //首页
	exports.index = function() {
		var cooperateSwiper,
			cooperatePrev,
			cooperateNext;
		//大banner滑动
		swipe.init({
			slideBox: '.slide-box',
			prev: '.slide-prev',
			next: '.slide-next',
			pagination: '.dib a',
			activeClass: 'on',
			auto: 3000,
			continuous: true
		});
		cooperateSwiper = swipe.init({
			slideBox: '.cooperation-slide-box',
			prev: '.cooperation-slide-prev',
			next: '.cooperation-slide-next',
			auto: false
		});
		cooperatePrev = $('.cooperation-slide-prev');
		cooperateNext = $('.cooperation-slide-next');
		swiperLen = $('.cooperation-slide-box').find('.box').size();
		cooperatePrev.addClass('disable');
		cooperateNext.on('click', function() {
			cooperatePrev.removeClass('disable');
			if (cooperateSwiper.getPos() === swiperLen - 1) {
				$(this).addClass('disable');
			};
		});
		cooperatePrev.on('click', function() {
			cooperateNext.removeClass('disable');
			if (cooperateSwiper.getPos() === 0) {
				$(this).addClass('disable');
			};
		});
		setFlowLayout();

	};
	//资讯
	exports.news = function() {
		setFlowLayout();
	}

	//show页面
	exports.show = function() {
		$(".look_big_image").bind("click", function(event) {
			var divName = '#mask';
			var sourceImage = $(this).attr("source");
			var imageWidth = parseInt($(divName).children().attr("width"));
			var imageHeight = parseInt($(divName).children().attr("height"));
			$(divName).html('<img src="' + sourceImage + '" width="' + imageWidth + '" height="' + imageHeight + '" />').attr("current_class", $(this).parent().attr("class"));
			var top = ($(window).height() - imageHeight) / 2;
			var left = ($(window).width() - imageWidth) / 2;
			var scrollTop = 0; //$(document).scrollTop();   
			var scrollLeft = 0; //$(document).scrollLeft();  
			$(divName).css({
				position: 'absolute',
				'top': top + scrollTop,
				left: left + scrollLeft
			});
			$(".model").show();
			if (left - 75 > 10) {
				$(".image-prev").css("left", left - 75);
				$(".image-next").css("right", left - 75);
			}
			$.each($(this).parents("span").children("span"), function(index, dom) {
				var style_top = parseInt($(dom).attr("x")) / 10000 * imageHeight;
				var style_left = parseInt($(dom).attr("y")) / 10000 * imageWidth;
				$(divName).append('<a class="pic-tip" style="left:' + style_left + 'px;top:' + style_top + 'px;">' + $(dom).attr("label") + '</a>')
			});
			event.stopPropagation();
		});
		$(".image-next, .image-prev").bind("click", function() {
			var current_class = $("#mask").attr("current_class");
			var pos = parseInt(current_class.replace('single_', ''));
			if ($(this).attr("class").indexOf("image-next") != -1) {
				pos = pos + 1;
			} else {
				pos = pos - 1;
			}
			if ($(".single_" + pos).size() > 0) {
				$(".single_" + pos).find(".look_big_image").click();
			}
		});
		$(".ui-dialog-close").bind("click", function(event) {
			$(".model").hide();
			event.stopPropagation();
		});
	}
	
	//整个页面布局
	exports.layout = function()
	{
		getRecommendProduct($("#product_list"), 20);
		//点击
		$(".limited-slide-next").bind("click", function()
		{
			getRecommendProduct($("#product_list"), 20);
		});
		//回到顶部
		$(window).scroll(function() {
			if ($(window).scrollTop() > 100) {
				$(".return-top").fadeIn(500);
			} else {
				$(".return-top").fadeOut(500);
			}
		});
		$(".return-top").click(function() {
			$('body,html').animate({
				scrollTop: 0
			}, 500);
			return false;
		});
	}
	
	//活动
	exports.activity = function()
	{
		
	}
});