﻿
var activeDiv = null;

function activateMenu(div) {
    var tabContent = $(div);
    if (activeDiv != null)
        activeDiv.setStyle('display', 'none');
    tabContent.setStyle('display', 'block');
    tabContent.setStyle('visibility', 'visible');
    tabContent.setOpacity(0.0);
    tabContent.fade('in');
    activeDiv = tabContent;

    $$('#tabberContainer a.tab-active').each(function(item) {
        try {
            item.removeClass('tab-active');
            item.addClass('tab-inactive');
        }
        catch (ex) {
        }
    });

    var suffix = div.substring(3);
    $('tab' + suffix).removeClass('tab-inactive');
    $('tab' + suffix).addClass('tab-active');
    positionTestBasket();
}

function positionTestBasket() {
    try {
        var tabberCoords = $('tabberContainer').getParent('table.tab-control').getCoordinates();
        if ($('testBasket') != undefined) {
            var topBase = tabberCoords.top + tabberCoords.height + 11;
            var leftBase = tabberCoords.left + tabberCoords.width - $('testBasket').getCoordinates().width;
            $('testBasket').setStyle('left', leftBase + 'px');
            $('testBasket').setStyle('visibility', 'visible');
            $('testBasket').tween('top', topBase + 'px');

            try {
                var offset = $(document).getScrollTop();
                if (offset < topBase) {
                    $('testBasket').tween('top', topBase + 'px');
                } else {
                    $('testBasket').tween('top', (offset + 20) + 'px');
                }
            } catch (ex) { }
        }
    }
    catch (ex) {
    }
    return true;
}

window.addEvent('load', function(event) {
    $('tabMethodology').addEvent('click', function(event) { return activateMenu('divMethodology'); });
    $('tabAlphabetical').addEvent('click', function(event) { return activateMenu('divAlphabetical'); });
    $('tabSearchTests').addEvent('click', function(event) { return activateMenu('divSearchTests'); });
    $$(' div.testmenu-details-container').each(function(item) {
        var coords = item.getCoordinates();
        if (coords.height < 300) {
            item.setStyle('height', '300px');
        }
    });

    window.addEvent('scroll', function(ev) {
        updateMessagePos();
        updateOverlayPos();
        positionTestBasket();
    });

    window.addEvent('resize', function(ev) {
        updateMessagePos();
        updateOverlayPos();
        return positionTestBasket();
    });

    updateTestBasketElements();
    window.fireEvent('scroll', null, 10);
    //positionTestBasket();
});

window.addEvent('domready', function() { positionTestBasket(); });

