/* 
 * Various functions
 */

// detect browser capabilities
var capable = (document.getElementById && document.getElementsByTagName);
// used for image swapping
var imgSrc = '';
// used by image preloader
var arrTmpImage = [];
// used by the pulldown menu
var pdActive = 0;

// Runs when the DOM is ready
$(function()
{ 
  // Set equal heights for elements
  //$('.equalize').equalHeights();

  replace_png_with_gif();

  // Default settings for modal windows
  $.nyroModalSettings({
    debug:false,
    autoSizable:false,
    width:855,
    height:540,
    modal:false
  });

  // Activate links which should open in a modal box 
  $('a.nyromodal').nyroModal({ 
    endShowContent: function(elts, settings) { activate_scrollpanels(); }
  });
  $('a.videoplayer').nyroModal({ 
    forceType:'iframe',
    width:640,
    height:570
  });
  $('a.iframemodal').nyroModal({ 
    forceType:'iframe'
  });
  //$('a.map_overlay').nyroModal( {
  //  forceType:null,
  //  endShowContent: function(elts, settings) { embed_map_swf(); },
  //  beforeHideContent: function(elts, settings, callback) { hide_map_swf(); $.fn.nyroModal.settings.hideContent(elts, settings, callback); }
  //} ); 

  // Large accordion used on Nursing Specialties and Professional Growth
  $("#large_accordion").accordion({
    autoHeight:false,
    icons:{'header':'content-icon-plus', 'headerSelected':'content-icon-minus'},
    change:function(event, ui) {
      // Need to re-activate custom scrollbars after accordion change
      $('.scroll-pane').jScrollPaneRemove();
      activate_scrollpanels();
    }
  });

  // This needs to be activated AFTER the accordion to ensure the Scroll Panel areas which contain an Accordion are set to the correct height
  $("#main_tabs").tabs({
    show:function(event, ui) {
      $('.scroll-pane').jScrollPaneRemove();
      activate_scrollpanels();
    }
  });

  activate_pulldown_menu();

  activate_newsletter_box();

  activate_home_sliders();

  activate_newwin_links();

  activate_rollover_images();
});


// Runs when the page is fully loaded
$(window).load(function() {
}); 


// Add mouseover actions here
function activate_rollover_images()
{
    // 'Discover the Strength..'
    //$("#pulldown_nav").hover(function(){
    //  $(this).attr("src","/images/tagline_on.gif");
    //},function(){
    //  $(this).attr("src","/images/tagline_off.gif");
    //});

    // 'Search Careers'
    $("#search_btn img").hover(function(){
      $(this).attr("src","/images/search_careers-over.gif");
    },function(){
      $(this).attr("src","/images/search_careers.gif");
    });

    // 'Submit Your Search'
    $("#begin_search").hover(function(){
      $(this).attr("src","/images/btn_begin_search-over.gif");
    },function(){
      $(this).attr("src","/images/btn_begin_search.gif");
    });

    // 'Bookmark This Page'
    $("#bookmark_search img").hover(function(){
      $(this).attr("src","/images/btn_bookmark-over.gif");
    },function(){
      $(this).attr("src","/images/btn_bookmark.gif");
    });

    // 'Reset'
    $("#reset_search img").hover(function(){
      $(this).attr("src","/images/btn_reset-over.gif");
    },function(){
      $(this).attr("src","/images/btn_reset.gif");
    });

    // 'More'
    $(".morebtn img").hover(function(){
      $(this).attr("src","/images/btn_more-over.gif");
    },function(){
      $(this).attr("src","/images/btn_more.gif");
    });
}


// For IE6, replace PNG images with GIF
function replace_png_with_gif()
{
  if(jQuery.browser.msie && parseInt(jQuery.browser.version)== 6)
  {
    var imgSrc = "";
    var newSrc = "";

    $("img").each(function(i,o){
      imgSrc = $(o).attr("src");
      if( /.png$/.test(imgSrc) )
      {
        newSrc = imgSrc.replace(".png", ".gif");
        $(o).attr("src", newSrc);
      }
    });
  }
}


// Initialize the pulldown menu
function activate_pulldown_menu()
{
  // Activate the pulldown menu
  $("#pulldown_nav").click(function()
  {
    // Is the menu currently active?
    if(pdActive == 0)
    {
      $("#pulldown_nav").attr("src", "/images/tagline_on.gif");
      //$("#pulldown_nav").css({ "-moz-box-shadow":"3px 3px 3px #666", "-webkit-box-shadow":"3px 3px 3px #666", "box-shadow":"3px 3px 3px #666" });
      $("#pulldown_menu").slideDown("slow");
      $("#pulldown_menu").attr("tabIndex", 0);
      $("#pulldown_menu").focus(); 
      pdActive = 1;
    }
    else
    {
      hide_pulldown_menu();
    }
    return false;
  });

  // Hide the pulldown menu
  $("#pulldown_menu").mouseleave(function(){
    hide_pulldown_menu();
  });
}

function hide_pulldown_menu()
{
  $("#pulldown_menu").slideUp("slow").queue(function(){
    $("#pulldown_nav").attr("src", "/images/tagline_off.gif");
    //$("#pulldown_nav").css({ "-moz-box-shadow":"none", "-webkit-box-shadow":"none", "box-shadow":"none" });
    $(this).dequeue();
    pdActive = 0;
  });
}


