$(document).ready(function() {
   var popOut = "#popout"; // Name of the popout container.
   var adBox = "#adbox"; // Name of the animated bit of the ad.
   var adWidth = $(adBox).width() + $("#cap").width(); // Width of the ad container.
   var adCookie = "ad-example"; // Name of the cookie to be set.

   function openAd() {
      $(popOut).width(adWidth+"px");
      $(adBox).animate({ marginLeft: "0" }, 700)
      $.cookie(adCookie, null);
   }
   
   function closeAd() {
      $(adBox).animate({ marginLeft: "-"+adWidth+"px" }, 800, "easeOutBounce",
         function(){ $(popOut).width($("#cap").width() + "px"); }
      );
      $.cookie(adCookie, 'closed');
   }

   $("#open").click(function() {
      if(!$.cookie(adCookie)) {
         closeAd();
      } else {               
         openAd();
      }
      return false;
   });
   $("#close").click(function() {
      closeAd();
      return false;
   });   
   
   if(!$.cookie(adCookie)) {
      $(popOut).animate({ opacity: 1.0 }, 100, "linear", openAd);
   }
});
