var img_count = 0;
var img_width = 322 * 3;
var current_first = 1;
var increment = 3;
var direction = "forward";
var scroll_speed = 30000;

$(document).ready(function(){

    img_count = $(".topImg").length;
   
    $(document).ready(function() {
        Recite.load({
        classname: 'recite-button'
        });
    });

    var interval = setInterval("scroll()", scroll_speed);
    
    var config = {
         over: function(){
                   $(this).animate({
                       height : 100
                   }, 700);
                },
         timeout: 500, // number = milliseconds delay before onMouseOut
             out:function(){
           $(this).animate({
               height : 25
           }, 200);
        },
        sensitivity : 20
    };

    $(".imgOverlay").hoverIntent(config);

    $("#topImgRight").click(function(){
       scrollLeft();
    });

    $("#topImgLeft").click(function(){
       scrollRight();
    });

    $(".imgOverlay").hover(function(){
        clearInterval(interval);
    }, function(){
        interval = setInterval("scroll()", scroll_speed);
    });

});

function scrollLeft(){
   
   var toScroll = current_first * img_width;
   if(((current_first * increment) + 1) < img_count){
       $("#topImgsInner .topImg").animate({
          // work out how much to push the scroller along
          left: -toScroll
       }, 800);
       current_first += 1;
   }
}

function scrollRight(){
   
   var toScroll = ((current_first -1)  * img_width) - img_width;

   if(current_first > 1){
       $("#topImgsInner .topImg").animate({
          // work out how much to push the scroller along
          left: -toScroll
       }, 800);
       current_first -= 1;
   }
}

function goToStart(){
    var toScroll = ((current_first)  * img_width);
    $("#topImgsInner .topImg").animate({
      // work out how much to push the scroller along
      left: 0
    }, 800);
    current_first = 1;
}

function scroll(){
    // work out which way to scroll

    if(current_first == 1){
        direction = "forward";
    }
    
    if((((current_first * increment) + 1) < img_count) && ( direction == "forward")){
        scrollLeft();
    }else{
        direction = "backward";
        goToStart();
    }
}


