// Wiki fetch system - fetches wiki pages once per session and caches by URL.
var wikiPageCache = {};
var wikiAnchorMap = {
"CJ-5" : { url: "https://jeep-cj.com/community/wiki/civilian/", anchor: "-cj5-1972-1975" },
"CJ-6" : { url: "https://jeep-cj.com/community/wiki/civilian/", anchor: "-the-cj-6-a-longer-cj-5" },
"CJ-7" : { url: "https://jeep-cj.com/community/wiki/civilian/", anchor: "-the-last-cj-the-cj-7" },
"CJ-8" : { url: "https://jeep-cj.com/community/wiki/civilian/", anchor: "-cj-8-scrambler" },
"258i6" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-258-straight-six" },
"258i6 lc" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-258-straight-six" },
"232i6" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-232-straight-six" },
"232i6 lc" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-232-straight-six" },
"304v8" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-304-v8" },
"258i6 - 1bbl" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-258-straight-six" },
"258i6 - 2bbl" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-258-straight-six" },
"232i6 - 1bbl" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-232-straight-six" },
"304v8 - 2bbl" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-304-v8" },
"360v8 - 2bbl" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-360-v8" },
"360v8 - 4bbl" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-360-v8" },
"360v8 2bbl" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-360-v8" },
"360v8 4bbl" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-360-v8" },
"151i4" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-gm-151-cid" },
"150i4-g" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-150-cid" },
"150i4-u" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-amc-150-cid" },
"C240 Diesel" : { url: "https://jeep-cj.com/community/wiki/engines/", anchor: "-isuzu-c240-diesel" },
"T-15 3 Speed" : { url: "https://jeep-cj.com/community/wiki/transmissions/", anchor: "-t-15-three-speed" },
"T-18 4 Speed" : { url: "https://jeep-cj.com/community/wiki/transmissions/", anchor: "-t-18-four-speed" },
"Turbo 400" : { url: "https://jeep-cj.com/community/wiki/transmissions/", anchor: "-th400-three-speed-automatic" },
"T-150 3 Speed" : { url: "https://jeep-cj.com/community/wiki/transmissions/", anchor: "-t-150-three-speed" },
"Auto Column Shift": { url: "https://jeep-cj.com/community/wiki/transmissions/", anchor: "-automatic-cj-transmissions" },
"Auto Floor Shift" : { url: "https://jeep-cj.com/community/wiki/transmissions/", anchor: "-automatic-cj-transmissions" },
"T4 4 Speed" : { url: "https://jeep-cj.com/community/wiki/transmissions/", anchor: "-t-4-four-speed" },
"4 Speed" : { url: "https://jeep-cj.com/community/wiki/transmissions/", anchor: "-weak-transmissions-of-the-1980-039-s" },
"5 Speed" : { url: "https://jeep-cj.com/community/wiki/transmissions/", anchor: "-t-5-five-speed" },
"MPV LHD": null, "LHD Export": null, "RHD Export": null,
"gvw_3750": null, "gvw_4150": null, "gvw_4500": null, "gvw_4750": null
};
// fnFetchWikiPage - returns cached HTML or fetches from wiki URL.
function fnFetchWikiPage(url) {
if (wikiPageCache[url]) { return Promise.resolve(wikiPageCache[url]); }
return fetch(url)
.then(function(r){ if(!r.ok){throw new Error("HTTP "+r.status);} return r.text(); })
.then(function(html){ wikiPageCache[url]=html; return html; });
}
// fnParseSection - finds section heading by anchor id and returns first paragraph text.
function fnParseSection(html, anchor) {
var tmp=document.createElement("div"); tmp.innerHTML=html;
var heading=tmp.querySelector("[id='"+anchor+"']");
if(!heading){return null;}
var node=heading.nextElementSibling, safety=0;
while(node&&safety<20){
safety++;
if(/^H[1-6]$/.test(node.tagName)){break;}
if(node.tagName==="P"){var t=node.textContent.trim(); if(t.length>10){return t;}}
node=node.nextElementSibling;
}
return null;
}
// fnGetDescAsync - async description lookup; fetches wiki or falls back to hardcoded string.
function fnGetDescAsync(key) {
var entry=wikiAnchorMap[key];
if(!entry){return Promise.resolve(wikiDescFallback[key]||"(No description available.)");}
return fnFetchWikiPage(entry.url)
.then(function(html){
var t=fnParseSection(html,entry.anchor);
return t||wikiDescFallback[key]||"(Description not found - see wiki link in decoder table.)";
})