$(document).ready(function() {
   replacenoscript();
   opennav();
   extendsidebar();
   intsidebar();
   navinit();
   navfadein();
   pagetype();
   styleglossary();
   glossaryprimarylinks();
   glossarylinks();
   glossaryrelatedlinks();
   definitionlinks();
   losehomefrombreadcrumb();
   randomicon();
   betterbookstyles();
   statusmessage();
   if ($('body').is('#node-human-lineage-through-time')){
      timelineinteraction();
   }
});


function replacenoscript () {
   $('#header').children('div').children('.content').children('ul.menu').children('li').children('ul.menu').css({'backgroundColor': 'transparent', 'left': '0px', 'textAlign': 'right', 'top': '245px', 'marginLeft': '0px', 'opacity': '1', 'padding': '0px', 'paddingBottom': '30px'}).children('a').css({'color': '#D5C6A9'});
} // replacenoscript

function extendsidebar() {
   var footeroffset = $('#footer').offset();
   var sidebaruloffset = $('#sidebar').children('div').offset();
   var toextend = footeroffset.top - sidebaruloffset.top - 40;
   $('#home').find('#sidebar').children('div').css({'height': toextend + 'px'});
} // extendsidebar

function navinit() {
   $('#block-menu-primary-links').children('.content').children('.menu').children('li').each(function(i) {
      var that = $(this).children('a').html();
      $(this).children('a').append('<em>' + that + '</em>');
   });
   $('#block-menu-secondary-links').children('.content').children('.menu').children('li').each(function(i) {
      var that = $(this).children('a').html();
      $(this).children('a').append('<em>' + that + '</em>');
   });
} // navinit

function navfadein() {
   $('#block-menu-primary-links li a').hover(function() {
      $(this).children('em').animate({'opacity': '1'}, 400);
   }, function() {
      $(this).children('em').animate({'opacity': '0'}, 400);
   });
   $('#block-menu-secondary-links li a').hover(function() {
      $(this).children('em').animate({'opacity': '1'}, 400);
   }, function() {
      $(this).children('em').animate({'opacity': '0'}, 400);
   });
} // navfadein

function intsidebar() {
   $('.menu li').each(function(i) {
      var subclassname = $(this).children('a').html();
      var subclassname = subclassname.replace(/\s+/g, '').toLowerCase() ;
      $(this).addClass(subclassname);
   });

   var toextend = $('div.content ul.menu li.navon ul').height() + 0;
   $('#sidebar').css({'marginTop': toextend + 'px', 'visibility': 'visible'});

   $('li.navon ul.menu').parent('li').parent('ul').parent('div.content').parent('div.block-menu').parent('div#header').parent('div#worldcontainer').children('div#main').children('div#sidebar').css({'paddingTop': '60px'});
} // intsidebar

function opennav() {
   $('ul.menu').find('ul.menu').css({'display': 'none'});
   var path = location.pathname;
   $('ul.menu li a[@href$="' + path + '"]').parent('li').addClass('navon').children('ul.menu').css({'display': 'block'});
   $('ul.menu li ul.menu a[@href$="' + path + '"]').parent('li').addClass('navon').parent('ul').parent('li').addClass('navon').children('ul.menu').css({'display': 'block'}).children('li.navon').children('ul.menu').css({'display': 'block'});
   $('ul.menu li ul.menu li ul.menu a[@href$="' + path + '"]').parent('li').addClass('navon').parent('ul').parent('li').addClass('navon').parent('ul').parent('li').addClass('navon').children('ul.menu').css({'display': 'block'}).children('li.navon').children('ul.menu').css({'display': 'block'}).children('li.navon').children('ul.menu').css({'display': 'block'});
} // opennav

function styleglossary() {
   $('ol.glossary').children('li').children('dl').css({'display': 'none'});
   $('ol.glossary').children('li').children('a').each(function(i) {
      $(this).css({'position': 'absolute', 'top': '-25px', 'left': i*27 + 'px', 'fontSize': '120%', 'width': '27px', 'textAlign': 'center'});
      $('ol.glossary').children('li:first-child').children('a').addClass('active').parent('li').children('dl').css({'display': 'block'});
   });

   $('ol.glossary').find('dt').children('a').each(function(i) {
      var that = $(this).html();
      var thatunfiltered = $(this).html();
      var that = that.replace(/\s+/g, '').toLowerCase();
      $(this).addClass(that);
      $(this).attr('href', '/search/node/' + thatunfiltered);
   });
} // styleglossary

