/* 
 * Functions to preload/swap images
 */


// detect browser capabilities
var capable = (document.getElementById && document.getElementsByTagName);


// 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] && MImages[a].img_id == id)
            return a;
    }
    return -1;
}

// 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( MImages[imgId].swapped == 0 && (filename.indexOf('_ON') == -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 == 1) 
        {
            theImg.src = MImages[imgId].img_out.src;
            MImages[imgId].swapped = 0;
        }
    }
}

// Set the image active and set the swapped bit (prevent reverting)
function setSwapped(image_id)
{
    var imgId = get_image(image_id);

    if(capable && MImages[imgId]) 
    {
        var theImg = document.getElementById(MImages[imgId].img_id);
        theImg.src = MImages[imgId].img_in.src;
        MImages[imgId].swapped = 2;
    }

    return;
}

// Reset all menu images to the 'OFF' state, optionally within a group
function resetMenuImages(groupname)
{
    if(groupname)
    {
        // Reset images within a group
        for(var a=0; a<=MImages.length; a++)
        {
            if(capable && MImages[a] && MImages[a].img_id) {
                // Only reset if it matches the groupname
                if( MImages[a].img_id.match(groupname) ) {
                    var theImg = document.getElementById(MImages[a].img_id);
                    theImg.src = MImages[a].img_out.src;
                    MImages[a].swapped = 0;
                }
            }
        } 
    }
    else
    {
        // Reset all images
        for(var a=0; a<=MImages.length; a++)
        {   
            if(capable && MImages[a] && MImages[a].img_id) {
                var theImg = document.getElementById(MImages[a].img_id);
                theImg.src = MImages[a].img_out.src;
                MImages[a].swapped = 0;
            }
        }
    }
    return;
}

/*
 * 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
 */
var newwin='';

// 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) ? 0 : 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;
}


// outputs HTML for the 'random' homepage image
function writeHomeImage(id)
{
    var images = Array(
        "homepage_photo1.jpg",
        "homepage_photo2.jpg",
        "homepage_photo3.jpg",
        "homepage_photo4.jpg",
        "homepage_photo5.jpg",
        "homepage_photo6.jpg",
        "homepage_photo7.jpg"
    );
    var alttext = Array(
        "El sitio del Departamento de Educaci&oacute;n de EE.UU. que sirve como fuente de informaci&oacute;n gratuita sobre c&oacute;mo costear y prepararse para los estudios postsecundarios.",
        "El sitio del Departamento de Educaci&oacute;n de EE.UU. que sirve como fuente de informaci&oacute;n gratuita sobre c&oacute;mo costear y prepararse para los estudios postsecundarios.",
        "El sitio del Departamento de Educaci&oacute;n de EE.UU. que sirve como fuente de informaci&oacute;n gratuita sobre c&oacute;mo costear y prepararse para los estudios postsecundarios.",
        "El sitio del Departamento de Educaci&oacute;n de EE.UU. que sirve como fuente de informaci&oacute;n gratuita sobre c&oacute;mo costear y prepararse para los estudios postsecundarios.",
        "El sitio del Departamento de Educaci&oacute;n de EE.UU. que sirve como fuente de informaci&oacute;n gratuita sobre c&oacute;mo costear y prepararse para los estudios postsecundarios.",
        "El sitio del Departamento de Educaci&oacute;n de EE.UU. que sirve como fuente de informaci&oacute;n gratuita sobre c&oacute;mo costear y prepararse para los estudios postsecundarios.",
        "El sitio del Departamento de Educaci&oacute;n de EE.UU. que sirve como fuente de informaci&oacute;n gratuita sobre c&oacute;mo costear y prepararse para los estudios postsecundarios. (Photo copyright Jupiter Images Corporation, 2006)"
    );
    var theimage = getIncImage('homeimage', images.length); 
    var txt = '<img id="bannerimage" width="586" height="164" src="/students/v06/images_sp/saotw/home/' + images[theimage] + '" alt="' + alttext[theimage] + '" />\n';
    document.getElementById(id).innerHTML = txt;

    return;
}

// get a cookie's value
function getCookie(name)
{   
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
}

// set a cookie's name and value
function setCookie(name, value) 
{
    var today = new Date();
    var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days
    // no expiry = session cookie
    document.cookie = name + "=" + escape(value) + "; path=/;"; // expires=" + expiry.toGMTString();
} 


// Increment the image if they've been here before.
// Otherwise, show the first one.
function getIncImage(cookieName, arrLength)
{
    var current = getCookie(cookieName);
    if(current) {
        current++;
        if(current >= arrLength) {
            current = 0; 
        }
    } else {
        current = 0;
    }
    
    setCookie(cookieName, current);
    return current;
}

// Cross-browser script to add event listeners
function addListener(element, event, listener, bubble) {
  if(element.addEventListener) {
    if(typeof(bubble) == "undefined") bubble = false;
    element.addEventListener(event, listener, bubble);
  } else if(this.attachEvent) {
    element.attachEvent("on" + event, listener);
  }
}

// Add Bookmark
function addbookmark()
{
      window.external.AddFavorite(location.href,document.title)
}


// Return current file name
function geturl(currentURL)
{
	str=window.location.href
	url=str.match(/\w+\.jsp/)
	//alert(url)
		if(url==currentURL)
			return true;
		else
			return false;
}
