window.addEvent('domready', function(){
  var doc = document.body.innerHTML;
  $$('input.overtext').each(function(el){
    new OverText(el);
  });
  if(require_account || agreement_version.length > 0) {
    passwordRequired();
  }
  if (doc.test('subscribe-form')) {
    $each($$('.subscribe-form'), function(elem){
      var inpt = elem.getElement('input.txt');
      if(!inpt) return;
      inpt.addEvent('click', function(){
        window.open('http://info.cloudera.com/NewsletterSubscribe.html', '_blank');
      });
    });
  }
  if (doc.test('carousel')) {
  var carousel = null;
  var delay = 8000;
  var timer = null;
  var paused = false;
    $$('div.carousel').each(function(div){
      carousel = new SimpleSlideShow.Carousel(div.getElement('.carousel-holder ul'), {
        slides: div.getElements('div.carousel-holder li'),
        nextLink: div.getElement('a.arrow-next'),
        prevLink: div.getElement('a.arrow'),
        onSlideDisplay: function() {
          if (!paused) {
            clearTimeout(timer);
            timer = setTimeout(function() {
              carousel.forward();
            }, delay);
          }
        },
        crossFadeOptions: {
          transition: 'quint:in:out',
          duration: 700
        }
      });
    });
    clearTimeout(timer);
    timer = setTimeout(function() {
      carousel.forward();
    }, delay);
    $$('a.arrow-next, a.arrow').addEvent('click', function() {
    clearTimeout(timer);
      paused = true;
  });
  }
  if (doc.test('show-gallery')) {
    var photos;
    $$('.show-gallery').addEvent('click', function(){
      if (photos) {
        photos.setStyle('display', 'block').position({allowNegative:false});
        return;
      }
      new Request({
        url: '/wp-content/themes/cloudera/assets/photos/gallery.html',
        onComplete: function(resp){
          var div = new Element('div', {html: resp}).inject(document.body);
          photos = $$('.office-photos')[0];
          photos.setStyle('display', 'block').inject(document.body, 'top').position({ allowNegative: false });
          photos.getElement('.btn-close').addEvent('click', function(){
            photos.setStyle('display', 'none');
          });
          new SimpleSlideShow({
            slides: photos.getElements('ul.visual li'),
            nextLink: photos.getElement('a.gallery-next'),
            prevLink: photos.getElement('a.gallery-prev'),
            currentIndexContainer: photos.getElement('.gallery-count'),
            maxContainer: photos.getElement('.gallery-max'),
            onSlideDisplay: function(i){
              photos.getElement('div.visual-txt p').set('html', photos.getElements('ul.visual li img')[i].get('alt'));
            }
          });
        }
      }).send();
    });
  }
  if (doc.test('screenshot-carousel')) {
    $$('.screenshot-carousel').each(function(carousel) {
      var thumbs = carousel.getElement('ul.thumbnails');
      var lis = thumbs.getElements('li');
      thumbs.addEvent('click:relay(li)', function(e, li){
        e.preventDefault();
        lis.removeClass('active');
        li.addClass('active');
        var link = li.getElement('a');
        if (link && link.get('href') && link.get('href').test(/jpg$/)) {
          carousel.getElement('.screenshot-screencast').setStyle('display', 'none');
          carousel.getElement('.screenshot-inner').setStyle('display','block').empty().adopt(new Element('img', {src: link.get('href')}));
        } else {
          carousel.getElement('.screenshot-inner').setStyle('display', 'none');
          carousel.getElement('.screenshot-screencast').setStyle('display', 'block');
        }
      });
    });
  }
  if (doc.test('testimonials')) {
    $$('.testimonials').each(function(testimonial){
      var imgs = testimonial.getElements('.quote_icons img');
      var quotes = testimonial.getElements('li div.quotes');
      var vis;
      var show = function(index){
        quotes.setStyle('display', 'none');
        quotes[index].setStyle('display', 'block');
        divot.position({
          relativeTo: imgs[index],
          offset: {
            x: 0,
            y: -60
          }
        });
        vis = index;
      };
      var divot = new Element('img', {
        src: '/assets/images/bg-quote-divot.gif',
        styles: {
          position: 'absolute',
          width: 40,
          height: 17,
          display: 'none'
        },
        events: {
          load: function(){
            this.setStyle('display', 'block');
            show(0);
          }
        }
      }).inject(testimonial);
      var timer = (function(){
        vis ++;
        if (vis >= imgs.length) vis = 0;
        show(vis);
      }).periodical(6000);
      testimonial.addEvent('mouseover:relay(img)', function(e, img){
        if (img == divot) return;
        var index = imgs.indexOf(img);
        show(index);
        $clear(timer);
      });
      
    });
  }
  if (doc.test('hadoop-posts')) {
    var dims = $$('.hadoop-posts')[0].getSize();
    $$('.grid_2 .hadoop-post object, .grid_2 .hadoop-post embed').each(function(elem){
      var size = elem.getSize();
      var w = (elem.get('width') || size.x).toInt();
      var h = (elem.get('height') || size.y).toInt();
      if(w <= dims.x) return;
      elem.set('width', dims.x);
      elem.set('height', h * dims.x / w);
    });
  }
  if(doc.test('abstracted-table')) {
    var tooltip = $$('.tooltip')[0];
    $$('td.abstract').each(function(elem){
      var lnk = elem.getElements('a')[0];
      var dv = elem.getElements('div')[0];
      elem.addEvent('mouseover', function(){
        tooltip.set('html', dv.get('html')).position({
          relativeTo: elem,
          offset: {
            x: -330
          }
        }).setStyle('display', 'block');
        elem.setStyle('background-color', '#F0F0F0');
        if(lnk) lnk.setStyle('color', '#000');
      }).addEvent('mouseout', function(){
        tooltip.setStyle('display', 'none').set('html', '');
        elem.setStyle('background-color', '#FFF');
        if(lnk) lnk.setStyle('color', '#017EC5');
      });
    });
  }
  if(doc.test('training-filter')) {
    function loadTraining(){
      $('training-body').addClass('loading');
      $('training-body').set('html', '');

      var req = new Request({
        url: '/ajax_training_events.php',
        evalScripts: false,
        data: { filter: $('training-filter').value },
        onComplete: function(text){
          $('training-body').removeClass('loading');
          $('training-body').set('html', text);
        }
      }).send();
    }

    $('training-filter').addEvent('change', loadTraining);
    loadTraining();
  }
  
  $$('object, embed').each(function(elem){
    elem.setAttribute('wmode', 'transparent');
  });
  
  $$('form').each(function(elem){
    var btn = elem.getElement('input[type="button"]') || elem.getElement('a.submit');
    if(!btn) return;
    var clicker = function(){
      elem.submit();
    }
    btn.addEvent('click', clicker);
  });

	//call multiBox
	var initMultiBox = new multiBox({
			mbClass: '.mb',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
			container: $(document.body),//where to inject multiBox
			descClassName: 'multiBoxDesc',//the class name of the description divs
			path: './Files/',//path to mp3 and flv players
			useOverlay: true,//use a semi-transparent background. default: false;
			maxSize: {w:800, h:600},//max dimensions (width,height) - set to null to disable resizing
			addDownload: false,//do you want the files to be downloadable?
			pathToDownloadScript: './Scripts/forceDownload.asp',//if above is true, specify path to download script (classicASP and ASP.NET versions included)
			addRollover: false,//add rollover fade to each multibox link
			addOverlayIcon: false,//adds overlay icons to images within multibox links
			addChain: false,//cycle through all images fading them out then in
			recalcTop: true,//subtract the height of controls panel from top position
			addTips: false,//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
			autoOpen: 0//to auto open a multiBox element on page load change to (1, 2, or 3 etc)
		});
	
});

function passwordRequired(){
  new Account($('password-required'), {
    maskTarget: $('main'),
    agreementVersion: agreement_version,
    campaignID: campaign_id,
    message: 'This page requires a quick registration. Please register to view the requested page. You will only have to do this once.',
    heading: 'Registration Required'
  });
}

function download(link){
  try{
    var pageTracker = _gat._getTracker("UA-2275969-4");
    pageTracker._trackEvent('Download', link.href);
    setTimeout('document.location = "' + link.href + '"', 100)
  } catch(err) {}
  return false;
}
