Periodically sliding images I wrote this code using jQuery, but I think it can be improved. Can you provide some feedback?

    var $i = 1,
    totalImg = $('.home-featured-bg .absolute div').length,
    $theWidth = $(window).width(),
    $theHeight = $(window).height();
    $('.home-featured-bg .absolute').css('width',$theWidth * totalImg+'px');
    $('.home-featured-bg .absolute .slide,.home-featured-bg .absolute .slide img').css({'width':$theWidth+'px','height' : $theHeight+'px'});

window.setInterval(function(){
    $('.events-links a').hide();
    if($i == 1)
    {

        var $el = $theWidth;
    }else{
        var $el = $theWidth * $i;
    }
    if($i &lt; totalImg)
    {
        $('.home-featured-bg .absolute').animate({'margin-left':'-'+$el+'px'},2000);
        $i++;
        $('.events-links a').eq($i - 1).css('display','block');
    }else{
        $('.home-featured-bg .absolute').animate({'margin-left':'0px'},2000);
        $i = 1;
        $('.events-links a').eq($i - 1).css('display','block');
    }


},3000);

$(window).resize(function(){
    totalImg = $('.home-featured-bg .absolute div').length,
    $theWidth = $(window).width(),
    $theHeight = $(window).height();
    $('.home-featured-bg .absolute').css('width',$theWidth * totalImg+'px');
    $('.home-featured-bg .absolute .slide,.home-featured-bg .absolute .slide img').css({'width':$theWidth+'px','height' : $theHeight});
});
Here few suggestion:

in the function  you have :

if($i == 1)
{

    var $el = $theWidth;
}else{
    var $el = $theWidth * $i;
}


could be replaced by a single line:

var $el = $theWidth * $i;


and you could modify the following if :

if($i &lt; totalImg)
{
    $('.home-featured-bg .absolute').animate({'margin-left':'-'+$el+'px'},2000);
    $i++;
    $('.events-links a').eq($i - 1).css('display','block');
}else{
    $('.home-featured-bg .absolute').animate({'margin-left':'0px'},2000);
    $i = 1;
    $('.events-links a').eq($i - 1).css('display','block');
}


by extract the $('.events-links a').eq($i - 1).css('display','block');   like this:

if($i &lt; totalImg)
{
    $('.home-featured-bg .absolute').animate({'margin-left':'-'+$el+'px'},2000);
    $i++;
}else{
    $('.home-featured-bg .absolute').animate({'margin-left':'0px'},2000);
    $i = 1;
}
$('.events-links a').eq($i - 1).css('display','block');