Index: src/lincity-ng/main.cpp
===================================================================
--- src/lincity-ng/main.cpp	(revision 1618)
+++ src/lincity-ng/main.cpp	(working copy)
@@ -21,6 +21,7 @@
 #include <sstream>
 #include <stdexcept>
 #include <memory>
+#include <new>
 #include <physfs.h>
 #include <SDL.h>
 #include <SDL_opengl.h>
@@ -179,6 +180,11 @@
     printf("[%s] is the write directory.\n", PHYSFS_getWriteDir());
 }
 
+void musicHalted() {
+    getSound()->changeTrack(NEXT_OR_FIRST_TRACK);
+    //FIXME: options menu song entry doesn't update while song changes.
+}
+
 void initVideo(int width, int height)
 {
     int bpp = 0;
@@ -553,6 +559,10 @@
             throw std::runtime_error(msg.str());
         }
         initVideo(getConfig()->videoX, getConfig()->videoY);
+        
+        //set a function to call when music stops
+        Mix_HookMusicFinished(musicHalted);
+        
 
         mainLoop();
 #ifndef DEBUG
Index: src/lincity-ng/Sound.cpp
===================================================================
--- src/lincity-ng/Sound.cpp	(revision 1618)
+++ src/lincity-ng/Sound.cpp	(working copy)
@@ -103,10 +103,64 @@
 
     setMusicVolume(getConfig()->musicVolume);
     setSoundVolume(getConfig()->soundVolume);
+    
 
-    // for now...
-    //playMusic("01 - pronobozo - lincity.ogg");
-    playMusic( getConfig()->playSongName );
+    std::string filename;
+    std::string fullname;
+    std::string directory = "music/";
+
+    totalTracks = 0;
+    
+    //list files in 'music/'
+    char **files= PHYSFS_enumerateFiles(directory.c_str());
+    char **fptr=files;
+
+    //inspect the total amount of tracks here.
+    while(*fptr)
+    {        
+        fullname = directory;
+        fullname.append( *fptr );
+        if(!PHYSFS_isDirectory(fullname.c_str())){
+            totalTracks++;
+        }
+        fptr++;
+
+    }
+    //allocate a song table for tracks.
+    playlist = new song[totalTracks];
+
+    //reset the pointer
+    fptr=files;
+    totalTracks = 0;
+    fullname = "";
+    
+    //Fill the playlist with data:
+    while(*fptr)
+    {
+        fullname = directory;
+        fullname.append( *fptr );
+        filename.assign( *fptr );
+
+        if(!PHYSFS_isDirectory(fullname.c_str())){
+            playlist[totalTracks].title = *fptr;
+            playlist[totalTracks].filename = directory + *fptr;
+            playlist[totalTracks].trackNumber = totalTracks;
+            std::cerr << "Found song " << *fptr << "\n";
+            totalTracks++;
+        }
+        fptr++;
+        
+
+    }
+    
+    PHYSFS_freeList(files);
+    
+    //'totalTracks' gets too high value in while loop. Let's fix it.
+    totalTracks = totalTracks-1;
+    
+    //Load background music
+    currentTrack = playlist[0];
+    playMusic();
 }
 
 Sound::~Sound()
@@ -172,33 +226,68 @@
 }
 
 void
-Sound::playMusic(const std::string& name)
+Sound::changeTrack(MusicTransport command)
 {
-    getConfig()->playSongName = name;
+    switch(command) {
+        case NEXT_TRACK:
+            if (currentTrack.trackNumber+1 <= totalTracks) {
+                currentTrack=playlist[currentTrack.trackNumber+1];
+                playMusic();
+            }
+            break;
+            
+        case NEXT_OR_FIRST_TRACK:
+            if (currentTrack.trackNumber+1 <= totalTracks) {
+                currentTrack=playlist[currentTrack.trackNumber+1];
+                playMusic();
+            } else {
+                //Jump to the beginning
+                currentTrack=playlist[0];
+                playMusic();
+            }
+            break;
+            
+        case PREV_TRACK:
+            if (currentTrack.trackNumber > 0) { 
+                currentTrack=playlist[currentTrack.trackNumber-1];
+                playMusic();
+            } else {
+                //Jump to the beginning
+                currentTrack=playlist[0];
+                playMusic();
+            }
+            break;
+    }
+
+}
+
+
+void
+Sound::playMusic()
+{
     if(!audioOpen)
         return;
 
-    musicFile = name;
     if(getConfig()->musicEnabled) {
         if(currentMusic) {
             Mix_FreeMusic(currentMusic);
             currentMusic = 0;
         }
-        if(musicFile == "")
-            return;
+//         if(currentTrack == "")
+//             return;
 
         // transform filename... because the music commands in SDL_Mixer don't
         // support reading callbacks to read from physfs directly
-        std::string filename = "music/";
-        filename += name;
-        const char* dir = PHYSFS_getRealDir(filename.c_str());
+        
+        const char* dir = PHYSFS_getRealDir(currentTrack.filename.c_str());
         if(dir == 0) {
-            std::cerr << "Warning couldn't find music file '" << name << "'.\n";
+            std::cerr << "Warning couldn't find music file '" << currentTrack.filename << "'.\n";
             return;
         }
-        filename = dir;
-        filename += "/music/";
-        filename += name;
+        std::string filename = dir;
+        filename += "/";
+        filename += currentTrack.filename;
+
         
         currentMusic = Mix_LoadMUS(filename.c_str());
         if(currentMusic == 0) {
@@ -207,7 +296,7 @@
             return;
         }
 
-        Mix_PlayMusic(currentMusic, -1);
+        Mix_PlayMusic(currentMusic, 1);
     }
 }
 
