var IHG = {
  _elementDoesNotExistError: {
    name: 'ElementDoesNotExistError',
    message: 'The specified DOM element does not exist, but is required for this effect to operate'
  }
};

IHG.Layer = function() {};

IHG.Layer.Base = function() {};
IHG.Layer.Base.prototype = {
  setOptions: function(options) {
    this.options = {
      maxViewable:  0
    }
    Object.extend(this.options, options || {});
	}
}

IHG.Layer.Group = Class.create();
Object.extend(Object.extend(IHG.Layer.Group.prototype, IHG.Layer.Base.prototype), {
  initialize: function(options) {
    this.setOptions(options);
	this.divs = new Array();
	this.displayed = new Array();
  },
  add: function( divLayer ) {
	  this.divs.push( divLayer );
	  if ( divLayer.isDisplayed ) {
		this.displayed.push( divLayer.name );
	  }
  },
  addDiv: function( divName, options ) {
	  var divLayer = new IHG.Layer.Div( divName, options );
	  this.divs.push( divLayer );
	  if ( divLayer.isDisplayed ) {
		this.displayed.push( divLayer.name );
	  }
  },
  show: function( divName, srcDiv ) {
	var divLayer = this.findDiv( divName );
	if ( divLayer != null )
	{
		if ( !divLayer.isDisplayed ) {
			if ( (this.options.maxViewable > 0) && (this.displayed.length == this.options.maxViewable) ) {
				this.hide( this.displayed.shift() );
			}
			this.displayed.push( divLayer.name );
		}
		divLayer.show( srcDiv );
	}
  },
  hide: function( divName ) {
	  var divLayer = this.findDiv( divName );
	  divLayer.hide();
	  this.displayed = this.displayed.without( divLayer.name );
  },
  toggle: function( divName ) {
	  var divLayer = this.findDiv( divName );
	  if ( divLayer.isDisplayed ) {
		this.hide( divName );
	  }
	  else {
		this.show( divName );
	  }
  },
  findDiv: function( divName ) {
	return this.divs.find( function(divLayer) { return (divLayer.name == divName); });
  }
});

IHG.Layer.DivBase = function() {};
IHG.Layer.DivBase.prototype = {
  setOptions: function(options) {
    this.options = {
	  hidden:       true,
	  url:        null,
	  params:	  ""
    }
    Object.extend(this.options, options || {});
	}
}

IHG.Layer.Div = Class.create();
Object.extend(Object.extend(IHG.Layer.Div.prototype, IHG.Layer.DivBase.prototype), {
  initialize: function(name,options) {
	this.name = name;
    this.setOptions(options);
	if ( !this.options.hidden ) {
		this.show();
	}
  },
  show: function( srcDiv ) {
	if ( srcDiv != null ) {
		$(this.name).innerHTML = $(srcDiv).innerHTML;
	}
	else if ( this.options.url != null ) {
		new Ajax.Updater(
			this.name, 
			this.options.url, 
			{
				method: 'get', 
				parameters: this.options.params
			});		
	}
	$(this.name).show();
  },
  hide: function() {
	$(this.name).hide();
  },
  isDisplayed: function() {
	  if ( $(this.name).style.display == "none" ) {
		  return false;
	  }
	  return true;
  },
  toggle: function() {
	if ( this.isDisplayed() )	{
		this.hide();
	}
	else {
		this.show();
	}
  }
});
