/**
 * Control various viewers on a page.
 */
var ObjectController = Class.create({
	initialize: function() {
		this.viewers = new Hash();
		this.activeViewer = null;
	},
	
	addViewer: function(viewer) {
		var viewerObj = {viewerInstance: viewer, viewAreas: viewer.getViewAreas()};
		
		this.viewers.set(viewer.getViewerName(), viewerObj);
		if (viewer.isVisible == true) {
			this.activeViewer = viewer.getViewerName();
		}
		
		// Give the viewer an instance of the controller
		viewer.setController(this);
	},
	
	makeVisible: function(viewerName) {
  		
		if (this.activeViewer != viewerName) {
			
			if (this.activeViewer) {
				var visibleViewer = this.viewers.get(this.activeViewer);
			
				// Hide the currently visible viewer.
				visibleViewer.viewAreas.mainViewArea.hide();
				
				if (visibleViewer.viewAreas.linkBarArea != null) {
				   visibleViewer.viewAreas.linkBarArea.hide();
				}
				
				// Let the currently visible viewer know it is not longer visible.
				visibleViewer.viewerInstance.setVisible(false);
			}
			
			// Show the new viewer
			var newViewer = this.viewers.get(viewerName);
			
			newViewer.viewAreas.mainViewArea.show();
			
			if (newViewer.viewAreas.linkBarArea != null) {
			   newViewer.viewAreas.linkBarArea.show();
			}
			
			newViewer.viewerInstance.setVisible(true);
			
			this.activeViewer = viewerName;
		}
	}
	
});

/**
 * Generic lightweight viewer that can be used for very simple viewers that only have 1
 * div to switch on and off.
 */
var GenericViewer = Class.create({
	/**
	 * The options variable is a generic object with the following properties:
	 * viewerName:  The unique name of this viewer.
	 * mainViewArea: For the controller: This is just the element to hide and show.
	 * initialVisibility: Whether the current viewer is initially visible or not.
	 * enableElement: The element that is clicked to enable this viewer.
	 * elementElementContainer: The container that the enable element is in.
	 * 		Used to hide the enable element if the mainViewArea doesn't exist.
	 * 
	 */
	initialize: function(options) {
		// The unique name of this viewer.
		this.viewerName = options.viewerName;
		
		// For the controller: This is just the element that will be shown and hidden.
		this.mainViewArea = options.mainViewArea;
		
		// Whether this viewer is initially visible or not.
		this.isVisible = options.initialVisibility;
		
		// The element that is clicked to enable this viewer.
		this.enableElement = options.enableElement;
		
		// The container that the enable element is in.
		// Used to hide the enable element if the mainViewArea doesn't exist.
		this.elementElementContainer = options.enableElementContainer;
		
		// There should be no controller at the moment, because it is added through setController.
		this.controller = null;
		
		// There is a chance this mainViewerArea may not exist so lets check if the mainViewArea is usable.
		if (this.mainViewArea) {
			// Setup the onclick event.
			Event.observe(this.enableElement, 'click', this.show.bindAsEventListener(this));
		}
		else {
			// Hide the enable element, if it exists. Because the main view area does not exist.
			if (this.elementElementContainer) {
				this.elementElementContainer.hide();
			}
		}
		
	},
	
	setController: function(objController) {
		this.controller = objController;
	},
	
	getViewAreas: function() {
		var areasObj = {mainViewArea: this.mainViewArea, linkBarArea: this.linkBarArea, alternateViewArea: this.alternateViewArea};
		return areasObj;
	},
	
	getViewerName: function() {
		return this.viewerName;
	},
	
	/**
	 * This is called by the controller to let the image viewer know of a visibility
	 * state change.
	 */
	setVisible: function(visibility) {
		this.isVisible = visibility;
	},
	
	/**
	 * When the enable element is clicked this method is called to request visibility.
	 */
	show: function(event) {
		if (this.isVisible == false) {
			// Ask controller to make us visible.
			this.controller.makeVisible(this.viewerName);
		}
	}
});