function glossaryprimarylinks() {
   $('ol.glossary').children('li').children('a').click(function() {
      $('ol.glossary').find('a.active').removeClass('active').parent('li').children('dl').toggle(1000);
      $(this).addClass('active').parent('li').children('dl').toggle(1000);
      return false;
   });
} // glossaryprimarylinks

function glossarylinks() {
   $('ol.glossary').find('dl').children('dd').children('p').children('a').click(function() {
      var that = $(this).html().replace(/\s+/g, '').toLowerCase();
      var thestartingletter = that.toLowerCase().substring(0,1);
      var parentstartingletter = $(this).parent('p').parent('dd').prev('dt').children('a').html().toLowerCase().substring(0,1);

      if (parentstartingletter == thestartingletter) {
         $('body').find('a.heyitsme').removeClass('heyitsme');
         $('a[class=' + that + ']').addClass('heyitsme');
         var heyitsmeoffset = $('a.heyitsme').offset();
         $('html,body').animate({'scrollTop': heyitsmeoffset.top}, 500);
         setTimeout( function () {$('a.heyitsme').animate({'backgroundColor': '#AA4411', 'color': '#ffffff'}, 500).animate({'backgroundColor': '#d5c6a9', 'color': '#002c41'}, 500).animate({'backgroundColor': '#AA4411', 'color': '#ffffff'}, 500).animate({'backgroundColor': '#d5c6a9', 'color': '#002c41'}, 500)}, 500 );
         return false;
      } else {
         $('html,body').animate({'scrollTop': '0'}, 500);
         var upperstartingletter = thestartingletter.toUpperCase();
         setTimeout( function () {$('ol.glossary').children('li').children('a:contains(' + upperstartingletter + ')').click()}, 500 );
         setTimeout( function () {
            $('body').find('a.heyitsme').removeClass('heyitsme');
            $('a[class=' + that + ']').addClass('heyitsme');
            var heyitsmeoffset = $('a.heyitsme').offset();
            $('html,body').animate({'scrollTop': heyitsmeoffset.top}, 500);
            setTimeout( function () {$('a.heyitsme').animate({'backgroundColor': '#AA4411', 'color': '#ffffff'}, 500).animate({'backgroundColor': '#d5c6a9', 'color': '#002c41'}, 500).animate({'backgroundColor': '#AA4411', 'color': '#ffffff'}, 500).animate({'backgroundColor': '#d5c6a9', 'color': '#002c41'}, 500)}, 500 );
         }, 2000 );
         return false;
      }
   });
} // glossarylinks

function glossaryrelatedlinks() {
   $('ol.glossary').find('dl').children('dd').children('ul').children('li').children('a').click(function() {
      var that = $(this).html().replace(/\s+/g, '').toLowerCase();
      var thestartingletter = that.toLowerCase().substring(0,1);
      var parentstartingletter = $(this).parent('li').parent('ul').parent('dd').prev('dt').children('a').html().toLowerCase().substring(0,1);
      if (parentstartingletter == thestartingletter) {
         $('body').find('a.heyitsme').removeClass('heyitsme');
         $('a[class=' + that + ']').addClass('heyitsme');
         var heyitsmeoffset = $('a.heyitsme').offset();
         $('html,body').animate({'scrollTop': heyitsmeoffset.top}, 500);
         setTimeout( function () {$('a.heyitsme').animate({'backgroundColor': '#AA4411', 'color': '#ffffff'}, 500).animate({'backgroundColor': '#d5c6a9', 'color': '#002c41'}, 500).animate({'backgroundColor': '#AA4411', 'color': '#ffffff'}, 500).animate({'backgroundColor': '#d5c6a9', 'color': '#002c41'}, 500)}, 500 );
         return false;
      } else {
         $('html,body').animate({'scrollTop': '0'}, 500);
         var upperstartingletter = thestartingletter.toUpperCase();
         setTimeout( function () {$('ol.glossary').children('li').children('a:contains(' + upperstartingletter + ')').click()}, 500 );
         setTimeout( function () {
            $('body').find('a.heyitsme').removeClass('heyitsme');
            $('a[class=' + that + ']').addClass('heyitsme');
            var heyitsmeoffset = $('a.heyitsme').offset();
            $('html,body').animate({'scrollTop': heyitsmeoffset.top}, 500);
            setTimeout( function () {$('a.heyitsme').animate({'backgroundColor': '#AA4411', 'color': '#ffffff'}, 500).animate({'backgroundColor': '#d5c6a9', 'color': '#002c41'}, 500).animate({'backgroundColor': '#AA4411', 'color': '#ffffff'}, 500).animate({'backgroundColor': '#d5c6a9', 'color': '#002c41'}, 500)}, 500 );
         }, 2000 );
         return false;
      }
   });
} // glossaryrelatedlinks

