var d=document;
var clicks=0;
var fb_loaded=false;
var tw_loaded=false;
$(d).ready(function() {
 if($('#tpn').length) {
  ddsmoothmenu.init({
   mainmenuid:"tpn",
   orientation:"h",
   classname:"dds",
   contentsource:"markup"
  });
 }
 if($('#anav').length) {
  ddsmoothmenu.init({
   mainmenuid:"anav",
   orientation:"h",
   classname:"dds",
   contentsource:"markup"
  });
 }
 $("#sch").keyup(function(event){ if(event.keyCode==13) srch(); });
 $('#lg').live({
  click: function() {
   node = $(this);
   clicks++;
   if(clicks==1){
    setTimeout(function() {
     if(clicks==1) location='/';
     else location='https://nilc.org/log_in.html';
     clicks=0;
    }, 300);
   }
  }      
 }); 
 $("input[rel],textarea[rel]").map(function() {
  if($(this).val() == "") $(this).val($(this).attr("rel"));
  $(this).focus(function(){
   if($(this).val() == $(this).attr("rel")) $(this).val("");
  });
  $(this).focusout(function(){
   if($(this).val() == "") $(this).val($(this).attr("rel"));
  });
 });
 $("#sup input").keyup(function(event){ if(event.keyCode==13) sup(); });
 $("#sup a").click(function() { sup(); });

 $('#lc nav span').each(function(el,idx){
  if(!$(this).parent().hasClass('open')) {
   $(this).parent().children('ul:first').hide();
  }
  $(this).hover(function(){
   if($(this).parent().hasClass('open')) $(this).css('background-image','url(/i/dtd.png)');
   else if($(this).parent().hasClass('sel')) $(this).css('background-image','url(/i/dtl.png)');
   else $(this).css('background-image','url(/i/btl.png)');
  },
  function(){
   if($(this).parent().hasClass('open')) $(this).css('background-image','url(/i/btd.png)');
   else if($(this).parent().hasClass('sel')) $(this).css('background-image','url(/i/btr.png)');
   else $(this).css('background-image','url(/i/otl.png)');
  });
  $(this).click(function(){
   if($(this).parent().hasClass('open')) {
    $(this).parent().children('ul:first').slideUp(400, function() {
     $(this).parent().removeClass('open');
     if($(this).parent().hasClass('sel')) $(this).parent().children('span').css('background-image','url(/i/dtl.png)');
     else $(this).parent().children('span').css('background-image','url(/i/btl.png)');
    });
   }
   else {
    $(this).css('background-image','url(/i/dtd.png)');
    $(this).parent().addClass('open');
    $(this).parent().children('ul:first').slideDown();
   }
  });
 });
 $("#shr").hover(function() {
  $(this).css("background-image","url(/i/share_on.png)");
  $("#shr div").fadeIn(300);
 },
 function() {
  $(this).css("background-image","url(/i/share_off.png)");
  $("#shr div").fadeOut(200);
 });
 $("#shr div a.sh_f").click(function() {
  if(!fb_loaded) {
   var str='<div id="fb-root"></div>';
   str+='<script>(function(d, s, id) {';
   str+='var js, fjs = d.getElementsByTagName(s)[0];';
   str+='if (d.getElementById(id)) return;';
   str+='js = d.createElement(s); js.id = id;';
   str+='js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";';
   str+='fjs.parentNode.insertBefore(js, fjs)';
   str+='}(document, "script", "facebook-jssdk"));</script>';
   $("body").prepend(str);

   str='<div class="fb-like" data-href="';
   str+=window.location.href;
   str+='" data-send="false" data-layout="box_count" data-width="74" data-show-faces="false"></div>';
   $("#shr>div").prepend(str);
   $("#shr div").hide().delay(200).show();
   $("#shr div a.sh_f").hide();

   fb_loaded = true;
  }
 });
 $("#shr div a.sh_t").click(function() {
  if(!tw_loaded) {
   $("#shr div a.sh_t").after('<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>');

   tw_loaded=true;
  }
 });
 $("#shr div a.sh_g").click(function() {
  var str='<div class="g-plusone" data-size="medium"></div>';
  str+='<script>(function() {var po=d.createElement("script"); po.type="text/javascript"; po.async=true;';
  str+='po.src="https://apis.google.com/js/plusone.js"; var s=d.getElementsByTagName("script")[0];';
  str+='s.parentNode.insertBefore(po,s); })();</script><p></p>';
  $("#shr div a.sh_g").before(str);

  $("#shr div").hide().delay(200).show();
  $("#shr div a.sh_g").hide();
 });
 $("#shr div a.sh_e").click(function() {
  var url="mailto:?subject="+encodeURIComponent(document.title);
  url+="&body="+encodeURIComponent(window.location.href);
  window.location.href=url;
  alert("If your email client does not appear after closing this dialog box, please check the configuration of your web browser and email client.");
 });

});
function srch() {
 var tmp=d.c.action;
 d.c.action='search.html';
 d.c.submit();
 d.c.action=tmp;
}
function sup() {
 d.dia.action="https://salsa.democracyinaction.org/dia/rest/process.jsp";
 d.dia.method="get";
 $("#sup").append('<input type="hidden" name="table" value="supporter"/>');
 $("#sup").append('<input type="hidden" name="organization_KEY" value="371"/>');
 $("#sup").append('<input type="hidden" name="redirect" value="https://nilc.org/subscription_confirmation.html"/>');
 d.dia.submit();
 $("#sup a").html("Sending...");
}

