var timer = null;
var offset = 5000;
var index = 0;

//��ͼ�����ֻ�
function slideImage(i){
    var id = 'image_'+ target[i];
    $('#'+ id)
        .animate({opacity: 1}, 800, function(){
            $(this).find('.word').animate({height: 'show'}, 'slow');
        }).show()
        .siblings(':visible')
        .find('.word').animate({height: 'hide'},'fast',function(){
            $(this).parent().animate({opacity: 0}, 800).hide();
        });
}
//bind thumb a
function hookThumb(){    
    $('#unuse 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(){
    $('#unuse li img').filter('#play_prev,#play_next')
        .bind('click', function(){
            if (timer){
                clearTimeout(timer);
            }
            var id = this.id;
            if (id == 'play_prev') {
                index--;
                if (index < 0) index = 3;
            }else{
                index++;
                if (index > 3) 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_'+ target[loop];
    $('#unuse li a.current').removeClass('current');
    //alert(3);
    if(loop==0)
    {
    	document.getElementById("m1").src="images/number_01.gif";
    	document.getElementById("m2").src="images/number_002.gif";
    	document.getElementById("m3").src="images/number_003.gif";
    	document.getElementById("m4").src="images/number_004.gif";
    }
    if(loop==1)
    {
    	document.getElementById("m1").src="images/number_001.gif";
    	document.getElementById("m2").src="images/number_02.gif";
    	document.getElementById("m3").src="images/number_003.gif";
    	document.getElementById("m4").src="images/number_004.gif";
    }
    if(loop==2)
    {
    	document.getElementById("m1").src="images/number_001.gif";
    	document.getElementById("m2").src="images/number_002.gif";
    	document.getElementById("m3").src="images/number_03.gif";
    	document.getElementById("m4").src="images/number_004.gif";
    }
    if(loop==3)
    {
    	document.getElementById("m1").src="images/number_001.gif";
    	document.getElementById("m2").src="images/number_002.gif";
    	document.getElementById("m3").src="images/number_003.gif";
    	document.getElementById("m4").src="images/number_04.gif";
    }
    //$('#'+ id).addClass('current');
}
function auto(){
	//alert(2);
    index++;
    if (index > 3){
        index = 0;
    }
    rechange(index);
    slideImage(index);
    timer = window.setTimeout(auto, offset);
}
$(function(){    
    //change opacity
    //$('div.word').css({opacity: 0.85});
	//document.getElementById("thumbs").style.display="block";
    auto();  
    hookThumb(); 
    hookBtn();
    
});