var currentVideoView, previousSectionId;
function limitToSingleVideo(view, outgoing, incoming) {
	if (incoming.id.match(/video$/)) {
		if (currentVideoView) {
			currentVideoView.selectSectionWithIdEvent(previousSectionId);
		}
		previousSectionId = outgoing.id;
		currentVideoView = view;
	}
}

var delegate = {
	willShow: limitToSingleVideo,
	didAppendContent: function(view, content) {
		this.view = view;
		var section = view.currentSection;
		if (section.movieLink) {
			if (!section.close) {
				section.close = new Element('a', { className:'close '+view.triggerClassName, href:'#' }).insert('关闭');
				section.content.insertBefore(section.close, section.content.firstChild);
			}
			var previous = (view.previousSection) ? view.previousSection.id : view.orderedSections[0];
			section.close.href = '#'+previous;
		}
		if (view.viewMasterId == 'gallery-container') {
			var container = view.view.view().parentNode;
			var newHeight = section.content.getHeight();
			if (AC.Detector.isFirefox()) {
				newHeight += 20;
			}
			if (AC.Detector.isWebKit() && !AC.Detector.isiPhone()) {
				newHeight += 10;
			}
			var currentHeight = $(container).getHeight();
			var percent = (newHeight / currentHeight) * 100;
			new Effect.Scale(container, percent, { scaleX:false, scaleContent:false, duration:view.options.animationDuration });
		}
	},
	didShow: function(view, outgoing, incoming) {
		this.didAppendContent(view, incoming.content);
		this.didShow = function() {};
	}
};

Event.onDOMReady(function() {
	// set up the view masters

	//MobileVrOptions = { baseName: '/ipodtouch/gallery/images/360/20090909_' };

	var hero = new AC.ViewMaster.Viewer($$('a.herolink'), 'hero', 'herolink', { silentTriggers:true });
	hero.setDelegate(Object.extend(delegate, {
		willShow: function(view, outgoing, incoming) {
			var show = incoming.id.match(/^hero-\d$/) ? true : false;
			limitToSingleVideo(view, outgoing, incoming);
		}
	}));
	//hero.setDelegate(MobileVrDelegate);
	
	
	var mouse = new AC.ViewMaster.Viewer($$('.gallery-mouse-sections'), 'gallery-mouse', 'gallery-mouse', { silentTriggers:true, animationDuration:.4 });
	mouse.viewMasterId = 'gallery-container';
	mouse.setDelegate(delegate);
	mouse.didAppendContent(mouse);
	var tracker = new AC.ViewMaster.Tracker('click');
});

