/* 
 * Common Functions 
 */


// Detect Browser Capabilities
var capable = (document.getElementById && document.getElementsByTagName);
// New Window
var newwin = '';
// Mouse Position
var mouseX = 0;
var mouseY = 0;
// Event Target
var target = '';


// MenuImage constructor
function MenuImage(img_id, img_out, img_in, img_act, swapped) 
{
    this.img_id = img_id;           // the image id
    this.img_out = new Image();     // the image to use when mouseout
    this.img_out.src = imageDir + img_out;
    this.img_in = new Image();      // the image to use when mouseover
    this.img_in.src = imageDir + img_in;
    this.img_act = new Image();     // the image when page active
    if(img_act) {
        this.img_act.src = imageDir + img_act;
    } else {
        this.img_act.src = '';
    }    
    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];
}

// get an image from an image_id
function get_image(id)
{
    for(var a=0; a<=MImages.length; a++)
    {
        if(MImages[a].img_id == id)
            return a;
    }
}

// swap in the image
function imgIn(image_id)
{
    var imgId = get_image(image_id);

    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' or currently active 
        if( filename.indexOf('_at') == -1 &&
           (filename.indexOf('_over') == -1 || filename.indexOf('_on') == -1) ) 
        {
            theImg.src = MImages[imgId].img_in.src;
            MImages[imgId].swapped = 1;
        }
    }
}

// swap out the image
function imgOut(image_id) 
{
    var imgId = get_image(image_id);

    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] = ""; 
    }
}


/*
 * Functions to open links in a new window
 */

// open a popup window
function popWindow(theUrl,width,height,full) {
    // use defaults if width and height were not supplied
    var ismoz = navigator.userAgent.indexOf("Gecko");
    var isie = navigator.userAgent.indexOf("MSIE");
    var default_width = 600;
    var default_height = 450;
    var offset_width = (isie != -1) ? 4 : 0;
    var offset_height = (isie != -1) ? 45 : 0;
    var popType = (full) ? ",scrollbars=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,location=yes,directories=yes" : ",scrollbars=no,resizable=no,status=no";
    
    var popWidth = (width) ? width + offset_width : default_width + offset_width;
    var popHeight = (height) ? height + offset_height : default_height + offset_height;
    var popLeft = self.screen.availWidth/2 - popWidth/2;
    var popTop = self.screen.availHeight/2 - popHeight/2;
    
    if(theUrl) {
        if(newwin)
            newwin.close();
        newwin = window.open(theUrl,'newwin','left='+popLeft+',top='+popTop+',width='+popWidth+',height='+popHeight+popType);
    }

    return;
}

// This will convert the querystring to an associative array
function querystring_to_array(q)
{
    var arr1 = q.split('&'); 
    var arr2 = new Array();
    var arr3 = new Array();

    for(var i=0; i<arr1.length; i++) {
        arr2 = arr1[i].split('=');
        arr3[arr2[0]] = arr2[1]; 
    }

    return arr3;
}


// Display description for menu items
function menuTextDisplay(e,obj,txt) 
{
    // Get the event object
    if (!e) var e = window.event;
    // Find position of mouse pointer in order to position the DHTML layer
    findMousePos(e); 
    // Find object which triggered the event 
    target = findMouseTarget(e);
    // Set 'onmouseout' event for the object
    // code here
    // Set a timer to show the DHTML layer if conditions are still valid
    menuTimer = window.setTimeout("showMenuTxt('" + txt + "')", 500); 
}


// Display description for menu items
function showMenuTxt(txt) 
{
    var div = document.getElementById('hidden'); 
    //var xDiff = Math.abs(x-mouseX);
    //var yDiff = Math.abs(y-mouseY);
    //alert(xDiff + ',' + yDiff);
    //alert(mouseX + ',' + mouseY);

    //if(xDiff > 190 || yDiff > 20) {
    //    div.style.visibility = 'hidden';
    //    return;
    //}


    div.innerHTML = txt; 
    div.style.position = 'absolute';
    div.style.left = (mouseX+25) + 'px';
    div.style.top = (mouseY-5) + 'px';
    //div.style.left = (x+75) + 'px';
    //div.style.top = (y-5) + 'px';
    div.style.backgroundColor = '#DDD8C6';
    div.style.border = '1px solid #666';
    div.style.padding = '0.25em 0.5em';
    div.style.visibility = 'visible';
}


// Find position of a DOM object
function findPos(obj) 
{
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

// Find position of mouse
function findMousePos(e)
{
    if (e.pageX || e.pageY) {
        mouseX = e.pageX;
        mouseY = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        mouseX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        mouseY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
    // mouseX and mouseY contain the mouse position relative to the document
    return;
}

// What is triggering the event?
function findMouseTarget(e)
{
    var targ;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    return targ;
}
