var capable = (document.getElementById && document.getElementsByTagName);

/* 
 * Functions to preload/swap images
 */

// MenuImage(image_id, normal_image, mouseover_image, image_swapped)
var MImages = new Array(
    new MenuImage('m_gen', 'top_homenav_geninfo.gif', 'top_homenav_geninfo-sel.gif', 0),
    new MenuImage('m_opp', 'top_homenav_opps.gif', 'top_homenav_opps-sel.gif', 0),
    new MenuImage('m_car', 'top_homenav_careers.gif', 'top_homenav_careers-sel.gif', 0),
    new MenuImage('m_new', 'top_homenav_latest.gif', 'top_homenav_latest-sel.gif', 0),
    new MenuImage('m_faq', 'top_homenav_faqs.gif', 'top_homenav_faqs-sel.gif', 0),
    new MenuImage('m_eve', 'top_homenav_events.gif', 'top_homenav_events-sel.gif', 0),
    new MenuImage('m_inf', 'top_homenav_keep.gif', 'top_homenav_keep-sel.gif', 0),
    new MenuImage('m_con', 'top_homenav_contactus.gif', 'top_homenav_contactus-sel.gif', 0)
);

// MenuImage constructor
function MenuImage(img_id, img_out, img_in, swapped) 
{
    this.img_id = img_id;           // the image id
    this.img_out = new Image();     // the image to use when mouseout
    this.img_out.src = 'images/home/'+img_out;
    this.img_in = new Image();      // the image to use when mouseover
    this.img_in.src = 'images/home/'+img_in;
    this.swapped = swapped;         // register if the image is swapped
    this.over = 0;                  // register if the image is 'over'
}

// take a full or partial path, and return it's last part
function basename(path) 
{
    var parts = path.split('/');
    return parts[parts.length-1];
}

// swap in the image
function imgIn(imgId) 
{
    if(capable && MImages[imgId]) 
    {
        var theObj = MImages[imgId];
        var theImg = document.getElementById(theObj.img_id);
        var filename = basename(theImg.src);

        // don't swap if image is 'down' 
        if(filename.indexOf('-sel') == -1) 
        {
            theImg.src = MImages[imgId].img_in.src;
            MImages[imgId].swapped = 1;
        }
    }
}

// swap out the image
function imgOut(imgId) 
{
    if(capable && MImages[imgId]) 
    {
        theImg = document.getElementById(MImages[imgId].img_id);
        // only revert if "swapped" is true
        if(MImages[imgId].swapped) 
        {
            theImg.src = MImages[imgId].img_out.src;
            MImages[imgId].swapped = 0;
        }
    }
}




/*
 * Functions to swap the background color of a div 
 */

var savedBG = new Array();

function changeBG(id, color)
{
    if(capable && id && color)
    {
        var objStyle = document.getElementById(id).style;
        savedBG[id] = objStyle.backgroundColor;
        objStyle.backgroundColor = "#"+color;
    }
}

function changeBackBG(id)
{
    if(capable && id && savedBG[id])
    {
        var objStyle = document.getElementById(id).style;
        objStyle.backgroundColor = savedBG[id];
        savedBG[id] = ""; 
    }
}