function definitionlinks() {
   $('ol.glossary').find('dl').children('dt').find('a').hover(function() {
      $(this).append('<span>(Search this site for ' + $(this).html() + ')</span>');
   }, function() {
      $(this).children('span').remove();
   });
} // definitionlinks

function losehomefrombreadcrumb() {
   $('#intro').children('h3').children('a:contains(Home)').remove();
   $('#intro').children('h3').children('a:nth-child(2)').before('<span>&#124;</span>');
} // losehomefrombreadcrumb

function randomicon() {
   var imgArray = ['icon1', 'icon2', 'icon3', 'icon4', 'icon5'];
   function ranNum() { return Math.floor(Math.random()*imgArray.length); }
   var ranNum = ranNum();
   var thefirst = imgArray[ranNum];
   var classtoadd = thefirst;
   $('span.icon').addClass(classtoadd);
} // randomicon

function pagetype() {
   var whatstheid = $('body').attr('id').substring(0,10);
   if (whatstheid == 'node-news-') {
      $('div#intro').children('h3').append('<a href="/archive/news">News</a>');
      $('li.news').addClass('navon').children('a').addClass('active');
      var thetitletoadd = $('#intro').children('h2').clone();
      $('div.single').prepend(thetitletoadd);
      $('span.newsdate').css({'display': 'block'});

      var howlongisthedate = $('span.newsdate').html().length;
      var starttheyear = howlongisthedate - 4;
      var theyear = $('span.newsdate').html().substring(starttheyear, howlongisthedate);

      $('div.single').prepend('<div id="archive-container"><dl><dt>Date</dt><dd><ul id="archive-years"><li class="allnews"><a href="/archive/news">All News</a></li><li><a href="/archive/news/"' + theyear + '">' + theyear + '</a></li></ul></dd></dl></div>');
      $('div#archive-container').css({'marginTop': '-20px', 'width': '190px', 'marginLeft': '-225px'}).find('a').css({'width': '190px', 'float': 'left', 'clear': 'left'});
      $('div#sidebar').css({'marginTop': '120px'});
      $('#content').find('.node').find('.content').find("p:contains('Teaser:')").addClass('iamtheteaser');
      $('#content').find('.node').find('.content').find("p:contains('teaser:')").addClass('iamtheteaser');
      $('#content').find('.node').find('.content').find("p:contains('TEASER:')").addClass('iamtheteaser');
      $('.iamtheteaser').each(function() {
         this.innerHTML = this.innerHTML.substring(7);
      });

      $('#content').find('.node').find('.content').find("p:contains('IMAGE:')").addClass('iamtheimage');
      $('#content').find('.node').find('.content').find("p:contains('Image:')").addClass('iamtheimage');
      $('#content').find('.node').find('.content').find("p:contains('image:')").addClass('iamtheimage');
      $('.iamtheimage').each(function() {
         var theimageurl = this.innerHTML = this.innerHTML.substring(6);
         var replacethea = $(this).children('a').attr('href');
         $(this).html('<img src="' + theimageurl + '" alt="Book Cover" />');
         if (replacethea != null) {
            $(this).html('<img src="' + replacethea + '" alt="Book Cover" />');
         }
      });
   }

   if (whatstheid == 'archive-ne') {
      $('li.news').addClass('navon').children('a').addClass('active');
      $('ul#archive-years').children('li:first-child()').addClass('allnews').children('a').append(' News');
      $('ul#archive-years').children('li').prependTo( $('ul#archive-years') );
      $('ul#archive-years').children('li.allnews').prependTo( $('ul#archive-years') );
      $('#content').find('.node').find('.content').find("p:contains('Teaser:')").addClass('iamtheteaser').siblings().css({'display': 'none'});
      $('#content').find('.node').find('.content').find("p:contains('teaser:')").addClass('iamtheteaser').siblings().css({'display': 'none'});
      $('#content').find('.node').find('.content').find("p:contains('TEASER:')").addClass('iamtheteaser').siblings().css({'display': 'none'});
      $('.iamtheteaser').each(function() {
         this.innerHTML = this.innerHTML.substring(7);
      });
      $('ul.tabs').remove();
      $('div#archive-container').css({'marginTop': '-20px', 'width': '190px', 'marginLeft': '-225px'}).find('a').css({'width': '190px', 'float': 'left', 'clear': 'left'});
   }

   if (whatstheid == 'node-bookr') {
      $('div#intro').children('h3').append('<a href="/node/resources">Resources</a><span>|</span><a href="/archive/book_reviews">Book Reviews</a>');

      var howlongisthedate = $('span.newsdate').html().length;
      var starttheyear = howlongisthedate - 4;
      var theyear = $('span.newsdate').html().substring(starttheyear, howlongisthedate);

      $('li.resources').addClass('navon').addClass('active-trail').children('ul').children('li.bookreviews').addClass('navon').append('<ul class="menu"><li><a href="/archive/book_reviews/' + theyear + '">' + theyear + '</a></li></ul>');
      $('li.resources').children('ul.menu').css({'display': 'block'});
      $('div#sidebar').css({'marginTop': '200px'});
      var thetitletoadd = $('#intro').children('h2').clone().css({'fontStyle': 'italic'});
      $('div.single').prepend(thetitletoadd);
      $('span.newsdate').css({'display': 'block'});
      $('span.newsdate').prepend('Reviewed on ');
   }

   if (whatstheid == 'archive-bo') {
      $('body').addClass('bookreviewsarchive');
      $('li.resources').addClass('navon').addClass('active-trail').children('ul').children('li.bookreviews').addClass('navon');
      $('div#intro').children('h3').append('<a href="/node/resources">Resources</a>');
      var goingintothesubnav = $('ul#archive-years').parent('dd').html();
      $('div#archive-container').remove();
      $('li.navon.bookreviews').append(goingintothesubnav);
      $('li.navon.bookreviews').children('ul#archive-years').addClass('menu');
      $('li.resources').children('ul.menu').css({'display': 'block'});
      $('ul#archive-years').children('li:first-child()').remove();
      $('ul#archive-years').children('li').prependTo( $('ul#archive-years') );
      $('div#content').find('h2.title').css({'fontStyle': 'italic'});
      $('#content').find('.node').find('.content').find("p:contains('Teaser:')").addClass('iamtheteaser').siblings().css({'display': 'none'});
      $('#content').find('.node').find('.content').find("p:contains('teaser:')").addClass('iamtheteaser').siblings().css({'display': 'none'});
      $('#content').find('.node').find('.content').find("p:contains('TEASER:')").addClass('iamtheteaser').siblings().css({'display': 'none'});
      $('.iamtheteaser').each(function() {
         this.innerHTML = this.innerHTML.substring(7);
      });
      $('ul.tabs').remove();
   }
} // pagetype

