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

// Hide the loading graphic and show the client logos
function show_portfolio_logos(domID)
{
  // Hide the progress bar
  $("#progbar").hide();
  $(domID).css("visibility","visible"); 
}


// Rotate homepage area (uses jQuery)
function rotate_home_image(intCurrent, delay)
{
    intCurrent++;
    if( intCurrent >= arrHomeImages.length ) {
        intCurrent = 0;
    }
    // Replace the image
    $("#mainImage").fadeOut("slow").queue(function() {
      $(this).attr("src", arrHomeImages[intCurrent]);
      $(this).fadeIn("slow"); 
      $(this).dequeue();
    });

    // Update the link
    $("#homeContent div.text").fadeOut("slow").queue(function() {
      $("a.homeLink").attr("href", arrHomeLinks[intCurrent]);
      $("a.homeLinkText").text( arrHomeLinkText[intCurrent] );
      $(this).fadeIn("slow"); 
      $(this).dequeue();
    });

    // Set timer to change area again
    hpTimer = setTimeout("rotate_home_image(" + intCurrent + ", " + delay + ")", delay);
} 

// Display the right column content when a tab is selected
function select_rightcol(data)
{
  // Hide the existing right column
  $("div.right div.selected").removeClass("selected").addClass("hide").fadeOut("slow").
    queue(function(){
      // Show the new right column
      $("div.right div#" + data.id + "_right").fadeIn("slow").removeClass("hide").addClass("selected");
      $(this).dequeue();
    }); 
} 

// Perform search & populate the portfolio list using Ajax
function portfolio_search()
{
  // Get search values
  var f_industry = encodeURIComponent( $("#f_industry").attr("value") );
  var f_type = encodeURIComponent( $("#f_type").attr("value") );
  var f_ptype = encodeURIComponent( $("#f_ptype").attr("value") );

  // Empty the bottom area
  $("div.bottom").empty();

  // Display the progress bar
  $("#progbar").show();

  // Show the reset all link
  if( f_industry != "" || f_type != "" ) {
    $("#reset_all").show();
  } else {
    $("#reset_all").hide();
  }  

  // Populate portfolio list
  $.get("getdata.php", { action:'list', industry:f_industry, type:f_type, ptype:f_ptype }, 
    function(data)
    {
      $("div.bottom").html(data);
      // Vertically center the logos
      $("#portfolio_list").css("height","auto");
      centerElements();
      // Highlight logos on mouseover
      activate_logos("div.logos li img");
      // Display the logos
      show_portfolio_logos("#portfolio_list");
    }, 'html');

} 

// Reset filter boxes
function reset_search_boxes()
{
    $("#f_industry").attr("value","");
    $("#f_type").attr("value","");
    portfolio_search();
}

// 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()
{
  $("a.newwin").click(function(){
    popWindow(this.href,900,600,1);
    return false;
  });
}
  
// Highlight logos on mouseover
function activate_logos(identifier)
{
  $(identifier).hover(function(){
    imgSrc = $(this).attr("src");
    if(imgSrc.indexOf('_gray') != -1) {
      imgSrc = imgSrc.replace(/_gray/, "_color");
      $(this).attr("src", imgSrc );
    } 
  }, function(){
    imgSrc = $(this).attr("src");
    if(imgSrc.indexOf('_color') != -1) {
      imgSrc = imgSrc.replace(/_color/, "_gray");
      $(this).attr("src", imgSrc );
    } 
  });
}

// Make promo areas with class='clickable' links (uses jQuery)
function make_promos_clickable()
{
  $("div.promo.clickable").click(function(){
    var link = $(this).find("a").attr("href");
    location.href = link;
  }).css("cursor","pointer");

  return false;
}

// Make Our Work promos expandable (uses jQuery)
function make_promos_expandable()
{
  $("div.promohead.expandable").hover(
    function(){
      // Moused into promo head - show the full promo
      $("div.promohead.expandable ~ div.promo.expandable").slideDown("slow");
      $("div.promohead.expandable").css({'margin-bottom':'0','border-bottom':'0'});
    }, 
    function(){
      // Moused out of promo head - close box in 1/4 second unless user moves into the promo area
      setTimeout('hidepromo()', 250);
    }
  );
  $("div.promo.expandable").hover(
    function(){
      // Moused into promo - set the flag
      boolInPromo = 1;
    }, 
    function(){
      // Moused out of promo - hide promo after 1/4 second
      boolInPromo = -1;
      setTimeout('hidepromo()', 250);
    }
  );
}

// Hide the expandable promo area (uses jQuery)
function hidepromo() {
  if(boolInPromo == -1) {
     $("div.promo.expandable").slideUp("slow").
       queue(function(){
         $("div.promohead.expandable").css({'margin-bottom':'10px','border-bottom':'1px dotted #666'});
         $(this).dequeue();
       });
  }
}

// Mouseovers for Our Work "view pdf" buttons (uses jQuery)
function activate_viewpdf()
{
  $("img.m_viewpdf").hover(
    function () {
      $(this).attr('src','images/view_pdf_over.gif');
    }, 
    function () {
      $(this).attr('src','images/view_pdf_off.gif');
    } 
  );
} 

// Rotate the background image (uses jQuery)
function next_bgimg() {
  currentImg++;
  if(currentImg > 3) currentImg = 0; 
  $("body").css("background-image","url(images/hd/"+arrBg[currentImg]+")");
  setTimeout('next_bgimg()', 5000);
}


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


