var domutil = require('./domutil');
var decode = require('./entities');
var debug = require('debug')('liftie:resort:vail');

module.exports = parse;

function getStatus(tr) {
  var node = domutil.child(tr, '2/0');
  if (node && node.attribs && node.attribs.alt) {
    return node.attribs.alt;
  }
  node = domutil.child(tr, 2);
  if (node && node.attribs && node.attribs.class) {
    return node.attribs.class.slice(0, -6);
  }
}


// common parser for Vail Resorts lift status
function parse(dom) {
  var liftStatus = domutil.collect(dom, '#Lifts tr', function(tr, index) {
    var name, status;

    if (index === 0) {
      return; // skip header
    }

    status = getStatus(tr);
    if (!status) {
      return;
    }
    name = decode(domutil.childText(tr, '0/0')).trim();
    if (/ Express Lift$/.test(name)) {
      name = name.slice(0, - ' Express Lift'.length);
    }

    return {
      name: name,
      status: status
    };
  });

  debug('vail Lift Status:', liftStatus);
  return liftStatus;
}
@@ -1,18 +1,19 @@
 var 
-domuti
+safeEva
 l = requ
@@ -21,16 +21,16 @@
 re('
-./domuti
+safe-eva
 l');
@@ -36,20 +36,19 @@
 ;%0Avar de
-code
+bug
  = requi
@@ -55,31 +55,51 @@
 re('
-./entities
+debug')('liftie:resort:vail
 ');%0A
+%0A
 var d
-ebug
+omutil
  = r
@@ -110,795 +110,814 @@
 re('
-debug')('liftie:resort:vail');%0A%0Amodule.exports = parse;%0A%0Afunction getStatus(tr) %7B%0A  var node = domutil.child(tr, '2/0');%0A  if (node && node.attribs && node.attribs.alt) %7B%0A    return node.attribs.alt
+./domutil');%0Avar select = require('../select');%0A%0Amodule.exports = parse;%0A%0A%0A%0Avar statuses = %5B'closed', 'open', 'hold'%5D;%0A%0Afunction extractLiftData(script) %7B%0A  function evalHelper(_, fn) %7B%0A    var data = %7B%7D;%0A    fn(data);%0A    return data
 ;%0A  %7D%0A
+%0A
   
-node = domutil.child(tr, 2);%0A  if (node && node.attribs && node.attribs.class) %7B%0A    return node.attribs.class.slice(0, -6);%0A  %7D%0A%7D%0A%0A%0A// common parser for Vail Resorts lift status%0Afunction parse(dom) %7B%0A  var liftStatus = domutil.collect(dom, '#Lifts tr', function(tr, index) %7B%0A    var name, status;%0A%0A    if (index === 0) %7B%0A      return; // skip header%0A    %7D%0A%0A    status = getStatus(tr);%0A    if (!status) %7B%0A      return;%0A    %7D%0A    name = decode(domutil.childText(tr, '0/0')).trim();%0A    if (/ Express Lift$/.test(name)) %7B%0A      name = name.slice(0, - ' Express Lift'.length)
+var data = safeEval(script, %7B require: evalHelper %7D);%0A  return data && data.TerrainStatusFeed && data.TerrainStatusFeed.Lifts %7C%7C %5B%5D;%0A%7D%0A%0A%0A// common parser for Vail Resorts lift status%0Afunction parse(dom) %7B%0A  var dataScript = select(dom, 'script')%0A    .map(function(script) %7B%0A      return domutil.allText(script).trim();%0A    %7D)%0A    .filter(function(script) %7B%0A      return script.startsWith('require');%0A    %7D)%5B0%5D;%0A%0A%0A  var liftStatus = extractLiftData(dataScript)%0A    .reduce(function(liftStatus, lift) %7B%0A      liftStatus%5Blift.Name.trim()%5D = statuses%5Blift.Status%5D
 ;%0A  
-  %7D%0A%0A
     
@@ -927,58 +927,28 @@
 urn 
-%7B%0A      name: name,%0A      status: s
+liftS
 tatus
+;
 %0A    %7D
-;%0A  
+, %7B
 %7D);%0A
@@ -1012,8 +1012,9 @@
 tatus;%0A%7D
+%0A

update vail resort lift parsing