// general namespace (NS) declaration
if (NS == null || typeof(NS) != "object") { var NS = new Object();}
// gallery namespace
if (NS.Gallery == null || typeof(NS.Gallery) != "object") { NS.Gallery = new Object();}

/*
 * controller class
 * This class is the entry point for the gallery component.
 * @id id of the object
 */
NS.Gallery.Controller = function (id) {
var DEPENDENCY_PREFIX = '/SlidingGallery//gallery/';
dep = new NS.Tools.Dependencies();
dep.add(DEPENDENCY_PREFIX + 'tools/Loader.js', 'NS.Tools.Loader');
dep.add(DEPENDENCY_PREFIX + 'tools/XmlLoader.js', 'NS.Tools.XMLLoader');
dep.add(DEPENDENCY_PREFIX + 'Constants.js', 'NS.Gallery.Constants');
dep.add(DEPENDENCY_PREFIX + 'PictureInfo.js', 'NS.Gallery.PictureInfo');
dep.add(DEPENDENCY_PREFIX + 'PictureStore.js', 'NS.Gallery.PictureStore');
dep.add(DEPENDENCY_PREFIX + 'Slider.js', 'NS.Gallery.Viewer');
dep.add(DEPENDENCY_PREFIX + 'Slideshow.js', 'NS.Gallery.Slideshow');
dep.add(DEPENDENCY_PREFIX + 'Viewer.js', 'NS.Gallery.Slider');

	var self = this;
	_id = id;

	var SUFFIX_MAINFRAME = '_gallery_frame';
	var SUFFIX_SLIDERFRAME = '_gallery_sliderframe';
	var SUFFIX_VIEWERFRAME = '_gallery_viewerframe';

	var _pictureStore;
	this.getPictureStore = function() {return _pictureStore;}
	// don't ask me why, but all the getters and setters are neccessary
	// it does not work without them
	var _currentPictureId = null;
	this.setCurrentPictureId = function(id) {_currentPictureId = _currentPictureId;}
	this.getCurrentPictureId = function() {return _currentPictureId;}

	var _slider = null;
	this.setSlider = function(slider) {this._slider = slider;}
	this.getSlider = function() {return this._slider;}

	var _viewer = null;
	this.setViewer = function(viewer) {this._viewer = viewer;}
	this.getViewer = function() {return this._viewer;}

	var slideshow = null;
	this.setSlideshow = function(slideshow) {this.slideshow = slideshow;}
	this.getSlideshow = function() {return this.slideshow;}


	/**
	 * build the HTML structure for the gallery
	 */
	var buildFrame = function() {
		var structure = '';
		structure += '<div id="' + _id + SUFFIX_MAINFRAME + '" class="frame galleryframe">';
		structure += '	<div id="' + _id + SUFFIX_SLIDERFRAME + '" class="frame sliderframe"></div>';
		structure += '	<div id="' + _id + SUFFIX_VIEWERFRAME + '" class="frame viewerframe"></div>';
		structure += NS.Gallery.Constants.TOOLTIP_HOWTOUSE;
		structure += '</div>';
		return structure;
	}

	/**
	 * view the picture with the passed Id
	 */
	this.viewPicture = function(id) {
		_currentPictureId = id;
		var picInfo = _pictureStore.getById(id);
		this.getSlider().selectPicture(id);
		this._viewer.loadPicture(picInfo);
	}
	
	/**
	 * parses the downloaded information and loads the gallery
	 */
	this.load = function(xml) {
		_pictureStore.parseXML(xml);
		self.setSlider(new NS.Gallery.Slider(_id + '._slider', _id));
		self._slider.render(_id + SUFFIX_SLIDERFRAME, _pictureStore.getAll());
		loadFirstPicture();
	}
	
	/**
	 * loads the first picture into the gallery
	 */
	var loadFirstPicture = function() {
		if (_pictureStore.getLength() > 0) {
			var info = _pictureStore.get(0);
			self.viewPicture(info.getId());
		}
	}
	
	/**
	 * required to save the insertpoint throughout several tries while all dependencies are loading
	 */
	var _insertpoint = null;
	/**
	 * render this module and appends it to the insertpoint
	 * @insertpointID ID of the insertpoint
	 */
	this.render = function(insertpointID) {
		if (_insertpoint == null) { _insertpoint = insertpointID; }
		// wait until all dependencies are met (all JS files are loaded)
		if(!dep.check()) { setTimeout(self.render, 200); }
		else {
			var insertpoint = document.getElementById(_insertpoint);
			insertpoint.innerHTML = NS.Gallery.Constants.TOOLTIP_LOADING;
			// load CSS
			NS.Tools.Loader.loadCSSfile(NS.Gallery.Constants.FILE_CSS);
			// initialize required objects
			_pictureStore = new NS.Gallery.PictureStore();
			self.setViewer(new NS.Gallery.Viewer(_id + '._viewer'));
			self.setSlideshow(new NS.Gallery.Slideshow(_id + '.slideshow', self));
			
			insertpoint.innerHTML = buildFrame();
			self._viewer.render(_id + SUFFIX_VIEWERFRAME);
			self.slideshow.render(_id + SUFFIX_VIEWERFRAME);
		
			NS.Tools.XMLLoader.importXML(self.load, NS.Gallery.Constants.FILE_PICTUREINFO);
		}
	}
	
}

