var id_img=null;
var next_img=null;
var interval=null;

function nextImg(){
    id_img = $(".links UL LI A.active").attr('rel');
    var i=$('.img-box IMG').index($("#"+id_img));
    var next_img=null;
    if (i<($('.img-box IMG').length-1)) {
       next_img= $("#"+id_img).next().attr('id');
    }
    else {
       next_img= $('.img-box IMG:first').attr('id');
    }
    $(".links UL LI A.active").removeClass('active');
        
    $(".links UL LI A[rel="+next_img+"]").addClass('active');

    $('#'+id_img).fadeOut(1500);
    $('#'+next_img).fadeIn(1500);
    $(".title-box-bg SPAN[title="+id_img+"]").parent().fadeOut(1500);
    $(".title-box-bg SPAN[title="+next_img+"]").parent().fadeIn(1500);
};

$(document).ready(function(){
	
	Cufon.replace('.title-box-bg SPAN');
	
    if ($('.rotate').length>0) { 
       if (!($('.links A').hasClass('active'))) {
              $('.links A:first').addClass('active');
       }
       interval=setInterval("nextImg()",5000);
    };
   $('.links A').click(function(){
       if ($(this).hasClass('active')) {return false;}
        
       $(".links UL LI A.active").removeClass('active'); 
       $(this).addClass('active');
       $('.img-box IMG').fadeOut(1500);
       $('.title-box-bg SPAN').parent().fadeOut(1500);
       id_img = $(this).attr("rel");
   
       $("#"+id_img).fadeIn(1500);
       $(".title-box-bg SPAN[title="+id_img+"]").parent().fadeIn(1500);
       clearInterval(interval);
       interval=setInterval("nextImg()",5000);
       return false;
    });
    
});
