var timer = null;
var offset = 5000;
var index = -1;

//大图交替轮换
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(){    
    $('.i_thumbs li a')
        .bind('mouseover', 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(){
    $('.i_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 = 7;
            }else{
                index++;
                if (index > 7) 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];
    $('.i_thumbs li a.current').removeClass('current');
    $('#'+ id).addClass('current');
}
function auto(){
 	$(".i_thumbs li a:even").addClass("hcolor");
   index++;
    if (index > 7){
        index = 0;
    }
    rechange(index);
    slideImage(index);
    timer = window.setTimeout(auto, offset);
}
$(function(){    
    //change opacity
    $('div.word').css({opacity: 0.85});
    auto();  
    hookThumb(); 
    hookBtn();
    
});





$(document).ready(function() {
	$imageShow = $('#imageShow');
	$images = $('#imageSrc li', $imageShow);
	$imageSwitches = $('#imageSwitch li', $imageShow);
	if ($images.size() > 0) {
		init();
//		var timer = setInterval(autoSwitch, 3000);
		$imageSwitches.each(function(index, item) {
			$(item).click(function() {
//				clearInterval(timer);
//				setTimeout(function() {
//					timer = setInterval(autoSwitch, 3000)
//				},
//				3000);
				$imageSwitches.children("img").css('border', '1px solid #FFFFFF').eq(index).css('border', '1px solid #FF0000');
				if (index != $imageShow.data('show')) {
					$imageShow.data('show', index);
					$images.hide().eq(index).fadeIn('slow');
				}
			});
		});
	}
	function autoSwitch() {
		$nowIndex = $imageShow.data('show') + 1;
		if ($images.size() > $nowIndex) {
			$imageSwitches.children("img").css('border', '1px solid #FFFFFF').eq($nowIndex).css('border', '1px solid #FF0000');
			$imageShow.data('show', $nowIndex);
			$images.hide().eq($nowIndex).fadeIn('slow');
		} else {
			init();
		}
	}
	function init() {
		$imageShow.data('show', 0);
		$images.hide().eq(0).fadeIn('slow');
		$imageSwitches.children("img").css('border', '1px solid #FFFFFF').eq(0).css('border', '1px solid #FF0000');
	}
});