define('channel', function(require, exports) {
	var swipe = require("plugins/swipe"),
		swiper = require("plugins/idangerous.swiper"),
		$ = require("jquery"),
		freetile = require("lib/ui/jquery.freetile"),
		share = require('plugins/share');
	require("plugins/pagination");
	require("plugins/imgZoom");
	require("plugins/slider");

	function setLayout() {
		$('.fluid-list-inner').freetile({
			animate: false,
			elementDelay: 0
		});
	};

	function getTopProduct(gender) {
		$.ajax({
			type: "post",
			url: '/default/getTopProduct',
			data: {
				gender: gender
			},
			dataType: "json",
			success: function(data) {
				if (data.code == 200) {
					var html = '';
					var pos = 1;
					var number_class = '';
					var number = '';
					$.each(data.data, function(key, val) {
						if (pos == 1) {
							number_class = 'number_1';
							number = '';
						} else {
							number = pos;
							number_class = 'number_2';
						}
						html += '<li><div class="goods-image">' +
							'<img src="' + val.product_img + '" alt=""/>' +
							'<span class="' + number_class + '">' + number + '</span></div>' +
							'<h3><a  target="_blank" href="' + val.product_url + '">' + val.product_name + '</a></h3><p>人气: ' + val.sale_sum + '</p></li>';
						pos++;
					});
					$(".show-goods-list").html('<ul>' + html + '</ul>');
				}
			}
		});
	};
	exports.common = function() {
		var goodsSwiper,
			limitedPrev,
			limitedNext,
			swiperLen,
			shareUrl,
			shareCover,
			detailTitle,
			detailSubtitle;


		//导航动画效果
		$('.nav').on('click', 'li', function() {
			$(this).addClass('current');
		});

		$('.nav').on('mouseover', 'li', function() {
			if (!$(this).hasClass('current')) {
				$(this).addClass('animate')
			}
		}).on('mouseout', 'li', function() {
			$('.nav').find('li').removeClass('animate');
		});

		//大banner滑动
		swipe.init({
			slideBox: '.slide-box',
			prev: '.slide-prev',
			next: '.slide-next',
			pagination: '.dib a',
			activeClass: 'on',
			auto: 3000,
			continuous: true
		});

		//限量商品滑动
		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');
		swiperLen = $('.limited-slide-box').find('.box').size();
		limitedPrev.addClass('disable');
		limitedNext.on('click', function() {
			limitedPrev.removeClass('disable');
			if (goodsSwiper.getPos() === swiperLen - 1) {
				$(this).addClass('disable');
			};
		});
		limitedPrev.on('click', function() {
			limitedNext.removeClass('disable');
			if (goodsSwiper.getPos() === 0) {
				$(this).addClass('disable');
			};
		});

		//tab
		$('.content-tab').on('click', 'li', function() {
			var nowIndex = $(this).index();
			$(this).addClass('current').siblings().removeClass('current');
			$('.content-main').hide().eq(nowIndex).show();
		});

		//回到顶部
		$(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;
		});

		//使用瀑布流布局
		setLayout();

		if ($('.share').size() > 0) {
			//微信分享鼠标事件
			$('.yohoo-share-button-wx').on('mouseover', function() {
				$('.wx-qrcode-dialog').show();
			}).on('mouseout', function() {
				$('.wx-qrcode-dialog').hide();
			});

			shareUrl = $(".share").attr("data-link");
			shareCover = $(".share").attr("cover-url");
			detailTitle = $('.detail-title').find('h2');
			detailSubtitle = $('.detail-title').find('h3');
			//分享
			share.init({
				shareUrl: shareUrl,
				img: shareCover,
				sinaText: '【YOHOOD 2015】' + detailTitle.text() + '\r\n' + detailSubtitle.text() + '@YOHO潮流志',
				fbTextFunction: function() {
					return {
						des: '【YOHOOD 2015】' + detailTitle.text() + '\r\n' + detailSubtitle.text(),
						name: '【YOHOOD 2015】' + detailTitle.text() + '\r\n' + detailSubtitle.text(),
						caption: '【YOHOOD 2015】' + detailTitle.text() + '\r\n' + detailSubtitle.text(),
						url: location.href
					}
				},
				tweetText: '【YOHOOD 2015】' + detailTitle.text() + '\r\n' + detailSubtitle.text(),
				qqText: '【YOHOOD 2015】' + detailTitle.text() + ' ' + detailSubtitle.text() + '#YOHOOD 2015'
			});
		}


	};

	//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 sortPos = 0;
		var productGenders = [];
		var currentGenderPos = 0;
		var productListNames = [];


		var cooperateSwiper,
			cooperatePrev,
			cooperateNext,
			swiperLen;

		if ($(".show-goods").size() > 0) {
			productGenders = $(".show-goods").attr("product_genders").split(',');
			currentGenderPos = $(".show-goods").attr("current_gender_pos");
			productListNames = $(".show-goods").attr("product_list_names").split(',');
			getTopProduct(productGenders[currentGenderPos]);
		}

		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');
			};
		});
		$(".goods_prev, .goods_next").bind("click", function() {
			currentGenderPos = parseInt($(".show-goods").attr("current_gender_pos"));
			if ($(this).attr("class") == 'goods_prev') {
				genderPos = parseInt(currentGenderPos - 1 >= 0 ? currentGenderPos - 1 : 0);
			} else {
				genderPos = parseInt(currentGenderPos + 1 < productGenders.length ? currentGenderPos + 1 : currentGenderPos);
			}
			if (genderPos != currentGenderPos) {
				$(".show-goods").attr("current_gender_pos", genderPos);
				getTopProduct(productGenders[genderPos]);
				$(".show-goods").find("span").remove();
				$(".show-goods").find("h2").prepend("<span>" + productListNames[genderPos] + "</span>");
			}
		});

	};
	//品牌
	exports.brand = function() {
		$('.brand-list').on('click', 'li', function() {
			if ($(this).find('.brand-introduction').size() > 0) {
				$(this).find('.brand-introduction').show();
				$('.overlay').show();
			}
		});
		$('.overlay').on('click', function() {
			$(this).hide();
			$('.brand-introduction').hide();
		});
	};
	//资讯
	exports.news = function() {
		setLayout();
	};

	//detail页面
	exports.detail = function() {
		var timer,
			nowIndex,
			detailSwiper;

		var relatedTop,
			relatedPost,
			relatedH,
			maxH;
		//详情页图片滚动
		if ($('.detail-slide').size() > 0) {
			//slide大图上的左右箭头显示
			$('.slide-shadow').on('mouseover', 'a', function() {
				if ($(this).hasClass('slide-shadow-prev')) {
					$('.slide-big-ctrl-prev').show();
				} else {
					$('.slide-big-ctrl-next').show();
				}
			}).on('mouseout', 'a', function() {
				$('.slide-big-ctrl-prev, .slide-big-ctrl-next').hide();
			});
			$('.detail-slide-body').on('mouseout', function() {
				$('.slide-big-ctrl-prev, .slide-big-ctrl-next').hide();
			});
			$('.detail-slide-big-ctrl').on('mouseenter', 'a', function() {
				$(this).show();
			});

			detailSwiper = new swiper('.detail-slide-body', {
				wrapperClass: 'detail-slide-piclist',
				slideClass: 'box',
				loop: true,
				slidesPerView: 'auto',
				slideElement: 'li'
			});

			$('.slide-big-ctrl-prev').bind('click', function(e) {
				e.preventDefault();
				detailSwiper.swipePrev();
			});
			$('.slide-big-ctrl-next').bind('click', function(e) {
				e.preventDefault();
				detailSwiper.swipeNext();
			});
			$('.detail-slide-ctrl-tabs').slider();
			$('.detail-slide-big-ctrl').on('click', 'a', function() {
				$('.detail-slide-ctrl-tabs').slider('slideTo', detailSwiper.activeLoopIndex);
			});
			$('.detail-slide-ctrl-tabs').on('click', 'li', function() {
				detailSwiper.swipeTo($(this).index());
			});
		};

		//侧栏推荐切换
		clearInterval(timer);

		function postSwitch() {
			$('.side-related-tab').find('a').each(function(i) {
				if ($(this).hasClass('current')) {
					$(this).removeClass('current');
				} else {
					$(this).addClass('current');
					$('.side-related-list').hide().eq(i).show();
				}
			});
		}
		timer = setInterval(function() {
			postSwitch();
		}, 5000);
		$('.side-related-post').on('mouseenter', function() {
			clearInterval(timer);
		}).on('mouseleave', function() {
			timer = setInterval(function() {
				postSwitch();
			}, 5000);
		})
		$('.side-related-tab').on('click', 'a', function() {
			nowIndex = $(this).index();
			if ($(this).hasClass('current')) return;
			$(this).addClass('current').siblings().removeClass('current');
			$('.side-related-list').hide().eq(nowIndex).show();
		});

		//右侧相关文章定位
		relatedPost = $('.side-related-post');
		//relatedPost.addClass('fix');
		relatedTop = relatedPost.offset().top;
		relatedH = relatedPost.outerHeight();
		maxH = $('.limited-goods').offset().top;
		$(window).on('scroll', function() {
			console.log($(this).scrollTop(), relatedTop);
			if ($(this).scrollTop() >= relatedTop) {
				//relatedPost.addClass('fix');
				relatedPost.css({
					position: 'fixed',
					top: 0
				});
				/*if (relatedPost.offset().top + relatedH >= maxH && relatedPost.hasClass('fix')) {
					relatedPost.css({
						'position': 'absolute',
						'top': maxH - relatedH
					})
				}
				if ($(this).scrollTop() <= maxH - relatedH) {
					relatedPost.css({
						'position': 'fixed',
						'top': 0
					});
				}*/
				//relatedPost.addClass('fix');

			} else {
				relatedPost.removeClass('fix').removeAttr('style');
				//relatedPost.removeClass('fix');
			}
		});

		//分享按钮位置
		var shareOffsetTop = $(".share").offset().top;
		$(window).scroll(function() {
			scrollTop = $(window).scrollTop();
			if (shareOffsetTop - scrollTop <= 0) {
				$(".share").css({
					"position": "fixed",
					"top": 10
				});
			} else {
				$(".share").css({
					"position": "absolute",
					"top": shareOffsetTop
				});
			}
		});
	};

	exports.siteActivity = function() {
		$('.site-activity-content').find('.layout-item').each(function() {
			$(this).imgZoom({
				imgTag: 'pic-tip'
			});
		});

		//活动日期定位
		var tabTop = $('.site-activity-tab').offset().top - 10,
			oTab = $('.site-activity-tab'),
			mainBodyOffsetTop,
			mainBodyHeight,
			tabOffsetHeightTop,
			distance,
			eleTop,
			tabPositionTop;
		$(window).on('scroll', function() {
			if (tabTop <= $(this).scrollTop()) {
				$('.site-activity-tab').css({
					"position": "fixed",
					"top": "0",
					"left": "50%",
					"width": "134px",
					"marginLeft": "356px",
					"marginTop": "10px"
				})


				mainBodyOffsetTop = $('.bd').offset().top;
				mainBodyHeight = $('.bd').outerHeight();
				tabOffsetHeightTop = oTab.offset().top + oTab.outerHeight();
				distance = mainBodyHeight + mainBodyOffsetTop;
				eleTop = distance - oTab.outerHeight();
				tabPositionTop = 10;

				if (tabOffsetHeightTop >= distance) {
					oTab.css({
						'position': 'absolute',
						'top': eleTop - tabPositionTop - 10
					});
				}
				if (oTab.offset().top - $(window).scrollTop() > tabPositionTop) {
					oTab.css({
						'position': 'fixed',
						'top': tabPositionTop
					});
				}
			} else {
				$('.site-activity-tab').removeAttr('style');
			};
		});
	};

	//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();
		});
		$(".ui-dialog-close").bind("click", function(event) {
			$(".model").hide();
			event.stopPropagation();
		});
		$('.show-wrap').find('.layout-item').each(function() {
			$(this).imgZoom({
				imgTag: 'pic-tip'
			});
		});

	}
});