/* Copyright 2009 Palm, Inc.  All rights reserved. */


// Builds on the Base scene and gives me a scroller object, with
// automatic pausing and such.

Photos.Common.scrollerThreshold = 3;

Photos.Scene.Scroll = Class.create(Photos.Scene.Base, {
	setup: function($super, listElement, attributes, offset) {
		$super();
		this.offset = offset || 0;
		this.rowRequester = new Photos.Common.RowRequester(
			this.controller.window);
		this.controller.setupWidget(listElement.id, attributes);
		this.rowHeight = attributes.initialAverageRowHeight;
		this.mainListElement = listElement;
		this.autoListen(Mojo.View.getScrollerForElement(listElement),
			Mojo.Event.scrollStarting,
			this.addScrollListener.bind(this), false);
		this.autoListen(this.mainListElement,
			Mojo.Event.listTap, this.handleSelection.bind(this));
	},

	cleanup: function($super) {
		$super();
		this.rowRequester.cleanup();
	},

	pause: function() {
		this.rowRequester.pause();
		if (this.thumbLoader) {
			this.thumbLoader.pause();
		}
	},

	resume: function(row) {
		this.rowRequester.resume(row);
		if (this.thumbLoader) {
			this.thumbLoader.resume(row);
		}
	},

	activate: function($super) {
		$super();
		this.resume();
	},

	deactivate: function() {
		this.pause();
	},

	imageDone: function(holder, image, complete) {
		image.onload = image.onerror = null;
		complete();
	},

	retryImageNoThumbnail: function(holder, image, path, complete) {
		Mojo.Log.info("Trying to load fallback:", path);
		if (Photos.Common.isMediaSynced(this.currentUrl)) {
			// there is no retry.
			image.src = this.params.applicationRoot +
						'images/broken-image.png';
		} else if (path.startsWith("/")) {
			image.onerror = this.useBrokenImage.bind(
					this, holder, image, complete);
			image.src = Photos.Common.extractfsPath + path +
						":0:0" + this.extractfsParams;
		} else if (path.startsWith("http://")) {
			Mojo.Log.error("HTTP fallback:", path);
			// A retry for http images, request could have timed out
			image.onerror = this.useUnknownImage.bind(
					this, holder, image, complete);
			image.src = '';
			image.src = path;
		}
	},

	useUnknownImage: function(holder, image, complete) {
		Mojo.Log.info("Loading fallback:", image.src);
		image.onerror = this.giveUpOnImage.bind(
						this, holder, image, complete);
		image.src = this.params.applicationRoot +
						'images/question-image.png';
	},

	useBrokenImage: function(holder, image, complete) {
		Mojo.Log.info("Loading fallback:", image.src);
		image.onerror = this.giveUpOnImage.bind(
						this, holder, image, complete);
		image.src = this.params.applicationRoot +
						'images/broken-image.png';
	},

	giveUpOnImage: function(holder, image, complete) {
		Mojo.Log.error("Gave up on image:", image.src);
		this.imageDone(holder, image, complete);
	},

	addScrollListener: function(event) {
		Mojo.Log.info("Scrolling started.");
		event.scroller.addListener(this);
		this.rowRequester.pause();
	},

	moved: function(done, position) {
		if (done || (Math.abs(this.lastPositionY - position.y) <
					Photos.Common.scrollerThreshold)) {
			this.resume(Math.floor(
				((this.controller.window.innerHeight / 2) -
				(position.y + this.offset)) / this.rowHeight));
		} else {
			this.pause();
		}
		this.lastPositionY = position.y;
	}
});

