// requires:
// updateHeaderMenuFooter.js
//
// function prefix = cmm_

// functions:
//
// showContent()
// adjustMainContentScrollHeight() 
// makeMenuItemsVisibleTimer
// moveInNewContent()
// moveOutOldContentThenLoadNewContent()
// loadNewContent()
// changeMainContent(strFile, strTitle)
// colourChangeLanguage(objLangRow, strColour, strBorderColour)
// highlightLanguage(objLangRow)
// unHighlightLanguage(objLangRow)
// unhighlightDeselectedMenuItems()
// highlightNewSelectedMenuItems()
// linkToPage()
// changeMenuItemClass(strOldStyleClass, strNewStyleClass)
// changeMainImage(strFilename, intImageWidth, intImageTop, intImageLeft, intImageHeight)
// moveOverCompuSquadTitleAndChangeMainContent()
// moveTitleTimerAndChangeMainContent()
// makeMenuItemsVisibleAndChangeMainContent()
// makeMenuItemsVisible()
// makeMenuItemsVisibleTimer
// insertMainImage(posX,posY,strImg)
// adjustMainImagePosition()
// changeMenuItemClass(strClass)
// changeMenuItemColour(strColour)
// fadeOutOldImageAndChangeMainImage(tdItem)
// changeLanguage(obj)
// displayError(e, strDescription)

function cmm_showContent()
{
    var e,ee;

    try
    {
        document.getElementById("divMainContent").style.MozOpacity = 1; //fix Mozilla bug
        document.getElementById("divMainContent").style.color = "#aaaaaa";
        
        try //statement can cause error in Avant Browser
        {
            document.getElementById("divMainContent").style.height = 
            parseInt(document.getElementById("divMainContent").style.height) + parseInt((gintMainContentHeight_new)/8);
        }
        catch (ee)
        {
            document.getElementById("divMainContent").style.height = document.getElementById("divMainContent").clientHeight + 1;
        }
        
        
        if (document.getElementById("divMainContent").offsetHeight >= gintMainContentHeight_new)
        {
            window.clearInterval(gInterval);
                                    
            //document.getElementById("divMainContent").style.height = "auto";
            document.getElementById("divMainContent").style.color = "black";
            
                                    
            try
            {
                document.getElementById("imgHeading").style.visibility = "visible"; // in case not already visible
                    
            }
            catch (ee)
            {
            }

            try
            {
                document.getElementById("divMainContentBorder").className = "mainContentBorder";
            }
            catch (ee)
            {
            }            

            gisFinishedDisplayingMainContent = true;
        }

    }
    catch (e)
    {
        cmm_displayError(e, "An error has occurred whilst trying to display the new content.");
        window.clearInterval(gInterval);
    }
	
}

function cmm_adjustMainContentScrollHeight()
{

    var e;

    try
    {
        var intDocumentHeight = 0;
        var intMainContentHeight = document.getElementById("divMainContentBorder").scrollHeight;
        //var otblFooter = document.getElementById("tblFooter");
        var oDivMainContent = document.getElementById("divMainContent");
        var oDivMainContentBorder = document.getElementById("divMainContentBorder");
                
        if (parseInt(document.body.clientHeight) > oDivMainContent.offsetTop - 75)
        {
            if (intMainContentHeight > (parseInt(document.body.clientHeight) -  oDivMainContent.offsetTop - 75))
            {
                if (navigator.appName.indexOf("Explorer") != -1)  // if internet explorer, use document.body.scrollHeight
                {
                        oDivMainContentBorder.style.overflowY = "scroll";
                }
                else
                {
                        oDivMainContentBorder.style.overflow = "-moz-scrollbars-vertical";
                }
                
                oDivMainContentBorder.style.height =
                document.body.clientHeight - (oDivMainContent.offsetTop + 95);
                 
                oDivMainContent.style.height = "auto";
                 
                gintMainContentHeight_new = parseInt(oDivMainContent.offsetHeight);
            }
            else
            {
                oDivMainContentBorder.style.height = "auto";
                oDivMainContentBorder.style.overflow = "visible";
            }
        }
        else
        {
            oDivMainContentBorder.style.height = "auto";
            oDivMainContentBorder.style.overflow = "visible";
        }
    }
    catch (e)
    {
    }
}

