var timer = null;
var offset = 3000;
var index = 0;
var total = 11;	//总轮换图片数-1

//bind thumb a
function hookThumb(){ 
    $('#thumbs li a')
        .bind('click', function(){
            if (timer) {
                clearTimeout(timer);
            }                
            var id = this.id; 
            index = getIndex(id.substr(6));
            rechange(index);
            //slideImage(index); 
            timer = window.setTimeout(auto, offset);  
            this.blur();            
            return false;
        });
}
//bind next/prev img
function hookBtn(){
    $('#thumbs li img').filter('#play_prev,#play_next')
        .bind('click', function(){
            var id = this.id;
            if (id == 'play_prev') {
                index--;
                if (index < 0) 
				{
					index = total;
					$('#thumbs_list li.dsb').removeClass('dsb');
					$('#thumbs_list li').addClass('dsn');
					var yu = total%11;
					for(var i=total-yu;i<total+1;i++)
					{
						$('#li_xixi-'+i).removeClass('dsn');
						$('#li_xixi-'+i).addClass('dsb');
					}
				}
			}else{
				index++;
				if (index > total) 
				{
					index = 0;
				}
           	}
            rechange(index);
            //slideImage(index);
            //timer = window.setTimeout(auto, offset);
        });
}
 
//get index
function getIndex(v){
    for(var i=0; i < target.length; i++){
        if (target[i] == v) return i;
    }
}

function rechange(loop){
    var id = 'thumb_xixi-'+ (loop+1);
	
	if (loop%4==0)	//一组轮换几张图片
	{
		$('#thumbs_list li.dsb').removeClass('dsb');
		$('#thumbs_list li').addClass('dsn');
		for(var i=loop+1;i<loop+11;i++)
		{
			$('#li_xixi-'+i).removeClass('dsn');
			$('#li_xixi-'+i).addClass('dsb');
		}
	}
    $('#thumbs li a.current').removeClass('current');
    $('#'+ id).addClass('current');
}
function auto(){
	if (index > total){
			index = 0;
	}
	
	rechange(index);
	timer = window.setTimeout(auto, offset);
	index++;
}

$(function(){
	auto();
	//hookThumb(); 
	hookBtn();
});
