﻿// JScript File

function setSpacerHeight() {
    var contentZoneHeight = document.getElementById("contentpageZone").offsetHeight;
    var footerZoneHeight = document.getElementById("footerZone").offsetHeight;
    var windowHeight = document.documentElement.clientHeight;

    if ((contentZoneHeight + footerZoneHeight) < windowHeight) {
        document.getElementById("spacerZone").style.height = windowHeight - contentZoneHeight - footerZoneHeight + "px";
    } else {
        document.getElementById("spacerZone").style.height = "20px";
    }

    document.getElementById("backgroundTable").style.height = windowHeight + "px";
}

function centerContainer(containerid, menucontainer, menuposition)
{
    // Used to center the main container of the webpage.
    var containerDiv = document.getElementById(containerid);
    containerDiv.style.marginTop = String(Math.ceil((document.documentElement.clientHeight - containerDiv.offsetHeight) / 2)) + "px";

    if (document.getElementById(menucontainer) != null)
    {
        showSelectedSubmenuTable(menucontainer, menuposition);
    }
}

function addSubmenuMouseEvents(container, menuposition, enableSubsubMenuFunctions)
{
    // Load the additional menu functionality with mouseover and mouseout events of each (sub)submenutable.
    document.getElementById(container).onmouseover = function(){setSubSubItemBackgrounds(container);}
    var submenuTables = document.getElementById(container).getElementsByTagName('table');

    for (var i = 0; i < submenuTables.length; i++)
    {
        // Check if additional subsubmenu functionality must be enabled (param enableSubsubMenuFunctions)
        if (enableSubsubMenuFunctions == true)
        {
            submenuTables[i].onmouseout = function(){setSubSubItemBackgrounds(container, 'btn_sub' + menuposition);setSubItemBackgrounds(container, 'btn_' + menuposition);}
            submenuTables[i].onmouseover = function(){setSubSubItemBackgrounds(container, 'btn_sub' + menuposition);setSubItemBackgrounds(container, 'btn_' + menuposition);}
        }
        else
        {
            submenuTables[i].onmouseout = function(){setSubItemBackgrounds(container, 'btn_' + menuposition);}
            submenuTables[i].onmouseover = function(){setSubItemBackgrounds(container, 'btn_' + menuposition);}
        }
    }    
}

function setSubItemBackgrounds(container, imgprefix)
{
    var submenuTables = document.getElementById(container).getElementsByTagName('table');
    var menuTable = '';
    
    // If there are no submenu items, there is only one table
    if (submenuTables.length == 1)
    {
        menuTable = submenuTables[0];
    }
    else
    {
        menuTable = submenuTables[submenuTables.length - 1];
    }
    
    // Sets correct background for submenu item based on the row position (first row different bg than last row)
    setTableCellBackgroundByRowPosition(menuTable, imgprefix);
    
    // Sets correct background for submenu item based on the height of the tablecell
    var submenuTableCells = menuTable.getElementsByTagName('td');
    setTableCellBackgroundByCellHeight(submenuTableCells, imgprefix);
}

function setSubSubItemBackgrounds(container, imgprefix)
{
    var menuTables = document.getElementById(container).getElementsByTagName('table');
    
    // If there are no submenu's, only the menu table is present. So check if more than 1 table (menutable) exist.
    // If only the menu table exists, do nothing.
    if (menuTables.length > 1)
    {
        // Set the correct background image for all SUBmenu table cells. 
        // Because the last table in the menuTables array is the menuTable, this table does not count.
        for (var i = 0; i < menuTables.length - 1; i++)
        {
            setTableCellBackgroundByRowPosition(menuTables[i], imgprefix);
        }
    }
}

