var displayMostRecent = 3;

function getMTLinkList(item) {
        var txt = '<ul class="inline">';
        
//        var frLink = item.find("link_fr");
//        var esLink = item.find("link_es");
//        var arLink = item.find("link_ar");
//        var zhLink = item.find("link_zh");
//        var ruLink = item.find("link_ru");

        var frLink = item.find("link[language='fr']");
        var esLink = item.find("link[language='es']");
        var arLink = item.find("link[language='ar']");
        var zhLink = item.find("link[language='zh']");
        var ruLink = item.find("link[language='ru']");

	
	console.log(frLink);
	console.log("French link: " + frLink.text());

        if (arLink.length > 0) {
                txt = txt + '<li><a href="' + arLink.text() + '">عربي</a></li>'
        }
        if (esLink.length > 0) {
                txt = txt + '<li><a href="' + esLink.text() + '">Español</a></li>'
        }
        if (frLink.length > 0) {
                txt = txt + '<li><a href="' + frLink.text() + '">Français</a></li>'
        }
        if (ruLink.length > 0) {
                txt = txt + '<li><a href="' + ruLink.text() + '">Русский</a></li>'
        }
        if (zhLink.length > 0) {
                txt = txt + '<li><a href="' + zhLink.text() + '">中文</a></li>'
        }

        txt = txt + "</ul>";
        return txt;
};

function loadTranscripts(committee, number) {
        var counter = 0;
	
	 if (number) {
                displayMostRecent = number;
                console.log("going to display transcripts: " + displayMostRecent);
        }

        $.get("rss_en.xml", function (data) {
        console.log(data);
	
	var stringXML = new XMLSerializer().serializeToString(data.documentElement);
	console.log("String XML: " + stringXML);

	var newStringXML = stringXML.replace(/<!--/g, "").replace(/-->/g,"");
	console.log(newStringXML);

	$($.parseXML(newStringXML)).find("item").each(function () { 
	  
	  var el = $(this);
          counter = counter + 1;
	  //console.log(el.find("enclosure"));
	 
	  var linkEnglish = $(el.find("link")[0]).text();
	  //console.log("Link English: " + linkEnglish);
	  //console.log($(el.find("link")[0]).text());

	  var thumbnail = "";
          if (el.find("enclosure").length > 0) {
		thumbnail = '<img class="listThumbnail" src="' + el.find("enclosure")[0].getAttribute("url") + '" alt="thumbnail"></img>';
	  }

          if (counter <= displayMostRecent) {
          var txt = '<tr class="sessionTranscript" id="' + el.find("guid").text() + '" onclick="window.location=' + "'" + linkEnglish + "'" + ';">' 
                + '<td>'+ el.find("description").text() + '</td>'
                + '<td>'+ el.find("pubDate").text() + '</td>'
                + '<td>'+ thumbnail + '</td>'
                + '<td>'+ '<a href="' + linkEnglish + '">English</a>' + '</td>'
                + '<td>'+ getMTLinkList(el) + '</td>'
                + '</tr>';

          $('#search_results').append(txt);
          }
 
    });
});

};

function getDocxLinkList(listOfLanguages, baseLink) {

        var txt = '<ul class="inline">';

        if (listOfLanguages.includes("en")) {
                txt = txt + '<li><a href="' + baseLink + '.docx">English</a></li>'
        }

        if (listOfLanguages.includes("ar")) {
                txt = txt + '<li><a href="' + baseLink.replace(/_en/g, "_ar") + '.docx">عربي</a></li>'
        }
        if (listOfLanguages.includes("es")) {
                txt = txt + '<li><a href="' + baseLink.replace(/_en/g, "_es") + '.docx">Español</a></li>'
        }
        if (listOfLanguages.includes("fr")) {
                txt = txt + '<li><a href="' + baseLink.replace(/_en/g, "_fr") + '.docx">Français</a></li>'
        }
        if (listOfLanguages.includes("ru")) {
                txt = txt + '<li><a href="' + baseLink.replace(/_en/g, "_ru") + '.docx">Русский</a></li>'
        }
        if (listOfLanguages.includes("zh")) {
                txt = txt + '<li><a href="' + baseLink.replace(/_en/g, "_zh") + '.docx">中文</a></li>'
        }
        if (listOfLanguages.includes("xx")) {
                txt = txt + '<li><a href="' + baseLink.replace(/_en/, "_xx") + '.docx">floor</a></li>'
        }


        txt = txt + "</ul>";
        return txt;

};


