﻿var videoIcons = new Array();
var moveIcons = true;
var windowWidth = 0;
var nextOnScreen = null;

function StartVideoGallery() {
    CloseAllVideoIcons();
    StartMove();
    GalleryTick();
}

function StartMove() {
    moveIcons = true;
}

function StopMove() {
    moveIcons = false;
}

function GalleryTick() {
    if (moveIcons || nextOnScreen != null) {
        for (var x = 0; x < videoIcons.length; ++x) {
            var l = videoIcons[x].style.left.replace('px', '');
            var w = videoIcons[x].style.width.replace('px', '');
            l -= 1;

            if (nextOnScreen != null) {
                l -= 10;
                var nLeft = parseInt(nextOnScreen.style.left.replace('px', ''));
                var nWidth = parseInt(nextOnScreen.style.width.replace('px', ''));
                if(nLeft + nWidth + 50 < windowWidth){
                    nextOnScreen = null;
                }
            }

            if (l < -w) {
                var preX = x - 1;
                if (preX == -1) {
                    preX = videoIcons.length - 1;
                }
                prel = parseInt(videoIcons[preX].style.left.replace('px', ''));
                prew = parseInt(videoIcons[preX].style.width.replace('px', ''));
                l = prel + prew + 15;
                
                if (l < windowWidth) {
                    l = windowWidth;
                }
            }

            videoIcons[x].style.left = l + 'px';
        }
    }

    setTimeout(function() { GalleryTick(); }, 30);
}

function CloseAllVideoIcons() {

    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        windowWidth = window.innerWidth;
    } else if (document.documentElement && (document.documentElement.clientWidth)) {
        //IE 6+ in 'standards compliant mode'
        windowWidth = document.documentElement.clientWidth;
    } else if (document.body && (document.body.clientWidth)) {
        //IE 4 compatible
        windowWidth = document.body.clientWidth;
    }

    var lastWidth = 0;
    var divs = document.getElementsByTagName('div');
    for (var x = 0; x < divs.length; ++x) {
        var item = divs[x];
        if (item.className == 'Video-Icon') {
            item.truewidth = parseInt(item.scrollWidth) + 12;
            item.style.width = item.truewidth + 'px';
            item.style.overflow = "hidden";
            var l = parseInt(lastWidth);
            item.style.left = l + 'px';
            lastWidth = parseInt(lastWidth) + parseInt(item.truewidth) + parseInt(15);

            videoIcons.push(item);
        }
    }
}

function ViewNext() {
    nextOnScreen = null;
    for (var x = 0; x < videoIcons.length; ++x) {
        var cLeft = parseInt(videoIcons[x].style.left.replace('px', ''));
        var cWidth = parseInt(videoIcons[x].style.width.replace('px', ''));
        if (cLeft + cWidth > windowWidth && nextOnScreen == null) {
            nextOnScreen = videoIcons[x];
        }
        
        if (cLeft + cWidth > windowWidth && nextOnScreen != null && cLeft < parseInt(nextOnScreen.style.left.replace('px', ''))) {
            nextOnScreen = videoIcons[x];
        }
    }
}