var ScrollerV_h = 85;
var ScrollerH_w = 160;
var timer;
var AllowScroll;

//Set variables for the scrollers
var CategoryProducts = 3; //<< - Amount of products on the bottom
var CategoryProductWidth = 113;

var RelatedProducts = 7; //<< - Amount of related products on the right
var RelatedProductHeight = 85;

var InnerScrollerH_w = CategoryProducts * CategoryProductWidth;
var InnerScrollerV_h = RelatedProducts * RelatedProductHeight;

//var InnerScrollerV_h = document.getElementById("InnerScrollerV").offsetHeight;
//var InnerScrollerH_w = document.getElementById("InnerScrollerH").offsetWidth;

//ALLOW OR DISABLE SCROLL

//Horizontal
function startScrollLeft()
{
    AllowScroll = 1;
    moveLeft();
}
function startScrollRight()
{
    AllowScroll = 1;
    moveRight();
}
//Vertical
function startScrollUp()
{
    AllowScroll = 1;
    moveUp();
}
function startScrollDown()
{
    AllowScroll = 1;
    moveDown();
}
//Stop
function stopScroller()
{
    AllowScroll = 0;
    moveStop();
}

//MOVE H SCROLLER TO THE LEFT
function moveLeft()
{
        var DifferenceH = (InnerScrollerH_w - ScrollerH_w) * (-1);
        var CurrentPosition = document.getElementById("InnerScrollerH").style.left;
        var CurrentPositionH = CurrentPosition.split("px");
        CurrentPositionH = CurrentPositionH.join("");
                
        if(CurrentPositionH > DifferenceH)
        {
            if(AllowScroll == 1)
            {
                $("#InnerScrollerH").animate({"left": "-=10px"}, 1);
                timer=setTimeout("moveLeft()",50);
            }
        }
}
//MOVE H SCROLLER TO THE RIGHT
function moveRight()
{
        var DifferenceH = (InnerScrollerH_w - ScrollerH_w) * (-1);
        var CurrentPosition = document.getElementById("InnerScrollerH").style.left;
        var CurrentPositionH = CurrentPosition.split("px");
        CurrentPositionH = CurrentPositionH.join("");
    
        if(CurrentPositionH <= -1)
        {
            if(AllowScroll == 1)
            {
                $("#InnerScrollerH").animate({"left": "+=10px"}, 1);
                timer=setTimeout("moveRight()",50);
            }
        }
}

//MOVE V SCROLLER UP and DOWN

function scrollVertical(action_value, func)
{
    $("#InnerScrollerV").animate({"top": action_value + "px"}, 1);
    timer = setTimeout(func,50);
}
function toDown(offset)
{
    if (offset != 0) { scrollVertical("+=" + getOffset(offset), "moveDown()"); }
}
function toUp(offset)
{
    if (offset != 0) { scrollVertical("-=" + getOffset(offset), "moveUp()"); }
}
function getOffset(offset) { return (offset < 10) ? offset : 10; }

function getCurrentTop()
{
    var value  = document.getElementById("InnerScrollerV").style.top;
    var result = value.split("px");
    return result.join("");
}

function moveDown()
{
    if (AllowScroll != 0)
    {
        var currTop = getCurrentTop();

        if (currTop < 0)  { toDown(-currTop); }
        else              { moveStop(); }
    }
}

function moveUp()
{
    if (AllowScroll != 0)
    {
        var maxHeight = document.getElementById("InnerScrollerV").scrollHeight
                      - document.getElementById("ScrollerV").offsetHeight;
        var currTop   = -getCurrentTop();

        if (currTop < maxHeight) { toUp(maxHeight - currTop); }
        else                     { moveStop(); }
    }
}

function moveStop()
{
   clearTimeout(timer);
}
