function RouteCollectionSelector(container)
{
        console.log("Initializating RouteCollectionSelector");

        this.container = container;
        var self = this;

        product.events.mapswitch.subscribe(function(){ self.update(); }, this);

        this.selector = new TabStrip(
                                                { id:                                   'trackcollectionselector'
                                                , btnclass:                             'tab'
                                                , btnclass_selected:    'selected'
                                                , usenode:                              null
                                                , onchange:                     function()
                                                                                        {
                                                                                                var chosencollection = self.selector.getValue();
                                                                                                console.log("Collection #"+chosencollection+" selected");

                                                                                                var prevcol = self.selector.prevValue;
                                                                                                if (prevcol != null && prevcol > 0)
                                                                                                        self.hideCollection(prevcol);

                                                                                                if (chosencollection > 0)
                                                                                                        self.showCollection(chosencollection);
                                                                                        }
                                                });

        if (mapmeta.mapid > 0)
                this.update();
}

RouteCollectionSelector.prototype.update = function()
{
        console.log("Loading list of track collections");
        var self = this;
        mapmeta.getRouteCollectionsForMap(mapmeta.mapid, function(result)
                        {
                                product.events.routecollectionloaded.fire(null);
                                self.recreatecontent(result);
                        });
}

RouteCollectionSelector.prototype.recreatecontent = function(result)
{
        console.log("Updating track collection list with loaded list");

        this.container.innerHTML = ''; // FIXME dirty hack, panel might have other content too

        var items = result.trackcollections;
        var self = this;

        var fraglist = document.createDocumentFragment();

        var tabby = this.getSelectionItem("Geen routes tonen", "", 0, null, "routeoption");
        this.selector.hookTab2({ tabElement: tabby, value: 0 });
        fraglist.appendChild(tabby);

        for(var cindex=0; cindex < items.length; cindex++)
        {
                var coll = items[cindex];

                // don't show collections which are empty
                if(coll.tracks.length == 0)
                  continue;

                var tabby = this.getSelectionItem(
                                                          coll.title
                                                        , coll.description
                                                        , coll.id
                                                        , null //function() { self.showCollection(coll.id); }
                                                        , "routeoption"
                                                        , coll.tracks.length
                                                        );

                this.selector.hookTab2({ tabElement: tabby, value: coll.id });

                fraglist.appendChild(tabby);
        }

        this.container.appendChild(fraglist);
}

RouteCollectionSelector.prototype.getSelectionItem = function(title, description, itemid, onclick, cssclass, trackcount)
{
        var itemcontainer = document.createElement('div');
        itemcontainer.className = cssclass;

        var ititle = document.createElement('div');
        ititle.className = 'title';
        ititle.onclick = onclick;
        ititle.itemid = itemid;
        ititle.innerHTML = title;
        itemcontainer.appendChild(ititle);

        if (trackcount != null)
        {
          var nodetc = document.createElement('div');
          nodetc.className = 'trackCount';
          nodetc.innerHTML = trackcount+" routes";
          ititle.appendChild(nodetc);
        }

        var idesc = document.createElement('div');
        idesc.className = 'description';
        idesc.innerHTML = description;
        itemcontainer.appendChild(idesc);

        return itemcontainer;
}


RouteCollectionSelector.prototype.hideCollection = function RouteCollectionSelector_hidecollection(collectionid)
{
        var collection = mapmeta.trackcollections.getById(collectionid);

        if (collection == null)
        {
                console.error("Failed to find collection #"+collectionid);
                return;
        }
/*
        // deselect all routes from the routecollection on the raw routes list
        if (window.RouteSelection)
        {
                var test = new RouteSelection();
                test.selectids(collection.tracks, false);
        }
*/
        for(var tel=0; tel<collection.tracks.length; tel++)
                mygmap.disableTrack(collection.tracks[tel]);
}

RouteCollectionSelector.prototype.showCollection = function RouteCollectionSelector_showCollection(collectionid)
{
        product.events.routecollectionselected.fire(collectionid);

        var collection = mapmeta.trackcollections.getById(collectionid);

        mapmeta.cancelTrackRequests();

        if (collection == null)
        {
                console.error("Failed to find collection #"+collectionid);
                return;
        }

        // check all routes in the routecollection on the raw routes list
        if (window.RouteSelection)
        {
                var test = new RouteSelection();
test.deselectAll();
                test.selectids(collection.tracks, true);
        }

        for(var tel=0; tel<collection.tracks.length; tel++)
        {
                mapmeta.loadTrackWithCallback(collection.tracks[tel]
                                , function(trackdata)
                                        {
                                                mygmap.drawTrack(trackdata);
                                        });
        }
}
