
// ### Part of the [Rosy Framework](http://github.com/ff0000/rosy)
/* item-detail-overlay.js */

// ## Local Namespace

// Home Page class

// Site-specifc namespace
var RuneScape = RuneScape || {};

// Page namespace
RuneScape.Module = RuneScape.Module || {};

// Home specific instance
RuneScape.Module.ItemDetailOverlay = (function () {

 // Extends RuneScape.Page
 return RuneScape.Module.extend({

  vars : {
   api : "http://services.runescape.com/m=itemdb_rs/a=254/api/catalogue/",
   target : null
  },

  // Home  page level functionality
  init : function (vars) {
   this.setupView();
  },

  setupView : function (callback) {
   var request = this.requestAPI(
    "detail",
    {item : this.vars.item},
    this.buildOverlay
   );
   if(callback){
    request
     .done(function(){ callback(true) })
     //.fail(function(){ callback(false) })
    ;
   }
  },

  setupEvents : function () {
   this.vars.shell.delegate(".catalogue-close", "click", $.proxy(this.onCloseClick, this));
  },

  requestAPI : function (type, params, callback) {
   return $.getJSON(this.vars.api + type + ".json", $.param(params)).then($.proxy(callback, this));
  },

  buildOverlay : function (json) {
   var detail = $.tmpl("detail", json.item),
    graph = detail.find(".graph"), module;

   this.vars.shell = this.vars.shell || $('<div id="catalogue-detail"></div>');

   this.vars.shell.appendTo(this.vars.target);
   this.vars.shell.html(detail);

   this.setupEvents();

   module = new RuneScape.Module.ItemDetailGraph({
    graph : graph
   });
  },

  onCloseClick : function (e) {
   var el = $(e.currentTarget).closest(".detail").hide();

   window.setTimeout(function () {
    el.remove();
   }, 0);
  }

 });
}.call(RuneScape));

