//*****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;
	});
  $('a.listenPopup').click(function() {
		window.open(jQuery(this).attr('href'),'listenPopup','width=600,height=260,resizable=no,scrollbars=no,toolbar=no,location=no');
    return false;
	});
  $('p#bannerPopup a').click(function() {
		window.open(jQuery(this).attr('href'),'listenPopup','width=600,height=260,resizable=no,scrollbars=no,toolbar=no,location=no');
    return false;
	});
}
//*****Clear search text out search box on focus**************************************************//
function clearSearchBox() {
	if (!document.getElementById('txtSearch')) return false;
	$('#txtSearch').focus(function() {
    if ($(this).val() == 'SEARCH') {
      $(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');
  }
}
//*****Switch the filedsets on the seach page**************************************************//
function switchFieldset() {
	$('#mediaSearch > ul li a').each(function (i) {
	  $(this).attr('id','section' + i);
  });
	$('#mediaSearch fieldset').each(function (i) {
	  $(this).attr('id','fieldset' + i);
  });
  $('#mediaSearch > ul li a').click(function() {
    var clicked = $(this).attr('id').substr(7,2);
    $('#mediaSearch > ul li a').each(function (i) {
	    $(this).removeClass('selected');
    });
    $(this).addClass('selected');
    $('#mediaSearch fieldset').each(function (i) {
	    $(this).addClass('hidden');
    });
    $('#fieldset' + clicked).toggleClass('hidden');
    return false;
  });
}
//*****Alternate row color on the search results**************************************************//
function alternateSearchColor() {
  $('.dailymessages table#mp3Info tr:odd').addClass('odd');
  $('.dailymessages table#mp3Info tr:even').addClass('even');
  $('.dailymessages div.wrapper:odd').addClass('odd');
  $('.dailymessages div.wrapper:even').addClass('even');
  $('.dailymessages table#devotionalList tr:odd').addClass('odd');
  $('.dailymessages table#devotionalList tr:even').addClass('even');
}
//********************************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();
  });
}
function trackGiving() {
	$('a#gwsblnk').click(function(e){
		var linkLocation = window.location.host + window.location.pathname.replace("/", "_")
		pageTracker._trackPageview('/giveonline/'+ linkLocation)
	})
}
//*****Load all functions**************************************************//
$(document).ready(function(){
  replaceAlign();
  replaceTarget();
  strictNewWindow();
  clearSearchBox();
  extendFooter();
  //switchFieldset();
  alternateSearchColor();
  revealCopy();
  trackGiving();
});
