// Copyright 2010 University of California, Berkeley Integrative Biology.

/**
 * @fileoverview Scripts that affect the entirety of the IB site. Some of these
 * scripts may require the gweb library located at http://www.google.com/js/gweb/core.js.
 * @author mrigoli@gmail.com (Mike Rigoli)
 */

/**
 * Create berkib namespace if it hasn't already been created.
 */
var berkib = berkib || {};



/**
 * Search constructor.
 * @constructor
 */
berkib.SearchBox = function() {
  this.eventInit();
};


/**
 * Sets up events and behavior for the carousel and its UI.
 */
berkib.SearchBox.prototype.eventInit = function () {
  var searchIcon = gweb.dom.getElement('search').getElementsByTagName('img')[0];
  var searchInput = gweb.dom.getElement('search').getElementsByTagName('input')[0];
  var searchForm = gweb.dom.getElement('search').getElementsByTagName('form')[0];

  gweb.events.listen(searchIcon, 'click', function(e) {
    searchForm.submit();
  }, null, this);

  gweb.events.listen(searchInput, 'focus', function(e) {
    e.target.value = '';
    e.target.style.color = '#000';
  }, null, this);

  gweb.events.listen(searchInput, 'keypress', function(e) {
    if (e.charCode == 13 || e.charCode == 3) {
      searchForm.submit();
    }
  }, null, this);
};


/**
 * Navigation constructor.
 * @constructor
 */
berkib.Nav = function(menuId) {
  this.mainMenu = gweb.dom.getElement(menuId);
  this.eventInit();
};

/**
 * Sets up events for menu/submenu items.
 */
berkib.Nav.prototype.eventInit = function() {

  this.subMenus = gweb.dom.query('ul', this.mainMenu);

  gweb.array.forEach(this.subMenus, gweb.bind(function(subMenu) {

    gweb.events.listen(subMenu.parentNode, 'mouseover', function(event) {
      this.show(subMenu);
    }, false, this);

    gweb.events.listen(subMenu.parentNode, 'mouseout', function(event) {
      this.hide(subMenu);
    }, false, this);
  }, this));
};

/**
 * Displays submenu.
 * @param {object} subMenu Submenu for the nav being selected.
 */
berkib.Nav.prototype.show = function(subMenu) {
  var offLeft = 10;

  if (gweb.userAgent.IE && gweb.userAgent.VERSION == '7.0') {
    offLeft = 40;
  }

  if (subMenu.parentNode.parentNode.id == 'top-nav') {
    subMenu.style.left = (subMenu.parentNode.offsetLeft - offLeft) + 'px';
    subMenu.style.top = (subMenu.parentNode.offsetTop + 13) + 'px';
  } else {
    subMenu.style.top = (subMenu.parentNode.offsetTop) + 'px';
    subMenu.style.left = (subMenu.parentNode.offsetLeft + 130) + 'px';
  }
  subMenu.style.visibility = 'visible';
}

/**
 * Hides a pulldown menu.
 * @param {HTMLElement} subMenu The dom element corresponding to the pulldown.
 */
berkib.Nav.prototype.hide = function(subMenu) {
  if (subMenu.parentNode.parentNode.id == 'top-nav') {
    subMenu.style.visibility = 'hidden';
  } else {
    subMenu.style.visibility = 'hidden';
  }
};


/**
 * Social Image constructor.
 * @constructor
 */
berkib.Social = berkib.Social || {};


berkib.Social.imgHover = function() {
  var allImages = gweb.dom.getElementsByTagNameAndClass('img', null, gweb.dom.getElement('social-network'));

  for (var i = 0; i < allImages.length; i++) {
    gweb.events.listen(allImages[i], 'mouseover', function() {
      this.src = this.src.replace('-up','-over');
    });

    gweb.events.listen(allImages[i], 'mouseout', function() {
      this.src = this.src.replace('-over','-up');
    });
  }
}


/**
 * Home Page Email namespace.
 */
berkib.HomePageEmail = berkib.HomePageEmail || {};

berkib.HomePageEmail.toggleDisplay = function(form, ty) {
  form.style.display = 'none';
  ty.style.display = 'block';
  setTimeout(gweb.bind(function(){
    ty.style.display = 'none';
  }, this), 3000);
};

berkib.HomePageEmail.formSetup = function() {
  var homeEmailForm = gweb.dom.getElement('home-email-form');
  var homeEmailFormEmail = gweb.dom.getElement('entry_0');
  var homeEmailFormThankYou = gweb.dom.getElement('home-email-form-ty');
  var homeEmailIcon = homeEmailForm.getElementsByTagName('img')[0];

  gweb.events.listen(homeEmailForm, 'submit', function() {
    berkib.HomePageEmail.toggleDisplay(homeEmailForm, homeEmailFormThankYou);
  }, null, this)

  gweb.events.listen(homeEmailFormEmail, 'focus', function(e) {
    e.target.value = '';
    e.target.style.color = '#000';
  }, null, this);

  gweb.events.listen(homeEmailIcon, 'click', function(e) {
    berkib.HomePageEmail.toggleDisplay(homeEmailForm, homeEmailFormThankYou);
    homeEmailForm.submit();
  }, null, this);
};


/**
 * Hide show function for the Department layer over the banner.
 */
berkib.DeptOverlay = function(deptLayer, deptLink, visitorsLink, closeOverlay, closeOverlayIcon) {
  this.deptLayer = gweb.dom.getElement(deptLayer);
  this.deptLink = gweb.dom.getElement(deptLink);
  this.visitorsLink = gweb.dom.getElement(visitorsLink);
  this.closeOverlay = gweb.dom.getElement(closeOverlay);
  this.closeOverlayIcon = gweb.dom.getElement(closeOverlayIcon);
  this.eventInit();
};

berkib.DeptOverlay.prototype.toggle = function(state) {
  if (state == 'close') {
    this.deptLayer.style.display = 'none';
  } else if (state == 'open') {
    this.deptLayer.style.display = 'block';
  } else {
    this.deptLayer.style.display == 'block' ?
        this.deptLayer.style.display = 'none' :
        this.deptLayer.style.display = 'block';
  }
};

berkib.DeptOverlay.prototype.eventInit = function() {
  gweb.events.listen(this.deptLink, 'click', function() {
    this.toggle('open');
  }, null, this);
  gweb.events.listen(this.visitorsLink, 'click', function() {
    this.toggle('close');
  }, null, this);
  gweb.events.listen(this.closeOverlay, 'click', function() {
    this.toggle('close');
  }, null, this);
  gweb.events.listen(this.closeOverlayIcon, 'mouseover', function() {
    this.closeOverlayIcon.src = 'images/frontpage/banner/dept/close-over.png';
  }, null, this);
  gweb.events.listen(this.closeOverlayIcon, 'mouseout', function() {
    this.closeOverlayIcon.src = 'images/frontpage/banner/dept/close.png';
  }, null, this);
};



