﻿(function() {
    $.fn.slider = function() {
    function repeat(str, n) {
            if(!isNaN(n))
                return new Array(n + 1).join(str);
        }

        return this.each(function() {
            // magic!
            var $slider_wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $slider_wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first')

            singleWidth = $single.outerWidth(),
                visible = Math.ceil($slider_wrapper.innerWidth() / singleWidth), //这里可以设置每次滚动几张图片
                currentPage = 1,
                pages = Math.ceil($items.length / visible);

            /* TASKS */

            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }

            // 2. create the carousel padding on left and right (cloned)
            $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');

            // 3. reset scroll
            $slider_wrapper.scrollLeft(singleWidth * visible);

            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;

                $slider_wrapper.filter(':not(:animated)').animate({
                    scrollLeft: '+=' + left
                }, 1000, function() {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $slider_wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $slider_wrapper.scrollLeft(singleWidth * visible * pages);
                    }

                    currentPage = page;
                });
            }

            // 5. insert the back and forward link
            $slider_wrapper.after('<a href="#" class="arrow back">&lt;</a><a href="#" class="arrow forward">&gt;</a>');

            // 6. bind the back and forward links
            $('a.back', this).click(function() {
                gotoPage(currentPage + 1);
                return false;
            });

            $('a.forward', this).click(function() {
                gotoPage(currentPage - 1);
                return false;
            });

            $(this).bind('goto', function(event, page) {
                gotoPage(page);
            });

            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function() {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);

$(document).ready(function () {
    // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
    var autoscrolling = true;
    
    $('.slider').slider().mouseover(function () {
        autoscrolling = false;
    }).mouseout(function () {
        autoscrolling = true;
    });
    
    setInterval(function () {
        if (autoscrolling) {
            $('.slider').trigger('next');
        }
    }, 3000);
});

//下拉菜单特效
$(document).ready(function(){

	$("ul.topnav li a").mouseover(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").slideUp(); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		})

	$(".select h4").mouseover(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.linklist").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.linklist").slideUp(); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() { 
			$(this).addClass("linkhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("linkhover"); //On hover out, remove class "subhover"
	});
});

