﻿/**********************************************
Author: JRN
Purpose: Contains custom operations for removing
         empty elements from a page
***********************************************/

function hideElementIfEmpty(e) {
    if (e != undefined && e != null) {
        if (e.innerText == '') {
            e.style.visibility = 'hidden';
        }
    }
}
function hideIfEmpty(id) {
    if (id != undefined && id != null) {
        var e = document.getElementById(id);
        if (e != undefined && e != null) {
            var s = e.innerText;
            s = s.replace(/&nbsp;/g, '');
            s = s.replace('&#32', '');
            s = s.replace(' ', '');
            if (s.length == 1) {
                e.style.display = 'none';
            }
        }
    }
}

function elementHasIMG(id) {
    var e = document.getElementById(id);
    if (e != undefined && e != null) {
        var i = e.getElementsByTagName("img");
        if (i.length > 0)
            return true;
    }
    return false;
}

function setClassByTagName(tag, c)
{
    var e = document.getElementsByTagName(tag);
    for(var i = 0; i < e.length; i++)
    {
        e[i].className = c;
        //alert(e[i].className);
    }
}
// Customize this function
function prepareForSifr()
{
    //alert('prepare');
    var hasImage = false;
    if (elementHasIMG("cbre_PageImageCenter")) {
        hasImage = true;
    }
    if (elementHasIMG("cbre_PageImageRight")) {
        hasImage = true;
    }    
    if(hasImage)
    {
        //alert('hasImage');
        setClassByTagName("h1", "hasImage");
        setClassByTagName("h5", "hasImage");
    }

}
function setNavSelected(a) {
    a.className += " selected";
}
function removeArrow(tr, title) {
    
    var a = tr.getElementsByTagName("td");
    if (a != null) {
        for (var i = 0; i < a.length; i++) {
            
            a[i].style.backgroundImage = 'none';
        }
    }
}
function clearSelected(links,x) {
    if (links != undefined && links != null) {
        for (var i = 0; i < links.length; i++) {
            if(i!=x)
            links[i].style.color = '#006b54';
        }
    }
}
function isDefaultASPX(url)
{
    if (url.indexOf("default.aspx") <= 0) {
        return false;
    }
    return true;
}
function getAttrVal(a)
{
 if(a != undefined && a!= null)
    return a.value.toLowerCase();
  
 return null;
}
function formatNavigation(url) {
    var result = false;
    if (url != undefined && url != null) {
        url = url.toLowerCase();
        var isDefaultPage = isDefaultASPX(url);
        // ensure correct item is selected
        if (!isDefaultPage) {
            var links = getNavigationElement().getElementsByTagName("a")
            if (links != undefined && links != null) {
                for (var i = 0; i < links.length; i++) {
                    var href = getAttrVal(links[i].attributes["href"])
                    if (href.toLowerCase() == url) {
                        clearSelected(links, i);
                        setNavSelected(links[i]);
                        result = true;
                    }
                }
            }
        }
        // remove Arrows for third level nav
        var nav = getNavigationElement();
        if (nav != null) {
            var rows = getNavigationElement().getElementsByTagName("tr");

            if (rows != null) {
                for (var x = 0; x < rows.length; x++) {
                    if (rows[x].attributes["title"] != null) {
                        if (rows[x].attributes["title"].value.toLowerCase().indexOf("noarrow") != -1) {
                            removeArrow(rows[x], rows[x].attributes["title"].value);
                        }
                    }
                }
            }
        }
    }
    return result;
}
function getNavigationElement() {
    return document.getElementById('cbre_MasterPageLeftColumn');
}

function ensureNavHighLighted(url) {
    return formatNavigation(url);
}