window.addEvent('load',function(){


var rollinappendage = '_o';
// jay johnston mootool auto-rollover for all classes called "over"
$$('.over').forEach(function(item,i) {
  // preload it!
  var newsrc = item.getProperty('src').substr(0,item.getProperty('src').indexOf('.jpg'))+rollinappendage+'.jpg';
  var preload = new Image(); preload.src=newsrc; 
  // sweet!  if the title doesn't exist, doulbe the alt :)
  // switch it to -o
  item.addEvent('mouseover',function() {
    var newsrc = item.getProperty('src').substr(0,item.getProperty('src').indexOf('.jpg'))+rollinappendage+'.jpg';
    if (newsrc!=rollinappendage+'.jpg') item.setProperty('src',newsrc); 
  });
  // switch it back to (not) -o
  item.addEvent('mouseout',function() {
    var newsrc = item.getProperty('src').substr(0,item.getProperty('src').indexOf(rollinappendage))+'.jpg';;
    if (newsrc!='.jpg') item.setProperty('src',newsrc);
  });
});
$$('img').forEach(function(item,i) {
  item.setProperty('title',item.getProperty('alt'));
});


  $('topNav').tween('opacity',1);

  /* jjohnston top nav custom for humancapitalgroup 2011-09-12 */
  /* mmm, floated and nested ul li a groupings */

  var right_align_drop_downs = 1;
  $$('.top_nav_a').each(function(el) {
    var myul = getaul(el);
    if (!myul) return 0;
    var move = 0;
    if (right_align_drop_downs) {
      var size = el.getSize();
      var move = (229 - size.x)*-1; 
    }
    myul.setStyles({'margin-left':move,'opacity':0,'display':'block','position':'absolute'});
    el.addEvents({
      'mouseenter': function() {
        var ch = el.getChildren('ul'); 
        ch.each(function(chel,i) {

          if (myul.getStyle('opacity').toInt()>0) return false;

          var kids = myul.getChildren();
//alert('kids count: '+kids.length);
          var myht = kids.length*32;
//alert('myht: '+myht);
          myul.setStyle('height',0);
          myul.morph({
            'opacity':1,
            'height':myht+'px'
          });
          
        });
      },
      'mouseleave': function() {
        eval('navtimeout'+myul.get('id')+'=setTimeout(function(){$("'+myul.get('id')+'").morph({"opacity":0,"height":0})},100);');
      }
    });
  });

  $$('#topNav ul li').addEvents({
    'mouseover':function() { sexyli(this); },
    'mouseout': function() { cya_li(this); }
  });



  // logo fading at bottom of homepage
  if ($('homeBottom')) {
    logo_chain.chain(function(){next_logo_img();}); 
    logo_chain.callChain.delay(2000,logo_chain);
  }


  // fade in text at top of page
  var hdiv = 'header_0';
  if ($(hdiv)) {
    for(i=1;i<3;i++) {
      var txt = (i==1) ? 'alt' : 'longdesc';
      var text = $(hdiv).get(txt);
      if (text!='') {
        // regular, single image fading
        var tdiv = new Element('div',{ 'opacity':0, 'id':'tdiv'+i, 'text':text });
        tdiv.inject(hdiv,'after');
        setTimeout('$("tdiv'+i+'").fade("in")',1000*i);
      }
    }
  } 
  

}); // end window load events...everything else is global in head of document