@@ -222,7 +311,7 @@
         return;
 
     if(enabled) {
-        playMusic(musicFile);
+        playMusic();
     } else {
         if(Mix_PlayingMusic()) {
             Mix_FadeOutMusic(1000);
Index: src/lincity-ng/main.hpp
===================================================================
--- src/lincity-ng/main.hpp	(revision 1618)
+++ src/lincity-ng/main.hpp	(working copy)
@@ -24,6 +24,7 @@
     MAINMENU, INGAME, QUIT, RESTART
 };
 
+
 void initVideo(int width, int height);
 void flipScreenBuffer();
 
Index: src/lincity-ng/Sound.hpp
===================================================================
--- src/lincity-ng/Sound.hpp	(revision 1618)
+++ src/lincity-ng/Sound.hpp	(working copy)
@@ -24,6 +24,24 @@
 #include <SDL.h> 
 #include <SDL_thread.h>
 
+//TODO make use of these:
+enum MusicState {
+    PLAY, PAUSE, STOP  
+};
+
+//Used for changing tracks.
+enum MusicTransport {
+    NEXT_TRACK, PREV_TRACK, NEXT_OR_FIRST_TRACK
+};
+
+struct song {
+    std::string title;
+    std::string filename;
+    int trackNumber;
+    float lowestTechLevel;
+    float highestTechLevel;
+};
+
 class Sound
 {
 public:
@@ -31,13 +49,17 @@
     ~Sound();
 
     void playSound(const std::string& name);
-    void playMusic(const std::string& name);
+    void playMusic();
+    void changeTrack(MusicTransport command);
     void enableMusic(bool enabled);
     /** set Music volume 0..100, 0=silent*/
     void setMusicVolume(int vol);
     /** set Sound effect volume (0..100) */
     void setSoundVolume(int vol);
+    /** information about current playing track. Title, filename, track number, and tech levels allowed to play it. **/
+    song currentTrack;
 
+
 private:
     static int soundThread(void* ptr);
     void loadWaves();
@@ -48,7 +70,8 @@
     bool audioOpen;
     SDL_Thread* loaderThread;
     Mix_Music* currentMusic;
-    std::string musicFile;
+    int totalTracks;
+    song * playlist;
 };
 
 Sound* getSound();
Index: src/lincity-ng/MainMenu.cpp
===================================================================
--- src/lincity-ng/MainMenu.cpp	(revision 1618)
+++ src/lincity-ng/MainMenu.cpp	(working copy)
@@ -315,7 +315,7 @@
     }
     //current background track
     musicParagraph = getParagraph( *optionsMenu, "musicParagraph");
-    musicParagraph->setText(getConfig()->playSongName);
+    musicParagraph->setText(getSound()->currentTrack.title);
 
     std::stringstream mode;
     mode << SDL_GetVideoSurface()->w << "x" << SDL_GetVideoSurface()->h;
@@ -601,49 +601,15 @@
 void
 MainMenu::changeTrack( bool next)
 {
-    std::string filename;
-    std::string directory = "music/";
-    std::string fullname;
-    std::string currentname = getConfig()->playSongName;
-    std::string prevname = currentname;
-    std::string nextname = currentname;
-
-    bool hit = false;
-
-    char **files= PHYSFS_enumerateFiles(directory.c_str());
-    char **fptr=files;
-    while(*fptr)
-    {
-        fullname = directory;
-        fullname.append( *fptr );
-        filename.assign( *fptr );
-
-        if(!PHYSFS_isDirectory(fullname.c_str())){
-            if( filename == currentname ){
-                hit = true;
-            } else if ( !hit ){
-                prevname = filename;
-            } else {
-                nextname = filename;
-                break;
-            }
-        }
-        fptr++;
-    }
-    PHYSFS_freeList(files);
-
     if(next){
-        if( nextname != currentname){
-            getSound()->playSound("Click");
-            getSound()->playMusic(nextname);
-        }
+        getSound()->playSound("Click");
+        getSound()->changeTrack(NEXT_TRACK);
+    
     } else {
-        if( prevname != currentname){
-            getSound()->playSound("Click");
-            getSound()->playMusic(prevname);
-        }
+        getSound()->playSound("Click");
+        getSound()->changeTrack(PREV_TRACK);
     }
-    musicParagraph->setText(getConfig()->playSongName);
+    musicParagraph->setText(getSound()->currentTrack.title);
 }
 void
 MainMenu::quitButtonClicked(Button* )
