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

// ## Local Namespace

// Home Page class

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

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

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

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

  vars : {
   currentLetter : "a",
   pageInterval : 12,
   api : "http://services.runescape.com/m=itemdb_rs/a=254/api/catalogue/"
  },

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

  setDOMReferences : function () {
   var parent = this.vars.parent,
    search = parent.find(".catalogue-search");

   $.extend(this.vars, {
    search : search,
    selects : search.find("select"),
    category : search.find(".category-cats"),
    catalogue : parent.find(".catalogue")
   });
  },

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

  setupCatalogue : function () {
   this.vars.category.bind("change", $.proxy(this.onCategoryChange, this));

   this.vars.catalogue.delegate("[data-alpha]", "click", $.proxy(this.onAlphaClick, this));
   this.vars.catalogue.delegate(".pagination a, .pagination div", "click", $.proxy(this.onPageClick, this));
   this.vars.catalogue.delegate(".item-content h5 a", "click", $.proxy(this.onItemClick, this));
  },

  onCategoryChange : function (e) {
   this.vars.currentCategory = this.vars.category.val();
   this.vars.catalogue.addClass('loading');
   this.requestAPI("category", {
    category : this.vars.currentCategory
   }, function (json) {
    this.buildAlphaView(json);
    this.vars.catalogue.removeClass('loading');
   });
  },

  onAlphaClick : function (e) {
   var $el = $(e.currentTarget);

   if (!$el.hasClass("disabled")) {
    this.setActiveAlpha($el);
   }

   e.preventDefault();
  },

  onPageClick : function (e) {
   var el = $(e.currentTarget),
    $pagination = el.closest('.pagination'),
    $spots = $pagination.find('ul').find('a'),
    page = $spots.index($spots.filter('.active'))+1,
    newPage = page,
    former;

   if (el.hasClass("Button")) {
    if (!el.hasClass("disabled")) {
     if (el.hasClass("prev")) {
      newPage = Math.max(newPage - 1, 1);
     }
     else if (el.hasClass("next")) {
      newPage = Math.min(newPage + 1, $spots.length);
     }
    }
   }
   else {
    newPage = el.data('page');
   }

   if(newPage != page){

    var $spots = $pagination.find("ul").find('a');
    $spots.removeClass("active").eq(newPage-1).addClass('active');

    if (newPage == 1) {
     $pagination.find(".prev.enabled").hide();
     $pagination.find(".prev.disabled").show();
    }
    else {
     $pagination.find(".prev.enabled").show();
     $pagination.find(".prev.disabled").hide();
    }

    if (newPage >= $spots.length){
     $pagination.find(".next.enabled").hide();
     $pagination.find(".next.disabled").show();
    }
    else{
     $pagination.find(".next.enabled").show();
     $pagination.find(".next.disabled").hide();
    }

    this.setActivePage(newPage);

   }

   e.preventDefault();
  },

  buildAlphaView : function (json) {
   var i, j, letter, count, alpha;

   this.vars.shell = $.tmpl("shell");
   this.vars.alphas = this.vars.shell.find("[data-alpha]");
   this.vars.activeAlpha = this.vars.shell.find(".catalogue-row a[data-alpha='" + this.vars.currentLetter + "']");
   this.vars.catalogue.html(this.vars.shell);

   for (i = 0, j = json.alpha.length; i < j; i++) {
    letter = json.alpha[i].letter;
    count = json.alpha[i].items;

    if (count === 0) {
     alpha = this.vars.alphas.filter("[data-alpha='" + letter + "']");
     alpha.addClass("disabled");
    }
   }

   this.setActiveAlpha(this.vars.activeAlpha);
  },

  setActiveAlpha : function (alpha) {
   this.vars.activeAlpha = this.vars.activeAlpha || alpha;

   // Remove all active class names
   this.vars.catalogue.find(".active").removeClass("active");

   this.vars.activeAlpha.siblings().remove();

   this.vars.activeAlpha = alpha;
   this.vars.currentLetter = this.vars.activeAlpha.data("alpha");

   // Add class name flags
   alpha.parent().addClass("active");
   alpha.closest(".catalogue-row").addClass("active");

   this.requestAPI("items", {
    category : this.vars.currentCategory,
    alpha : this.vars.currentLetter,
    page : 0
   }, this.buildItemView);
  },

  buildItemView : function (json) {
   var pages = Math.ceil(json.total / this.vars.pageInterval),
    parent, mask, i = 0, j, nxt, prv;

   this.vars.itemShell = $.tmpl("item-shell");
   this.vars.alphaItems = this.vars.itemShell.find(".alpha-items");
   this.vars.alphaPager = this.vars.itemShell.find(".pagination ul");

   this.vars.activeAlpha.parent().append(this.vars.itemShell);

   while (i < pages) {
    this.vars.alphaPager.append('<li><a' + ((i === 0) ? ' class="active"' : '') + ' data-page="' + (i + 1) + '"></a></li>');
    i++;
   }

   this.updateItems(json);

   parent = this.vars.activeAlpha.parent();
   mask = parent.find(".mask");

   mask.css("left", parent.position().left);

   this.vars.itemShell.find(".prev.enabled").hide();
   if(pages <= 1){
    this.vars.itemShell.find(".next.enabled").hide();
   }
   else{
    this.vars.itemShell.find(".next.disabled").hide();
   }

  },

  setActivePage : function (page) {
   this.requestAPI("items", {
    category : this.vars.currentCategory,
    alpha : this.vars.currentLetter,
    page : window.parseFloat(page)
   }, this.updateItems);
  },

  updateItems : function (json) {
   var items = $.tmpl("item", json.items);
   this.vars.alphaItems.html(items);
  },

  onItemClick : function (e) {
   var el = $(e.currentTarget);

   this.setActiveItem(el);
   e.preventDefault();
  },

  setActiveItem : function (item) {
   var overlay = new RuneScape.Module.ItemDetailOverlay({
    item : item.data("itemId"),
    target : this.vars.itemShell
   });
  }
 });
}.call(RuneScape));