function getLanguageLinkList(listOfLanguages, baseLink) {

        var txt = '<ul class="inline">';

        if (listOfLanguages.includes("en")) {
                txt = txt + '<li><a href="' + baseLink + '">English</a></li>'
        }

        if (listOfLanguages.includes("ar")) {
                txt = txt + '<li><a href="' + baseLink.replace(/_en/g, "_ar") + '">عربي</a></li>'
        }
        if (listOfLanguages.includes("es")) {
                txt = txt + '<li><a href="' + baseLink.replace(/_en/g, "_es") + '">Español</a></li>'
        }
        if (listOfLanguages.includes("fr")) {
                txt = txt + '<li><a href="' + baseLink.replace(/_en/g, "_fr") + '">Français</a></li>'
        }
        if (listOfLanguages.includes("ru")) {
                txt = txt + '<li><a href="' + baseLink.replace(/_en/g, "_ru") + '">Русский</a></li>'
        }
        if (listOfLanguages.includes("zh")) {
                txt = txt + '<li><a href="' + baseLink.replace(/_en/g, "_zh") + '">中文</a></li>'
        }
        if (listOfLanguages.includes("xx")) {
                txt = txt + '<li><a href="' + baseLink.replace(/_en/, "_xx") + '">floor</a></li>'
        }


        txt = txt + "</ul>";
        return txt;

};



function loadTranscriptsMultiLanguage(committee, number, listOfLanguages, descriptionFieldName) {
        var counter = 0;

	var fieldName = "description";
	if (typeof descriptionFieldName !== 'undefined') {
		fieldName = descriptionFieldName;
	}

        if (listOfLanguages) {
                console.log("Processing languages: " );
                console.log(listOfLanguages);
        } else {
                listOfLanguages = [ "en" ];
                console.log("Defaulting languages to " );
                console.log(listOfLanguages);
        }

         if (number) {
                displayMostRecent = number;
                console.log("going to display transcripts: " + displayMostRecent);
        }

        $.get("rss_en.xml", function (data) {
        console.log(data);

        var stringXML = new XMLSerializer().serializeToString(data.documentElement);
        console.log("String XML: " + stringXML);

        var newStringXML = stringXML.replace(/<!--/g, "").replace(/-->/g,"");
        console.log(newStringXML);

        $($.parseXML(newStringXML)).find("item").each(function () {


          var el = $(this);
          counter = counter + 1;
          //console.log(el.find("enclosure"));

          var linkEnglish = $(el.find("link")[0]).text();
          console.log("Link English: " + linkEnglish);
          //console.log($(el.find("link")[0]).text());


          var baseLink = "NA";
          baseLink = linkEnglish;

          var thumbnail = "";
          if (el.find("enclosure").length > 0) {
                thumbnail = '<img class="listThumbnail" src="' + el.find("enclosure")[0].getAttribute("url") + '" alt="thumbnail"></img>';
          }

	  var descriptionContent = "";

	  if (fieldName == 'description') {
		  descriptionContent = el.find(fieldName).text().replaceAll("_","/");
	  } else {
		  descriptionContent = el.find(fieldName).text();
	  }

          if (counter <= displayMostRecent) {
          var txt = '<tr class="sessionTranscript" id="' + el.find("guid").text() + '" onclick="window.location=' + "'" + linkEnglish + "'" + ';">'
                + '<td>'+ descriptionContent + '</td>'
                + '<td>'+ el.find("pubDate").text().slice(0,-12) + '</td>'
                + '<td>'+ thumbnail + '</td>'


                //+ '<td>'+ '<a href="' + linkEnglish + '">English</a>' + '</td>'
                + '<td>' + getLanguageLinkList(listOfLanguages, baseLink) + '</td>'

                + '<td>' + getDocxLinkList(listOfLanguages, baseLink) + '</td>'

                //+ '<td>'+ getMTLinkList(el) + '</td>'
                + '</tr>';

          $('#search_results').append(txt);
          }

    });
});

};


