﻿function DisableButton(button) {
    var btn = $(button);
    if (btn) {
        $(button).setOpacity('0');
        $(button).setStyle({ 'cursor': 'default' });
    }
}

function DeleteButton(button) {
    var btn = $(button);

    if (btn) {
        $(button).setOpacity('0');
        $(button).setStyle({ 'cursor': 'default' });
        //$(button).onclick = null;
    }
}

function ScrollableControlMoveRight(controlID, imageWidth, imgHorzSpacing, multiplier, leftImageButton, rightImageButton) {

    var totalWidth = (imageWidth + imgHorzSpacing);

    var moveAmount = GetRightScrollAmount(controlID, imageWidth, imgHorzSpacing, multiplier, leftImageButton, rightImageButton);


    new Effect.Move(controlID, { x: -moveAmount, y: 0, mode: 'relative' });
    //new Effect.Move(controlID, { x: -totalWidth * multiplier, y: 0, mode: 'relative' });

    return false;
}

function ScrollableControlMoveLeft(controlID, imageWidth, imgHorzSpacing, multiplier, leftImageButton, rightImageButton) {
    var totalWidth = (imageWidth + imgHorzSpacing);

    var moveAmount = GetLeftScrollAmount(controlID, imageWidth, imgHorzSpacing, multiplier, leftImageButton, rightImageButton);

    new Effect.Move(controlID, { x: moveAmount, y: 0, mode: 'relative' });
    //new Effect.Move(controlID, { x: totalWidth * multiplier, y: 0, mode: 'relative' });

    return false;
}

function ScrollableControlMoveUp(controlID, imageHeight, imgVertSpacing, multiplier) {
    var totalHeight = (imageHeight + imgVertSpacing);

    var moveAmount = GetUpScrollAmount(controlID, imageHeight, imgVertSpacing, multiplier);

    new Effect.Move(controlID, { x: 0, y: moveAmount, mode: 'relative' });
    //new Effect.Move(controlID, { x: 0, y: totalHeight * multiplier, mode: 'relative' });

    return false;
}

function ScrollableControlMoveDown(controlID, imageHeight, imgVertSpacing, multiplier) {
    var totalheight = (imageHeight + imgVertSpacing);

    var moveAmount = GetDownScrollAmount(controlID, imageHeight, imgVertSpacing, multiplier);

    new Effect.Move(controlID, { x: 0, y: -moveAmount, mode: 'relative' });
    //new Effect.Move(controlID, { x: 0, y: -totalheight * multiplier, mode: 'relative' });

    return false;
}

function GetDownScrollAmount(controlID, imageHeight, imgVertSpacing, multiplier) {
    var returnValue = ((imageHeight + imgVertSpacing) * multiplier);

    var parents = $(controlID).ancestors();

    if (parents && parents.length > 0) {
        var parentDiv = parents[0];
        var theControl = $(controlID);

        var totalHeight = returnValue;

        var parentHeight = parentDiv.getHeight();
        var theControlHeight = theControl.getHeight();

        var parentTop = parentDiv.cumulativeOffset()[1];
        var theControlTop = theControl.cumulativeOffset()[1];

        if ((theControlTop + theControlHeight - totalHeight) <= (parentTop + parentHeight)) {
            returnValue = (theControlTop + theControlHeight) - (parentTop + parentHeight);
        }
    }

    return returnValue;
}

function GetUpScrollAmount(controlID, imageHeight, imgVertSpacing, multiplier) {
    var returnValue = ((imageHeight + imgVertSpacing) * multiplier);

    var parents = $(controlID).ancestors();

    if (parents && parents.length > 0) {
        var parentDiv = parents[0];
        var theControl = $(controlID);

        var totalHeight = returnValue;

        var parentHeight = parentDiv.getHeight();
        var theControlHeight = theControl.getHeight();

        var parentTop = parentDiv.cumulativeOffset()[1];
        var theControlTop = theControl.cumulativeOffset()[1];

        if ((theControlTop + totalHeight) >= parentTop) {
            returnValue = parentTop - theControlTop;
        }
    }

    return returnValue;
}

function GetRightScrollAmount(controlID, imageWidth, imgHorzSpacing, multiplier, leftImageButton, rightImageButton) {
    var returnValue = ((imageWidth + imgHorzSpacing) * multiplier);

    var parents = $(controlID).ancestors();

    if (parents && parents.length > 0) {

        var parentDiv = parents[0];
        var theControl = $(controlID);

        var totalWidth = returnValue;

        var parentWidth = parentDiv.getWidth();
        var theControlWidth = theControl.getWidth();

        var parentLeft = parentDiv.cumulativeOffset()[0];
        var theControlLeft = theControl.cumulativeOffset()[0];

        $(rightImageButton).setOpacity('1');
        $(rightImageButton).setStyle({ 'cursor': 'pointer' });
        $(leftImageButton).setOpacity('1');
        $(leftImageButton).setStyle({ 'cursor': 'pointer' });

        if ((theControlLeft + theControlWidth - totalWidth) <= (parentLeft + parentWidth)) {

            $(rightImageButton).setOpacity('0');
            $(rightImageButton).setStyle({ 'cursor': 'default' });

            if ((theControlLeft + theControlWidth) > (parentLeft + parentWidth))
                returnValue = (theControlLeft + theControlWidth) - (parentLeft + parentWidth);
            else {
                returnValue = 0;
            }
        }

    }

    return returnValue;
}

function GetLeftScrollAmount(controlID, imageWidth, imgHorzSpacing, multiplier, leftImageButton, rightImageButton) {

    var returnValue = ((imageWidth + imgHorzSpacing) * multiplier);

    var parents = $(controlID).ancestors();

    if (parents && parents.length > 0) {

        var parentDiv = parents[0];
        var theControl = $(controlID);

        var totalWidth = returnValue;

        var parentWidth = parentDiv.getWidth();
        var theControlWidth = theControl.getWidth();

        var parentLeft = parentDiv.cumulativeOffset()[0];
        var theControlLeft = theControl.cumulativeOffset()[0];

        $(rightImageButton).setOpacity('1');
        $(rightImageButton).setStyle({ 'cursor': 'pointer' });
        $(leftImageButton).setOpacity('1');
        $(leftImageButton).setStyle({ 'cursor': 'pointer' });

        if ((theControlLeft + totalWidth) >= parentLeft) {
            $(leftImageButton).setOpacity('0');
            $(leftImageButton).setStyle({ 'cursor': 'default' });
            returnValue = parentLeft - theControlLeft;
        }
    }

    return returnValue;
}