// Declare a namespace for the site
var OpeningDayHoliday = (function(ns) {
    ns.page = 1;
    ns.resizeGenerator = function () {
      var h = $('body#generator div#wrap').height();
      parent.$.colorbox.resize({height:h});
    }
    ns.allowNext = function() {
      if ( OpeningDayHoliday.page < 7 ) {
        if ( $("body#generator form ol>li:nth-child(" + OpeningDayHoliday.page + ") input[type=text]").val() || $("body#generator form ol>li:nth-child(" + OpeningDayHoliday.page + ") input[type=radio]:checked").val() ) {
          if ( OpeningDayHoliday.page === 6 && $("input[name=q6]:checked").val() === 'Other' && !$('input#q6text').val() ) {
            
          } else {
            $("form input#btn-next").show();
            OpeningDayHoliday.resizeGenerator();
          }
        } else {
          $("form input#btn-next").hide();
        }
      } else {
        if ( $("body#generator form ol>li:nth-child(" + OpeningDayHoliday.page + ") input[type=radio]:checked").val() ) {
          $("body#generator form input#generate-btn").show();
          OpeningDayHoliday.resizeGenerator();
        }
      }
    };
    ns.generateExecuse = function() {
      var q1, q2, q3, q4, q5, q6, q7, str;
      
      q1 = $("input[name=q1]").val();
      q2 = $("input[name=q2]").val();
      q3 = $("input[name=q3]:checked").val();
      q4 = $("input[name=q4]:checked").val();
      q5 = $("input[name=q5]:checked").val();
      q6 = $("input[name=q6]:checked").val();
      q7 = $("input[name=q7]:checked").val();
      
      if ( q6 === 'Other' ) {
        q6 = $("input#q6text").val();
      }
      
      switch ( parseInt(q4) )
      {
        case 0:
          // The Hardcore Heckler
          str = q2 + ",\n\nI canʼt believe it slipped my mind! I completely forgot today was my little sisterʼs ballet recital. Normally, iʼm freaked out around that much ";
          str += "frilly stuff, but the sis is going to her big competition next week. I guess sheʼs taking after her older bro who, in case youʼve ";
          str += "forgotten, was all-state quaterback my senior year. Sure, I feel a bit out of place being the only guy backstage cheering them on ";
          str += "while snacking on " + q6 + ". Moms usually come up to me and ask, “What are you doing here?” I normally just say " + q5 + " So, yeah, it looks ";
          str += "like I wonʼt be making it " + q3 + " today. Better me than you.\n\n" + q7 + "\n" + q1;
          break;
        
        case 1:
          // The Stats Nerd
          str = q2 + ",\n\nToday started off like any other. I was checking to see who my latest Twitter followers were on my iPad when I saw that ";
          str += "Amazon was having a sale on " + q6 + ". I was so startled that I spilled my latte, tripped on my cat and broke my glasses. ";
          str += "I couldnʼt see a thing! Luckily, I was able to hitch a ride with my neighbor to Forever 20/20 to get some new glasses. ";
          str += "The employee behind the counter asked me “What are you doing here?” I quickly replied with " + q5 + ", which she ";
          str += "didnʼt seem to appreciate. You see, without my glasses I am what doctors consider “legally blind” and my condition is so unique that they will not have a new pair for me until late this evening. ";
          str += "They are also fresh out of seeing eye dogs at the moment. It seems I will not be making it " + q3 + " today.\n\n" + q7 + "\n" + q1;
          break;
        
        case 2:
          // The Enthusiasitc Cheerleader
          str = q2 + ",\n\nOMG! You arenʼt going to believe what happened this morning! I was looking in the mirror and noticed something wasnʼt right. There was ";
          str += "a whisker! Not just any whisker, iʼm talking a full on Tom Selleck-ish whisker! Thereʼs no way I could be seen in public like this, especially ";
          str += "at the Gossip Girl marathon party iʼm going to tonight. I did what any normal girl would do in my situation, I threw a paper bag over ";
          str += "my head and scheduled an emergency laser hair removal appointment. So here I am at Lazer Shaver. The receptionist is a bit nosey and smells like " + q6;
          str += ". They asked me “What are you doing here?” and I told them " + q5 + " instead of revealing my real reason. My surgery isn’t for another four hours, so ";
          str += "I'm stuck here rocking the Tom Selleck until then. There is no way I will be making it " + q3 + " today. Sorry.\n\n" + q7 + "\n" + q1;
          break;
        
        case 3:
          // the Bandwagon Socializer
          str = q2 + ",\n\nThis may come as a surprise to you, but I wonʼt be making it " + q3 + " today. You see, I had stopped off at the market to get you your favorite ";
          str += "chocolates; the ones with the clever sayings on the wrapper. On my way out, something amazing caught my eye. I thought I was ";
          str += "dreaming, but no. It was a vending machine filled with " + q6 + ". Itʼs as if my prayers had been answered. As luck would have it, my " + q6 + " got stuck in the spindle.";
          str += " They were on the bottom row, so I reached up into the machine to grab them. The clerk rushed over and asked “What are you doing here?” Too embarrassed to tell the truth, I just said " + q5 + " I donʼt think they bought it ";
          str += "because the vending machine company was notified and they are on their way out here to set me free. Unfortunately, they are located in ";
          str += "the next state, so I wonʼt be making it " + q3 + " today. On a brighter note, I will have chocolate for you tomorrow.\n\n" + q7 + "\n" + q1;
          break;
        
        default:
          str = "Whoops. There was an error. How's that for an excuse?";
          break;
      }
      $("body#generator form ol").hide();
      $("body#generator form input#generate-btn").hide();
      $("body#generator div.addthis_toolbox").show();
      $("body#generator form ol").after('<pre>' + str + '</pre>');
      OpeningDayHoliday.resizeGenerator();
    };

    return ns;
})(window.OpeningDayHoliday || {});