// Initialize the custom scroll panels
function activate_scrollpanels()
{
  // Activate the scroll panels
  $('.scroll-pane').jScrollPane({
    showArrows:true,
    scrollbarWidth:15
  });
}

// Initialize the homepage sliders
function activate_home_sliders()
{
  // Homepage upper slider
  $('#slidepanel_upper').serialScroll({
    target:'.panel_wrapper',
    items:'div.inner',
    prev:'span.prev',
    next:'span.next',
    axis:'x',
    navigation:'#slider_nav li a',
    duration:900,
    interval:5000,
    force:true,
    cycle:true,
    onBefore:function( e, elem, $pane, $items, pos ) {
      e.preventDefault();
      if( this.blur ) this.blur();
      $("#slider_nav li a").removeClass("active");
      $("#slider_nav li:eq("+pos+") a").addClass("active");
    },
    onAfter:function( elem ) { }
  });

  // Homepage lower slider
  $('#slidepanel_lower').serialScroll({
    target:'.panel_wrapper',
    items:'div.inner',
    prev:'span.prev',
    next:'span.next',
    axis:'x',
    exclude:1,
    duration:900,
    force:true,
    cycle:true,
    onBefore:function( e, elem, $pane, $items, pos ) {
      e.preventDefault();
      if( this.blur ) this.blur();
    },
    onAfter:function( elem ) { }
  });

}


// Activate the entire table row as a link in the 'job search results' 
function enable_job_search_links()
{
  var myUrl = "";

  // Make all text in the TR red and underlined when hovered over
  $(".search_results tbody tr").hover(function(){
    $(this).find("td").css("color","#AF154F");
    $(this).find("td a").css("color","#AF154F");
    $(this).find("td").css("text-decoration","underline");
    $(this).find("td a").css("text-decoration","underline");
  },function(){
    $(this).find("td").css("color","#3E3E3E");
    $(this).find("td a").css("color","#3E3E3E");
    $(this).find("td").css("text-decoration","none");
    $(this).find("td a").css("text-decoration","none");
  });

  // Unbind previous click event
  $(".search_results tbody tr").unbind("click");

  // Make the entire TR clickable
  $(".search_results tbody tr").click(function(){
    myUrl = $(this).find("a").attr("href");
    if(myUrl) {
      location.href = myUrl;
    }
  });
}


// Reset the job search form
function reset_form(domID)
{
  $(domID + " input").each(function(){
    if( $(this).attr("type") == "text" ) {
      $(this).attr("value", "");
    }
    if( $(this).attr("type") == "radio" ) {
      $(this).attr("checked", 0);
    }
  });
  $(domID + " select").each(function(){
    $(this).attr("value", "");
  }); 
}


// Newsletter box functionality
function activate_newsletter_box()
{
  $("#newsletter_box").click(function(){
    if( $(this).attr("value") == "Email Address" ) {
      $(this).attr("value", "");
    }
  }); 
  $("#newsletter_box").blur(function(){
    if( $(this).attr("value") == "" ) {
      $(this).attr("value", "Email Address");
    }
  }); 
}


// Add browser bookmark
function add_bookmark(url, title)
{
  if($.browser.msie) {
    window.external.AddFavorite(url, title);
  } else {
    alert('Press CTRL-D to bookmark this page');
  }
}


// Make links with a class of 'newwin' open in a new window
function activate_newwin_links()
{
  var cont = true;
  var msg = "";
  var width = 800;
  var height = 600;
  var compact = 1;
  var arrRelVars = [];

  $("a.newwin").click(function(){
    if( $(this).attr("rel") && $(this).attr("rel") != "" )
    {
      var arrRel = $(this).attr("rel").split(",");
      var arrTmp = [];
      for(var i=0; i<arrRel.length; i++) {
        arrTmp = arrRel[i].split("=");
        arrRelVars[arrTmp[0]] = arrTmp[1]; 
      }

      // Get link attributes
      msg = (arrRelVars['m'] != "") ? arrRelVars['m'] : msg;
      compact = (arrRelVars['c'] != "") ? 0 : 1;

      if(arrRelVars['w'] != "") {
        width = (arrRelVars['w'] != "max") ? parseInt(arrRelVars['w']) : screen.width;
      }
      if(arrRelVars['h'] != "") {
        height = (arrRelVars['h'] != "max") ? parseInt(arrRelVars['h']) : screen.height;
      }

      // Message with confirmation
      if(msg) { 
        cont = confirm(msg);
      }
    }
    if(cont) {
      popWindow(this.href, width, height, compact);
    }
    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];
    }
}

// Change a form element's type in IE
function changeInputType(oldObject, oType) {
  var newObject = document.createElement('input');
  newObject.type = oType;
  if(oldObject.style.width) newObject.style.width = oldObject.style.width;
  if(oldObject.onclick) newObject.onclick = oldObject.onclick;
  if(oldObject.size) newObject.size = oldObject.size;
  if(oldObject.value) newObject.value = oldObject.value;
  if(oldObject.name) newObject.name = oldObject.name;
  if(oldObject.id) newObject.id = oldObject.id;
  if(oldObject.className) newObject.className = oldObject.className;
  oldObject.parentNode.replaceChild(newObject,oldObject);
  return newObject;
}



