// FeatureViewer
var FeatureViewer = Class.create();
Object.extend(Object.extend(FeatureViewer.prototype, AC.ViewMaster.Viewer.prototype), {	
	initialize: function(contents, view, triggerClassName, options) {
		this.triggerClassName = triggerClassName;
		this.sections = $H();
		this.orderedSections = [];
		this.contents = contents;
		
		this.options = options || {};
		
		var initialSection = null;
		if(contents) {
			for (var i = 0; i < contents.length; i++) {
				//contents could be a NodeList, so we're going to use that API
				//I added an item method to Array in apple_core
				section = this.addSection(contents.item(i))
			
				if (!initialSection) {
					initialSection = section;
				}
			}
		}
		//Moved down to workaround a bug: in Safari, the results of getElementsByClassName is a NodeList.
		//If we do new AC.SwapView(view) before looping on the NodeList, the NodeList get emptied....
		this.view = new AC.SwapView(view);
		this.view.setDelegate(this);

		var hashInitialId = document.location.hash.replace(/#/, ''), hashSection;
		var initialFirstId = (hashInitialId.length > 0) ? hashInitialId.split('_')[0] : this.options.initialId;
		hashInitialId = initialFirstId;
		
		if(hashInitialId !== this.view._viewId) {
            hashSection = this.initialSectionFromId(hashInitialId);
        }
		if(hashSection) initialSection = hashSection;
		
		if(!initialSection && typeof this.options.initialId === "string" && this.options.initialId.length > 0) {
			initialSection = this.initialSectionFromId(this.options.initialId);
		}

		//TODO do we want to show the initial section right away? seems like we have to but if no delegates are set yet this will be a bit different than subsequent calls to show
		this.show(initialSection);
		
		Event.observe(document, 'click', this._triggerClicked.bindAsEventListener(this));
		//In IE click event isn't sent when there is no text/image physically under the mouse, but the mouseup is, so we need to listen to that
		if(AC.Detector.isIEStrict()) {
			Event.observe(document, 'mouseup', this._triggerClicked.bindAsEventListener(this));
		}
	},
	
	triggerClicked: function(evt, element) {
		var section = null;	
		if (this.options.silentTriggers) {
			Event.stop(evt);
		}
	
		if (!!element.href.match(/#previous/)) {
			section = this.getPreviousSection();
		} else if (!!element.href.match(/#next/)) {
			section = this.getNextSection();
		} else {
			var matches = element.href.match(/#(.*)$/);
			if(matches) {
				var contentId = matches[1];
			}
			else {
				contentId = element.name;
			}
			section = this.sections.get(contentId);
		}
		
		//No section means either a lazy initialization of sections
		//or a section for which the content is remote.
		if(!section) {
			section = this.addSection(element);
		}
		
		if(section.isContentRemote() && !section.isContentLoaded()) {
			Event.stop(evt);
		}
		
		//stop if the trigger is trying to open the current section
		if (section == this.currentSection) {
			Event.stop(evt);
			return;
		} else if (!section) {
			return;
		}
		
		this._didShowInitial = true;
		this.show(section);
		
		if(typeof this.options.subViewer === "object") {
			var subSectionFirstTopic = $$('#MASKED-'+contentId+' div ul li > a')[0];
			this.options.subViewer._categoryChanged(subSectionFirstTopic);
		}

	}
});