// Create a closure to maintain scope of the '$' and remain compatible with other frameworks
(function($) {
  
  // called once the DOM is ready, same as $(document).ready();
  $(function() {
    if ( jQuery().countDown ) {
      $('#countdown_dashboard').countDown({
        targetDate: {
          'day':    31,
          'month':  3,
          'year':   2011,
          'hour':   15,
          'min':    15,
          'sec':    0
        }
      });
    }
    
    if ( jQuery().colorbox ) {
      $('a.launch-generator, a.switch-info').colorbox({
        iframe: true,
        href: function() { return $(this).attr('href'); },
        width: 594,
        height: 480,
        initialWidth: 594,
        opacity: 0.6,
        returnFocus: false,
        scrolling: false
      });
    }
    
    $("body#generator form ol").after('<input type="button" id="btn-next" value="Next" />');
    
    $("body#generator form ol li").hide();
    $("body#generator form ol li input[type=radio]").change(OpeningDayHoliday.allowNext);
    $("body#generator form ol li input[type=text]").keyup(OpeningDayHoliday.allowNext);
    $("body#generator form ol li:first-child").show();
    $("body#generator form input#generate-btn").hide();
    $("body#generator div.addthis_toolbox").hide();
    
    $("form input#btn-next").hide().click(function() {
      $(this).blur().hide();
      $("body#generator form ol>li:nth-child(" + OpeningDayHoliday.page + ")").hide();
      OpeningDayHoliday.page++;
      $("body#generator form ol>li:nth-child(" + OpeningDayHoliday.page + ")").show();
      OpeningDayHoliday.resizeGenerator();
    });
  });

  // called after images and css are loaded, use for validation, news ticker, etc.
  $(window).bind("load", function() {
    
    // open external links in a new window
    $("a[href^='http']").click(function(){
      window.open(this.href);
      return false;
    }).attr("title", "Opens in a new window");
    
    if ( parent.length ) {
      OpeningDayHoliday.resizeGenerator();
    }
  
  });
  
})(jQuery);
