/*
/*  triumph.js
*/

var COTMTriumph = new Object({
  observeSubmitButton: function () {
    $("submit").observeClick(function () {
      new Ajax.Request("/submit", {
        method: "get",
        onSuccess: function (response) {
          COTMTriumph.showOverlay(response.responseText);
        }
      })
    });
  },
  
  observeShareButtons: function () {
    $$(".share .facebook a").each(function (a) {
      a.observeClick(function () {
        COTMTriumph.openPopup(a.href, { width: 800, height: 500 });
      });
    });
    
    $$(".share .twitter a").each(function (a) {
      a.observeClick(function () {
        COTMTriumph.openPopup(a.href, { width: 960, height: 640 });
      });
    });
  },
  
  showOverlay: function (html) {
    if (!$("blackout"))
      COTMTriumph.insertBlackout();
    if (!$("overlay"))
      COTMTriumph.insertOverlay();
    
    $("overlay").update(html);
    
    var form = $("overlay").down("form");
    if (form.classNames().include("story"))
      COTMTriumph.observeStoryForm(form);
    
    new Effect.Appear("blackout", { duration: 0.25 });
    new Effect.Appear("overlay",  { duration: 0.25 });
  },
  
  hideOverlay: function () {
    new Effect.Fade("blackout", { duration: 0.25 });
    new Effect.Fade("overlay",  { duration: 0.25 });
  },
  
  observeStoryForm: function (form) {
    $("story_body").observe("change",  COTMTriumph.updateCharCount);
    $("story_body").observe("keyup",   COTMTriumph.updateCharCount);
    $("story_body").observe("keydown", COTMTriumph.updateCharCount);
    
    $(form).down(".submit.button").observeClick(function () {
      COTMTriumph.submitStory(form);
    });
    
    $(form).down(".cancel.button").observeClick(function () {
      if ($("story_body").value != "") {
        if (confirm("Are you sure?"))
          COTMTriumph.hideOverlay();
      } else {
        COTMTriumph.hideOverlay();
      }
    });
  },
  
  submitStory: function (form) {
    new Ajax.Request("/submit", {
      method:     "post",
      parameters: $(form).serialize(),
      onSuccess: function (response) {
        COTMTriumph.hideOverlay();
        $("header").down(".main").update(response.responseText);
      }
    })
  },
  
  updateCharCount: function () {
    var text  = $("story_body").value.strip();
    var chars = text.strip().length;
    $("chars").update(chars);
    
    var button = $("story_body").up("form").down(".button.submit");
    
    var maxChars = parseInt($("max-chars").textValue());
    
    if (chars > maxChars || text == "")
      button.addClassName("disabled");
    else
      button.removeClassName("disabled");
  },
  
  updateHeaderBackground: function () {
    var image = new Image();
    image.onload = function () {
      $("header").setStyle({
        backgroundImage: "url(\"/images/header.png\")"
      });
    };
    image.src = "/images/header.png";
  },
  
  insertBlackout: function () {
    $("content").insert({
      bottom: Builder.node("div", { id: "blackout" })
    });
    $("blackout").hide();
  },
  
  insertOverlay: function () {
    $("content").insert({
      bottom: Builder.node("div", { id: "overlay" })
    });
    $("overlay").hide();
  },
  
  openPopup: function (url, options) {
    var width  = parseFloat(options.width);
    var height = parseFloat(options.height);
    
    options = $H(options);
    options.update({
      top:  screen.availHeight * 0.25 - height * 0.25,
      left: screen.availWidth  * 0.50 - width  * 0.50,
      location: "yes", toolbar: "no", status: "no",
      menubar: "no", scroll: "yes", resize: "yes"
    });
    
    var features = options.map(function (pair) {
      return pair.key + "=" + pair.value;
    }).join(",");
    
    return window.open(url, "popup", features);
  }
});

Element.addMethods({
  textValue: function (el) {
    return el.innerText || el.textContext;
  },
  
  observeClick: function (el, func) {
    el = $(el);
    if (el.tagName == "A")
      el.onclick = Prototype.K.bind(el, false);
    el.observe("click", func);
  }
});

document.observe("dom:loaded", function () {
  COTMTriumph.observeSubmitButton();
  COTMTriumph.observeShareButtons();
  
  Cufon.replace($$("#main h1"));
});

