var timer = null;
var offset = 5000;
var index = 0;
var indexText = 0;
var target = ["01","02","03","04"];
var change_color=['#232020','#379ead','#ffcc00','#3798ad']
var targetText=["01","02","03","04"]
//´óÍ¼½»ÌæÂÖ»»
function slideImage(i){
    var id = 'image_'+ target[i];
//	alert(id);
    $('#'+ 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();
        });
}
function slideText(i){
    var id = 'text_'+ targetText[i];
	for(var j=0;j<targetText.length;j++){
		if(j != i){
			$('#'+ 'text_'+ targetText[j]).fadeOut("slow"); 
		}
	}
    $('#'+ id).fadeIn("slow"); 
	
}
//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(){
            if (timer){
                clearTimeout(timer);
            }
            var id = this.id;
            if (id == 'play_prev') {
                index--;
                if (index < 0) index = 4;
            }else{
                index++;
                if (index > 5) 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];
 	if(target[loop]=="01"){
		$('#thumbs_bg').css('background-color',change_color[loop]); 
	}
	if(target[loop]=="02"){
		$('#thumbs_bg').css('background-color',change_color[loop]); 
	}
	if(target[loop]=="03"){
		$('#thumbs_bg').css('background-color',change_color[loop]); 
	}
	if(target[loop]=="04"){
		$('#thumbs_bg').css('background-color',change_color[loop]); 
	}
   $('#thumbs li a.current').removeClass('current');
    $('#'+ id).addClass('current');
}
function auto(){
    if (index > 4){
        index = 0;
    }
    if (indexText > 4){
        indexText = 0;
    }
    rechange(index);
    slideImage(index);
    slideText(indexText);
    index++;
	indexText++;
    timer = window.setTimeout(auto, offset);
}
function loadImg(name){
	//alert(name);
	$('#'+name).hide();
}
function poptop(){
	$display=$('#helpPanel').css('display');
	if($display=='none'){
		$('#helpPanel').slideDown();
	}else{
		$('#helpPanel').slideUp();
	}
}
$(function(){    
    //change opacity
	
    $('div.word').css({opacity: 0.85});
    $('#thumbs_bg').css({opacity: 0.30});
    auto();  
    hookThumb(); 
    hookBtn();
	$('#popup_download').click(function(){poptop()});
	$('#img1').load(function(){loadImg('load1');});
 	$('#img2').load(function(){loadImg('load2');});
	$('#img3').load(function(){loadImg('load3');});
	$('#img4').load(function(){loadImg('load4');});
   
});