function cmm_hideContentThenLoadNewContent()
{
    var e;

    try
    {
        var intBound = parseInt((gintMainContentHeight_old)/6) + 2;

        if (parseInt(document.getElementById("divMainContent").style.height) <= intBound)
        {	
            document.getElementById("divMainContent").style.color = "whitesmoke";
            document.getElementById("divMainContent").style.MozOpacity = 0; // fix Mozilla error
            window.clearInterval(gInterval);		
            document.getElementById("divMainContent").style.visibility = "hidden";
            document.getElementById("divMainContent").style.height = 2;	
            cmm_loadNewContent();
        }	
        else
        {
            document.getElementById("divMainContent").style.height = 
            (parseInt(document.getElementById("divMainContent").style.height) - parseInt((gintMainContentHeight_old)/6)) + 1;			
        }

    }
    catch (e)
    {
        cmm_displayError(e, "An error has occurred whilst trying to hide the old content.");
        window.clearInterval(gInterval);
    }
	
}

function cmm_moveInNewContent()
{	
    document.getElementById("divMainContent").style.color = "whitesmoke"; //disappear
    document.getElementById("divMainContent").style.MozOpacity = 0; // fix Mozilla error
    ftr_alignContent(); //just in case not already aligned
    document.getElementById("divMainContent").style.height = "auto";
    gintMainContentHeight_new = document.getElementById("divMainContent").offsetHeight;
    ftr_alignContentAndFooter(); //just in case footer is now in bad position.
    document.getElementById("divMainContent").style.height = 2;
    gInterval = window.setInterval("cmm_showContent()",32);
	
}

function cmm_moveOutOldContentThenLoadNewContent()
{
    gintMainContentHeight_old = document.getElementById("divMainContent").offsetHeight;
    document.getElementById("divMainContent").style.height = gintMainContentHeight_old;
    gInterval = window.setInterval("cmm_hideContentThenLoadNewContent()",3);
}

function cmm_loadNewContent()
{
    var strFile, strTitle;
    var e;
    strFile = gstrCurrentFile;
    strTitle = gstrCurrentTitle;
    
    document.getElementById("divMainContent").style.height = 2;
    //frames['frameStorageMainContent'].location.href = "generated_html/"+gstrLang+"/"+ strFile +"_"+gstrLang+".html";
    // href caused errors with back button.
    
    try
    {
        frames['frameStorageMainContent'].location.replace("generated_html/"+gstrLang+"/"+ strFile +"_"+gstrLang+".html");
    }
    catch (e)
    {
        frames['frameStorageMainContent'].location.replace("generated_html/"+gstrLang+"/home_"+gstrLang+".html");
    }
		
}

function cmm_updateNewContent(strFrameName)
{

    var strTitle = gstrCurrentTitle;
        
    if ((frames[strFrameName].document.getElementById("taHtmlContainer")) && 
       (document.getElementById("tblMenu"))) //make sure that the menu is loaded
    {
        
        gstrFrameToUse[gstrFrameToUse.length] = strFrameName;
        gstrDivToUse[gstrDivToUse.length] = 'divMainContent';
        
        leh_displayContents();
        
        document.getElementById("divMainContent").style.visibility = "visible";
        cmm_moveInNewContent();
    
        document.title = "CompuSquad Ltd. - " + strTitle;
    }
}

function cmm_changeMainContent(strFile, strTitle)
{
    var e, ee;
    
    try
    {
        gstrCurrentFile = strFile;
        gstrCurrentTitle = strTitle; 
        
        try
        {
                document.getElementById("divMainContentBorder").style.overflowY = "hidden"; //for testing - 9th June 2004
        }
        catch (ee)
        {
        }
        cmm_moveOutOldContentThenLoadNewContent();
    }
    catch (e)
    {
        cmm_displayError(e, "An error has occurred whilst trying to change the main content.");
    }
}

