$(document).ready(function() {
  
  //'disables' the correct main navigation button
  $(".nav").children("li").each(function() {
      var current = "nav current-" + ($(this).attr("class"));
      var parentClass = $(".nav").attr("class");
      if (parentClass == current) {
          $(this).addClass('selected');
      }
  });
  
  //'disables' the correct sub navigation button
  $(".subnav").children("li").each(function() {
      var current = "subnav current-" + ($(this).attr("class"));
      var parentClass = $(".subnav").attr("class");
      if (parentClass == current) {
          $(this).addClass('selected');
      }
  });
  
  
  //writes the registration module on the page
  writeForm();
  
  function writeForm(){
    var formHtml = '<div class="registerHeader"><img src="images/nav/registerFormHeader.png" /></div><form name="share" action="register.php" method="get"><input type="text" name="registerEmail" value="" id="registerEmail" style="width:243px;position:relative;left:10px; top:0px;" /></form><div class="registerSendBtn"></div><div class="registerCancelBtn"></div>';
    $('#registerForm').html(formHtml);
  }
  
  //function to center element, first parameter can be 'vertical', 'horizontal', or 'both'
  //second parameter is number of pixels added to the y position
  jQuery.fn.center = function (pos, topOffset) {
    settings = jQuery.extend({
      pos: 'both',
      topOffset: 0
    }, pos, topOffset);
    
    this.css("position","absolute");
    
    if(pos == "vertical" || pos == "both"){
      this.css("top", (( $(window).height() - this.height() ) / 2+$(window).scrollTop()) + topOffset + "px");
    }
    
    if(pos == "horizontal" || pos == "both"){
      this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    }
    
    return this;
  }
  
  //register module button functions
  $(".homeRegisterBtn").click(function() {
    $('#registerForm').show();
    document.share.registerEmail.focus();
  });
  
  $(".homeRegisterBtn").hover(function() {
    $(this).css({ 'background' : 'url(images/home/homeRegisterBtn.png) no-repeat 0px -18px'});
  }, function() {
    $(this).css({ 'background' : 'url(images/home/homeRegisterBtn.png) no-repeat 0px 0px'});
  });
  
  $(".registerBtn").hover(function() {
     $(this).css({ 'background' : 'url(images/nav/registerBtn.png) no-repeat 0px -18px'});
  }, function() {
     $(this).css({ 'background' : 'url(images/nav/registerBtn.png) no-repeat 0px 0px'});
  });
  
  $(".twitterBtn").hover(function() {
     $(this).css({ 'background' : 'url(images/nav/twitterBtn.png) no-repeat 0px -18px'});
  }, function() {
     $(this).css({ 'background' : 'url(images/nav/twitterBtn.png) no-repeat 0px 0px'});
  });
  
  $(".facebookBtn").hover(function() {
     $(this).css({ 'background' : 'url(images/nav/facebookBtn.png) no-repeat 0px -18px'});
  }, function() {
     $(this).css({ 'background' : 'url(images/nav/facebookBtn.png) no-repeat 0px 0px'});
  });
  
  $(".starworksBtn").hover(function() {
     $(this).css({ 'background' : 'url(images/nav/starworksBtn.png) no-repeat 0px -18px'});
  }, function() {
     $(this).css({ 'background' : 'url(images/nav/starworksBtn.png) no-repeat 0px 0px'});
  });
  
  $(".registerBtn").click(function() {
    $('#registerForm').show();
    document.share.registerEmail.focus();
  });
  
  $(".registerSendBtn").hover(function() {
     $(this).css({ 'background' : 'url(images/nav/registerSendBtn.png) no-repeat 0px -18px'});
  }, function() {
     $(this).css({ 'background' : 'url(images/nav/registerSendBtn.png) no-repeat 0px 0px'});
  });
  
  $(".registerSendBtn").click(function() {
    var email = $("input#registerEmail").val();	
    var dataString = 'email=' + email;
		
    $.ajax({
      type: "POST",
      url: 'scripts/register.php',
      data: dataString,
        success: function() {
          $('#registerForm').html('<div id="message"><div class="registerHeader"><img src="images/nav/registerThankYou.png" /></div><p><span style="color:#666;margin-left:10px;">You have been added to our mailing list.</span></p></div>');
          $('#message').hide()
          .fadeIn(1500, function() {
            $('#message');
          });

        }
    });
  });
  
  $(".registerCancelBtn").hover(function() {
     $(this).css({ 'background' : 'url(images/nav/registerCancelBtn.png) no-repeat 0px -18px'});
  }, function() {
     $(this).css({ 'background' : 'url(images/nav/registerCancelBtn.png) no-repeat 0px 0px'});
  });
  
  $(".registerCancelBtn").click(function() {
    $('#registerForm').hide();
  });
  
});