function showSelectedSubmenuTable(container, position, offset)
{
    if (document.getElementById(container) != null) {
        // Get all tables in the container
        var menuTables = document.getElementById(container).getElementsByTagName('table');

        // Get id of the selected menuitem (td)
        var menuCellIdSelected;
        if (menuTables.length > 0) {
            menuTableCells = menuTables[menuTables.length - 1].getElementsByTagName('td');

            for (var j = 0; j < menuTableCells.length; j++) {
                if (menuTableCells[j].className.search('Highlighted') != -1) {
                    menuCellIdSelected = menuTableCells[j].id;
                }
            }        
        }

        // Do the following for all submenu tables
        if (menuTables.length > 1) {
            for (var i = 0; i < menuTables.length - 1; i++)
            {
                // Get all items of submenu[i] (td)
                var tableCells = menuTables[i].getElementsByTagName('td');

                // Check submenu items if one is currently highlighted
                for (var j = 0; j < tableCells.length; j++)
                {
                    if (tableCells[j].className.search('Highlighted') != -1)
                    {
                        var currentSubmenuTable = menuTables[i];
                        var currentMenuCell = document.getElementById(menuCellIdSelected);

                        // I guess the same functions should be triggered here like in the onmouseover event on the tablecell that makes the submenu expand.
                        MousedOverMenuctl00_ContentPlaceHolder1_CMSMenu1(true, document.getElementById(menuCellIdSelected), '', 'ctl00_ContentPlaceHolder1_CMSMenu1', '', '', '', 'IE', '');
                        skm_shimSetVisibility(true, menuTables[i].id);                 
                        
                        // Set correct submenu item background image.
                        setSubSubItemBackgrounds(container, 'btn_sub' + position);
                        if (offset != null) {
                            // Correct top offset to align submenu with selected menuitem
                            currentSubmenuTable.style.top = (skm_getAscendingTops(currentMenuCell) + currentMenuCell.offsetHeight) + offset + 'px';
                        }
                    }            
                }
            }
        }
    }
}

/*---------------------------------------------------------------------------------------------------------*/

function setTableCellBackgroundByCellHeight(tableCells, imgprefix)
{
    // Check the height of each cell.
    // Depending on certain heights, change the background image matching the heigth of the tablecell.
    var tableCellCurrentBg = '';
    var tableCellNewBg = '';

    for (var i = 0; i < tableCells.length; i++)
    {
        tableCellCurrentBg = getBackgroundImage(tableCells[i]);

        // alert(tableCellCurrentBg);
        // alert(tableCells[i].offsetHeight);
        
        if(tableCells[i].offsetHeight > 35)
        {
            // If the cellheight is bigger than 35px, use bigger bg images
            // NOTE: 35px is based on the current font size (8pt) with two rows;
            if (tableCellCurrentBg.search('_3rows') == -1)
            {
                // no match
                tableCellNewBg = tableCellCurrentBg.substring(0, tableCellCurrentBg.lastIndexOf('.')) + '_3rows' + tableCellCurrentBg.substring(tableCellCurrentBg.lastIndexOf('.'));
            }
            else
            {
                // match
                tableCellNewBg = checkMouseOver(tableCells[i], tableCellCurrentBg, imgprefix);
            }
            
            if (tableCellNewBg)
            {
			    setBackgroundImage(tableCells[i], tableCellNewBg);
            }
        }
        else if(tableCells[i].offsetHeight > 22)
        {
            // If the cellheight is bigger than 22px, use bigger bg images
            // NOTE: 20px is based on the current font size (8pt);
            if (tableCellCurrentBg.search('_2rows') == -1)
            {
                // no match
                tableCellNewBg = tableCellCurrentBg.substring(0, tableCellCurrentBg.lastIndexOf('.')) + '_2rows' + tableCellCurrentBg.substring(tableCellCurrentBg.lastIndexOf('.'));
            }
            else
            {
                // match
                tableCellNewBg = checkMouseOver(tableCells[i], tableCellCurrentBg, imgprefix);
            }
            
            if (tableCellNewBg)
            {
			    setBackgroundImage(tableCells[i], tableCellNewBg);
            }
        }
    }
}

