function getPageName() {
  var pageName = location.pathname.split("/").slice(-1)[0];
  console.log(pageName);
  return pageName;
}

function isPageTranslation() {
  var pageName = getPageName();
  if (pageName == null) {
        console.log("Error retrieving page name");
        return false;
  } else {
        if (pageName.includes("_tr_")) {
          return true;
        } else {
          return false;
        }
  }
}

function detectPageLanguage() {
  var pageName = getPageName();
  // default to English
  if (pageName == null) {
        return "en";
  }
  var detectedLanguage = "en";
  let dLang1 = pageName.match(/_([a-z][a-z])_tr_[a-z][a-z]\.html/);
  if (dLang1 == null || dLang1[1] == null) {

    let dLang = pageName.match(/_([a-z][a-z])\.html/);
    if (dLang == null || dLang[1] == null) {
          console.log("Did not detect any language");
          detectedLanguage = "en";
    }
    else {
          detectedLanguage = dLang[1];
    }
  }
  else {
	detectedLanguage = dLang1[1];
  }

  console.log("Returning following language: " + detectedLanguage);
  return detectedLanguage;
}


function initTranscriptMenu() {

  var detectedLanguage = detectPageLanguage();
  var isTranslation = isPageTranslation();


  //var listItems = document.querySelectorAll('#transcriptList li');
  //console.log(listItems);

  var listItems = $('#transcriptList li');
  hideListItems(listItems, detectedLanguage);

  var listItems2 = $('#textList li');
  hideListItems(listItems2, detectedLanguage);

  var listItems3 = $('#transcriptList2 li');
  adjustTranscriptLinksNew(listItems3, detectedLanguage);

  var listItems4 = $('#mtList li');
  hideMTLinks(listItems4, detectedLanguage);

  console.log("Done with menu init");
}





function adjustTranscriptLink(listItem, detectedLanguage) {
  if (listItem == null) return;
  console.log("link adjusting...");

  var linkItem = $($(listItem).children('a')[0]);
  if (linkItem == null) return;

  var pageName = getPageName();
  if (pageName == null) return;

  //console.log(pageName);
  //console.log(linkItem.attr('href'));

  if (linkItem.attr('href').includes(pageName)) {
    //console.log("No difference in link / page name");
    return;
  }  else if (pageName.includes(linkItem.attr('href').replace(".html", ""))) {
    // no need to remove / adjust the link
    // console.log("Skipping link adjusting");
    return;
  } else {
    console.log("There is a discrepancy to :" + linkItem.attr('href'));
  }

  //console.log("Link href: " + linkItem.attr('href'));

  console.log("Replacing " + linkItem.attr('href') + " ||| lang: _" + detectedLanguage + " inside " + linkItem.attr('href').replace("_" + detectedLanguage, ""));

  if (! linkItem.attr('href').includes("_" + detectedLanguage)) {
    // nothing to do
  } else {
   //console.log("adjusting...");
    // need to remove the language from the link
   var originalLink = linkItem.attr('href');
   var newLink = linkItem.attr('href').replace("_" + detectedLanguage, "");

   var listItems = $("a[href$='" + originalLink + "']");
   if (!(listItems == null && listItems[0] == null)) {

     for (var i=0; item=listItems[i]; i++) {
       $(item).attr("href", newLink);
       //console.log("Replaced in ");
       //console.log(item);
     }

   }

   //console.log("list Items: ");
   //console.log(listItems);

   //console.log("Replacing _" + detectedLanguage + " inside " + newLink);
   linkItem.attr('href', newLink);
   //console.log(newLink);
  }
  //console.log(linkItem);

}

function hideMTLinks(listItems, detectedLanguage) {
	//if (detectedLanguage == 'en') return;

	for (var i=0; item=listItems[i]; i++) {
                var li = $(item);
		if (li.hasClass("translationLink")){
                        var linkLanguage = li.attr("lang");
                        var linkItem = $(li.children('a')[0]);
			
			// hide if source and target are the same
			if (linkLanguage == detectedLanguage) {
				li.hide();
			}

			// for languatges other than en, hide translations unless into english
			if (detectedLanguage != 'en' && linkLanguage != 'en') {
				li.hide();
			}
		}

	}

}

function adjustTranscriptLinksNew(listItems, detectedLanguage) {
	for (var i=0; item=listItems[i]; i++) {
		var li = $(item);
		if (li.hasClass("transcriptLink")){
			var linkLanguage = li.attr("lang");
			var linkItem = $(li.children('a')[0]);
			// adjust link
			var newLink = "";
			if (linkLanguage != "xx") {
				newLink = linkItem.attr("href").replaceAll("_" + detectedLanguage, "_" + linkLanguage);
			} else {
				newLink = linkItem.attr("href").replace("_" + detectedLanguage, "_" + linkLanguage);

			}
			linkItem.attr("href", newLink);
		}
	}

}

function hideListItems(listItems, detectedLanguage) {
  console.log("Calling list items");
  //var listItems = $('#transcriptList li');
  for (var i=0; item=listItems[i]; i++) {
        var li = $(item);
        console.log(li);
        if (li.hasClass("transcriptLink")){
          if (li.attr("lang") == detectedLanguage) {
            // check if link needs to be adjusted for backwards compatibility
            adjustTranscriptLink(li, detectedLanguage);
          } else {
            //li.style.display = "none";
            li.hide();
            //console.log("Removing " + i);
            //console.log(li);
          }
        }
        if (li.hasClass("translationLink")) {
        //console.log("detectedLanguage: " + detectedLanguage);
        //console.log("item language: " + li.attr("lang"));
          if (li.attr("lang") == detectedLanguage) {
            li.hide();
            //li.style.display = "none";
            //console.log("Removing " + i);
            //console.log(li);
          }

        }
	if (li.hasClass("txtLink")) {
        //console.log("detectedLanguage: " + detectedLanguage);
        //console.log("item language: " + li.attr("lang"));
          if (li.attr("lang") == detectedLanguage) {
            //li.hide();
	    // do not adjust the link here
	  } else {
	     li.hide();
	  }
            //li.style.display = "none";
            //console.log("Removing " + i);
            //console.log(li);
          //}

        }



  }
}




