
// extend Apple Core's slider class
var slider = Class.create();
Object.extend(Object.extend(slider.prototype, AC.Slider.prototype), {

	// get's called in initialize upon construction
	populate: function() {
		var items = document.getElementsByClassName('slideritem');
		for (var i=0; i<items.length; i++) {
			var sliderItem = new AC.SliderItem(items[i]);
			this.items.push(sliderItem);
		}

		this.render(3);
	},

	renderPlaceholderItems: function(start, finish) {
		var list = [];
		for (var i=start; i<finish; i++) {
			var listItem = this.items[i].render().cloneNode(true);
			listItem = $(listItem);

			// retain unique ids
			var garbage = listItem.select('.content');
			garbage.each(Element.remove);

			listItem.addClassName('cloned');
			list.push(listItem);
		}
		return list;
	}

});

// extend the view master
Object.extend(AC.ViewMaster.Viewer.prototype, {
	previouslyActive: null,
	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, element);
	},
	
	show: function(section, selector) {
		if(selector) {
			var profileTitle = selector.down('h4').innerHTML.stripTags();
			AC.ViewMaster.Section.prototype.profileTitle = profileTitle;
			AC.Tracking.trackPage({
				pageName: 'Apple - Science - '+ profileTitle,				
				prop6: 'Apple - Science - '+ profileTitle,	
				prop13: 'Apple - Science - '+ profileTitle
			});			
		} 
		
		if(selector) {
			selector.addClassName('active');
			if(this.previouslyActive) this.previouslyActive.removeClassName('active');
		}
		if (this._locked || section == this.currentSection) {
			return;
		}

		this._locked = true;
		this.previousSection = this.currentSection;
		this.currentSection = section;
		this.view.setContent(section);

		if (typeof this.options.ensureInView === "boolean" && this.options.ensureInView) {
			if (this._didShowInitial) {

				var yOffset = section.content.viewportOffset()[1];
				//if the content is above viewport to pretty far down the page bring it into view
				if (yOffset < 0 || yOffset > (document.viewport.getHeight() * .75)) {
					new Effect.ScrollTo(section.content, {duration: 0.3});
				}
			} else {
				//ensure we're at the top of the page when the page has 
				//'loaded' otherwise a requested anchor is followed and the 
				//page may have started where the element was prior to styling
				$(document.body).scrollTo();
			}
		}
	}
});

Object.extend(AC.ViewMaster.Section.prototype, {
	didShow: function(viewer) {
		var needsController = this.hasMovie() && !this.isMobile;
		if (needsController) {
			this._movieController = new AC.QuicktimeController();
			this.controllerPanel.innerHTML = '';
			this.controllerPanel.appendChild(this._movieController.render());
		}
		this._playMovie();
		if (needsController) {
			this._onMoviePlayable = this._movieController.monitorMovie.bind(this._movieController);
			this._onMovieFinished = this.didFinishMovie.bind(this);
			this._movieController.attachToMovie(this.movie, {
				onMoviePlayable: function() {
					this._movieController.monitorMovie();
				}.bind(this),
				onMovieFinished: function() {
					var movieName = 'V@E: Science - '+ this.profileTitle;
					AC.Tracking.trackClick({
						prop13: movieName
					}, this, 'o', movieName);
					this.didFinishMovie();
				}.bind(this)
			});
		}
	},
	
	replayMovie: function() {
		this._playMovie();		
		this._movieController.attachToMovie(this.movie, {
			onMoviePlayable: function() {
				var movieName = 'V@R: Science - '+ this.profileTitle;
				AC.Tracking.trackPage({
					pageName: movieName,
					prop4: this.movieLink.href,
					prop6: movieName,
					prop13: movieName
				});
				this._movieController.monitorMovie();
			}.bind(this),
			onMovieFinished: function() {
				var movieName = 'V@E: Science - '+ this.profileTitle;
				AC.Tracking.trackClick({
					prop13: movieName
				}, this, 'o', movieName);
				this.didFinishMovie();
			}.bind(this)
		});
	}
});

