/* 
 * Various functions
 */

// Detect browser capabilities
var capable = (document.getElementById && document.getElementsByTagName);
// Used by image preloader
var arrTmpImage = [];
// Default value to appear in search box
var sbValue = 'Google';

// Actions to run on typical page load
$(function() 
{ 
  // Activate menus
  $('ul.sf-menu').superfish({
    autoArrows:true,
    dropShadows:false
  });

  // Put default value in the search form
  $("#searchbox").attr("value", sbValue);

  // Attach behaviors to search form
  $("#searchbox").focus(
    function() {
      if( $(this).attr("value") == sbValue ) {
        $(this).attr("value", "");
      }
    }).blur(
    function() {
      if( $(this).attr("value") == "") {
        $(this).attr("value", sbValue);
      }
    });

  // Attach behavior to search button
  $("#searchbtn").click(
    function() {
      if( $("#searchbox").attr("value") == "" || $("#searchbox").attr("value") == sbValue ) {
        return false;
      } else {
        return true;
      }
    });

  // Activate the 'new window' links
  activate_newwin_links();
}); 



// This runs when the page is fully loaded
jQuery(window).load(function() {
  // Fix heights of columns
  $("#wrapper").equalHeights();
  //$("#content_container").equalHeights();
});



// Make links with a class of 'newwin' open in a new window
function activate_newwin_links()
{
  $(".newwin").click(function(){
    if(this.href && this.href != '') {
      popWindow(this.href,800,600,1);
      return false;
    }
  });

  // Links with a class of 'offsite' open in a new window after an interstitial page
  $(".offsite").click(function(){
    if(this.href && this.href != '') {
      popWindow('leaving.asp?' + this.href, 800, 600, 1);
      return false; 
    }
  });
} 

// 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'
}

// 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 0;
}

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

// swap in the image
function imgOn(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('_over') == -1 && filename.indexOf('_on') == -1 ) 
        //{
            theImg.src = MImages[imgId].img_act.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 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) ? 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;
}

function basename(path) {
  return path.replace(/\\/g,'/').replace( /.*\//, '' );
}

function dirname(path) {
  return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
}

// Preload an array of images
function preload(arrImages)
{
    for(var i=0; i<arrImages.length; i++) {
        arrTmpImage[i] = new Image;
        arrTmpImage[i].src = arrImages[i];
    }
}

function addbookmark(title, url)
{
  if($.browser.msie)
  {
    // Internet Explorer
    window.external.AddFavorite(url, title);
  }
  else if($.browser.mozilla) 
  {
    // Firefox 
    alert('Please press CTRL-D to bookmark this page!');
  }
  else if($.browser.opera) 
  {
    // Opera
    var elem = document.createElement('a');
    elem.setAttribute('href',url);
    elem.setAttribute('title',title);
    elem.setAttribute('rel','sidebar');
    elem.click();
  } 
  else if($.browser.safari)
  {
    // Webkit-based browsers
    alert('Please press CTRL-D to bookmark this page!');
  }
}

