/**
 * SlidePlayer for jQuery
 *
 * author: feiyu
 * e-mail: feiyu@asgard.cn
 * website: http://feiyu.asgard.cn
 *
 * Version: 0.1.0
 * 此版本为预览版，功能尚未完善，不能够支持同一页面的多个SlidePlayer
 * 也不支持参数设置，请直接按此源码的注释进行修改
 * 效果只做了“出”的效果
 * 其它样式的修改，请直接改CSS
 */
(function ($)
{
    var slide;
    var index = 0;
    var count = 0;
    var time = 3000; //这里为每个广告的间隔时间
    var done = true;
    $.fn.SlidePlayer = function (opts)
    {

        count = $('.SlidePlayer-List li', this[0]).length;


        startSlide($('.SlidePlayer-List li', this[0])[0]);
        $('.SlidePlayer-Trigger li').bind('click', function ()
        {
            if (done && !$(this).is('.selected'))
            {
                
                // Slide($(this).parent().find('>').index(this), this);
                Slide($(this).parent().children().index(this), this)
            }
        });
    };
    //切换
    function Slide(ix, obj)
    {
        if (ix >= 0) index = ix;
        else index++;

        if (index > count - 1) index = 0;


        stopSlide();
        done = false;
        var father = $(obj).parents('.SlidePlayer:eq(0)');
        var list = $('.SlidePlayer-List', father);
        var trigger = $('.SlidePlayer-Trigger', father);
        var old = $('>.selected', list);
        if (old.length > 0)
        {
            old.css('z-index', 10);
            $('>:eq(' + index + ')', list).addClass('selected').show();
            old.fadeOut(300, function ()
            {	//改fadeOut为hide,slideUp,slideDown等可以换效果，300表示动画时间
                $(this).css('z-index', 1).removeClass('selected');
                done = true;
                startSlide(obj);
            });
            trigger.find('li.selected').removeClass('selected');
            $('>:eq(' + index + ')', trigger).addClass('selected');
        }
    }
    //停止自动
    function stopSlide()
    {
        clearTimeout(slide);
    }
    //开始自动
    function startSlide(obj)
    {
        if(count>1)
            slide = setTimeout(function () { Slide(-1, obj) }, time);
    }
})(jQuery);
