// JavaScript Document
var sc=0;
var ie=1;
	$(document).ready(function(){
		if(navigator.userAgent.indexOf("MSIE 6.0")>0)//IE 6.0
       { 
        ie=0;
       } 
		$(".menu_0").mouseover(function(){
				 this.src="images/z"+this.id+".jpg";
		});		
		$(".menu_0").mouseout(function(){
				 this.src="images/"+this.id+".jpg";
		});	
		var yu=parseInt(($(".wrapper ul li").size())%5);
		var count=parseInt(($(".wrapper ul li").size())/5);
		if(yu!=0)
		{
			sc=count+1;
		}else{
			sc=count;
		}
		
		var imgs="";
		for(var i=0;i<sc;i++)
		{
			
			if(i==0){
				imgs+="<img id='sortimg"+i+"' src='http://www.333cn.com/images/yuan_02.jpg' width='8' height='8' />&nbsp;";
				}else
				imgs+="<img id='sortimg"+i+"' src='http://www.333cn.com/images/yuan_01.jpg' width='8' height='8' />&nbsp;";
		}
		$(".yuan_01").append(imgs);
		
	});
	
	$.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth)-ie, 
            currentPage = 1,
            pages = Math.ceil($items.length / visible);   
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }
       
        $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,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, 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 id="a1" class="arrow back"></a><a id="a2" class="arrow forward"></a>');
        
      
        $("a.back", this).click(function () { 
			var p;
			var cp=currentPage - 1;
			
			if(cp==0)
			{
				p=sc;
			}else{
				p=cp;
			}
			$(".yuan_01 img").each(function(i){
											
					if((p-1)==i)
					{
						$(this).attr("src","http://www.333cn.com/images/yuan_02.jpg");
					}else{
						$(this).attr("src","http://www.333cn.com/images/yuan_01.jpg");
					}
			});
		   return gotoPage(p);
			
        });
        
        $("a.forward", this).click(function () {
											
			var p;
			if(currentPage==sc)
			{
				p=0;
			}else{
				p=currentPage;
			}
			
			$(".yuan_01 img").each(function(i){						
					if(p==i){
						$(this).attr("src","http://www.333cn.com/images/yuan_02.jpg");
					}else{
							$(this).attr("src","http://www.333cn.com/images/yuan_01.jpg");
					}
			});
            return gotoPage(p + 1);
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};

$(document).ready(function () {
  $('.infiniteCarousel').infiniteCarousel();
});	
