/*scroll.js*/

(function($) {
	$.fn.ScrollPn = function(options) {
		options = $.extend({
			num: '',
			auto: false,
			direction: 'left',
			time: 2000,
			speed: 1000
		},
		options);
		return this.each(function() {
			var $wrapper = $('> div', this).css('overflow', 'hidden');
			var $slider = $wrapper.find('> ul');
			var $items = $slider.find('> li');
			var $single = $items.filter(':first');
			var singleWidth = $single.outerWidth();
			var visible = Math.ceil($wrapper.innerWidth() / singleWidth);
			if (options.num == '') {
				num = visible
			} else {
				num = options.num
			}
			var currentPage = 1;
			var pages = Math.ceil($items.length / num);
			$items.filter(':first').before($items.slice( - visible).clone().addClass('cloned'));
			$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
			$items = $slider.find('> li');
			$wrapper.scrollLeft(singleWidth * visible);
			function gotoPage(page) {
				var dir = page < currentPage ? -1 : 1;
				var n = Math.abs(currentPage - page);
				var left;
				if (options.num != '') {
					left = singleWidth * dir * options.num * n
				} else {
					left = singleWidth * dir * visible * n
				}
				$wrapper.filter(':not(:animated)').animate({
					scrollLeft: '+=' + left
				},
				options.speed,
				function() {
					if (page == 0) {
						$wrapper.scrollLeft(singleWidth * visible * pages);
						page = pages
					} else if (page > pages) {
						$wrapper.scrollLeft(singleWidth * visible);
						page = 1
					}
					currentPage = page
				});
				return false
			}
			$wrapper.after('<a class="arrow back"></a><a class="arrow forward"></a>');
			$('a.back', this).click(function() {
				return gotoPage(currentPage - 1)
			});
			$('a.forward', this).click(function() {
				return gotoPage(currentPage + 1)
			});
			$(this).bind('goto',
			function(event, page) {
				gotoPage(page)
			});
			if (options.auto) {
				var $SetAuto = setInterval(autoplay, options.time);
				$('a.back').mouseover(function() {
					clearInterval($SetAuto)
				}).mouseout(function() {
					$SetAuto = setInterval(autoplay, options.time)
				});
				$('a.forward').mouseover(function() {
					clearInterval($SetAuto)
				}).mouseout(function() {
					$SetAuto = setInterval(autoplay, options.time)
				});
				$wrapper.mouseover(function() {
					clearInterval($SetAuto)
				}).mouseout(function() {
					$SetAuto = setInterval(autoplay, options.time)
				})
			};
			function autoplay() {
				if (options.direction == 'left') {
					gotoPage(currentPage + 1)
				}
				if (options.direction == 'right') {
					gotoPage(currentPage - 1)
				}
			};
			function repeat(str, num) {
				return new Array(num + 1).join(str)
			}
		})
	}
})(jQuery);

/*tab.js*/
(function($) {
	$.fn.tab = function(options) {
		options = $.extend({
			event: 'mouseover'
		},
		options);
		return this.each(function() {
			$(this).children('ul').children('li').eq(0).addClass('selected').siblings().removeClass('selected');
			$(this).children('div').children().eq(0).show().siblings().hide();
			$(this).children('ul').children('li').bind(options.event,
			function() {
				var index = $(this).index();
				$(this).addClass('selected').siblings().removeClass('selected');
				$(this).parent().next().children().eq(index).show().siblings().hide()
			})
		})
	}
})(jQuery);

/*slide.js*/
(function($) {
	$.fn.slide = function(options) {
		options = $.extend({
			event: 'mouseover',
			time: 2000
		},
		options);
		return this.each(function() {
			var $enet = $(this);
			var $pic = $enet.find('> ul').addClass('pic');
			var $num = '';
			var $n = 0;
			for (i = 0; i < $pic.children().length; i++) {
				$num += '<li><img src=\"' + $pic.children().eq(i).find('img').attr('src') + '\" /></li>'
			}
			$pic.after('<ul class="num">' + $num + '</ul>');
			$num = $pic.next();
			$pic.children().eq(0).show().addClass('selected').siblings().hide();
			$num.children().bind(options.event,
			function() {
				index = $(this).index();
				$(this).addClass('selected').siblings().removeClass('selected');
				$pic.children().eq(index).animate({
					opacity: 'show'
				},
				{
					duration: 1000
				}).siblings().animate({
					opacity: 'hide'
				},
				{
					duration: 1000
				})
			}).eq(0).addClass('selected');
			$pic.mouseover(function() {
				clearInterval($SetAuto)
			}).mouseout(function() {
				$SetAuto = setInterval(autoplay, options.time)
			});
			$num.mouseover(function() {
				clearInterval($SetAuto)
			}).mouseout(function() {
				$SetAuto = setInterval(autoplay, options.time)
			});
			var $SetAuto = setInterval(autoplay, options.time);
			function autoplay() {
				myShow($n);
				$n++;
				if ($n >= $pic.children().length) {
					$n = 0
				}
			};
			function myShow(index) {
				$pic.children().eq(index).animate({
					opacity: 'show'
				},
				{
					duration: 1000
				}).siblings().animate({
					opacity: 'hide'
				},
				{
					duration: 1000
				});
				$num.children().eq(index).addClass('selected').siblings().removeClass('selected')
			}
		})
	}
})(jQuery);

/*SiderTool.js*/
(function($) {
	$.fn.SiderTool = function(options) {
		options = $.extend({},
		options);
		return this.each(function() {
			var warp = $(this);
			var Big = warp.children(':first');
			var thum = warp.children(':last');
			var i = 0;
			var len = thum.find('ul li').length;
			var elePrev = thum.find('a.back');
			var eleNext = thum.find('a.forward');
			var Thumheight = thum.find('ul li').outerHeight(true);
			var num = Math.ceil((thum.find('div').height()) / Thumheight);
			if (!$('#focus div:last')[0]) return;
			thum.find('ul').css('height', Thumheight * len);
			if (len <= num) eleNext.addClass('gray');
			function prev() {
				if (elePrev.hasClass('gray')) {
					return
				}
				thum.find('ul').animate({
					marginTop: -(--i) * Thumheight
				},
				500);
				if (i < len - num) {
					eleNext.removeClass('gray')
				}
				if (i == 0) {
					elePrev.addClass('gray')
				}
			}
			function next() {
				if (eleNext.hasClass('gray')) {
					return
				}
				thum.find('ul').animate({
					marginTop: -(++i) * Thumheight
				},
				500);
				if (i != 0) {
					elePrev.removeClass('gray')
				}
				if (i == len - num) {
					eleNext.addClass('gray')
				}
			}
			elePrev.bind('click', prev);
			eleNext.bind('click', next);
			thum.find('div ul li').each(function(n, v) {
				$(this).click(function() {
					if (n - i == 2) {
						next()
					}
					if (n - i == 0) {
						prev()
					}
					thum.find('div ul li.selected').removeClass('selected');
					$(this).addClass('selected');
					show(n)
				}).mouseover(function() {
					$(this).addClass('hover')
				}).mouseout(function() {
					$(this).removeClass('hover')
				})
			});
			function show(i) {
				Big.find('ul li').eq(i).css('display', 'block').siblings().css('display', 'none')
			}
			show(0)
		})
	}
})(jQuery)


