$(function() {
  
  $('a.vote_link').click(function() {
    $('#indicator').show();
    $.ajax({
      type: "POST",
      url: $(this).attr('ref'),
      dataType: "json",
      success: function(json) {
        if (json['success'] == true) {
          $('#indicator').hide();
          $(this).attr('disabled', 'false');
          $('div.vote_count').text(json['votes']);
          $('a.vote_link').qtip('show');
        } else {
          $('#indicator').hide();
          alert(json['message']);
        }
      }
    });
  });
  
  $('a.vote_link').qtip({
    content: {
      prerender: true,
      text: "<h3>Thank you for voting in Intel's Access Your Core Contest!</h3>" +
        "<p>Your vote qualifies you to enter our weekly voter's sweepstakes for a chance to win a laptop powered by an Intel&reg; Corei5&trade; processor. Simply enter your email address below, and we'll only contact you if you win.</p>" +
        '<form id="enter_form">' +
        '<input class="textbox" type="text" name="email" /><br />' +
        '<input class="checkbox" type="checkbox" name="terms" /><label class="checkbox" for="terms">I\'ve read the <a href="/terms" target="_blank">Terms and Conditions</a> and <a href="http://www.intel.com/sites/sitewide/en_US/privacy/privacy.htm?iid=ftr+privacy" target="_blank">Privacy Statement</a>.</label>' +
        '<div margin-top: 5px><button class="small" type="submit" name="submit" value="Enter">Enter</button> or ' +
        '<a href="javascript:void(0)" class="close_enter">close</a></div>' +
        '</form>'
    },
    position: {
      target: $(document.body),
      corner: 'center'
    },
    style: {
      background: '#d4f0fe',
      padding: '20px',
      width: 440
    },
    show: {
      when: {
        target: $('#bogus_target'),
        event: 'click'
      }
    },
    hide: {
      when: {
        target: $('#bogus_target'),
        event: 'click'
      }
    },
    api: {
      onShow: function() {
        $('input:first').focus();
      }
    }
  });
  
  $('#enter_form').submit(function() {
    if ($('input[name=terms]', this).attr('checked') == false) {
      alert('Please accept the Terms and Conditions and Privacy Statement.');
      return false;
    }
    var submitButton = $('button[type=submit]', this);
    submitButton.attr('disabled', 'true');
    $('#indicator').show();
    $.ajax({
      type: "POST",
      url: "/signups",
      data: {email : $('input[name=email]', this).val()},
      dataType: "json",
      success: function(json) {
        if (json['success'] == true) {
          $('#indicator').hide();
          $('a.vote_link').qtip('api').updateContent('Thanks for entering!<br/><a href="javascript:void(0)" class="close_enter">close</a>');
        } else {
          $('#indicator').hide();
          submitButton.attr('disabled', '');
          alert('Please enter a valid email address.');
        }
      }
    });
    return false;
  });
  
  $('a.close_enter').live('click', function() {
    $('a.vote_link').qtip('hide');
  });
    
});