view open-web-challenges.js @ 16:906296a2d436

Added a second challenge.
author Atul Varma <varmaa@toolness.com>
date Tue, 31 Mar 2009 07:13:03 -0700
parents 9a759b8be515
children 3e4f23f2c911
line wrap: on
line source

var OWC = {
  _makeHintButton: function makeHintButton(hints) {
    var hintButtonContainer = $('<div class="hint-button"></div>');
    var hintButton = $('<span></span>');
    hintButton.text($('#msg-show-first-hint').text());
    hintButton.click(
      function() {
        hints.find('p:hidden:first').fadeIn();
        if (hints.find('p:hidden').length)
          $(this).text($('#msg-show-another-hint').text());
        else
          $(this).text($('#msg-no-more-hints').text());
      });
    hintButtonContainer.append(hintButton);
    return hintButtonContainer;
  },

  load: function load(id) {
    var challenge = $('#challenge-' + id);
    var content = challenge.find('.content').clone();
    var current = $('#current-challenge');
    current.empty();

    var headerText = $('#msg-header').text().replace('{id}', id);
    current.append($('<h1></h1>').text(headerText));
    document.title = headerText;

    current.append(content);

    var hints = $('<div class="hints"></div>');
    hints.append(challenge.find('.hints').clone());
    current.append(this._makeHintButton(hints));
    current.append(hints);

    var validatorCode = challenge.find('.validator').text();

    var validate = eval('(' + validatorCode + ')');

    function maybeDisplaySuccess() {
      if (validate(current)) {
        var success = $('<div id="success"></div>');
        success.text($('#msg-success').text());
        success.wrapInner('<h1></h1>');
        success.hide();
        current.children().addClass('completed');
        current.append(success);
        success.fadeIn('slow');
        window.clearInterval(intervalId);
      }
    }
    var intervalId = window.setInterval(maybeDisplaySuccess, 100);

    current.fadeIn();
  }
};

$(window).ready(
  function() {
    function showChallenge() {
      OWC.load(2);
    }

    if ($('#_firebugConsole').length)
      showChallenge();
    else {
      jQuery.getScript(
        'firebug-lite.js',
        function() {
          if (window.firebug.version) {
            firebug.init();
            firebug.d.console.cmd.log(
              ('Because Firebug wasn\'t detected, you are ' +
               'now using Firebug lite. Unfortunately, this means that ' +
               'some hints won\'t necessarily work for you, and you ' +
               'may need to use some clever tricks to get through ' +
               'some of the challenges.')
            );
          }
          showChallenge();
        });
    }
  });