//*****Replace align attribute with class**************************************************//
function replaceAlign() {
  if (!document.getElementsByTagName('img')) return false;
  $('img[align="left"]').addClass('left').removeAttr('align');
	$('img[align="right"]').addClass('right').removeAttr('align');
	$('img[align="middle"]').addClass('middle').removeAttr('align');
}
//*****Replace target attribute with class**************************************************//
function replaceTarget() {
  if (!document.getElementsByTagName('a')) return false;
  $('a[target]').addClass('newwindow').removeAttr('target');
}
//*****The following function make it possible to have web standard popups**************************************************//
function strictNewWindow() {
  if (!document.getElementsByTagName('a')) return false;
  $('a.newwindow').click(function() {
		window.open($(this).attr('href'));
    return false;
	});
}
//*****Add swfobject flash call**************************************************//
function initializeFlash() {
	if (!document.getElementById('flash-header')) return false;
	var flashvars = {};
	var params = {wmode: 'transparent'};
	var attributes = {};
	attributes.id = "flash-header";
	swfobject.embedSWF("/swf/flash-home.swf", "flash-header", "960", "273", "8.0.0", "/swf/expressInstall.swf", flashvars, params, attributes);
}
//*****Clear search text out search box on focus**************************************************//
function clearSearchBox() {
	$('#txtSearch').focus(function() {
    if ($(this).val() == 'SEARCH') {
      $(this).val('');
	  }
  });
	$('#firstName').focus(function() {
  	if ($(this).val() == 'First Name') {
      $(this).val('');
	  }
  });
	$('#lastName').focus(function() {
  	if ($(this).val() == 'Last Name') {
      $(this).val('');
	  }
  });
	$('#CatalogSearch').focus(function() {
	if ($(this).val() == 'SEARCH THE CATALOG') {
    $(this).val('');
	  }
});
}
//*****Extend the footer to the bottom of the page**************************************************//
function extendFooter() {
  var footerHeight = $('#footer').height();
  var pageHeight = $(document.body).height();
  var windowHeight = $(document).height();
  var difference = windowHeight - pageHeight;
  if (pageHeight < windowHeight) {
    $('#footer').height(footerHeight + difference);
  } else {
    $('#footer').height('52px');
  }
}
//*****Extend the footer to the bottom of the page**************************************************//
function clearFloats() {
	if (!document.getElementById('linkList')) return false;
  $('ul#linkList li:even').addClass('left');
}
//*****Cycle banner ads on homeapge**************************************************//
function bannerAds() {
	if (!document.getElementById('bannerAd')) return false;
  var count = 0;
  var banner_count = $('#bannerAd ul li').size();
  $('#bannerAd ul li:eq(' + count + ')').toggleClass('noShow');
  $('p#bannerPrevious a').click(function () {
    if (count == 0) {
      count = banner_count - 1;
    } else {
      count -= 1;
    }
    $('#bannerAd ul li').addClass('noShow');
    $('#bannerAd ul li:eq(' + count + ')').toggleClass('noShow');
  });
  $('p#bannerNext a').click(function () {
    if (count == banner_count - 1) {
      count = 0;
    } else {
      count += 1;
    }
    $('#bannerAd ul li').addClass('noShow');
    $('#bannerAd ul li:eq(' + count + ')').toggleClass('noShow');
  });
}
//********************************Reveal/Toggle on click*********************************//
function revealCopy() {
  $('div.toggle').hide();
  $('.reveal').addClass('inactive');
  $('.revealCopyWrap .reveal').click(function () {	
	$(this).toggleClass('inactive');
	$(this).toggleClass('active');
  	$(this).parent().find('div.toggle').slideToggle();
  });
}
//***********************Open Select option in new window on library page*******************//
function dbGo() {
	if (!document.getElementById('PopularDatabases')) return false;
	$('#PopularDatabases').change(function(){
		if ($('#PopularDatabases option:selected').val() != ''){
		  window.open($('#PopularDatabases option:selected').val());	
		} 
	return false;
	})
}
function trackGiving() {
	$('.giveNow a').click(function(e){
		var linkLocation = window.location.host + window.location.pathname.replace("/", "_")
		pageTracker._trackPageview('/giveonline/'+ linkLocation)
	})
}

//*****Load all functions**************************************************//
$(document).ready(function(){
  dbGo();
  replaceAlign();
  replaceTarget();
  strictNewWindow();
  initializeFlash();
  clearSearchBox();
  bannerAds();
  clearFloats();
  extendFooter();
  revealCopy();
	trackGiving();
});