function cmm_colourChangeLanguage(objLangRow, strColour, strBorderColour)
{
    var objLangTextCell = objLangRow.cells[1];
    objLangTextCell.style.color = strColour;
}

function cmm_highlightLanguage(objLangRow)
{
    cmm_colourChangeLanguage(objLangRow, "yellow", "yellow");
    menu_closeSubMenu();
}

function cmm_unHighlightLanguage(objLangRow)
{
    cmm_colourChangeLanguage(objLangRow, "white", "black");
}

function cmm_unhighlightDeselectedMenuItems()
{
    var otdOldMenuItem = null;
            
    /// must do this document.all because of duplicates 
    // I clone submenus		
    // try this code first:

    otdOldMenuItem = document.getElementsByName(gstrTdCurrentMenuItemId).item(1);
            
    // if not an array, then simply take value from one Id
            
    if (otdOldMenuItem != null)
    {
            otdOldMenuItem.style.backgroundColor = "";
            otdOldMenuItem = document.getElementsByName(gstrTdCurrentMenuItemId).item(0);
            otdOldMenuItem.style.backgroundColor = "";
    }
    
    otdOldMenuItem = document.getElementById(gstrTdCurrentMenuItemId);

    if (gstrTdCurrentMenuItemParentId != null)
    {			
            document.getElementById(gstrTdCurrentMenuItemParentId).style.backgroundColor = "";
    }

    otdOldMenuItem.style.backgroundColor = "";

}

function cmm_highlightNewSelectedMenuItems(tdItem, strItemParent)
{
    gstrTdCurrentMenuItemId = tdItem.getAttribute("id");

    if (strItemParent != null)
    {
            gstrTdCurrentMenuItemParentId = strItemParent;
            document.getElementById(strItemParent).style.backgroundColor = "#089F99";
    }
    else
    {
            gstrTdCurrentMenuItemParentId = null;
    }
       
    tdItem.style.backgroundColor = "#089F99";	

    /// must do this document.all because of duplicates 
    // I clone submenus		
    // try this code first:

    var tdCopyItem = document.getElementsByName(gstrTdCurrentMenuItemId).item(0);

    if (tdCopyItem != null)
    {
            tdCopyItem.style.backgroundColor = "#089F99";
    }
}

function cmm_linkToPage(tdItem, strItemParent)
{
    var e;
    
    if (tdItem == null) //in case switching langs and link not in new lang
    {
        gstrTdCurrentMenuItemId = "menuItem_1"; //beware of this - check that works in IE5
        if (gstrTdCurrentMenuItemParentId != null)
        {			
            document.getElementById(gstrTdCurrentMenuItemParentId).style.backgroundColor = "";
            gstrTdCurrentMenuItemParentId = null;
        }
        
        strItemParent = null;
        tdItem = document.getElementById(gstrTdCurrentMenuItemId);
        
    }
    
    if (gisFinishedDisplayingMainContent)
    {
        try
        {
                clearInterval(gIntervalFadeOutImage); //stop fading out image if already doing so
        }
        catch (e){}

        try
        {
                clearInterval(gIntervalFadeInImage); //stop fading in image if already doing so
        }
        catch (e){}

        gisFinishedDisplayingMainContent = false;

        cmm_unhighlightDeselectedMenuItems();

        if (strItemParent == "")
        {
                strItemParent = null;
        }

        cmm_highlightNewSelectedMenuItems(tdItem, strItemParent);
                                        
        with (tdItem)
        {
            cmm_changeMainContent(getAttribute("filedata"), getAttribute("title"));
        }
                                        
        cmm_fadeOutOldImageAndChangeMainImage(tdItem);	

    }
}

function cmm_moveOverCompuSquadTitleAndChangeMainContent()
{
    gisFinishedDisplayingMainContent = false;
    gintMoveCompuSquadPercent = 0;
    gIntervalMoveOverCompu = window.setInterval("cmm_moveTitleTimerAndChangeMainContent()",1);
}


