﻿var itemsList = new Array;
var currentItemIndex = 0;
var helpList;
var helpBlock;
var navBlock, prevLink, forwLink, thankLink;

function init()
{
    helpList = document.getElementById('help_list');

    for (var i = 0; i < helpList.childNodes.length; ++i) {
        if (1 == helpList.childNodes.item(i).nodeType) {
            itemsList.push(helpList.childNodes.item(i).firstChild);
            helpList.childNodes.item(i).style.display = 'none';
        }
    }

    itemsList[0].style.display = 'block';

    helpBlock = document.createElement('div');
    helpBlock.style.height = '530px';
    helpBlock.style.overflowY = 'auto';
    navBlock = document.createElement('div');
    navBlock.className = 'navbar';
    navBlock.style.textAlign = 'center';
    prevLink = document.createElement('span');
    prevLink.className = 'linkInactive';
    prevLink.style.background = "url('img/back.png') no-repeat top left";
    prevLink.style.padding = '2px 0 2px 19px';
    prevLink.appendChild(document.createTextNode('Предыдущий'));
    prevLink.onclick = showPrevious;
    forwLink = document.createElement('span');
    forwLink.className = 'linkActive';
    forwLink.style.background = "url('img/forward.png') no-repeat top right";
    forwLink.style.padding = '2px 19px 2px 0';
    forwLink.appendChild(document.createTextNode('Следующий'));
    forwLink.onclick = showNext;
    thankLink = document.createElement('span');
    thankLink.className = 'linkReady';
    thankLink.onclick = thankYou;
    thankLink.appendChild(document.createTextNode('Помогло'));
    navBlock.appendChild(prevLink);
    navBlock.appendChild(thankLink);
    navBlock.appendChild(forwLink);

    helpList.parentNode.replaceChild(helpBlock, helpList);
    helpBlock.appendChild(itemsList[0]);
    helpBlock.parentNode.appendChild(navBlock);
}

function thankYou()
{
    for (var i = 0; i < helpList.childNodes.length; ++i) {
        if (helpList.childNodes.item(i).nodeType && 1 == helpList.childNodes.item(i).nodeType) {
            helpList.childNodes.item(i).style.display = 'none';
        }
    }

    var thankYouBlock = document.createElement('div');
    thankYouBlock.appendChild(document.createTextNode('Спасибо за использование нашей помощи!'));
    thankYouBlock.style.textAlign = 'center';
    thankYouBlock.style.padding = '50px 0';
    helpBlock.parentNode.replaceChild(thankYouBlock, helpBlock);
    navBlock.parentNode.removeChild(navBlock);
}

function showNext()
{
    if (currentItemIndex < itemsList.length - 1 && currentItemIndex >= 0) {
        ++currentItemIndex;
        helpBlock.removeChild(helpBlock.firstChild);
        helpBlock.appendChild(itemsList[currentItemIndex]);

        if (0 == currentItemIndex) {
            prevLink.className = 'linkInactive';
        } else {
            prevLink.className = 'linkActive';
        }

        if (itemsList.length - 1 == currentItemIndex) {
            forwLink.className = 'linkInactive';
        } else {
            forwLink.className = 'linkActive';
        }
    }
}

function showPrevious()
{
    if (currentItemIndex <= itemsList.length - 1 && currentItemIndex > 0) {
        --currentItemIndex;
        helpBlock.removeChild(helpBlock.firstChild);
        helpBlock.appendChild(itemsList[currentItemIndex]);

        if (0 == currentItemIndex) {
            prevLink.className = 'linkInactive';
        } else {
            prevLink.className = 'linkActive';
        }

        if (itemsList.length - 1 == currentItemIndex) {
            forwLink.className = 'linkInactive';
        } else {
            forwLink.className = 'linkActive';
        }
    }
}

var start, end, link;

function checkPageLoadingSpeed(elem)
{
    link = elem;

    start = (new Date()).getTime();
    var testImage = new Image();
    testImage.onload = pictureLoadingFinished;
    testImage.src = 'img/screenshot.bmp?code=' + start;

    /* вывод результатов */
    var output = document.getElementById('output');

    while (output.childNodes.length > 0) {
        output.removeChild(output.firstChild);
    }
    
    var loading = document.createElement('img');
    loading.src = 'img/loader.gif';
    loading.setAttribute('title', 'Пожалуйста, подождите...');
    loading.setAttribute('alt', 'Пожалуйста, подождите...');
    loading.setAttribute('float', 'left');
    loading.style.margin = '0px 5px -5px 0px';
    output.appendChild(loading);
    output.appendChild(document.createTextNode('Пожалуйста, подождите...'));
}

function pictureLoadingFinished()
{
    end = (new Date()).getTime();
    var diff = (end - start) / 1000;
    var speed = parseInt(368254 * 8 / diff);
    speed = parseInt(speed / 1024);
    
    /* вывод результатов */
    var output = document.getElementById('output');

    while (output.childNodes.length > 0) {
        output.removeChild(output.firstChild);
    }

    output.appendChild(document.createTextNode('Скорость: ' + speed + ' кбит/с'));

    /* Изменить текст ссылки */
    while (link.childNodes.length > 0) {
        link.removeChild(link.firstChild);
    }
    
    link.appendChild(document.createTextNode('Повторить проверку'));
}
