
$(document).ready(function(){		  
		

  function initTabs() {
    $('#career div.career-blocks-links')
      .bind('click', function(e) {
        var $t = $(e.target);
		
        if ($t.is('a')) {
          e.preventDefault();
          var href = $t.attr('href').split('#')[1];
          var $dest = $('#' + href);
          if ($dest.is('.current'))
            return;
          $('#career div.career-block.current')
            .hide()
            .removeClass('current');
          $dest.show().addClass('current');
          $t.siblings().removeClass('current').end().addClass('current');
        }
      });  
  }
  
  function initTooltips() {
    $('#career ul.color-coded li')
      .hover(function() {
        $(this).addClass('hover').css('z-index', 10);
        var tt = $(this).find('div.tooltip').css({visibility: 'hidden'});
        var block = $(this).parents('div.career-block');
        if (tt.length) {
          var oh = tt[0].offsetHeight;
          var top = getYPosition(tt[0]);
          var vh = getViewportHeight();
          var scroll = getYScrollPosition();

          if (top + oh > vh + scroll) { // control de posición vertical
            var ntop = 0 - (oh + 8);
            tt.css({ top: ntop + 'px' });
          }

          var left = getXPosition(tt[0]);
          if (left + tt[0].offsetWidth > getXPosition(block[0]) + block[0].offsetWidth) { // control de posición horizontal
            var nleft = -(tt[0].offsetWidth - this.offsetWidth); 
            tt.css({'margin-left': nleft + 'px'});
          }
          
          tt.css({visibility: ''});
		 
          
        }
      },
      function() {
        $(this).removeClass('hover').css('z-index', '');
        $(this).find('div.tooltip').css('top', '');
      });
  }
  
  function getYPosition(el) {
    var ypos = 0;
    do {
      ypos += el.offsetTop;
    } while (el = el.offsetParent);
    return ypos;
  }
  
  function getXPosition(el) {
    var xpos = 0;
    do {
      xpos += el.offsetLeft;
    } while (el = el.offsetParent);
    return xpos;
  }
  
  function getViewportHeight() {
    if (window.innerHeight != window.undefined) { // MOS, IE7
      return window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight != window.undefined && document.documentElement.clientHeight != 0) { // IE6
      return document.documentElement.clientHeight;
    }
    else { // older IE versions
      return document.body.clientHeight;
    }
  }
  
  function getYScrollPosition() {
    if(window.pageYOffset) {
      return window.pageYOffset;
    }
    else {
      return  Math.max(document.body.scrollTop, document.documentElement.scrollTop);
    }
  }
	$(initTabs);
	$(initTooltips);
    
  
});