function cmm_moveTitleTimerAndChangeMainContent()
{
    var e,ee;
    
    try
    {
        gintMoveCompuSquadPercent = gintMoveCompuSquadPercent + 10;
        document.getElementById("tdMoveCompuSquad").width = gintMoveCompuSquadPercent;

        if (gintMoveCompuSquadPercent >= (document.body.clientWidth - 400))
        {
            document.getElementById("tdCompuSquad").style.textAlign = "right";
            document.getElementById("tdMoveCompuSquad").width = "100%";
            window.clearInterval(gIntervalMoveOverCompu);
            document.getElementById("imgLogo").style.display = "block";
            cmm_makeMenuItemsVisibleAndChangeMainContent();
            try // a fix for a bug I don't understand
            {
                document.getElementById("td_"+gstrLang).style.border = "1px white solid"; //highlight language selected
                                                                                          //most probably English
            }
            catch (ee) {}
                
        }

    }
    catch (e)
    {
        cmm_displayError(e, "An error has occurred whilst trying to move the title over.");
        window.clearInterval(gIntervalMoveOverCompu);
    }
	
}

function cmm_makeMenuItemsVisibleAndChangeMainContent()
{
    cmm_makeMenuItemsVisible(true);
}

function cmm_makeMenuItemsVisible(isChangeMainContent)
{	
    gintCurrentColourIndex = 0;
    gisChangeMainContent = isChangeMainContent;
    gahexColourChanges = new Array("aabbbb","afbfbf","bbcccc","bfcfcf","ccdddd",
                                   "cfdfdf","ddeeee","dfefef","ffffff");

    gIntervalMenuItemsVisible = window.setInterval("cmm_makeMenuItemsVisibleTimer()",2);
	
}

function cmm_makeMenuItemsVisibleTimer()
{
    var e;
    
    try
    {

        cmm_changeMenuItemColour("#"+gahexColourChanges[gintCurrentColourIndex]);
        ++gintCurrentColourIndex;	

        if (gintCurrentColourIndex >= gahexColourChanges.length)
        {
            window.clearInterval(gIntervalMenuItemsVisible);
            cmm_changeMenuItemClass("unhighlightedItem", "finalHighlightedItem");
            cmm_changeMenuItemColour("");
            if (gisChangeMainContent) 
            {
            
                var otdHome = document.getElementById("menuItem_1");

                with (otdHome)
                {
                    gisLoadingImage = true;
                    cmm_changeMainContent(getAttribute("filedata"), getAttribute("title"));
                    window.setTimeout("cmm_changeMainImage('"+getAttribute("image")+"', '"+ getAttribute("imageWidth") + "', '"+
                                      getAttribute("imageTop") + "', '" + getAttribute("imageLeft") + "', '" +
                                      getAttribute("imageHeight")+"')", 600);
                }
            }
            
            document.getElementById("tblHomeContact").style.visibility = "visible";
        }
    }
    catch (e)
    {
        cmm_displayError(e, "An error has occurred whilst doing the initial page display.");
        window.clearInterval(gIntervalMenuItemsVisible);
    }
    
}

function cmm_changeMenuItemClass(strOldStyleClass, strNewStyleClass)
{
    var e;
    var elements;

    elements = document.getElementById("tblMenu").rows[0].cells;

    for (var i = 0; i < elements.length; i++)
    {
        try
        {
            if (elements.item(i).className == "unhighlightedItem")
            {
                    elements.item(i).className = strNewStyleClass;
            }
        }
        catch (e)
        {
        }

    }
}

function cmm_changeMenuItemColour(strColourValue)
{
    var elements;

    elements = document.getElementById("tblMenu").rows[0].cells;

    for (var i = 0; i < elements.length; i++)
    {
        elements.item(i).style.color = strColourValue;
    }
}


