﻿//this javascript makes sure the images in the sliding gallery will repeat infinitely and never shows blank space


var galleryRotatingPeriod = 5000; // number of milliseconds between each auto right scroll
var shiftParam = 282; // this param needs to be set to same value as horizontal field in the ajax:AnimationExtender control
var mainScrollWrapper = null; // this needs to be set to the div control containing the gallery
var scrollLeftButton = null; // this needs to be set to the left scroll button
var scrollRightButton = null; // this needs to be set to the right scroll button

var is2x2Gallery = false; // indicate whether this javascript is being used for 2x2 gallery
var numberOf2x2GalleryItems = 5; // for the 2x2 gallery, the number of items needs to be set


// internal variables
var lastScrollLeft = true;
var isGalleryRotatingPaused = false;
var nextGalleryRotateTimeout = null; //timer for triggering the next auto right scroll



function galleryRotationInit(is2x2Gallery_, galleryRotatingPeriod_, shiftParam_, mainScrollWrapperId, scrollLeftButtonId, scrollRightButtonId, numberOf2x2GalleryItems_) {

    try {
        shiftParam = shiftParam_;
        is2x2Gallery = is2x2Gallery_;
        mainScrollWrapper = document.getElementById(mainScrollWrapperId);
        scrollLeftButton = document.getElementById(scrollLeftButtonId);
        scrollRightButton = document.getElementById(scrollRightButtonId);
        galleryRotatingPeriod = galleryRotatingPeriod_;
        numberOf2x2GalleryItems = numberOf2x2GalleryItems_;


        mainScrollWrapper.onmouseover = pauseGalleryRotating;
        mainScrollWrapper.onmouseout = resumeGalleryRotating;
        scrollLeftButton.onmouseover = pauseGalleryRotating;
        scrollRightButton.onmouseover = pauseGalleryRotating;


        nextGalleryRotateTimeout = setTimeout(goToNextGalleryScreen, galleryRotatingPeriod);
        scrollRightButton.style.display = "none";
        scrollLeftButton.style.display = "none";

        if (is2x2Gallery)
            Padding2x2Gallery();
    }
    catch (err1)
    { }
}


// when mouse over the gallery
function pauseGalleryRotating() {

    try {

        scrollRightButton.style.display = "block";
        scrollLeftButton.style.display = "block";
        isGalleryRotatingPaused = true;
        clearTimeout(nextGalleryRotateTimeout);
    }
    catch (err1)
    { }
}

// when mouse leave the gallery
function resumeGalleryRotating() {
     try {
        scrollRightButton.style.display = "none";
        scrollLeftButton.style.display = "none";
        isGalleryRotatingPaused = false;
        nextGalleryRotateTimeout = setTimeout(goToNextGalleryScreen, galleryRotatingPeriod);
    }
    catch (err1)
    { }
}

//auto right scroll
function goToNextGalleryScreen() {
    try 
    {
        if (isGalleryRotatingPaused) {
            clear(nextGalleryRotateTimeout);
            return;
        }    

        scrollRightButton.click();
        nextGalleryRotateTimeout = setTimeout(goToNextGalleryScreen, galleryRotatingPeriod);
    }
    catch (err1)
    { }
}



//this function is for doubling the size of the 2x2 gallery in order to make sure the number of elements contained is an even number.
// the padding pattern is as follows


// 1 2 3                  to               1 2 3 4 5
// 4 5                                     4 5 1 2 3 

function Padding2x2Gallery() {

    //node 4
    var nodeToInserBefore = getFirstNChildNodeOfType(mainScrollWrapper, 'DIV', numberOf2x2GalleryItems - Math.floor(numberOf2x2GalleryItems / 2) + 1);

    var numberOfNodeToInsert = Math.floor(numberOf2x2GalleryItems / 2);  // { 4, 5 }


    //prepend { 4, 5 } before { 4, 5 }
    for (var y = 1; y <= numberOfNodeToInsert; y++) {
        var newNode = getLastNChildNodeOfType(mainScrollWrapper, 'DIV', y).cloneNode(true);
        mainScrollWrapper.insertBefore(newNode, nodeToInserBefore);
        nodeToInserBefore = newNode;
    }

    //append { 1, 2, 3 } to the end
    for (var x = 1; x <= numberOf2x2GalleryItems - Math.floor(numberOf2x2GalleryItems / 2); x++) {
        var newNode = getFirstNChildNodeOfType(mainScrollWrapper, 'DIV', x).cloneNode(true);
        mainScrollWrapper.appendChild(newNode);
    }
}


