function scrollablePlay() {
    if($('#actionButtonPlay').attr('title')!='Playing') {
        var horizontalScrollIndex = apiHorizontal.getIndex();
        apiHorizontal.play();
        //apiHorizontal.begin();
        apiHorizontal.seekTo(horizontalScrollIndex,0);
        $('#actionButtonPlay').attr('src','images/Decipher/next0.gif');
        $('#actionButtonPause').attr('src','images/Decipher/pause.gif');
        $('#actionButtonPause').attr('title','Click to Pause');
        $('#actionButtonPlay').attr('title','Playing');
        //Setting isHovered 1, we make the scroller go on
        //if(typeof(isHovered) != 'undefined') { isHovered = 0; }
        scrollableCompleteHorizontalOnSeek();
    }
}

function scrollableStop() {
    apiHorizontal.stop();
    $('#actionButtonPause').attr('src','images/Decipher/pause0.gif');
    $('#actionButtonPlay').attr('src','images/Decipher/next.gif');
    $('#actionButtonPause').attr('title','Paused');
    $('#actionButtonPlay').attr('title','Click to Play');
}

function scrollableCompleteHorizontalOnSeek() {
    if($('#actionButtonPlay').attr('title')=='Playing') {
        /*
        var testArray = apiHorizontal.getItems().find('#item').toArray();
        var testString = "";
          for (var i = 0; i < testArray.length; i++) {
            testString = testString+testArray[i].innerHTML+':::::';
          }
        alert('arraySize:'+testArray.length+' array:'+testString);
        */
        // jQuery Tools clones 2 items for Circular plugin so subtract 2 if using Circular plugin.
        // We do not need Circular plugin for this
        var horizontalScrollSize = apiHorizontal.getSize();
        var horizontalScrollIndex = apiHorizontal.getIndex();
        if (horizontalScrollIndex==horizontalScrollSize-1) {
            var verticalScrollSize = apiVertical.getSize();
            var verticalScrollIndex = apiVertical.getIndex();
            //alert('Vertical Scroll Size:'+verticalScrollSize+' Vertical Scroll index:'+verticalScrollIndex+' Horizontal Scroll Size:'+horizontalScrollSize+' Horizontal Scroll index:'+horizontalScrollIndex);
            apiHorizontal.stop();

            if (verticalScrollIndex==verticalScrollSize-1)
                setTimeout("if($('#actionButtonPlay').attr('title')=='Playing' && !isHovered ) apiVertical.begin()",window.ScrollableCompleteInterval);
            else
                setTimeout("if($('#actionButtonPlay').attr('title')=='Playing' && !isHovered ) apiVertical.next()",window.ScrollableCompleteInterval);
                //setTimeout("if($('#actionButtonPlay').attr('title')=='Playing') apiVertical.seekTo("+(verticalScrollIndex+1)+",0)",3000);
        }
        //setTimeout("scrollableCompleteCircular()",4000);
    }
}

function scrollableCompleteVerticalOnSeek() {
    if($('#actionButtonPlay').attr('title')=='Playing') {
        apiHorizontal.play();
        apiHorizontal.begin();
    } else {
        if (apiHorizontal.getIndex != 0)
            apiHorizontal.begin();
    }
    scrollableCompleteHorizontalOnSeek();
}

function pauseScrollableCompleteByHovering() {
    //If no item has been hovered yet, the variable isHovered doesn't exist, so we create it.
    if(typeof(isHovered) == 'undefined') { window.isHovered = 0; }

    $(".scrollableHorizontal .items .item").hover(function() { // Whenever an item is hovered
        if($('#actionButtonPlay').attr('title')=='Playing') {
            //alert('test1');
            scrollableStop();
            $('#actionButtonPlay').attr('title','Paused by Hovering');
            isHovered = 1; //Setting isHovered 1, we stop the scroller from going on
        }
    }, function() {
        if($('#actionButtonPlay').attr('title')=='Paused by Hovering') {
            //alert('test2');
            scrollablePlay();
            isHovered = 0;//Setting isHovered 1, we make the scroller go on
        }
    });

    $("#scrollableVerticalNavi").hover(function() { // Whenever an item is hovered
        if($('#actionButtonPlay').attr('title')=='Playing') {
            //alert('test1');
            scrollableStop();
            $('#actionButtonPlay').attr('title','Paused by Hovering');
            isHovered = 1; //Setting isHovered 1, we stop the scroller from going on
        }
    }, function() {
        if($('#actionButtonPlay').attr('title')=='Paused by Hovering') {
            //alert('test2');
            scrollablePlay();
            isHovered = 0;//Setting isHovered 1, we make the scroller go on
        }
    });
}