function updateTestBasketElements() {
    $$(' div.testmenu-testbasket-content').each(function(item) {
        var basketItemsContainer = item.getElement(' div.testmenu-testbasket-items');
        var legendItem = item.getElement(' div.testmenu-testbasket-legend');
        var pdfCatalogLink = item.getElement(' a[linktype=report]');
        var pdfFormLink = item.getElement(' a[linktype=requisition]');
        var separatorDiv = item.getElement(' div.testbasket-separator');
        var removeLinks = item.getElements(' a.testmenu-testbasket-item-remove');
        var detailLinks = item.getElements(' a.testmenu-testbasket-item-caption');

        if (basketItemsContainer.get('html').trim().length == 0) {
            // basket is empty
            legendItem.setStyle('display', '');
            basketItemsContainer.setStyle('display', 'none');
            //pdfFormLink.setStyle('display', 'none');
            pdfCatalogLink.setStyle('display', 'none');
        }
        else {
            // basket has items
            legendItem.setStyle('display', 'none');
            basketItemsContainer.setStyle('display', '');
            //pdfFormLink.setStyle('display', '');
            pdfCatalogLink.setStyle('display', '');
        }

        removeLinks.each(function(link) {
            link.removeEvents();
            link.addEvent('click', function() {
                testBasketRemove(separatorDiv.getProperty('handlerurl'), separatorDiv.getProperty('moduleid'), link.getProperty('testid'), link);
                return false;
            });
        });

        detailLinks.each(function(link) {
            link.removeEvents();
            var navUrl = separatorDiv.getProperty('navigateurl');
            if (navUrl.indexOf('?') == -1) {
                navUrl = navUrl + '?TestID=';
            } else {
                navUrl = navUrl + '&TestID=';
            }
            link.setProperty('href', navUrl + link.getProperty('testid'));
        });

        $$('a.testmenu-testbasket-addlink-active').each(function(link) {
            link.removeEvents();
            link.addEvent('click', function() {
                testBasketAdd(separatorDiv.getProperty('handlerurl'), separatorDiv.getProperty('moduleid'), link.getProperty('testid'), link);
                return false;
            });
        });

        $$('a.testmenu-testbasket-addlink-inactive').each(function(link) {
            link.removeEvents();
            link.setProperty('href', 'javascript:void(0);');
            var testId = link.getProperty('testid');
            var isInBasekt = false;
            removeLinks.each(function(removeLink) {
                testIDs = testIDs + removeLink.getProperty('testid') + ',';
                if (testId == removeLink.getProperty('testid')) {
                    isInBasekt = true;
                }
            });
            if (!isInBasekt) {
                link.set('class', 'testmenu-testbasket-addlink-active');
                link.addEvent('click', function() {
                    testBasketAdd(separatorDiv.getProperty('handlerurl'), separatorDiv.getProperty('moduleid'), link.getProperty('testid'), link);
                    return false;
                });
            }
        });

        var testIDs = '';
        removeLinks.each(function(removeLink) {
            testIDs = testIDs + removeLink.getProperty('testid') + ',';
        });
        pdfCatalogLink.setProperty('href', separatorDiv.getProperty('pdfurl') + '&TestIDs=' + testIDs);
        //pdfFormLink.setProperty('href', separatorDiv.getProperty('pdfurl') + '&TestIDs=' + testIDs + '&req=1');
    });
}

function showMessage(message) {
    showOverlay();
    var updatePanel = $(document.body);
    var coords = updatePanel.getCoordinates();

    if ($('messageContainer') != null) $('messageContainer').destroy();
    var leftPx = ((coords.width / 2) - 125);
    var topPx = ((coords.height / 2) - 90);

    var messageEl = new Element('div', {
        'styles': {
            'background-color': '#fcfcfc',
            'width': '350px',
            'position': 'absolute',
            'border': 'solid 1px #ccc',
            'left': leftPx + 'px',
            'top': (topPx + $(document).getScrollTop()) + 'px'
        }
    });
    messageEl.set('id', 'messageContainer');
    updatePanel.appendChild(messageEl);
    messageEl.set('html', '<div style="margin: 6px; font-size: 11px; font-family: Verdana; padding-top: 6px;">' + message + '</div><div style="text-align: right; margin: 6px; margin-top: 14px; border-top: solid 1px #ccc; padding-top: 6px;"><button style="width: 4em; height:2.5em;" onclick="hideMessage(); return false;">OK</button></div>');
}

function hideMessage() {
    hideOverlay();
    if ($('messageContainer') != null) $('messageContainer').destroy();
}

function showOverlay() {
    var updatePanel = $(document.body);
    var coords = updatePanel.getCoordinates();

    if ($('blockingOverlay') != null) $('blockingOverlay').destroy();
    var overlay = new Element('div', {
        'styles': {
            'background-color': '#000',
            'height': coords.height + 'px',
            'width': coords.width + 'px',
            'position': 'absolute',
            'left': coords.left + 'px',
            'top': (coords.top + $(document).getScrollTop()) + 'px'
        }
    });
    overlay.set('id', 'blockingOverlay');
    updatePanel.appendChild(overlay);
    overlay.setOpacity(0.0);
    overlay.fade(0.4);
}

function hideOverlay() {
    if ($('blockingOverlay') != null) $('blockingOverlay').destroy();
}

