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

PageControls = Class.create({

	initialize: function(controller) {

		this._controller = controller;

		// We can always go back.
		this._back = true;
		this._forward = false;

		this._backModel = {
			disabled: !this._back,
			label: MenuData.NavigationMenu.Back.label,
			icon: MenuData.NavigationMenu.Back.icon,
			command: MenuData.NavigationMenu.Back.command
		};

		this._forwardModel = {
			disabled: !this._forward,
			label: MenuData.NavigationMenu.Forward.label,
			icon: MenuData.NavigationMenu.Forward.icon,
			command: MenuData.NavigationMenu.Forward.command
		};

		this._model = {
			visible: true,
			items: [this._backModel, {}, {}]
		};

		this._currentPageModel = {};
	},

	setup: function(withIdle) {

		if (withIdle) {

			var idleModel = {
				type: 'idle',
				label: MenuData.NavigationMenu.Reload.label,
				icon: MenuData.NavigationMenu.Reload.icon,
				command: MenuData.NavigationMenu.Reload.command
			};

			this._model.items.push(idleModel);
		}

		this._controller.setupWidget(Mojo.Menu.commandMenu, {menuClass: 'no-fade'}, this._model);
	},

	cleanup: function() {

		if (this._animation) {
			this._animation.stop();
			delete this._animation;
		}
	},

	_setBackForward: function(back, forward) {

		var items = $A();
		this._backModel.disabled = !back;
		items.push(this._backModel);

		if (forward) {
			this._forwardModel.disabled = !forward;
			items.push(this._forwardModel);
		} else {
			items.push({});
		}

		// The spacer... right aligns the following button.
		items.push({});

		this._back = back;
		this._forward = forward;

		return items;
	},

	updateBackForward: function(back, forward) {

		// Add the back/forward buttons
		this._model.items = this._setBackForward(back, forward);
		// The spacer... right aligns the following button.
		this._model.items.push(this._currentPageModel);

		this._controller.modelChanged(this._model);
	},

	showIdle: function() {

		//Mojo.Log.info("PageControls#showIdle()");
		var idleModel = {
			type: 'idle',
			label: MenuData.NavigationMenu.Reload.label,
			icon: MenuData.NavigationMenu.Reload.icon,
			command: MenuData.NavigationMenu.Reload.command
		};

		if (this._animation) {
			this._animation.stop();
			delete this._animation;
		}

		// Add the back/forward buttons
		this._model.items = this._setBackForward(this._back, this._forward);
		// The spacer... right aligns the following button.
		this._model.items.push(idleModel);
		this._controller.modelChanged(this._model);

		this._currentPageModel = idleModel;
	},

	showProgress: function() {

		try {

			//Mojo.Log.info("PageControls#showProgress()");
			var progressModel = {
				type: 'progress',
				label: MenuData.NavigationMenu.Stop.label,
				icon: MenuData.NavigationMenu.Stop.icon,
				command: MenuData.NavigationMenu.Stop.command
			};

			if (this._animation) {
				this._animation.stop();
			}

			this._animation = new PageProgressAnimation({
				controller: this._controller,
				model: progressModel
			});

			// Add the back/forward buttons
			this._model.items = this._setBackForward(this._back, this._forward);
			this._model.items.push(progressModel);
			this._controller.modelChanged(this._model);
			this._animation.start(0);

			this._currentPageModel = progressModel;

		} catch (e) {
			Mojo.Log.logException(e, "#showProgress()");
		}
	},

	updateProgress: function(progress) {

		try {
			if (this._currentPageModel.type === 'progress') {
				this._animation.setProgress(progress);
			}
		} catch (e) {
			Mojo.Log.logExcepton(e, '#updateProgress()');
		}
	},

	showSearch: function() {

		try {

			//Mojo.Log.info("PageControls#showSearch()");
			var searchModel = {
				type: 'search',
				label: MenuData.NavigationMenu.Search.label,
				icon: MenuData.NavigationMenu.Search.icon,
				command: MenuData.NavigationMenu.Search.command
			};

			if (this._animation) {
				this._animation.stop();
			}

			this._animation = new PageSearchAnimation({
				controller: this._controller,
				model: searchModel
			});

			// Add the back/forward buttons
			this._model.items = this._setBackForward(this._back, this._forward);
			this._model.items.push(searchModel);
			this._controller.modelChanged(this._model);
			this._animation.start();

			this._currentPageModel = searchModel;

		} catch (e) {
			Mojo.Log.logException(e, '#showSearch()');
		}
	},

	setVisible: function(visible) {
		this._controller.setMenuVisible(Mojo.Menu.commandMenu, visible);
	}
});
