// JavaScript Document

var timerSlide = null;
var offsetSlide = 5000;
var indexSlide = 0;

//´óÍ¼½»ÌæÂÖ»»
function slideImage(i){
    var id = 'image_'+ target[i];
    jQuery('#'+ id)
        .animate({opacity: 1}, 800, function(){
            jQuery(this).find('.word').animate({height: 'show'}, 'slow');
        }).show()
        .siblings(':visible')
        .find('.word').animate({height: 'hide'},'fast',function(){
            jQuery(this).parent().animate({opacity: 0}, 800).hide();
        });
}
//bind thumb a
function hookThumb(){    
    jQuery('#thumbs li a')
        .bind('click', function(){
            if (timerSlide) {
                clearTimeout(timerSlide);
            }                
            var id = this.id;            
            indexSlide = getindexSlide(id.substr(4));
            rechange(indexSlide);
            slideImage(indexSlide); 
            timerSlide = window.setTimeout(auto, offsetSlide);  
            this.blur();            
            return false;
        });
}
//bind next/prev img
function hookBtn(){
    jQuery('#thumbs li img').filter('#play_prev,#play_next')
        .bind('click', function(){
            if (timerSlide){
                clearTimeout(timerSlide);
            }
            var id = this.id;
            if (id == 'play_prev') {
                indexSlide--;
                if (indexSlide < 0) indexSlide = 4;
            }else{
                indexSlide++;
                if (indexSlide > 4) indexSlide = 0;
            }
            rechange(indexSlide);
            slideImage(indexSlide);
            timerSlide = window.setTimeout(auto, offsetSlide);
        });
}
//get indexSlide
function getindexSlide(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.current').removeClass('current');
    jQuery('#'+ id).addClass('current');
}
function auto(){
    indexSlide++;
    if (indexSlide > 4){
        indexSlide = 0;
    }
    rechange(indexSlide);
    slideImage(indexSlide);
    timerSlide = window.setTimeout(auto, offsetSlide);
}
jQuery(function(){    
    //change opacity
    jQuery('div.word').css({opacity: 0.2});
    auto();  
    hookThumb(); 
    hookBtn();
    
});

/**
 * @author Alexander Farkas
 * v. 1.02
 */


(function(jQuery) {
	jQuery.extend(jQuery.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);
