;(function(window, $) {

	// Slider Constructor
	var Slider = function(options) {
		var that = this,
			i;

		// Default options		默认配置
		that.options = {
			// Element
			wrapper: ".slider",
			ul: $(".slider ul"),
			li: $(".slider li"),
			trigger: $(".slider .trigger"),
			prev: null,
			next: null,

			// options
			isAuto: true,
			autoTime: 3000,
			mode: "opacity",
			triggerEvent: "mouseenter"
		};

		// User defined options		定义的对象和配置
		for (i in options) {

			that.options[i] = options[i];
		}

		that.len = that.options.li.length;
		this.curIndex = 0;

		that.init();
	};

	// Prototype
	Slider.prototype = {
		autoTimer: null,
		triggerSpan: null,

		go: function(target) {
			var _this = this;

			_this.stopAuto();

			// 判断传入的参数是 "next", "prev"
			if (typeof target == "string") {

				target = (_this.curIndex + _this.len + (target == "next" ? 1 : -1)) % _this.len;
			}

			_this.opacityTo(target);

			// 判断是否有targger,更新标示
			if (_this.triggerSpan !== null) {

				_this.triggerSpan.eq(_this.curIndex).removeClass("cur")
					.end().eq(target).addClass("cur");
			}
			_this.curIndex = target;
			_this.startAuto();
		},

		// opacity 滑动
		opacityTo: function(target) {
			var _this = this;

			_this.options.li.eq(_this.curIndex).css("z-index", 1).stop().animate({"opacity": 0}, 500);
			_this.options.li.eq(target).stop().css("z-index", 2).animate({"opacity": 1}, 500);
		},

		// opacity 初始化
		_initOpacity: function() {
			var _this = this;

			//_this.options.ul.css({"position": "relative"});
			//_this.options.li.css({"opacity": 0, "position": "absolute", "left": 0, "top": 0});
			_this.options.li.css({"opacity": 0});
		},


		// 生成trigger span
		_createTrigger: function() {
			var _this = this,
				i = _this.len,
				triggerHTML = "";

			while (i--) {

				triggerHTML += "<span></span>";
			}

			_this.options.trigger.html(triggerHTML);
		},

		onEvent: function() {
			var _this = this;
			
			// trigger span绑定事件
			if (_this.options.trigger !== null) {
				_this.triggerSpan = _this.options.trigger.find("span");

				_this.triggerSpan.on(_this.options.triggerEvent, function() {
					var index = $(this).index();

					if (index != _this.curIndex) {
						_this.go(index);
					}
				});
			}

			// prev,next绑定切换事件
			if (_this.options.prev !== null) {

				_this.options.prev.on("click", function() {

					_this.go("prev");
				});
				_this.options.next.on("click", function() {

					_this.go("next");
				});
			}
		},

		// 开启自动滑动
		startAuto: function() {
			var _this = this;

			_this.autoTimer = setInterval(function() {

				_this.go("next");
			}, _this.options.autoTime);
		},

		// 停止自动滑动
		stopAuto: function() {

			window.clearInterval(this.autoTimer);
		},

		// 初始化Slider
		init: function() {
			var _this = this;

			// 判断切换模式，不同模式使用不同的初始化方法
			switch (_this.options.mode) {

				case "opacity": 

					_this._initOpacity();
					break;
			}

			// 判断是否有trigger,生成span
			if (_this.options.trigger[0]) {

				_this._createTrigger();
			}

			_this.onEvent();

			// 判断是否自动切换
			if (_this.options.isAuto) {

				_this.startAuto();
			}

			// 初始化结束默认进入第一个
			_this.go(0);
		}
	};

	// 对外接口
	window.Slider = Slider;

})(window, jQuery);

$(function() {
	/**** 首页新闻自动滚动 *****/ 
	var $rmdCont = $(".header .rmd"),
		rmdTimer = null;

	function rmdAnimate() {
		$rmdCont.stop().animate({scrollTop: 20}, 500, function() {

			$rmdCont[0].scrollTop = 0;
			$rmdCont.find("a").eq(0).appendTo($rmdCont);
		});
	}
	if ($rmdCont.find("a").length >= 1) {
		rmdTimer = setInterval(rmdAnimate, 5000);

		$rmdCont.hover(function() {

			clearInterval(rmdTimer);
		}, function() {

			rmdTimer = setInterval(rmdAnimate, 5000);
		});
	}


	/*** 二级导航 *******/ 
	$(".wrap-nav").hover(function() {
		
		$(".wrap-sub-nav").stop().animate({height: 160}, 500);
	}, function() {

		$(".wrap-sub-nav").stop().animate({height: 0}, 500);
	});
	
	/*** Fixed *******/ 
	(function() {
		$(".index-fixed .btn").on("click", function(){
			$(".index-fixed-cont").stop().animate({left: -282}, 500);
		}).trigger("click");

		$(".index-fixed-cont .close").on("click", function() {
			$(".index-fixed-cont").stop().animate({left: 33}, 500);
		});
	})();

});