def BrowseByCountry():
    #fill list alphabetically with unique countrycodes
    unique_countrycode=[]
    for countrycode in chi_countrycode:
        if countrycode not in unique_countrycode:
            unique_countrycode.append(countrycode)
            sorted_unique_countrycode=sorted(unique_countrycode)
    #make menu alphabetically
    for code in sorted_unique_countrycode:
        countryflag = os.path.join(path, 'resources', 'media', 'countryflags', code + ".png")
        li = plugintools.xbmcgui.ListItem(plugintools.lookup_countryname(code.strip()), iconImage=str(countryflag), thumbnailImage="")
        li.setProperty('fanart_image', mainfanart)
        plugintools.xbmcplugin.addDirectoryItem(handle=addon_handle, url=base_url+"?countrycode="+str(code.strip()) , listitem=li,isFolder=True)
    plugintools.close_item_list()
def ShowOrganisationsFromCountry(countrycode):
    for chirow in chi:
        if chirow[2].strip() == countrycode:
            plugintools.add_item(
            # See http://bytes.com/topic/python/answers/590441-how-strip-mulitiple-white-spaces
            title=" ".join(chirow[0].split()) + ", " + " ".join(chirow[1].split()) + ", " + plugintools.lookup_countryname(chirow[2].strip()),
            url="plugin://plugin.video.youtube/user/" + chirow[3].strip() + "/",
            thumbnail=os.path.join(path, 'resources', 'media', 'icons', "icon_" + chirow[4].strip() + ".png"),
            fanart=os.path.join(path, 'resources', 'media', 'fanarts', "fanart_" + chirow[4].strip() + ".jpg"),
            folder=True)
    plugintools.close_item_list()
# Name of institution, City, Countrycode, Youtube channel ID, abbreviation of institute (for icons and fanart)
# Put in in list chi[]
chi = []
chi = plugintools.read_local_csv(institutionspath)
# Break down chi[]. Set OrganisationName, city, countrycode,YoutubeChannel-ID, abbreviation, local thumb and local fanart for every CH institution
chi_name, chi_city, chi_countrycode, chi_countryname, chi_YOUTUBE_CHANNEL, chi_abbrev, chi_twitterHandle, chi_thumb, chi_fanart = (
    [] for i in range(9))

# Strip leading and traling blanks from list entries, as well as multiple inner spaces
# See http://bytes.com/topic/python/answers/590441-how-strip-mulitiple-white-spaces
for i in range(len(chi)):
    chi_name.append(" ".join(chi[i][0].split()))
    chi_city.append(" ".join(chi[i][1].split()))
    chi_countrycode.append(chi[i][2].strip())
    chi_countryname.append(
        plugintools.lookup_countryname(chi_countrycode[i].strip()))
    chi_YOUTUBE_CHANNEL.append(chi[i][3].strip())
    chi_abbrev.append(chi[i][4].strip())
    chi_thumb.append(
        os.path.join(path, 'resources', 'media', 'icons', "icon_" + chi_abbrev[i] + ".png"))
    chi_fanart.append(os.path.join(path, 'resources', 'media', 'fanarts',
                                   "fanart_" + chi_abbrev[i] + ".jpg"))

def BrowseByCountry():
    #fill list alphabetically with unique countrycodes
    unique_countrycode=[]
    for countrycode in chi_countrycode:
        if countrycode not in unique_countrycode:
            unique_countrycode.append(countrycode)
            sorted_unique_countrycode=sorted(unique_countrycode)
    #make menu alphabetically
# Name of institution, City, Countrycode, Youtube channel ID, abbreviation of institute (for icons and fanart)
# Put in in list chi[]
chi = []
chi = plugintools.read_local_csv(localfilepath)
# Break down chi[]. Set OrganisationName, city, countrycode,YoutubeChannel-ID, abbreviation, local thumb and local fanart for every CH institution
chi_name, chi_city, chi_countrycode, chi_countryname, chi_YOUTUBE_CHANNEL, chi_abbrev, chi_twitterHandle, chi_thumb, chi_fanart = (
    [] for i in range(9))

# Strip leading and traling blanks from list entries, as well as multiple inner spaces
# See http://bytes.com/topic/python/answers/590441-how-strip-mulitiple-white-spaces
for i in range(len(chi)):
    chi_name.append(" ".join(chi[i][0].split()))
    chi_city.append(" ".join(chi[i][1].split()))
    chi_countrycode.append(chi[i][2].strip())
    chi_countryname.append(
        plugintools.lookup_countryname("countrydata.csv", chi_countrycode[i].strip()))
    chi_YOUTUBE_CHANNEL.append(chi[i][3].strip())
    chi_abbrev.append(chi[i][4].strip())
    chi_thumb.append(
        os.path.join(path, 'resources', 'media', 'icons', "icon_" + chi_abbrev[i] + ".png"))
    chi_fanart.append(os.path.join(path, 'resources', 'media', 'fanarts',
                                   "fanart_" + chi_abbrev[i] + ".jpg"))


# Entry point
def run():
    plugintools.log("heritagechannel.run")
    # Get params
    params = plugintools.get_params()
    # if params.get("action") is None:
    main_list(params)