bool ossimPlanetTextureLayerGroup::addAfterIdx(ossim_int32 idx,
                                               osg::ref_ptr<ossimPlanetTextureLayer> layer, 
                                               bool notifyFlag)
{
   bool result = false;
   
   theChildrenListMutex.lock();
   if(containsLayerNoMutex(layer))
   {
      return result;
   }
   if(idx == -1)
   {
      layer->addParent(this);
      layer->addCallback(theChildListener);
      theChildrenList.insert(theChildrenList.begin(), layer);
      theChildrenListMutex.unlock();
      dirtyExtents();
      dirtyStats();
      result = true;
      if(notifyFlag)
      {
         notifyLayerAdded(layer);   
      }
   }
   else if(idx < theChildrenList.size())
   {
      layer->addParent(this);
      layer->addCallback(theChildListener);
      theChildrenList.insert(theChildrenList.begin()+idx+1, layer);
      theChildrenListMutex.unlock();
      dirtyExtents();
      dirtyStats();
      result = true;
      if(notifyFlag)
      {
         notifyLayerAdded(layer);   
      }
   }
   else if(idx == theChildrenList.size())
   {
      layer->addParent(this);
      layer->addCallback(theChildListener);
      theChildrenList.push_back(layer.get());
      theChildrenListMutex.unlock();
      dirtyExtents();
      dirtyStats();
      result = true;
      if(notifyFlag)
      {
         notifyLayerAdded(layer);   
      }
   }
   else
   {
     theChildrenListMutex.unlock();
   }
   return result;
}
bool ossimPlanetTextureLayerGroup::addBottom(osg::ref_ptr<ossimPlanetTextureLayer> layer, 
                                             bool notifyFlag)
{
   bool result = false;
   if(layer.valid())
   {
      if(!containsLayerNoMutex(layer))
      {
         layer->addParent(this);
         layer->addCallback(theChildListener);
         theChildrenListMutex.lock();
         theChildrenList.push_back(layer);
         theChildrenListMutex.unlock();
         dirtyExtents();
         dirtyStats();
         result = true;
         if(notifyFlag)
         {
            notifyLayerAdded(layer);   
         }
      }
   }

   return result;
}
bool ossimPlanetTextureLayerGroup::addTop(osg::ref_ptr<ossimPlanetTextureLayer> layer, 
                                          bool notifyFlag)
{
   if(layer.get() == this) return false;
//   OpenThreads::ScopedLock<OpenThreads::Mutex> lock(theChildrenListMutex);
   bool result = false;
   
   if(layer.valid())
   {
      if(!containsLayer(layer))
      {
         layer->addParent(this);
         layer->addCallback(theChildListener);
         theChildrenListMutex.lock();
         theChildrenList.insert(theChildrenList.begin(), layer);
         theChildrenListMutex.unlock();
         dirtyExtents(); // notify parent layers
         dirtyStats();
         result = true;
         if(notifyFlag)
         {
            notifyLayerAdded(layer);
         }
      }
   }

   return result;
}
bool ossimPlanetTextureLayerGroup::addBeforeIdx(ossim_uint32 idx,
                                                osg::ref_ptr<ossimPlanetTextureLayer> layer, 
                                                bool notifyFlag)
{
   bool result = false;

   theChildrenListMutex.lock();
   if((idx < theChildrenList.size())&&(!containsLayerNoMutex(layer)))
   {
      layer->addParent(this);
      layer->addCallback(theChildListener);
      theChildrenList.insert(theChildrenList.begin()+idx, layer);
      theChildrenListMutex.unlock();
     dirtyExtents();
      dirtyStats();
      result = true;
      if(notifyFlag)
      {
         notifyLayerAdded(layer);   
      }
   }
   else
    {
      theChildrenListMutex.unlock();
    }

   return result;
}
bool ossimPlanetTextureLayerGroup::replaceLayer(ossim_uint32 idx,
                                                osg::ref_ptr<ossimPlanetTextureLayer> layer, 
                                                bool notifyFlag)
{
   OpenThreads::ScopedLock<OpenThreads::Mutex> lock(theChildrenListMutex);
    bool result = false;
    if(layer.valid()&&idx < theChildrenList.size())
    {
       if(!containsLayerNoMutex(layer))
       {
          if(theChildrenList[idx].valid())
          {
             theChildrenList[idx]->removeCallback(theChildListener);
             theChildrenList[idx]->removeParent(this);
          }
          layer->addParent(this);
          layer->addCallback(theChildListener);
          theChildrenList[idx] = layer.get();
          dirtyExtents();
          dirtyStats();
          result = true;
          if(notifyFlag)
          {
             notifyLayerAdded(layer);
          }
       }
    }
    
    return result;
}