function cmm_changeLanguage(obj)
{
    if (gisFinishedDisplayingMainContent)
    {
        
        var otrLanguages = document.getElementById("trLanguages");
        for (i=0; i<otrLanguages.cells.length; ++i)
        {
            otrLanguages.cells[i].style.borderColor = "#037E7F";
        }
        
        //document.getElementById("tblHomeContact").style.visibility = "hidden";

        obj.style.borderColor = "white";
        gstrLang = obj.getAttribute("lang");
        gintCountToDisplay = 2;
        gisLinkToCurrentlySelectedPage = true;
        uhmf_updateFooter();
        uhmf_updateMenu();
        init_preloadPages(); //have pages ready and cached for use.
    }
}

function cmm_changeMainImage(strFilename, intImageWidth, intImageTop, intImageLeft, intImageHeight)
{
    var odivImg = document.getElementById("divImg");
    var e, ee;

    try
    {

        if (odivImg.filters) // internet Explorer
        {
            odivImg.filters.alpha.opacity = 0; //make sure image has disappeared
        }
        else //Mozilla
        {
            odivImg.style.MozOpacity = 0;				
        }
                    
        odivImg.style.visibility = "visible";

        try // clear interval just in case not already cleared.
        {
                clearInterval(gIntervalFadeOutImage);
        }
        catch (ee)
        {
        }

        if (intImageTop == "")
        {
                odivImg.style.top = gintImageDefaultTop + "px";
        }
        else
        {
                odivImg.style.top = intImageTop + "px";
        }

        if (intImageLeft == "")
        {
                odivImg.style.left =  gintImageDefaultLeft + "px";
        }
        else
        {
                odivImg.style.left = intImageLeft + "px";		
        }
        
        if (navigator.userAgent.indexOf("Opera") != -1)  // if internet explorer, use document.body.scrollHeight
        {
            odivImg.style.left = (parseInt(odivImg.style.left) + 40) + "px";
        }                

        if (intImageWidth == "")
        {
                gintCurrentImageWidth = gintImageDefaultWidth;
        }
        else
        {
                gintCurrentImageWidth = parseInt(intImageWidth);
        }

        if (intImageHeight == "")
        {
                gisRestrictImageHeight = false;
        }
        else
        {
                gisRestrictImageHeight = true;
                gintMaxImageHeight = intImageHeight;
        }

        gintCurrentImageTop = odivImg.style.top;

        odivImg.style.width = gintCurrentImageWidth + "px";	

        odivImg.style.backgroundImage = "url(images/" + strFilename + ".gif)";

        if (odivImg.filters) // internet Explorer
        {
                odivImg.filters.alpha.opacity = 0;
        }
        else //Mozilla
        {
                odivImg.style.MozOpacity = 0;
        }

        cmm_adjustMainImagePosition();
        odivImg.style.visibility = "visible";
        gIntervalFadeInImage = window.setInterval("cmm_appearImage()", 30);

    }
    catch(e)
    {
        cmm_displayError(e, "An error has occurred whilst trying to insert a new main image.");
    }
}

