// define global variables
var nav = navigator.userAgent;
var crs = (nav.indexOf('MSIE') != -1 && nav.substring((nav.indexOf('MSIE') + 4),(nav.indexOf('MSIE') + 6)) < 6) ? "hand" : "pointer";
var page = (
window.location.href.lastIndexOf('.html') != -1 || window.location.href.lastIndexOf('.php') != -1) ? window.location.href.substring(window.location.href.lastIndexOf('/')+1,window.location.href.lastIndexOf('.')) : 'index';

// common functions to run before page loads
PreloadMenuImages();
PreloadImages('images/bkgd.gif','images/logo.jpg');
SetPositionIEMac();

function OnloadEvents()
{
    BustFrames();
    RemoveLinkBox();
    SetExternalLinks();
    NavbarHover();
    SetActiveMenuLink();
    RemoveAltTooltips();
    if (typeof(ActiveMenu) == 'function') ActiveMenu();
    if (typeof(SetExternalLinksIE) == 'function') SetExternalLinksIE();
}

function RemoveAltTooltips() 
{ 
    if (document.images && nav.indexOf('MSIE') != -1) 
    { 
        var i, imgs = document.images; 
        if (imgs.length > 0 && imgs[0].getAttribute)
        {
            for (i = 0; i < imgs.length; i++) 
            { 
                if (imgs[i].getAttribute('alt') != '' && imgs[i].getAttribute('title') == '') imgs[i].setAttribute('title','');
                if (imgs[i].parentNode.getAttribute('title') != '') imgs[i].setAttribute('title',imgs[i].parentNode.getAttribute('title'));
            }
        } 
    } 
}

function PreloadMenuImages()
{
    /* this function is called before the page has loaded, so object detection does not work */
    /* create an array of folders whose pages do not use the menu */
    var exempt = new Array('/lab/','/dev/','/test/','/portfolio/'), e = 0;
    while (e < exempt.length)
    {
        if (window.location.href.indexOf(exempt[e]) != -1) return;
        e++;
    }
    PreloadImages('images/index_o.gif','images/access_o.gif','images/contact_o.gif','images/portfolio_o.gif','images/webstuff_o.gif','images/doodle_o.gif');
}

function PreloadImages()
{
    if (!document.images) return;
    for (var i = 0; i < PreloadImages.arguments.length; i++)
    {
        var newImg = new Image();
        newImg.src = PreloadImages.arguments[i];
    }
}

function NavbarHover()
{
    var menuitems = ['index','webstuff','portfolio','contact','access','doodle'];
    if (document.getElementById && document.getElementById('navbar') != null)
    {
        for (var i = 0; i < menuitems.length; i++)
        {
            if (document.getElementById(menuitems[i]).id != page) imgHover(document.getElementById(menuitems[i]));
        }
    }

}

function SetActiveMenuLink()
{
    if (document.getElementById && document.getElementById('navbar') != null)
    {
        var pg = document.getElementById(page);
        if (pg != null)
        {
            pg.src = 'images/' + page + '_o.gif';
            pg.onmouseover = function ()
            {
                this.style.cursor = 'default';
            }
        }
    }
}

function imgHover(ob)
{
    ob.onmouseover = function ()
    {
        this.src = 'images/' + this.id + '_o.gif';
    }
    ob.onmouseout = function ()
    {
        this.src = 'images/' + this.id + '.gif';
    }
}

function SetLink(el)
{
    document.getElementById(el).onmouseover = function ()
    {
        this.style.cursor = crs;
    }
    document.getElementById(el).onmouseout = function ()
    {
        this.style.cursor = "default";
    }
}

function BustFrames()
{
    if (window.top.location != window.self.location)
    {
        window.top.location.href = window.self.location.href;
    }
}

function RemoveLinkBox()
{
    if (!document.links) return;
    for (i = 0; i < document.links.length; i++)
    {
        document.links[i].onfocus = function ()
        {
            if (this.blur) this.blur();
        }
    }
}

function SetExternalLinks()
{
    if (!document.getElementsByTagName) return;
    var l, lnx = document.getElementsByTagName('a');
    for (l = 0; l < lnx.length; l++)
    {
        if (lnx[l].getAttribute('href') && lnx[l].getAttribute('rel') == 'external') lnx[l].target = 'external';
    }
}

function OpenWindow(url,name,features)
{
    wind = window.open(url,name,features);
    return wind;
}

function SetPositionIEMac()
{
    if (nav.indexOf('MSIE') != -1 && nav.indexOf('Mac') != -1)
    {
        var wrap = document.getElementById('wrapper');
        (wrap != null) ? wrap.style.marginTop = '20px' : setTimeout("SetPositionIEMac()",50);
    }
}

