function initMenu(){
	var nodes = document.getElementById("nav").getElementsByTagName("li");
	for (var i=0; i<nodes.length; i++)
	{
		nodes[i].onmouseover = function()
		{
			this.className += " hover";
		}
		nodes[i].onmouseout = function()
		{
			this.className = this.className.replace(" hover", "");
		}
	}
}
if (document.all && !window.opera) attachEvent("onload", initMenu);

	
//function EndRequestHandler(sender, args) {
// SetUpCalendar();
//}
//function RegisterCalendarHandler() {
//  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
//}

$(function(){
  $('#slides').cycle({
    timeout:9000,
    pager:'.switcher',
    pagerAnchorBuilder:pagerMaker
  });
	$('.pageTitle[id*=IssueTitle] ~ br').remove();
	$('.pageTitle[id*=IssueTitle]').remove(); 
	SetUpCalendar();
	$('a:external').each(function(){
	  var extLink = $(this);
	  extLink.click(function(e){
	    if(exitMsg())
	    {
	      window.location = '/webreturn/?url=' + encodeURI(extLink.attr('href'));
	    }
	    else
	    {
	      return false;
	    }
	  });
	});
	Spotlight();
	SpotlightB();
	//RegisterCalendarHandler();
	WirePlaceholders();
	SeedSignup();
	FormToAkamaiRedirect('waysandmeans.house.gov');
	PopupControl('popup','#Popup',[1]);
	//PopupControl('popup','#hearingPopup',[1,2,3,4,5,6,7,8,9,10]);
});

// JavaScript External Link Exit Message
function exitMsg(){
  var answer = confirm("You are now leaving the Committee on Ways and Means website. Thank you for visiting. Neither this office nor the U.S. House of Representatives is responsible for the content of the non-House site you are about to access.")

  if (!answer)
  return false;

  return true;
  //end of Exit Message
}


function inputSupportsPlaceholder() {
  var i = document.createElement('input');
  return 'placeholder' in i;
}
function WirePlaceholders(){
  if (!inputSupportsPlaceholder()) {
    $('input[placeholder]').each(function () {
      var textBox = $(this);
      if (textBox.val() == '')
      { textBox.val(textBox.attr('placeholder')); }
      textBox.focus(function () {
        if (textBox.val() == textBox.attr('placeholder'))
        { textBox.val(''); }
      });
      textBox.blur(function () {
        if (textBox.val() == '')
        { textBox.val(textBox.attr('placeholder')); }
      });
      textBox.parents('form').submit(function(){
        if (textBox.val() == textBox.attr('placeholder'))
        { textBox.val(''); }
      });
    });
  }
}

function Spotlight(){
  var startSpot = parseInt(readCookie('spotlight'),10);
  if(isNaN(startSpot)){startSpot = 0;}
  if(startSpot>=$('.spotlights > li').length){startSpot = 0;}
  $('.spotlights').cycle({
    timeout:0,
    prev:'.back',
    next:'.continue',
    startingSlide: startSpot,
    after: IncrementStartSpotLong
  });
}

function IncrementStartSpotLong(currSlideElement, nextSlideElement, options, forwardFlag)
{
  var nextIndex = $('.spotlights li').index(nextSlideElement) + 1;
  IncrementStartSpot(nextIndex);
}

function IncrementStartSpot(newindex)
{
  var newStart = newindex;
  if(newStart>=$('.spotlights > li').length){newStart = 0;}
  createCookie('spotlight',newStart.toString(),365);
}

function SpotlightB(){
  var startSpotB = parseInt(readCookie('spotlightB'),10);
  if(isNaN(startSpotB)){startSpotB = 0;}
  if(startSpotB>=$('.spotlightsB > li').length){startSpotB = 0;}
  $('.spotlightsB').cycle({
    timeout:0,
    prev:'.backB',
    next:'.continueB',
    startingSlide: startSpotB,
    after: IncrementStartSpotBLong
  });
}

