﻿//2010-12-10

$(function() {
    //传入这三个id名可以控制不同区块，同时要重新定义样式
    begin_slide('slideshow_wrapper', 'slideshow_photo', 'slideshow_footbar');
});

function begin_slide(slideshow_wrapper_com,slideshow_photo_com,slideshow_footbar_com)
{
	//生成下部小按钮
	var length = $('#'+slideshow_photo_com+' a').length;
	for(var i = 0; i < length; i++)
	{
		$('<div class="slideshow-bt" index="'+(length-i)+'"></div>').appendTo('#'+slideshow_footbar_com);
    }
    $('#'+slideshow_footbar_com+' .slideshow-bt:last').addClass('bt-on');
    $('#'+slideshow_footbar_com+' .slideshow-bt').mouseenter(function(e)
    {
		slideTo_com(this, slideshow_photo_com,slideshow_footbar_com);
    });

	
    var indexAllowAutoSlide = true;
    $('#'+slideshow_wrapper_com).mouseenter(function()
    {
		indexAllowAutoSlide = false;
    }).mouseleave(function()
    {
		indexAllowAutoSlide = true;
    });

	//滚动
    setInterval(function()
    {
		if (indexAllowAutoSlide) slideDown_com(slideshow_photo_com, slideshow_footbar_com);
    }, 3000);

}

function slideDown_com(slideshow_photo_com, slideshow_footbar_com)
{
	var currentBt = $('#'+slideshow_footbar_com+' .slideshow-bt.bt-on');
    if (currentBt.length <= 0) return;
    var nxt = currentBt.get(0).previousSibling;
    slideTo_com(nxt?nxt:$('#'+slideshow_footbar_com+' .slideshow-bt:last').get(0), slideshow_photo_com,slideshow_footbar_com);
}

function slideTo_com(o, slideshow_photo_com,slideshow_footbar_com)
{
	if (!o) return;
	var currentIndex = $('#'+slideshow_footbar_com+' .slideshow-bt.bt-on').attr('index'),
		current = $('#'+slideshow_photo_com+' a[index='+currentIndex+']');
	var nxt = $('#'+slideshow_photo_com+' a[index='+$(o).attr('index')+']');
	if (currentIndex == $(o).attr('index')) return;
	
	if (nxt.find('img[imgsrc]').length > 0)
	{
		var img =nxt.find('img[imgsrc]');
		img.attr('src',img.attr('imgsrc')).removeAttr('imgsrc');
	}
	
	$('#'+slideshow_footbar_com+' .slideshow-bt.bt-on').removeClass('bt-on');
	$(o).addClass('bt-on');
	
	nxt.css('z-index',2);
	
	current.css('z-index',3).fadeOut(500,function()
	{
		$(this).css('z-index','1').show();
		var img = nxt.next('a').find('img[imgsrc]');
		if (img.length > 0)
		{
			img.attr('src',img.attr('imgsrc')).removeAttr('imgsrc');
		}
	});
}

