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

ImageviewAssistant = Class.create({
	initialize: function(imagePath) {
		this.imagePath = imagePath;
	},

	setup: function() {
		var dimensions = {
			'width': this.controller.window.innerWidth + "px",
			'height': this.controller.window.innerHeight + "px"
		};
		this.controller.get('imageViewWidget').setStyle(dimensions);
		var model = {
			index: 0,
			background: "black"
		};
		this.controller.setupWidget('imageViewWidget', {}, model);

		this.controller.listen('imageViewSave', Mojo.Event.tap, this.save.bind(this));
	},

	ready: function() {
		this.controller.get('imageViewWidget').mojo.centerUrlProvided(this.imagePath,this.imagePath);
	},

	save: function() {
		var filePath = this.imagePath.strip();
    	if (filePath.indexOf("file://") === 0) {
			filePath = filePath.substring(7);
		}

		var saveButton = this.controller.get('imageViewSave');
		if (saveButton) {
			saveButton.update($L("Copying to Photos..."));
		}

		Message.saveAttachment(this.controller, filePath, this.saveSuccess.bind(this), this.saveFailure.bind(this));
	},

	saveSuccess: function() {
		var popScene = function() { this.controller.stageController.popScene(); }.bind(this);
		popScene.delay(1.5);
	},

	saveFailure: function() {
		var saveButton = this.controller.get('imageViewSave');
		if (saveButton) {
			saveButton.update($L("Copy to Photos"));
		}

		this.simpleAlert($L('Error saving photo'), $L('There was an error saving this photo to mass storage'));		
	},

	simpleAlert: function(title,message) {
		this.controller.showAlertDialog({
			onChoose: function(){},
			title: title,
			message: message,
			preventCancel: false,
			choices: [{
				label: $L('OK'),
				value: 'Ok'
			}]
		});			
	}
});
