//////////////////////////////////////////////////////////////////
//
//  InterActive Studio JS file
//
//  Author: Craig Nelson, Classic Labs
//

  // popup window from index page
  function popUpWindow(url) {
    window.open(url, 'InterActiveStudio','width=1024,height=750,scrollbars=yes');
  }
  // --popup window from index page

  // top menu animation
  Event.observe(window, 'load', function() {
    var elements = $$('#header a');
    elements.each(function(e) {
      if (e.id != 'home-link') {
        var childEl = e.down();
        var y = 47;
        if (!e.hasClassName('active')) {
          Event.observe(e.id, 'mouseover', function(event) {
            childEl.style.position = 'relative';
            childEl.style.top = y + 'px';
          });
          Event.observe(e.id, 'mouseout', function(event) {
            childEl.style.position = 'relative';
            childEl.style.top = 0 + 'px';
          });
        }
      }
    });
  });
  // --top menu animation
  
  // loading indicator for slideshow
  Event.observe(window, 'load', function() {
    var slideshowStrip = $('slideshow-strip');
    if (slideshowStrip) { // check to see if we are on a page that has a slideshow
      $('slideshow-loading-indicator').hide();
      $('slideshow-strip').style.visibility = 'visible';
    }
  });
  // --loading indicator for slideshow
  
  // dynamic image preloading for right menu items
  var imagePaths = [];
  var imageObjVariables = [];
  
  document.observe("dom:loaded", function() {
    var els = $$('#content-right .rollover');
    var protocol = window.location.protocol;
    var host = window.location.hostname;
    var pattern1 = /"/g; // for ie since it places quotes around the backgroundImage url
    var pattern2 = 'url(' + protocol + '//' + host;
    var pattern3 = '.jpg)';
    els.each(function(e) {
      var str = e.getStyle('backgroundImage').replace(pattern1, '');
      str = str.replace(pattern2, '');
      str = str.replace(pattern3, '_over.jpg');
      imagePaths.push(str);
    });
    for (var i=0; i<imagePaths.length; i++) {
      imageObjVariables.push('image' + [i]);
      imageObjVariables[i] = new Image();
      imageObjVariables[i].src = imagePaths[i];
    }
  });
  // --dynamic image preloading for right menu items
  
  // article and reference rollovers
  Event.observe(window, 'load', function() {
    var elements = $H({ 
      article_001: '&lt;Innovation Now!&gt;', 
      article_002: '&lt;Gary Hamel. Fast Company. Dec. 2002. pg 115&gt;' 
    });
    elements.each(function(e) {
      var el = $(e.key);
      if (el) { // in case we are on a page that doesn't need this function
        Event.observe(el.id, 'mouseover', function() {
          el.innerHTML = e.value;
        });
        Event.observe(el.id, 'mouseout', function() {
          if (el.hasClassName('article')) {
            el.innerHTML = '&lt;article&gt;';
          }
          else {
            el.innerHTML = '&lt;reference&gt;';
          }
        }); 
      }
    });
  });
  // --article and reference rollovers
  
  // slideshow
  var currentPosition = 0;
  var moveInProgress;
  
  Event.observe(window, 'load', function() {
    var slides = $$('.slideshow-nav li a');
    Effect.Transitions.easing = function(pos) {
      return (pos==1) ? 1 : -Math.pow(2, -10 * pos) + 1;
    }
    slides.each(function(e) {
      Event.observe(e.id, 'click', function(event) {
        move(e.id);
      });
    });
  });
  
  function move(id) {
    var strip = $('slideshow-strip');
    var cleanedID = id.replace('slide-nav-', '');
    var currentSlide = cleanedID;
    if (currentPosition == 0 && currentSlide == 0 || moveInProgress == true) {
      return;
    }
    else {
      var newPosition = (cleanedID * 600);
      if (newPosition > currentPosition) {
        var pixelsToMove = +-(newPosition - currentPosition);
        new Effect.Move(strip, {
          x: pixelsToMove,
          y: 0,
          duration: .4,
          transition: Effect.Transitions.easing,
          beforeStart: function() {
            moveInProgress = true;
          },
          afterFinish: function() {
            currentPosition = newPosition;
            moveInProgress = false;
          }
        });
      }
      if (newPosition < currentPosition) {
        var pixelsToMove = (currentPosition - newPosition);
        new Effect.Move(strip, {
          x: pixelsToMove,
          y: 0,
          duration: .4,
          transition: Effect.Transitions.easing,
          beforeStart: function() {
            moveInProgress = true;
          },
          afterFinish: function() {
            currentPosition = newPosition;
            moveInProgress = false;
          }
        });
      }
    }
  }
  // --slideshow
  
  // main menu and portfolio right menu - current active page highlighting
  document.observe("dom:loaded", function() {
    var portfolioKeys = ['portfolio', 'modular_ideations', 'patient_care', 'discovery_workshop', 'site_development', 'build_studies'];
    var trendsKeys = ['trends', 'people', 'sustainability', 'technology'];
    var profileKeys = ['profile', 'team', 'learn', 'know', 'create'];
    var portfolioRightMenuElements = $$('.portfolio-right-menu li a');
    
    portfolioKeys.each(function(item) {
      if (window.location.pathname.endsWith(item + '.php')) {
        $('portfolio-link').addClassName('active');
      }
    });
    trendsKeys.each(function(item) {
      if (window.location.pathname.endsWith(item + '.php')) {
        $('trends-link').addClassName('active');
      }
    });
    profileKeys.each(function(item) {
      if (window.location.pathname.endsWith(item + '.php')) {
        $('profile-link').addClassName('active');
      }
    });
    portfolioRightMenuElements.each(function(e) {
      if (e.href.match(window.location.pathname) && !e.href.match('portfolio')) {
        e.addClassName('active');
      }
    });
  });
  // --main menu and portfolio right menu - current active page highlighting
  
  // dynaminc y content placement within content-left through margin-top
  document.observe("dom:loaded", function() {
    if ($('content-left-resizer')) {
      var height = $('content-left-resizer').offsetHeight;
      if (height < 555) {
        $('content-left-resizer').down().style.marginTop = (555-height) + 'px';
        $('content-left-resizer').style.visibility = 'visible';
      }
    }
  });
  // --dynaminc y content placement within content-left through margin-top
  
  // email rollovers for team page
  Event.observe(window, 'load', function() {
    var emailRollover = $$('.team-member-info a');
    emailRollover.each(function(e) {
      Event.observe(e.identify(), 'mouseover', function(event) {
        e.down().innerHTML = e.href.replace('mailto:', '');
      });
      Event.observe(e.identify(), 'mouseout', function(event) {
        e.down().innerHTML = '&lt;email&gt;';
      });
    });
  });
  // --email rollovers for team page
  
  // external links
  document.observe('dom:loaded', function() {
    var anchors = $$('a');
    anchors.each(function(e) {
      if (e.getAttribute('rel') == 'external') {
        e.target = '_blank';
      }
    });
  });
  // --external links
  
  // google analytics
  Event.observe(window, 'load', function() {
    try {
      var pageTracker = _gat._getTracker("UA-7309504-1");
      pageTracker._trackPageview();
    } catch(err) {}
  });
  // --google analytics

  // menu animation scriptaculous version
  /*
  Event.observe(window, 'load', function() {
    var elements = $$('#header a');
    var yValue;
    elements.each(function(e) {
      if (e.id != 'home-link') {
        var childEl = e.down();
        var childElID = e.down().identify();
        yValue = childEl.style.top;
        e.onmouseover = function(event) {
          new Effect.Move(childElID, { 
            duration: .2,
            x: 0, 
            y: 40 
          });
        }
        e.onmouseout = function(event) {
          new Effect.Move(childElID, { 
            duration: .2,
            x: 0, 
            y: -yValue 
          });
        }
      }
    });
  });
  */
  // --menu animation scriptaculous version