/* front page logo fading */
var delay_front_logos = 8; // in seconds
/////////////////////////////
var cur_pos = 1;
var cur_img = 4;
var logo_chain = new Chain();
function next_logo_img() {
  //if (cur_pos>4) cur_pos = 1;

  // this let us do one random image
  //var cur_pos = $random(1,4);

  if (imgs.length-1<cur_img) cur_img = 0;
  // clean up
  if ($('remove')) $('remove').destroy();
  // replace the img in pos
  var newimg = $('hb'+cur_pos).clone();

  newimg.set('tween',{duration:1000});

  var margnl = $('hb'+cur_pos).getStyle('margin-left');
  newimg.set({'src':imgs[cur_img],'alt':alts[cur_img],'title':alts[cur_img]});
  newimg.setStyles({'position':'absolute','opacity':0});
  newimg.inject('hb'+cur_pos,'after'); 
  //newimg.set('tween',{duration:2000});
  newimg.fade('in');
  $('hb'+cur_pos).set('id','remove').setStyle('margin-left',margnl);
  newimg.set('id','hb'+cur_pos);
  // reload and refire
  //cur_pos++;
  cur_img++;

  logo_chain.chain(function(){next_logo_img();}); 
  
  if (cur_pos==4) {
    cur_pos = 1;
    logo_chain.callChain.delay(delay_front_logos*1000,logo_chain);
  }
  else {
    cur_pos += 1;
    logo_chain.callChain.delay(200,logo_chain);
  }
}

/* front page top area banner header image multi-rotational module: */
var cur_top_img = 0;
var fade_chain = '';
var init = 0;
var cnt = 0;
var txt1 = '';
var txt2 = '';
function rotate_top_area(count_of_images) {
  //return true;
  if (init==0) {
    for(j=1;j<count_of_images-1;j++) {
      // this allows the styles sheet hidden divs to fade in
      $('header_'+j).setStyles({'opacity':0,'visibility':'visible'});
    }
    fade_chain = new Chain();
    init = 1;
    cnt = count_of_images;
  }

  // let's grab the next image
  cur_top_img = (cur_top_img==cnt-1) ? 0 : cur_top_img+1; 

  // never start with a full chain
  fade_chain.clearChain();
  // pickup the current div
  var hdiv = 'header_'+cur_top_img;
  // now get the two div texts from the html attys
  for(var i=1;i<=2;i++) {
    var txt = (i==1) ? 'alt' : 'longdesc';
    eval('txt'+i+' = $(hdiv).get(txt);');
  }

  // fade out existing texts
  fade_chain.chain(function() { $('tdiv1').fade('out'); });
  fade_chain.chain(function() { $('tdiv2').fade('out'); });

  // set the next text
  fade_chain.chain(function() {
    for(var j=1;j<=2;j++) $('tdiv'+j).set('text',eval('txt'+j));
  });

  // fade in the next bg image (tricky cause the _0 img doesn't fade)
  if (cur_top_img==0) {
    fade_chain.chain(function() {
      for(var j=1;j<cnt-1;j++) $('header_'+j).fade('hide');
      $('header_'+(cnt-1)).fade('out');
    });
  } else {
    fade_chain.chain(function() {
      $('header_'+cur_top_img).fade('in');
    });
  }

  // fade out existing texts
  fade_chain.chain(function() { $('tdiv1').fade('in'); });
  fade_chain.chain(function() { $('tdiv2').fade('in'); });

  // loop
  fade_chain.chain(function(){rotate_top_area(cnt);});


  // fade out existing text 
  fade_chain.callChain.delay(   0,fade_chain);
  fade_chain.callChain.delay( 500,fade_chain);
  // set the text
  fade_chain.callChain.delay(1000,fade_chain);
  // fade next top area image
  fade_chain.callChain.delay(1100,fade_chain);
  // fade in first and second text
  fade_chain.callChain.delay(2000,fade_chain);
  fade_chain.callChain.delay(2500,fade_chain);
  // call the top area again
  fade_chain.callChain.delay(8000,fade_chain);

}

/* these three are for the drop down menus */
function getaul(el) {
  if (!el) return 0;
  var myid = el.get('id').substr(1).toInt();
  var myul = $('ul'+myid);
  return myul;
}
function sexyli(el) {
  el.set('tween',{duration:100});
  el.tween('background-color','#FFFFFF');
  el.setStyle('line-height','29px');
}
function cya_li(el) {
  el.set('tween',{duration:'long'});
  el.tween('background-color','#F7EFE4');
  el.setStyle('line-height','30px');
}




