// decode the on-network array entity format into an object format closer to that used before
// makes much more sense as an object, means that existing code didn't need to change, and it's what the
// stock intel site does internally too (the array format is only on the network)


// anonymous wrapper function
(function(){
  window.decodeArray = function(){};


  function parseMod(arr) {
    if(arr == null) { return null; }
    return {
      owner: arr[0],
      name: arr[1],
      rarity: arr[2],
      stats: arr[3],
    };
  }
  function parseResonator(arr) {
    if(arr == null) { return null; }
    return {
      owner: arr[0],
      level: arr[1],
      energy: arr[2],
    };
  }
  function parseArtifact(arr) {
    if (arr == null) { return null; }
    // empty artifact data is pointless - ignore it
    if (arr.length == 3 && arr[0] == "" && arr[1] == "" && arr[2].length == 0) { return null; }
    return {
      type: arr[0],
      displayName: arr[1],
      fragments: arr[2],
    };
  }


  var summaryArrayLength = 14;

//there's also a 'placeholder' portal - generated from the data in links/fields. only has team/lat/lng

  function corePortalData(a) {
    return {
      // a[0] == type (always 'p')
      team:          a[1],
      latE6:         a[2],
      lngE6:         a[3]
    }
  };

  function summaryPortalData(a) {
    return {
      level:         a[4],
      health:        a[5],
      resCount:      a[6],
      image:         a[7],
      title:         a[8],
      ornaments:     a[9],
      mission:       a[10],
      mission50plus: a[11],
      unknown12:     a[12],
      timestamp:     a[13]
    };
  };

  window.decodeArray.portalSummary = function(a) {
    if (!a) return undefined;

    if (a[0] != 'p') throw 'Error: decodeArray.portalSUmmary - not a portal';

    if (a.length == 4) {
      return corePortalData(a);
    }

    if (a.length != summaryArrayLength) {
      console.warn('Portal summary length changed - portal details likely broken!');
      debugger;
    }

    return $.extend(corePortalData(a), summaryPortalData(a));
  }

  window.decodeArray.portalDetail = function(a) {
    if (!a) return undefined;

    if (a[0] != 'p') throw 'Error: decodeArray.portalDetail - not a portal';


    //TODO look at the array values, make a better guess as to which index the mods start at, rather than using the hard-coded summaryArrayLength constant


    // the portal details array is just an extension of the portal summary array
    // to allow for niantic adding new items into the array before the extended details start,
    // use the length of the summary array
    return $.extend(corePortalData(a), summaryPortalData(a),{
      mods:      a[summaryArrayLength+0].map(parseMod),
      resonators:a[summaryArrayLength+1].map(parseResonator),
      owner:     a[summaryArrayLength+2],
      artifact:  parseArtifact(a[summaryArrayLength+3]),
    });
    
  }


})();

@@ -1008,40 +1008,8 @@
 %7D%0A%0A%0A
-  var summaryArrayLength = 14;%0A%0A
 //th
@@ -1108,16 +1108,50 @@
 at/lng%0A%0A
+  var CORE_PORTA_DATA_LENGTH = 4;%0A
   functi
@@ -1313,16 +1313,55 @@
 %7D%0A  %7D;%0A%0A
+  var SUMMARY_PORTAL_DATA_LENGTH = 14;%0A
   functi
@@ -1685,16 +1685,84 @@
 ;%0A  %7D;%0A%0A
+  var DETAILED_PORTAL_DATA_LENGTH = SUMMARY_PORTAL_DATA_LENGTH+4;%0A%0A%0A
   window
@@ -1934,17 +1934,38 @@
 ngth == 
-4
+CORE_PORTA_DATA_LENGTH
 ) %7B%0A    
@@ -2007,42 +2007,251 @@
     
-if (a.length != summaryArrayLength
+// NOTE: allow for either summary or detailed portal data to be passed in here, as details are sometimes%0A    // passed into code only expecting summaries%0A    if (a.length != SUMMARY_PORTAL_DATA_LENGTH && a.length != DETAILED_PORTAL_DATA_LENGTH
 ) %7B%0A
@@ -2584,16 +2584,171 @@
 rtal';%0A%0A
+    if (a.length != DETAILED_PORTAL_DATA_LENGTH) %7B%0A      console.warn('Portal detail length changed - portal details may be wrong');%0A      debugger;%0A    %7D%0A
 %0A    //T
@@ -2867,34 +2867,42 @@
 d-coded 
-summaryArrayLength
+SUMMARY_PORTAL_DATA_LENGTH
  constan
@@ -3205,34 +3205,42 @@
       a%5B
-summaryArrayLength
+SUMMARY_PORTAL_DATA_LENGTH
 +0%5D.map(
@@ -3269,34 +3269,42 @@
 ators:a%5B
-summaryArrayLength
+SUMMARY_PORTAL_DATA_LENGTH
 +1%5D.map(
@@ -3339,34 +3339,42 @@
 :     a%5B
-summaryArrayLength
+SUMMARY_PORTAL_DATA_LENGTH
 +2%5D,%0A   
@@ -3407,26 +3407,34 @@
 t(a%5B
-summaryArrayLength
+SUMMARY_PORTAL_DATA_LENGTH
 +3%5D)

fix warning/debug break on details passed to summary decode