function setTableCellBackgroundByRowPosition(menuTable, imgprefix)
{
    // Depending on the number of rows of the table, determine:
    // - If table has one row, set one-row background.
    // - If table has more than one row, set multiple-row background AND use different backgrounds for first and last row.

    var tableRows = menuTable.getElementsByTagName('tr');
    var tableCellCurrentBg = '';
    var tableCellNewBg = '';
    
    // If table has one row, set one-row background.
    if (tableRows.length == 1)
    {
        tableCellCurrentBg = getBackgroundImage(tableRows[0].getElementsByTagName('td')[0]);
        
        if (tableCellCurrentBg.search('_one') == -1)
        {
            // no match
            tableCellNewBg = tableCellCurrentBg.substring(0, tableCellCurrentBg.lastIndexOf('.')) + '_one' + tableCellCurrentBg.substring(tableCellCurrentBg.lastIndexOf('.'));
        }
        else
        {
            // match
            tableCellNewBg = checkMouseOver(tableRows[0].getElementsByTagName('td')[0], tableCellCurrentBg, imgprefix);
        }

        if (tableCellNewBg)
        {
		    setBackgroundImage(tableRows[0].getElementsByTagName('td')[0], tableCellNewBg);
        }
    }
    else
    {
        // If table has more than one row, set multiple-row background AND use different background for last row.
        if (tableRows.length > 1)
        {
            for (var j = 0; j < tableRows.length; j++)
            {
                tableCellCurrentBg = getBackgroundImage(tableRows[j].getElementsByTagName('td')[0]);
                switch (j)
                {
                    case 0:
                        // Detect first row of table.
                        if (tableCellCurrentBg.search('_first') == -1)
                        {
                            // no match
                            tableCellNewBg = tableCellCurrentBg.substring(0, tableCellCurrentBg.lastIndexOf('.')) + '_first' + tableCellCurrentBg.substring(tableCellCurrentBg.lastIndexOf('.'));
                        }
                        else
                        {
                            // match
                            tableCellNewBg = checkMouseOver(tableRows[j].getElementsByTagName('td')[0], tableCellCurrentBg, imgprefix);
                        }
                        break;
                        
                    case tableRows.length - 1:
                        // Detect last row of table.
                        if (j == tableRows.length - 1)
                        {
                            if (tableCellCurrentBg.search('_last') == -1)
                            {
                                // no match
                                tableCellNewBg = tableCellCurrentBg.substring(0, tableCellCurrentBg.lastIndexOf('.')) + '_last' + tableCellCurrentBg.substring(tableCellCurrentBg.lastIndexOf('.'));
                            }
                            else
                            {
                                // match
                                tableCellNewBg = checkMouseOver(tableRows[j].getElementsByTagName('td')[0], tableCellCurrentBg, imgprefix);
                            }
                        }
                        break;
                    
                    default:
                        tableCellNewBg = checkMouseOver(tableRows[j].getElementsByTagName('td')[0], tableCellCurrentBg, imgprefix);
                        break;
                }
                
                if (tableCellNewBg)
                {
		            setBackgroundImage(tableRows[j].getElementsByTagName('td')[0], tableCellNewBg);
                }
            }
        }
    }
}

function getBackgroundImage(object)
{
    // Gets background image from object
    if(object.currentStyle)
    { 
        // IE Opera
        return object.currentStyle.backgroundImage; 
    } 
    else
    { 
        // Firefox needs the full css code to work 
        return getComputedStyle(object,'').getPropertyValue('background-image');
    }
}

function setBackgroundImage(object, bgimage)
{
    object.style.backgroundImage = bgimage;
/*
    if(object.currentStyle)
    { 
        // IE Opera
        object.currentStyle.setAttribute('cssText','background-image: ' + bgimage + ';');
    } 
    else
    { 
        // Firefox needs the full css code to work 
        getComputedStyle(object,'').setPropertyValue('background-image') = bgimage;
    }
*/
}

function checkMouseOver(tablecell, bgimage, imgprefix)
{
    var returnvalue;

    // Check if MouseOver class is active on table cell.
    // Set correct image depending on outcome.
    if(tablecell.className.search('MouseOver') != -1)
    {
        // Set image for mouseover: add 'over' to the image url.
        if (bgimage.search('_over') == -1)
        {
            // no match
            returnvalue = bgimage.substring(0, bgimage.search(imgprefix)) + imgprefix + '_over' + bgimage.substring(bgimage.search(imgprefix) + imgprefix.length);
        }
        else
        {
            // match
            returnvalue = bgimage;
        }                
    }
    else
    {
        // Set image for normal: remove 'over' if it is present in the image url.
        if (bgimage.search('_over') != -1)
        {
            returnvalue = bgimage.substring(0, bgimage.search('_over')) + bgimage.substring(bgimage.search('_over') + '_over'.length);
        }
    }
    
    return returnvalue;
}