$(document).ready(function() {

 // Change default newsletter text on field focus
 newsletterfocus = false;
 $("#newsletter_email").focus(function() {
  if(!newsletterfocus) {
   $(this).val("");
   $(this).css("color", "#000");
   newsletterfocus = true;
  }
 });
 
 // Handle form submission dynamically
 $("#newsletter_form").submit(function() {
  $("#newsletteroverlaybg").css({'width':$(".newsletter").css("width"),'height':$(".newsletter").css("height"),'display':'block'});
  $("#newsletteroverlay").css({'width':$(".newsletter").css("width"),'height':$(".newsletter").css("height"),'display':'block'});
  $.get("/js/tools/newslettersignup.php?email="+$("#newsletter_email").val(), function(data) {
   if(data=="success") {
    successwait = setInterval("newsletterSuccess()", 2000);
   }
   else if(data=="registered") {
    $("#newsletteroverlaybg").css({'display':'none'});
    $("#newsletteroverlay").css({'display':'none'});
	alert($("#newsletter_email").val()+" is already in our newsletter mailing list");
   }
   else {
    $("#newsletteroverlaybg").css({'display':'none'});
    $("#newsletteroverlay").css({'display':'none'});
	alert("Unable to complete your request. Please check that the e-mail address you entered is valid and try again.");
   }
  });
  return false;
 });
 
 // Handle poll submission
 $("#pollform").submit(function() {
  var x = document.pollform;
  if(x.authstatus.value==1) {
   if(x.poll.value != "") {
    window.open("/poll/?id="+x.pollid.value+"&choice="+x.poll.value, "window-pollform", "width=500,height=400,scrollbars=no,toolbar=no,menubar=no");
   }
   else {
    alert("Please select an option and try again.");
   }
  }
  else {
   alert("In order to submit a vote in the Terrier Bytes poll, you need to be logged in.");
  }
  return false;
 });
 
 $("#showpollresults").click(function() {
  window.open($(this).attr("href"), "window-pollform", "width=500,height=400,scrollbars=no,toolbar=no,menubar=no");
  return false;
 });
 
 // Handle homepage tabs
 $("#tablinks li a").click(function() {
  var id1 = $(this).attr("id").replace("tablink","");
  $("#tabs div").each(function() {
   var id2 = $(this).attr("id").replace("tab","");
   if(id1==id2) {
    $("#tab"+id2).css("display","block");
	$("#tablink"+id2).parent().addClass("active");
   }
   else {
    $("#tab"+id2).css("display","none");
	$("#tablink"+id2).parent().removeClass("active");
   }
  });
  return false;
 });
 
});

// Show success message
function newsletterSuccess() {
 $("#newsletteroverlay").html("<p>Sign up successful!</p><p style='font-weight: normal;'>Thank you for signing up for the Terrier Bytes newsletter.</p>");
 clearInterval(successwait);
}
