﻿$(document).ready(function(){
    
	$("div.playImg:first").before("<ul id='fadepic'></ul>");
	
	$("div.playImg:first").hide();
	var imgs=$("div.playImg:first a");
	var imgArr= $("ul#fadepic:first");
	for(var i=0;i<imgs.length;i++)
	{
		imgArr.append("<li><p><span></span></p></li>")
		imgArr.find("li:eq("+i+") p:first span:first").append(imgs[i]);

	}
    //当前显示的图片下标
    var showIndex=0;
    //隐藏的图片下标
    var hideIndex=0;
    //播放时间
    var playTime=2300;
    //暂停时间
    var suspendedTime=3000;
    //所有轮播的图片对象
    var imgArr=$("ul#fadepic:first li");
    $("ul#fadepic:first li").not(":first").hide();            
    $.extend({
        next:function(){
            hideIndex=showIndex;
            ++showIndex;                    
            if(showIndex>=imgArr.length)
            {
                showIndex=0;
                hideIndex=imgArr.length-1;
            }
            
            imgArr.eq(hideIndex).fadeTo(playTime,0);
            //setTimeout("function(){this.imgArr.eq(hideIndex).hide()}",playTime)
            imgArr.eq(showIndex).fadeTo(playTime,1)
        },
        pre:function(){
            hideIndex=showIndex;
            --showIndex;
            if(showIndex<0)
            {
                showIndex=imgArr.length-1;
                hideIndex=0;
            }
            imgArr.eq(hideIndex).fadeTo(playTime,0);
            //setTimeout("function(){this.imgArr.eq(hideIndex).hide()}",playTime)
            imgArr.eq(showIndex).fadeTo(playTime,1)
        },
        suspendedPlay:function(){
            self.setTimeout("$.next()",suspendedTime);
        }
    });
    
    $("a#pre:first").click(function(){
        $.pre();
    });
    
    $("a#next:first").click(function(){
        $.next();
    });
    
    $.suspendedPlay();
    
    var autoPlay=self.setInterval("$.suspendedPlay()",playTime+suspendedTime);
    
    imgArr.mouseover(function(){
        self.clearInterval(autoPlay);
    });
    
    imgArr.mouseout(function(){
        //$.next();
        autoPlay=self.setInterval("$.next()",playTime+suspendedTime);
    });
    
});