function betterbookstyles() {
   var whatstheid = $('body').attr('id').substring(0,10);
   if (whatstheid == 'node-bookr') {
      $('#content').find('.node').find('.content').find('p').children('img').parent('p').addClass('ihaveanimage');
      $('#content').find('.node').find('.content').find('p').children("a:contains('Amazon')").parent('p').addClass('igotoamazon');
      $('#content').find('.node').find('.content').find("p:contains('REVIEWER')").css({'clear': 'both'});
      $('#content').find('.node').find('.content').find("p:contains('Teaser:')").addClass('iamtheteaser');
      $('#content').find('.node').find('.content').find("p:contains('teaser:')").addClass('iamtheteaser');
      $('#content').find('.node').find('.content').find("p:contains('TEASER:')").addClass('iamtheteaser');
      $('.iamtheteaser').each(function() {
         this.innerHTML = this.innerHTML.substring(7);
      });
      $('#content').find('.node').find('.content').find("p:contains('IMAGE:')").addClass('iamtheimage');
      $('#content').find('.node').find('.content').find("p:contains('Image:')").addClass('iamtheimage');
      $('#content').find('.node').find('.content').find("p:contains('image:')").addClass('iamtheimage');
      $('.iamtheimage').each(function() {
         var theimageurl = this.innerHTML = this.innerHTML.substring(6);
         var replacethea = $(this).children('a').attr('href');
         $(this).html('<img src="' + theimageurl + '" alt="Book Cover" />');
         if (replacethea != null) {
            $(this).html('<img src="' + replacethea + '" alt="Book Cover" />');
         }
      });
   }
} // betterbookstyles

