function CGallery(jsonGallery, parent)
{
  this.parent            = parent;
	this.name              = jsonGallery.name;
	this.id                = jsonGallery.id;
	this.parentId          = jsonGallery.parentId;
	this.idx               = jsonGallery.idx;
	this.subGalleryCnt     = jsonGallery.galleryCnt;
	this.photoCnt          = jsonGallery.photoCnt;
	this.subGalleries      = new Object();
	this.photos            = new Object();
	this.photoIdxMap       = new Array();
	this.subgalleryIdxMap  = new Array();
	
	if(jsonGallery.subGalleries!=null)
	{
		for(var id in jsonGallery.subGalleries)
		{
		  var item = jsonGallery.subGalleries[id];
		  var gallery = new CGallery(item, this);
			this.subGalleries[id] = gallery;
			this.subgalleryIdxMap[gallery.getIdx()] = id;
		}	
	}
	
	if(jsonGallery.photos!=null)
	{
	  var idx=0;
		for(var id in  jsonGallery.photos)
	  {
		  var item = jsonGallery.photos[id];
		  var photo = new CPhoto(item, this);
		  this.photos[id] = photo;
		  this.photoIdxMap[idx++] = id;
	  }
	}
}

CGallery.prototype.getParent          = function()    {return this.parent;};
CGallery.prototype.getSubGalleryCount = function()    {return this.subGalleryCnt;};
CGallery.prototype.getSubGalleryById  = function(id)  {return this.subGalleries[id];};
CGallery.prototype.getID              = function()    {return this.id;};
CGallery.prototype.getParentID        = function()    {return this.parentId;};
CGallery.prototype.getIdx             = function()    {return this.idx;};
CGallery.prototype.getName            = function()    {return this.name;};
CGallery.prototype.getPhotoById       = function(id)  {return this.photos[id];};
CGallery.prototype.getPhotoCount      = function()    {return this.photoCnt;};

CGallery.prototype.getSubGalleryByIdx    = function(idx)
{
  var id = this.subgalleryIdxMap[idx];
  return this.getSubGalleryById(id);
};

CGallery.prototype.getPhotoByIdx    = function(idx)
{
  var id = this.photoIdxMap[idx];
  return this.getPhotoById(id);
};

CGallery.prototype.matches = function(other)
{
  if(this.getID()==other.getID())
    return true;
  
  if(this.getParent()!=null)
  {
    if(other.getParent()!=null)
      return this.getParent().getID()==other.getParent().getID();
    return false;
  }
  
  return other.getParent()==null;
};

CGallery.prototype.setSubGallery = function(gallery)
{
  if(gallery.getParentID()==this.getID())
  {
    gallery.parent = this;
    this.subGalleries[gallery.getID()] = gallery;
    this.subgalleryIdxMap[gallery.getIdx()] = gallery;
  }  
};
