(function() {

  /* 
  
  Coffeescript Data Class
  
  www.owainlewis.com
  
  SimpleParser - Base class to get and parse data
  PubMapper - Extends SimpleParser and handles get HTML requests for pub data i.e getAllPubHtml
  AttractionMapper - Extends SimpleParser and handles get HTML requests for attraction data i.e getAllAttractionsHtml
  */

  var AttractionParser, Enumerable, HasProperty, ParseError, PubParser, SimpleParser;
  var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };

  Enumerable = Object.prototype.propertyIsEnumerable;

  HasProperty = Object.prototype.hasOwnProperty;

  ParseError = function(message) {
    this.name = "ParseError";
    return this.message = "Item could not be found. Is this an invalid ID?";
  };

  ParseError.prototype = Error.prototype;

  SimpleParser = (function() {

    function SimpleParser() {
      this.pubs = PUBS;
      this.attractions = ATTRACTIONS;
      this.collection = [];
    }

    SimpleParser.prototype.get = function(item, attr) {
      return this.collection.select(function(item) {
        return item.get(attr);
      });
    };

    return SimpleParser;

  })();

  PubParser = (function() {

    __extends(PubParser, SimpleParser);

    /* 
    
    Call superclass constructor
    */

    function PubParser() {
      PubParser.__super__.constructor.apply(this, arguments);
    }

    /*
        Return a pub by ID eg parser.getPubById(1)
        @returns {Object} The pub object
    */

    PubParser.prototype.getPubById = function(id) {
      var result;
      result = null;
      if (typeof arguments[0] === "number") {
        id = Number.prototype.toString.call(id);
      }
      _.each(this.pubs, function(pub) {
        if (pub['id'] === id) return result = pub;
      });
      return result;
    };

    /*
        Returns an array of pubs
        @returns {Array} pub Object
    */

    PubParser.prototype.getAllPubs = function() {
      return this.pubs;
    };

    /*
        Returns the value of a pub attribute such as getPubAttribute(1, "name")
        @params id {Number} attr {String}
    */

    PubParser.prototype.getPubAttribute = function(id, attr) {
      var pub;
      pub = this.getPubById(id);
      if (pub !== null) {
        return pub[attr];
      } else {
        throw ParseError;
      }
    };

    /*
        A super convenience method for getting formatted HTML from a pub ID
    */

    PubParser.prototype.getPubHTML = function(id) {
      var pub, temp;
      temp = "<h2><%= name %></h2>\n<img id=\"pub_image\" src=\"assets/pubs/<%= img %>\" />\n<section class='details'>\n  <div class='left'>\n  <h3>Address</h3>\n  <p><%= address %><br/><%= postcode %></p>\n  <h3>Telephone</h3>\n  <p class=\"tel\"><%= phone %></p>\n  </div>\n  <div class='right'>\n    <a href=\"#/directions/<%=postcode%>\" class='detail_button' id='take'></a>\n    <a href=\"#/single/<%=id%>\" class='detail_button' id='show'></a>\n  </div>\n</section>\n<section class='facilities'>\n  <ul class='f_left'>\n    <li class='<%= family %>' id='family'></li>\n    <li class='<%= internet %>' id='internet'></li>\n    <li class='<%= sport %>' id='sport'></li>\n  </ul>\n  <ul class='f_right'>\n    <li class='<%= food %>' id='food'></li>\n    <li class='<%= garden %>' id='garden'></li>\n    <li class='<%= access %>' id='access'></li>\n  </ul>\n</section>\n<section class='social'>\n  <h2>Share your pint of pride...</h2>\n  <div class='f_left'>\n    <h3>With friends...</h3>\n    <a href=\"<%= link %>\" id='share_fb' target='_blank'></a>\n  </div>\n  <div class='f_right'>\n    <h3>With followers...</h3>\n    <a href=\"http://twitter.com/?status=I'm enjoying a pint of London Pride at the <%= name_escaped %>. %23Wheninlondon\" target=\"_blank\" id='share_tw'></a>\n  </div>\n</section>\n<section class='details'>\n<h3>Description</h3>\n<p><%= description %></p>\n</section>\n<section class='details opening_times'>\n  <h3>Opening Times</h3>\n    Mon: <%= hours[0] %> | Tue: <%= hours[1] %><br/>\n    Wed: <%= hours[2] %> | Thurs: <%= hours[3] %><br/>\n    Fri: <%= hours[4] %> | Sat: <%= hours[5] %><br/>\n    Sun: <%= hours[6] %>\n</section>";
      pub = this.getPubById(id);
      return _.template(temp, {
        id: pub['id'],
        img: pub['image'],
        phone: pub['phone'],
        address: pub['address'],
        description: pub['description'],
        name: pub['name'],
        family: pub['family'],
        internet: pub['wifi'],
        sport: pub['live_sports'],
        food: pub['food'],
        garden: pub['beer_garden'],
        access: pub['accessible'],
        name_escaped: escape(pub['name']),
        postcode: pub['postcode'],
        hours: pub['opening_hours'],
        link: "https://www.facebook.com/dialog/feed?app_id=104273736349096&link=https://developers.facebook.com/docs/reference/dialogs/&  picture=http://www.wheninlondon.com/assets/images/fullers.png&name=Drink%20London%20Pride%20at%20" + pub['name'] + "&caption=I%20am%20enjoying%20a%20pint%20of%20London%20Pride%20at%20" + pub['name'] + "&description=Join%20me%20for%20a%20drink%20at%20" + pub['address'] + ", " + pub['postcode'] + "&message=When%20in%20London&redirect_uri=http://www.wheninlondon.com"
      });
    };

    PubParser.prototype.allPubsAsHtml = function() {};

    return PubParser;

  })();

  AttractionParser = (function() {

    __extends(AttractionParser, SimpleParser);

    function AttractionParser() {
      AttractionParser.__super__.constructor.apply(this, arguments);
    }

    AttractionParser.prototype.getAllAttractions = function() {
      return Array.prototype.slice.call(this.attractions);
    };

    AttractionParser.prototype.getAttractionsHTML = function() {
      var attractions, lambda;
      attractions = this.getAllAttractions();
      lambda = function() {
        var res;
        res = _.map(attractions, function(a) {
          return "<li><a href='#/postcode/" + a.postcode + "'>" + a.name + "</a></li>";
        });
        return res.join('');
      };
      return lambda.call(this);
    };

    return AttractionParser;

  })();

  window.parser = new PubParser();

  window.parser2 = new AttractionParser();

}).call(this);

