$(document).ready(setupSubscribeClear)

function setupSubscribeClear() {
   $('#nbs_subscribe_email').focus(function () {
      if (this.value == this.getAttribute('altvalue')) {
         this.value = '';
      }
   })
   .blur(function () {
      if (this.value == '') {
         this.value = this.getAttribute('altvalue');
      }
   });
}

/* let CallToAction block scroll with window, when it's scrolled far enough */
$(document).ready(applyCtaAutoScroll);
var jCta, domCta, ctaAbsPosition, ctaRelPosition;

function applyCtaAutoScroll() {
   jCta = $('.calltoaction');
   domCta = jCta.get(0);

   //skip setup when there's no CTA block
   if (jCta.length == 0) {
      return;
   }

   ctaAbsPosition = getPosition(domCta).top;
   ctaRelPosition = domCta.offsetTop;

   setInterval(setToScrollHeight, 100);
}

function setToScrollHeight() {
   var scrollHeight = getScrollHeight();

   if (ctaAbsPosition < scrollHeight+11) {
      jCta.css({top: ctaRelPosition + (scrollHeight - ctaAbsPosition) +11 });
   }
   else {
      jCta.css({top: ctaRelPosition });
   }
}

/* int getScrollHeight
   returns the scrolled distance of the main document in pixels.
   In other words, the height of the top portion of the page that's not visible
   due to scrolling.
*/
function getScrollHeight() {
   var distance = 0;

   if (self.pageYOffset) {
      // all except Explorer
      distance = self.pageYOffset;
   }
   else if (document.documentElement && document.documentElement.scrollTop) {
      // Explorer 6 Strict
      distance = document.documentElement.scrollTop;
   }
   else if (document.body) {
      // all other Explorers
      distance = document.body.scrollTop;
   }

   return distance;
}

function addressLookup(fZip, fStreet, fCity) {
   // notify user of action
   fStreet.val("Opzoeken..");
   fCity.val("Opzoeken..");
   fStreet.attr("readonly", true);
   fCity.attr("readonly", true);

   // get data from L2O
   $.get("/templates/snippets/ajax_zipcode.asp?field=street&zipcode="+fZip.val(), 
    function(data) { fStreet.val(data); }
   );
   $.get("/templates/snippets/ajax_zipcode.asp?field=city&zipcode="+fZip.val(), 
    function(data) { fCity.val(data); }
   );

   // re-enable input
   fStreet.removeAttr("readonly");
   fCity.removeAttr("readonly");

}

function zipChanged(sForm){
   var oForm = document.forms[sForm];
   oForm.zipchanged.value = 1;
   oForm.submit();
}