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

Photos.Common.Feedback = {};
Photos.Common.feedbackItems = {};
Photos.Common.Feedback.DoneDelay = 1.0;
Photos.Common.Feedback.ShowDelay = 0.5;
Photos.Common.Feedback.HideDelay = 0.5;

Photos.Common.newFeedback = function(controller, element, text, image, templatePrefix) {
	var feedback = {
		text: text,
		image: image,
		container: element,
		controller: controller,
	};
	Mojo.Log.info("New feedback:", text);
	Photos.Common._buildFeedbackItemUI(element, feedback, templatePrefix);
	var key = feedback.element.identify();
	Mojo.Log.info("Done building feedbackUI:", key);
	Photos.Common.feedbackItems[key] = feedback;

	Mojo.Animation.animateStyle(feedback.element, 'height', 'bezier', {
		from: 48,
		to: 1,
		duration: Photos.Common.Feedback.ShowDelay,
		curve: 'over-easy',
		reverse: true
	});

	return key;
};

Photos.Common.deleteFeedback = function(key, text) {
	var feedback = Photos.Common.feedbackItems[key];
	if (feedback === undefined) {
		return false;
	}

	feedback.complete = true;
	feedback.textContainerElement.update(text);
	feedback.spinnerElement.mojo.stop();
	feedback.checkmarkElement.style.display = "";
	Photos.Common._feedbackFadeOff.delay(
		Photos.Common.Feedback.DoneDelay,
		feedback.element, feedback.contentElement);
	delete Photos.Common.feedbackItems[key];
	return true;
};

Photos.Common._feedbackFadeDrop = function(element) {
	element.down().lastValue = 9;

	Mojo.Animation.animateValue(
		Mojo.Animation.queueForElement(element),
		'linear',
		function (value) {
			this.removeClassName("frame" + this.lastValue);
			this.lastValue = Math.round(value);
			this.addClassName("frame" + this.lastValue);
		}.bind(element.down()),
		{
			reverse: false,
			from: 9,
			to: 1,
			duration: Photos.Common.Feedback.HideDelay
		});

	Mojo.Animation.animateStyle(element, 'height', 'bezier', {
		from: 48,
		to: 1,
		duration: Photos.Common.Feedback.HideDelay,
		curve: 'over-easy',
		reverse: false,
		onComplete: function(){this.remove();}.bind(element)
	});
};

Photos.Common._feedbackFadeOff = function(element) {
	element.down().lastValue = 9;
	Mojo.Animation.animateValue(
		Mojo.Animation.queueForElement(element),
		'linear',
		function (value) {
			this.style.opacity = value / 100.0;
		}.bind(element.down()),
		{
			reverse: false,
			from: 100,
			to: 0,
			duration: Photos.Common.Feedback.HideDelay,
			onComplete: function() {
				this.setStyle({marginLeft: "500px"});
				Mojo.Animation.animateStyle(
				this, 'height', 'bezier', {
					from: 48,
					to: 1,
					duration: Photos.Common.Feedback.HideDelay,
					curve: 'over-easy',
					reverse: false,
					onComplete: function(){this.remove();}.bind(this)
				});
			}.bind(element)
	});
};

Photos.Common._feedbackSlideOff = function(element, contentElement) {
	Mojo.Animation.animateStyle(element, 'margin-left', 'bezier', {
		from: 0,
		to: 500,
		duration: Photos.Common.Feedback.HideDelay,
		curve: 'over-easy',
		reverse: false,
		onComplete: function(element) {
			// hide the content so positioning works right
			contentElement.style.display = "none";

			// then animate the main div down a size.
			Mojo.Animation.animateStyle(element, 'height', 'bezier',
			{
				from: 48,
				to: 1,
				duration: Photos.Common.Feedback.HideDelay,
				curve: 'over-easy',
				reverse: false,
				onComplete: function(element){element.remove();}
			});
		}
	});
};

Photos.Common._buildFeedbackItemUI = function(parent, feedback, templatePrefix) {
	var child = feedback.controller.document.createElement('div');
	child.style.height = "1px";
	child.update(Mojo.View.render({
		template: templatePrefix + "fullscreen/feedback",
		object: feedback
	}));
	
	feedback.element = child;
	feedback.spinnerElement = child.down(".feedback-spinner");
	feedback.contentElement = child.down(".feedback-content");
	feedback.checkmarkElement = child.down(".feedback-checkmark");
	feedback.textContainerElement = child.down(".feedback-filename");

	feedback.controller.setupWidget(feedback.spinnerElement.identify(),
			{spinnerSize: "small"}, {spinning: true});

	feedback.controller.instantiateChildWidgets(feedback.element);
	feedback.controller.showWidgetContainer(feedback.element);

	parent.appendChild(child);
};