function IncrementStartSpotBLong(currSlideElement, nextSlideElement, options, forwardFlag)
{
  var nextIndex = $('.spotlightsB li').index(nextSlideElement) + 1;
  IncrementStartSpotB(nextIndex);
}

function IncrementStartSpotB(newindex)
{
  var newStartB = newindex;
  if(newStartB>=$('.spotlightsB > li').length){newStartB = 0;}
  createCookie('spotlightB',newStartB.toString(),365);
}

function SetUpCalendar(){
	$('.CalendarControl tr').each(function(){
    if($(this).children('td:empty, th:empty').length === $(this).children('td, th').length){
      $(this).remove(); // or $(this).hide();
    }
  });
}

function pagerMaker(index, DomElement){
  return '<li><a href="#">'+index+'</a></li>';
}

function EscapeAtSign(element){
  element.value = element.value.replace(/@/g,'%40');
}

function SeedSignup()
{
  var homePageEmail = $('#HomeEmailInput').val();
  if (homePageEmail != null)
  {
    $('#HomeEmailInput').val(homePageEmail.replace(/%40/g,'@'));
  }
  
  //$('#HomeEmailInput').val($('#HomeEmailInput').val().replace(/%40/g,"@"));
  var formID = $.url().param('ID');
  if(formID == '313')
  {
    SeedHelper('email', $('input[id$=EmailControl]'));
    SeedHelper('firstname', $('#' + $('label:contains(First Name)').attr('for')));
    SeedHelper('lastname', $('#' + $('label:contains(Last Name)').attr('for')));
  }
}

function SeedHelper (parameterString, targetInput)
{
  var param = $.url().param(parameterString);
  if(param!=null)
  {
    param = unescape(param.replace(/\+/g, ' '));
    targetInput.val(param);
  }
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// Check whether links are external:
// (Only works with elements that have href):
$.extend($.expr[':'],{
  external: function(a,i,m) {
    if(!a.href) {return false;}
    //if(a.hostname.search(/house\.gov/gi)!=-1) {return false;}
    if(a.hostname.search(/waysandmeans\.house\.gov/gi)!=-1) {return false;}
    if(a.hostname.search(/waysandmeans.edgeboss.net/gi)!=-1) {return false;}
    if(a.hostname.search(/waysandmeans.granicus.com/gi)!=-1) {return false;}
    if(a.hostname.search(/documentquery/gi)!=-1) {return false;}
    if(a.hostname.search(/documentsingle/gi)!=-1) {return false;}
    if(a.hostname.search(/documentprint/gi)!=-1) {return false;}
    if(a.hostname.search(/twitter/gi)!=-1) {return false;}
    if(a.hostname.search(/postback/gi)!=-1) {return false;}
    if(a.href.search(/javascript:/gi)!=-1) {return false;}
    return a.hostname && a.hostname !== window.location.hostname;
  }
});
// JS Popup port
function PopupControl(cookieName, popupLink, integerArray){
  if($(popupLink).size()>0){
    if(readCookie(cookieName + '-session') != 'true'){
      var visitCount = parseInt(readCookie(cookieName), 10);
      if(isNaN(visitCount)){
        visitCount = 1; }
      for (index = 0; index < integerArray.length; index++){
        if(integerArray[index] == visitCount){
          setTimeout('$("'+popupLink+'").click()',200); }
      }
      createCookie(cookieName + '-session', 'true');
      createCookie(cookieName, visitCount + 1, 365);
    }
  }
}
// Send relative links to unSSL canonical DNS
function FormToAkamaiRedirect(defaultDomain)
{
  if(location.hostname != defaultDomain
    || location.protocol != 'http:')
  {
    $('a[href^=\\/]').each(function(){
      var origUrl = $(this).attr('href');
      $(this).attr('href', 'http://' + defaultDomain + origUrl);
    });
  }
}

