var has = function(selector, action) {
  Event.observe(document, "dom:loaded", function() {
    $$(selector).each(action);
  });
}

has("#key input", function(input) {
  input.observe("focus", function() {
    Tweetaways.focusKeyForm();
  });
});

has("#contest input", function(input) {
  input.observe("focus", function() {
    Tweetaways.focusContestForm();
  });
});

has("#free input", function(input) {
  input.observe("focus", function() {
    Tweetaways.focusFreeForm();
  });
});

Tweetaways = {
  updateKeyResult: function(result) {
    $("key_result").update(result);
    $("key_result").show();
  },
  
  updateContestResult: function(result) {
    $("contest_result").update(result);
    $("contest_result").show();
  },
  
  updateFreeResult: function(result) {
    $("free_result").update(result);
    $("free_result").show();
  },
  
  updateDrawingResult: function(result) {
    $("drawing_result").update(result);
    $("drawing_result").show();
  },
  
  updateRedrawingResult: function(result) {
    $("redrawing_result").update(result);
    $("redrawing_result").show();
  },
  
  hideKeyForm: function() {
    $("key").down("form").hide();
  },
  
  hideContestForm: function() {
    $("contest").down("form").hide();
  },
  
  hideFreeForm: function() {
    $("free").down("form").hide();
  },
  
  hideDrawingForm: function() {
    $("drawing").down("form").hide();
  },
  
  focusKeyForm: function() {
    $("key").addClassName("focus");
    $("contest").removeClassName("focus");
    $("free").removeClassName("focus");
  },
  
  focusContestForm: function() {
    $("key").removeClassName("focus");
    $("contest").addClassName("focus");
    $("free").removeClassName("focus");
  },
  
  focusFreeForm: function() {
    $("key").removeClassName("focus");
    $("contest").removeClassName("focus");
    $("free").addClassName("focus");
  },
  
  prefillContestUsername: function(username) {
    $("contest_username").value = username;
    $("free_username").value = username;
  },
  
  resetContestForm: function() {
    this.focusContestForm();
    $("contest_phrase").value = "";
    $("contest_result").update("");
    $("contest_result").hide();
    $("contest").down("form").show();
  },
  
  resetFreeForm: function() {
    this.focusFreeForm();
    $("free_phrase").value = "";
    $("free_result").update("");
    $("free_result").hide();
    $("free").down("form").show();
  },
  
  resendKeyWithContestForm: function() {
    new Ajax.Request("/keys", {
      asynchronous: true,
      evalScripts: true,
      parameters: {
        "key[username]": $F("contest_username"),
        "with_contest_form": 1
      }
    })
  },
  
  resendKeyWithFreeForm: function() {
    new Ajax.Request("/keys", {
      asynchronous: true,
      evalScripts: true,
      parameters: {
        "key[username]": $F("free_username"),
        "with_free_form": 1
      }
    })
  },
  
  resendKeyWithDrawingForm: function() {
    new Ajax.Request("/keys", {
      asynchronous: true,
      evalScripts: true,
      parameters: {
        "key[username]": $F("drawing_username"),
        "with_drawing_form": 1
      }
    })
  },
  
  resendKeyWithRedrawingForm: function() {
    new Ajax.Request("/keys", {
      asynchronous: true,
      evalScripts: true,
      parameters: {
        "key[username]": $F("redrawing_username"),
        "with_redrawing_form": 1
      }
    })
  },
  
  resendKeyWithEditPhraseForm: function() {
    new Ajax.Request("/keys", {
      asynchronous: true,
      evalScripts: true,
      parameters: {
        "key[username]": $F("edit_phrase_username"),
        "with_edit_phrase_form": 1
      }
    })
  },
  
  submitForm: function(html) {
    var form = new Element("div").insert(html).down();
    Element.insert(document.body, form);
    form.submit();
    form.remove();
  },
  
  previewFreeEntries: function() {
    var username = $F("free_username");
    
    if (username.blank()) {
      alert("You need to enter your Twitter username.");
      return;
    }
    
    var phrase = $F("free_phrase");
    
    if (phrase.blank()) {
      alert("You need to enter the contest's qualifying phrase.");
      return
    }
    
    window.open("http://search.twitter.com/search?ref=" + encodeURIComponent(username) + "&q=" + encodeURIComponent(phrase));
  },
  
  keyFormIsValid: function() {
    if ($F("key_username").blank()) {
      alert("You need to enter your Twitter username.");
      return false;
    }
    
    return true;
  },
  
  contestFormIsValid: function() {
    if ($F("contest_username").blank()) {
      alert("You need to enter your Twitter username.");
      return false;
    }
    
    if ($F("contest_key_id").blank()) {
      alert("You need to enter your Tweetaways key.");
      return false;
    }
    
    if ($F("contest_phrase").blank()) {
      alert("You need to enter the contest's qualifying phrase.");
      return false;
    }
    
    return true;
  },
  
  freeFormIsValid: function() {
    if ($F("free_username").blank()) {
      alert("You need to enter your Twitter username.");
      return false;
    }
    
    if ($F("free_key_id").blank()) {
      alert("You need to enter your Tweetaways key.");
      return false;
    }
    
    if ($F("free_phrase").blank()) {
      alert("You need to enter the contest's qualifying phrase.");
      return false;
    }
    
    return true;
  },
  
  drawingFormIsValid: function() {
    if ($("drawing").hasClassName("disabled")) {
      return false;
    }
      
    if ($F("drawing_key_id").blank()) {
      alert("You need to enter your Tweetaways key.");
      return false;
    }
    
    return true;
  },
  
  redrawingFormIsValid: function() {
    if ($("redrawing").hasClassName("disabled")) {
      return false;
    }
      
    if ($F("redrawing_key_id").blank()) {
      alert("You need to enter your Tweetaways key.");
      return false;
    }
    
    return true;
  },
  
  showEditPhraseForm: function() {
    $("phrase").down("form").show();
    $("phrase").down(".current").hide();
  },
  
  cancelEditPhraseForm: function() {
    $("phrase").down("form").hide();
    $("phrase").down(".current").show();
    $("edit_phrase_result").hide();
  },
  
  updateAndShowCurrentPhrase: function(phrase) {
    $("contest_phrase").value = phrase;
    $("edit_phrase_key_id").value = "";
    $("phrase").down(".current strong").innerHTML = phrase.escapeHTML();
    
    $("phrase").down("form").hide();
    $("edit_phrase_result").hide();
    $("phrase").down(".current").show();
  },
  
  updateEditPhraseResult: function(result) {
    $("edit_phrase_result").update(result);
    $("edit_phrase_result").show();
  },
  
  editPhraseFormIsValid: function() {
    if ($F("contest_phrase").blank()) {
      alert("You need to enter the contest's qualifying phrase.");
      return false;
    }
    
    if ($F("edit_phrase_key_id").blank()) {
      alert("You need to enter your Tweetaways key.");
      return false;
    }
    
    return true;
  },
  
  disableDrawingSubmit: function() {
    $("drawing").addClassName("disabled");
    $("drawing_choose_winner").value = "Choosing a winner...";
  },
  
  enableDrawingSubmit: function() {
    $("drawing").removeClassName("disabled");
    $("drawing_choose_winner").value = "Choose a winner!";
  },
  
  disableRedrawingSubmit: function() {
    $("redrawing").addClassName("disabled");
    $("redrawing_choose_winner").value = "Choosing another winner...";
  },
  
  enableRedrawingSubmit: function() {
    $("redrawing").removeClassName("disabled");
    $("redrawing_choose_winner").value = "Choose another winner!";
  }
};
