(function($){
	$.fn.clearDefault = function(){
		return this.each(function(){
			var default_value = $(this).val();
			$(this).focus(function(){
				if ($(this).val() == default_value) $(this).val("");
			});
			$(this).blur(function(){
				if ($(this).val() == "") $(this).val(default_value);
			});
		});
	};
})(jQuery);
$(document).ready(function(){
  FormToAkamaiRedirect('budget.house.gov');
  $('div.gallery').cycle({
    timeout:6000,
    speed:500,
    slideExpr:'.gallery-holder ul > li',
    pager:'ul.links-block',
    pagerAnchorBuilder: function(index, DOMelement){
      return '<li><a href="#">'+(index+1)+'</a></li>';
    },
    activePagerClass:'active',
    next:'.gallery .next',
    prev:'.gallery .prev',
    before: onBefore
  });
  $('.swicher .pause').each(function(){
    var playpause = $(this);
    playpause.click(function(e){
      e.preventDefault();
      if(playpause.text()=='pause')
      {
        $('div.gallery').cycle('pause');
        playpause.text('play');
      }
      else
      {
        $('div.gallery').cycle('resume');
        playpause.text('pause');
        pauseAllVideos();
      }
    });
  });
	$('a:not([href])').css('text-decoration','none').css('color','#000');
//	$('a:external').each(function(){
//	  var dest = $(this).attr('href');
//	  $(this).attr('href','/webreturn/?url='+encodeURI(dest));
//	});
	$('input#update').clearDefault();
});

function onBefore(currSlideElement, nextSlideElement, options, forwardFlag)
{
	pauseAllVideos();
}

function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById(playerId);
  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}

function onytplayerStateChange(newState) {
  //alert("Player's new state: " + newState);
  if(newState == 1)
  {
    $('div.gallery').cycle('pause');
    $('.swicher .pause').text('play');
  }
  else if(newState == 2 || newState == 0)
  {
    $('div.gallery').cycle('resume');
    $('.swicher .pause').text('pause');
  }
}

function pauseAllVideos()
{
  var video = $('object[id*=ytplay], embed[id*=ytplay]');
  video.each(function(){
    try{
      this.stopVideo();
    }
    catch(err)
    {
      //var vidError = err;
      //var nothing = 6;
    }
  }); 
}

// 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(/facebook\.com\/sharer.php/i.test(a.href)) {return false;}
    return a.hostname && a.hostname !== window.location.hostname;
  }
});
// 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);
    });
  }
}