function LeftScrollGallery() {

    try 
    {
        if (lastScrollLeft) {

            if (is2x2Gallery) {
                var TopDiv = getFirstNChildNodeOfType(mainScrollWrapper, 'DIV', numberOf2x2GalleryItems);
                var BottomDiv = getLastChildNodeOfType(mainScrollWrapper, 'DIV');

                var InsertBeforeDiv = getFirstNChildNodeOfType(mainScrollWrapper, 'DIV', numberOf2x2GalleryItems + 1);

                mainScrollWrapper.insertBefore(TopDiv, mainScrollWrapper.firstChild);
                mainScrollWrapper.insertBefore(BottomDiv, InsertBeforeDiv);
            }
            else {
                var lastDiv = getLastChildNodeOfType(mainScrollWrapper, 'DIV');
                mainScrollWrapper.insertBefore(lastDiv, mainScrollWrapper.firstChild);
            }


            mainScrollWrapper.style.position = "absolute";
            mainScrollWrapper.style.left = "-" + shiftParam + "px";
        }
        lastScrollLeft = true;
    }
    catch (err1)
    { }

}


function RightScrollGallery() {
    try 
    {
        if (!lastScrollLeft) {

            if (is2x2Gallery) {
                var TopDiv = getFirstNChildNodeOfType(mainScrollWrapper, 'DIV', 1);
                var BottomDiv = getFirstNChildNodeOfType(mainScrollWrapper, 'DIV', numberOf2x2GalleryItems + 1);
                var InsertBeforeDiv = getFirstNChildNodeOfType(mainScrollWrapper, 'DIV', numberOf2x2GalleryItems + 2);

                mainScrollWrapper.appendChild(BottomDiv);
                mainScrollWrapper.insertBefore(TopDiv, InsertBeforeDiv);
            }
            else {
                var firstDiv = getFirstChildNodeOfType(mainScrollWrapper, 'DIV');
                mainScrollWrapper.appendChild(firstDiv);
            }

            mainScrollWrapper.style.position = "absolute";
            mainScrollWrapper.style.left = "0px";
        }
        lastScrollLeft = false;

    }
    catch (err1)
    { }
}






// helper functions

//get the first child node of the specified node type
function getFirstChildNodeOfType(parentNode, nodeType) {
    for (var x = 0; x < parentNode.childNodes.length; x++) {
        var childNode = parentNode.childNodes[x];
        if (childNode.nodeName == nodeType)
            return childNode;
    }
    return null;
}


//get the last child node of the specified node type
function getLastChildNodeOfType(parentNode, nodeType) {
    for (var x = parentNode.childNodes.length - 1; x >= 0; x--) {
        var childNode = parentNode.childNodes[x];
        if (childNode.nodeName == nodeType)
            return childNode;
    }
    return null;
}

//get the N th child node of the specified node type
function getFirstNChildNodeOfType(parentNode, nodeType, N) {
    for (var x = 0; x < parentNode.childNodes.length; x++) {
        var childNode = parentNode.childNodes[x];
        if (childNode.nodeName == nodeType) {
            N = N - 1;
            if (N == 0)
                return childNode;
        }
    }
    return null;
}

//get of N th (counting from the end)  child node of the specified node type
function getLastNChildNodeOfType(parentNode, nodeType, N) {
    for (var x = parentNode.childNodes.length - 1; x >= 0; x--) {
        var childNode = parentNode.childNodes[x];
        if (childNode.nodeName == nodeType) {
            N = N - 1;
            if (N == 0)
                return childNode;
        }
    }
    return null;
}
  