function updateMessagePos() {
    var updatePanel = $(document.body);
    var coords = updatePanel.getCoordinates();
    var leftPx = ((coords.width / 2) - 125);
    var topPx = ((coords.height / 2) - 90);
    if ($('messageContainer') != null) {
        var el = $('messageContainer');
        el.setStyle('top', (topPx + $(document).getScrollTop()) + 'px');
        el.setStyle('left', leftPx + 'px');
    }
}

function updateOverlayPos() {
    var updatePanel = $(document.body);
    var coords = updatePanel.getCoordinates();
    if ($('blockingOverlay') != null) {
        var overlay = $('blockingOverlay');
        overlay.setStyle('width', coords.width + 'px');
        overlay.setStyle('height', coords.height + 'px');
        overlay.setStyle('top', (coords.top + $(document).getScrollTop()) + 'px');
        overlay.setStyle('left', coords.left + 'px');
    }
}

function sendComment(handlerurl, moduleid, testid, name, email, comment) {
    var asyncRequest = new Request.HTML({
        url: handlerurl,
        method: 'get',
        async: true,
        encoding: 'ISO-8859-1',
        evalScripts: true
    });

    // successful handler
    asyncRequest.addEvent('success',
                function(responseTree, responseElements, responseHTML, responseJavaScript) {
                    showMessage(responseHTML);
                    if (responseHTML.toString().substring(0, 5) != 'Error') {
                        $(name).value = '';
                        $(email).value = '';
                        $(comment).value = '';
                    }
                });

    // failure handler
    asyncRequest.addEvent('failure',
                function(xhr) {
                    showMessage('Error: ' + xhr.status + ' ' + xhr.statusText);
                });

    // Send the ajax request
    asyncRequest.send('ModuleId=' + moduleid + '&TestID=' + testid + '&Author=' + escape($(name).value) + '&Email=' + escape($(email).value) + '&Comment=' + escape($(comment).value) + '&ts=' + (new Date()).getTime().toString());

}

function testBasketAdd(handlerurl, moduleid, testid, sourceEl) {
    var asyncRequest = new Request.HTML({
        url: handlerurl,
        method: 'get',
        async: true,
        encoding: 'ISO-8859-1',
        evalScripts: true
    });

    // successful handler
    asyncRequest.addEvent('success',
                function(responseTree, responseElements, responseHTML, responseJavaScript) {
                    $$(' div.testmenu-testbasket-items').each(function(item) {
                        item.set('html', responseHTML);
                        updateTestBasketElements();
                        sourceEl.set('class', 'testmenu-testbasket-addlink-inactive');
                        sourceEl.removeEvents();
                        sourceEl.addEvent('click', function() { return false; });
                    });
                });

    // failure handler
    asyncRequest.addEvent('failure',
                function(xhr) {
                    showMessage('Error: ' + xhr.status + ' ' + xhr.statusText);
                });

    // Send the ajax request
    asyncRequest.send('ModuleId=' + moduleid + '&TestID=' + testid + '&action=add&ts=' + (new Date()).getTime().toString());

}

function testBasketRemove(handlerurl, moduleid, testid, sourceEl) {
    var asyncRequest = new Request.HTML({
        url: handlerurl,
        method: 'get',
        async: true,
        encoding: 'ISO-8859-1',
        evalScripts: true
    });

    // successful handler
    asyncRequest.addEvent('success',
                function(responseTree, responseElements, responseHTML, responseJavaScript) {
                    $$(' div.testmenu-testbasket-items').each(function(item) {
                        item.set('html', responseHTML);
                        updateTestBasketElements();
                    });
                });

    // failure handler
    asyncRequest.addEvent('failure',
                function(xhr) {
                    showMessage('Error: ' + xhr.status + ' ' + xhr.statusText);
                });

    // Send the ajax request
    asyncRequest.send('ModuleId=' + moduleid + '&TestID=' + testid + '&action=remove&ts=' + (new Date()).getTime().toString());

}

function loadFlashMovie(elementID, flashPath) {
    var obj = new Swiff(flashPath, {
        width: 330,
        height: 250,
        container: $(elementID)
    });
}