var timer = null;
var offset = 6000;
var index = -1;
var target = ["1","2","3","4","5"];

function slideImage(i){
    var id = 'image_'+ target[i];
    $('#'+ id).animate({opacity: 1}, 800, function(){
        }).show().siblings(':visible').find('.word').animate({height: 'hide'},'fast',function(){
            $(this).parent().animate({opacity: 0}, 800).hide();
        });
}
function hookThumb(){
    jQuery('#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;
        });
}
function hookBtn(){
    jQuery('#thumbs 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 = 4;
            }else{
                index++;
                if (index > 4) index = 0;
            }
            rechange(index);
            slideImage(index);
            timer = window.setTimeout(auto, offset);
        });
}
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];
    jQuery('#thumbs li a.thumb-current').removeClass('thumb-current');
    jQuery('#'+ id).addClass('thumb-current');
}
function auto(){
    index++;
    if (index > 4){
        index = 0;
    }
    rechange(index);
    slideImage(index);
    timer = window.setTimeout(auto, offset);
}
$(function(){
    auto();
    hookThumb();
    hookBtn();
});
