﻿$(document).ready(function () {
    //首页轮换    
    $('.flash li img').click(function(){
        var $self = $('.flash li'),
            $size = $self.size(),
            $current = $size - $(this).nextAll().size() - 1;
        
        if (this.className.indexOf('f_tab')!=-1) return;
        
        $(".flash li img.f_tab").fadeTo(200,0.6)
		.removeClass("f_tab")
		.animate({top:22,width:70},300);

        $(this).animate({top:4,width:80},150)
		.addClass("f_tab")
		.fadeTo(200,1);
		
        var arry = $(this).parent().attr("id").split("|");
        var Ustr = arry[0];
        var Istr = arry[1];

        $("#fcn a").children().addClass("gray").end()
			.fadeTo(500,0.1,function(){
			    $(this).attr("href",Ustr);
				$(this).children().attr("src",Istr).removeClass("gray");	  //更改图片
				$(this).fadeTo(500,1);
		})
        
    });
    
    //案例展示
    var atScrolling = true;

    $('div.infiniteCarousel').infiniteCarousel().hover(
        function(){atScrolling = false;},
        function(){atScrolling = true;}
    );
    
    setInterval(function () {
        if (atScrolling) {
            $('div.infiniteCarousel').trigger('next');
        }
    }, 3000);
});
//案例展示
(function(){
    $.fn.infiniteCarousel = function(){
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function(){
            var $self    = $(this),
                $wrapper = $('> div', this),
                $items   = $wrapper.children('div'),
                $single  = $items.filter(':first'),
                
                singleWidth = $single.outerWidth(),
                visible = Math.ceil($self.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil($items.length / visible);
            
            if ($items.length % visible != 0) {
                $wrapper.append(repeat('<div class="empty" />', visible - ($items.length % visible)));
                $items = $wrapper.children('div');
            }
            // 填充空白
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $wrapper.children('div');
            // 初始化滚动条位置
            $self.scrollLeft(singleWidth * visible);

            function gotoPage(page){
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;

                $self.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    if (page > pages) {
                        $self.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $self.scrollLeft(singleWidth * visible * pages);
                    }
                    
                    currentPage = page;
                });
            };

            $('img.up').click(function () {
                gotoPage(currentPage + 1);
                return false;
            });
            
            $('img.next').click(function () {
                gotoPage(currentPage - 1);
                return false;
            });
            
            $(this).bind('goto', function (event, page) {
                gotoPage(page);
            });
            
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);