/* 
 * Various functions
 */

// detect browser capabilities
var capable = (document.getElementById && document.getElementsByTagName);
// did the user mouse into the promo area?
var boolInPromo = -1;
// used for image swapping
var imgSrc = '';
// home page data to rotate - BRANDING, TA, COMMS, METRICS
//var arrHomeImages = [ "images/home/main_brand.jpg", "images/home/main_talent.jpg", "images/home/main_comms.jpg", "images/home/main_metrics.jpg" ];
//var arrHomeLinks = [ "offerings.php#branding", "offerings.php#talent", "offerings.php#internal", "offerings.php#metrics" ];
//var arrHomeLinkText = [ "ENGAGE BRANDING", "ENGAGE TALENT ACQUISITION", "ENGAGE INTERNAL COMMUNICATIONS", "ENGAGE METRICS" ];
// used by image preloader
var arrTmpImage = [];


// Mouseovers for the main nav (uses jQuery)
function activate_nav(nav)
{
  $(nav).hover(
    function () {
      imgIn(this.id);
    }, 
    function () {
      imgOut(this.id);
    } 
  );
}


// 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 = [];

  $(".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;
}


// Runs when the DOM is ready
$(function()
{ 
  // If the user has JS but no Flash, display the Non-Flash content
  try
  {
    if(pluginlist && pluginlist.indexOf("Flash") == -1) {
      $("#main_flash .hide").show();
    }
  }
  catch(e){}

  // Default settings for modal windows
  $.nyroModalSettings({
    debug:false,
    autoSizable:false,
    width:778,
    height:500,
    // forceType:'iframe'
    modal:false
  }); 

  // Activate links which should open in a modal box 
  $('a.nyroModal').nyroModal( { } );
  $('a.lightbox').nyroModal( { } ); 
  $('a.interview').nyroModal( { width:540, height:260 } ); 
  $('a.rsvp').nyroModal( { width:820, height:480 } ); 
  $('a.keep_in_touch').nyroModal( { width:820, height:590 } ); 
  $('a.youtube').nyroModal( { width:648, height:397, forceType:'iframe' } ); 
  $('a.video').nyroModal( { width:654, height:408, forceType:'iframe' } ); 
  $('a.video2').nyroModal( { width:575, height:395, forceType:'iframe' } ); 
  $('a.map_overlay').nyroModal( {
    forceType:null,
    width:996,
    height:415,
    endShowContent: function(elts, settings) { embed_map_swf(); },
    beforeHideContent: function(elts, settings, callback) { hide_map_swf(); $.fn.nyroModal.settings.hideContent(elts, settings, callback); }
  } ); 

});