function cmm_adjustMainImagePosition()
{
    if (!gisAdjustMainImage) {return false;} //used when scrolling image for effect

    var odivImg = document.getElementById("divImg");
    var odivMainContent = document.getElementById("divMainContent");
    var e;	

    odivImg.style.height = 1;
    odivImg.style.visibility = "hidden";
    
    var intDocumentHeight = 0;
    
    if (navigator.userAgent.indexOf("Opera") != -1)
    {
        if (document.body.clientHeight > parseInt(document.body.scrollHeight))
        {
            intDocumentHeight = document.body.clientHeight;
        }
        else
        {
            intDocumentHeight = parseInt(document.documentElement.scrollHeight);
        }
    }
    else if (navigator.appName.indexOf("Explorer") != -1)  // if internet explorer, use document.body.scrollHeight
    {
        intDocumentHeight = parseInt(document.body.scrollHeight);
    }
    else // if mozilla or netscape, use document.documentElement.scrollHeight
    {
            intDocumentHeight = parseInt(document.documentElement.scrollHeight);
    }

    try
    {
        if (intDocumentHeight - odivImg.offsetTop - 25 > 0)
        {
            if ((!gisRestrictImageHeight) || (gintMaxImageHeight > intDocumentHeight - odivImg.offsetTop - 25))
            {
                    odivImg.style.height = (intDocumentHeight - odivImg.offsetTop - 25);
            }
            else
            {
                    odivImg.style.height = gintMaxImageHeight;
            }
        }
        else
        {
                odivImg.style.height = 0;
        }

        if (odivImg.offsetLeft > (document.body.clientWidth - 15))
        {	
                odivImg.style.width = 0;
        }
        else 
        {
        
            if (odivImg.offsetLeft + parseInt(gintCurrentImageWidth) > (document.body.clientWidth - 15))
            {
                    odivImg.style.width = document.body.clientWidth - odivImg.offsetLeft - 15;
            }
            else
            {
                    odivImg.style.width = gintCurrentImageWidth;
            }
        }

        odivImg.style.visibility = "visible";
    }
    catch (e)
    {
        cmm_displayError(e, "An error has occurred whilst trying to adjust the image position.");
    }

	
}

function cmm_fadeOutOldImageAndChangeMainImage(tdItem)
{
    gisLoadingImage = true;
    gIntervalFadeOutImage = window.setInterval("cmm_disappearImage()", 23);

    with (tdItem)
    {
        window.setTimeout("cmm_changeMainImage('"+getAttribute("image")+"', '"+ getAttribute("imageWidth") + "', '"+
                                                  getAttribute("imageTop") + "', '" + getAttribute("imageLeft") + "', '" +
                                                  getAttribute("imageHeight")+"')", 780);
    }
}

function cmm_disappearImage()
{

    var odivImg = document.getElementById("divImg");
    var e;

    try
    {
        if (odivImg.filters) // internet Explorer
        {
            if (odivImg.filters.alpha.opacity >= 3)
            {
                    odivImg.filters.alpha.opacity = parseInt(odivImg.filters.alpha.opacity) - 3;
            }
            else
            {
                    odivImg.filters.alpha.opacity = 0;
                    clearInterval(gIntervalFadeOutImage);
            }
        }
        else //Mozilla
        {

            if (parseFloat(odivImg.style.MozOpacity) >= .021)
            {
                    odivImg.style.MozOpacity = (parseFloat(odivImg.style.MozOpacity*100) - 2)/100;
            }
            else
            {
                    odivImg.style.MozOpacity = 0;				
                    clearInterval(gIntervalFadeOutImage);
            }
        }
    }
    catch (e)
    {
        clearInterval(gIntervalFadeOutImage);
        cmm_displayError(e, "Error has occurred whilst fading in image.");
    }
}


function cmm_appearImage()
{

    var odivImg = document.getElementById("divImg");
    var e;

    try
    {

        if (odivImg.filters) // internet Explorer
        {
            if (odivImg.filters.alpha.opacity <= 40)
            {
                    odivImg.filters.alpha.opacity = parseInt(odivImg.filters.alpha.opacity) + 5;
            }
            else
            {
                    odivImg.filters.alpha.opacity = 45;
                    clearInterval(gIntervalFadeInImage);
            }
        }
        else //Mozilla
        {
            if (parseFloat(odivImg.style.MozOpacity) <= .40)
            {
                    odivImg.style.MozOpacity = (parseFloat(odivImg.style.MozOpacity * 100) + 5)/100;
            }
            else
            {
                    odivImg.style.MozOpacity = .45;
                    clearInterval(gIntervalFadeInImage);
            }
        }
        gisLoadingImage = false;
    }
    catch (e)
    {
        gisLoadingImage = false;
        clearInterval(gIntervalFadeInImage);
        cmm_displayError(e, "Error has occurred whilst fading in image.");
    }
}

function cmm_displayError(e, strDescription)
{
    alert(strDescription + " \nError Description: " + e.message + "\nPlease report this to: info@compusquad.ie.");
    init_clearAllIntervals();
}