function statusmessage() {
   setTimeout( function () {$('div.messages.status').animate({'opacity': '0'}, 1500)}, 3000 );
   setTimeout( function () {$('div.messages.status').animate({'height': '0px', 'padding': '0px'}, 500)}, 4500 );
} // statusmessage



function timelineinteraction() {
$.getScript("/javascript/jquery-ui.js", function(){

//Getting the names out of <strong> and making them omnipresent

   $('#ages').children('li').find('li').each(function(i) {
      thehreftoset = $(this).children('a').attr('href')
      $(this).prepend('<a class="omnipresent" rel="external" target="_blank"></a>');
      $(this).children('.omnipresent').attr('href', thehreftoset).html($(this).children('a').children('strong').html());
      $(this).children('.omnipresent').children('br').remove();
      $(this).children('.omnipresent').children('span').remove();
      
      $(this).children('a').children('strong').text($(this).children('a').children('strong').children('span').text());
   }); //each


   theslidervalue = 0;

   //hover interaction
   $('#ages').children('li').children('ul').children('li').children('a').hover(function() {
      $(this).parent('li').children('a').css({'color': '#aa4411'});
   }, function() {
      $(this).parent('li').children('a').css({'color': '#333333'});
   }); //hover

   //setup the legend and arrows
   $('#timeline').append('<ul id="legend"></ul>');
   $('#ages').children('li').each(function(i) {
      $('#legend').append('<li><a>'+(7-i)+'</a></li>');
   }); //each
   $('#legend').after('<div class="legendlegend">millions of years</div>');
   $('#timeline').append('<a class="leftward">Left</a>');
   $('#timeline').append('<a class="rightward">Right</a>');

   //legend and arrow interaction
   $('a.rightward').click(function() {
      if (theslidervalue < 699) {
         theslidervalue = theslidervalue+233;
   		$('ul#ages').animate({'left': (-theslidervalue)});
   		$('div.ui-slider-handle').animate({'left': (theslidervalue)/2.33});
      } else {
         theslidervalue = 932;
   		$('ul#ages').animate({'left': (-theslidervalue)});
   		$('div.ui-slider-handle').animate({'left': 400});
      }
   }); //click rightward

   $('a.leftward').click(function() {
      if (theslidervalue > 233) {
         theslidervalue = theslidervalue-233;
   		$('ul#ages').animate({'left': (-theslidervalue)});
   		$('div.ui-slider-handle').animate({'left': (theslidervalue)/2.33});
      } else {
         theslidervalue = 0;
   		$('ul#ages').animate({'left': (theslidervalue)});
   		$('div.ui-slider-handle').animate({'left': 0});
      }
   }); //click leftward

   //set the slider in motion
   $("#slider").slider({
   	min: 0,
   	max: 932,
   	slide: function(event, ui) {
         theslidervalue = ($(this).children().children().position().left)*2.33;
   		$('ul#ages').css({'left': (-theslidervalue)});
   	}
   });


}); //getscript
} //timelineinteraction






