/* Copyright 2009 Palm, Inc.  All rights reserved. */
/*global Mojo, Class, $L */
/*jslint rhino:true, undef:true */

var PowerPointAssistant = Class.create({

	g_nTimePerSlide: 2500,
	pptScrollTop: 168,
	pptSize: 50,
	pptCmdMenu: [
		{},
		{ items: [
			{ icon:'back', command:'prev-cmd' },
			{ template: 'document/command_bar', command: 'edit-cmd'},
			{ icon:'forward', command:'next-cmd' }
		]},
		{}
	],

	initialize: function(docAssistant) {
		this.docAssistant = docAssistant;
		this.controller = docAssistant.controller;
		this.nextSlide = this.nextSlide.bind(this);
		this.invalidateScreen = this.invalidateScreen.bind(this);
		this.slide_to = undefined;
		this.invalidate = undefined;
	},

    orientationChanged: function(orientation) {
        Mojo.Log.info("PPT: orientation changed to: " + orientation);

        this.controller.serviceRequest( 'palm://com.palm.office/ppt/fitWideMode',
            {
                parameters: {   docid: this.docAssistant.document_id,
                                orientation: orientation },

                onSuccess: function(payload) {

                    if (payload.errorCode !== undefined || payload.returnValue !== true) {
                        Mojo.Log.error("fitWideMode failed: " + Object.toJSON( payload ) );
                        return;
                    }
                }.bind(this),

                onFailure: function(payload) {
                    Mojo.Log.error("fitWideMode failed: " + Object.toJSON( payload ) );
                }
            });
    },

	docLoaded: function() {
		this.controller.serviceRequest( 'palm://com.palm.office/ppt/getSlideCount',
        {
            parameters: { docid: this.docAssistant.document_id },

            onSuccess: function(payload) {
                if (payload.errorCode !== undefined || payload.slidecnt === undefined) {
                    Mojo.Log.error("getSlideCount failed to get slide count: " + Object.toJSON( payload ) );
                    return;
                }

				this.docAssistant.setPageCount(payload.slidecnt);
                this.docAssistant.updatePageFootOf();
                this.doNotes();
            }.bind(this),

            onFailure: function(payload) {
                Mojo.Log.error("getSlideCount failed: %s", Object.toJSON( payload ) );
            }

        });
	},

	getCommandMenu: function() {
		return this.pptCmdMenu;
	},

	invalidateScreen: function() {
		this.docAssistant.adapter.invalidateScreen();

		if (this.docAssistant.notesShowing) {
		   this.getPageNote();
		}

		if (this.docAssistant.slideShowRunning) {
			if (this.docAssistant.getCurrentPage() != this.docAssistant.getPageCount() - 1) {
				this.slide_to = this.controller.window.setTimeout( this.nextSlide, this.g_nTimePerSlide);
			}
			//else
				//this.stopSlideShow();
		}
	},


	/********************
	 **** SLIDE SHOW ****
	 *******************/
	nextSlide: function() {
		// If we are in slide show mode then clear the timeouts
		if (this.docAssistant.slideShowRunning)
		{
			this.controller.window.clearTimeout(this.slide_to);
			this.controller.window.clearTimeout(this.invalidate);
		}

		this.gotoPage(this.docAssistant.getCurrentPage() + 1);
	},


	startSlideShow: function() {

		// The slide show is at the end... Lets restart it
		if (this.docAssistant.getCurrentPage() == this.docAssistant.getPageCount() - 1) {
			this.docAssistant.setCurrentPage(-1);
		}

		// Notes are running we need to turn them off
		if (this.docAssistant.notesShowing) {
			this.docAssistant.notesShowing = !this.docAssistant.notesShowing;
			this.doNotes();
		}

		this.controller.serviceRequest( 'palm://com.palm.office/ppt/fitWideMode',
			{
				parameters: { docid: this.docAssistant.document_id },

				onSuccess: function(payload) {

					if (payload.errorCode !== undefined || payload.returnValue !== true) {
						Mojo.Log.error("fitWideMode failed: " + Object.toJSON( payload ) );
						return;
					}

					this.slide_to = this.controller.window.setTimeout( this.nextSlide, this.g_nTimePerSlide);

					this.docAssistant.slideShowRunning = true;
					this.docAssistant.setMenuCheck('ppt-play', this.docAssistant.slideShowRunning);
				}.bind(this),

				onFailure: function(payload) {
					Mojo.Log.error("fitWideMode failed: " + Object.toJSON( payload ) );
				}
			});
	},


	stopSlideShow: function() {

		this.controller.serviceRequest( 'palm://com.palm.office/ppt/fitWideMode',
			{
				parameters: { docid: this.docAssistant.document_id },

				onSuccess: function(payload) {
					if (payload.errorCode !== undefined || payload.returnValue !== true) {
						Mojo.Log.error("fitWideMode failed: " + Object.toJSON( payload ) );
						return;
					}

					this.controller.window.clearTimeout(this.slide_to);
					this.controller.window.clearTimeout(this.invalidate);
					this.docAssistant.slideShowRunning = false;
					this.doNotes();
					this.docAssistant.showMenuBars();
					this.docAssistant.setMenuCheck('ppt-play', this.docAssistant.slideShowRunning);

				}.bind(this),

				onFailure: function(payload)
				{
					Mojo.Log.error("fitWideMode failed: " + Object.toJSON( payload ) );
				}
			});
	},


	gotoPage: function( nSlide ) {

		if ( nSlide != this.docAssistant.getCurrentPage() ) {

			this.controller.serviceRequest( 'palm://com.palm.office/ppt/jumpToSlide',
				{
					parameters: { docid: this.docAssistant.document_id, slide:  nSlide },

					onSuccess: function(payload)
					{
						if (payload.errorCode !== undefined || payload.slideNumber === undefined) {
							Mojo.Log.error("gotoPage failed to get slide number: %s", Object.toJSON( payload ) );
							return;
						}

						this.docAssistant.setCurrentPage(payload.slideNumber);
						this.docAssistant.updatePageFootOf();
						this.invalidate = this.controller.window.setTimeout( this.invalidateScreen, 500);
					}.bind(this),

					onFailure: function(payload) {
						Mojo.Log.error("gotoPage failed: %s", Object.toJSON( payload ) );
					}
				});

		}
	},


	getPageNote: function()
	{
		this.controller.serviceRequest( 'palm://com.palm.office/ppt/getSlideNote',
			{
				parameters: { docid: this.docAssistant.document_id },

				onSuccess: function(payload) {
					if (payload.errorCode !== undefined || payload.note === undefined) {
						Mojo.Log.error("getSlideNote failed to get note: " + Object.toJSON( payload ) );
						return;
					}

					this.controller.get('noteScroll').mojo.revealTop(0);
					this.controller.get('noteText').innerHTML = payload.note;

					this.docAssistant.showElement('pptNotes');

					// Make sure we set the height to the right value
					if (this.docAssistant.viewMenuVisible) {
						this.controller.get('noteScroll').style.height = this.pptScrollTop + 'px';
					}
					else {
						this.controller.get('noteScroll').style.height = this.pptScrollTop + this.pptSize+'px';
					}
				}.bind(this),

				onFailure: function(payload) {
					Mojo.Log.error("getSlideNote failed: " + Object.toJSON( payload ) );
				}
			});
	},


	doNotes: function() {

		Mojo.Log.info("doNotes notesShowing = %d", this.docAssistant.notesShowing);

		this.docAssistant.setMenuCheck('ppt-notes', this.docAssistant.notesShowing);

		if (this.docAssistant.notesShowing) {
			this.getPageNote();
		}
		else {
			this.docAssistant.hideElement('pptNotes');
		}
	},


	showPPTOutline: function() {

		this.controller.serviceRequest( 'palm://com.palm.office/ppt/getSlideList',
			{
				parameters: { docid: this.docAssistant.document_id },

				onSuccess: function(payload) {
					if (payload.errorCode !== undefined || payload.list === undefined) {
						Mojo.Log.error("getSlideList failed to get note: " + Object.toJSON( payload ) );
						return;
					}

					this.controller.stageController.pushScene('outline', this.docAssistant, payload.list);
				}.bind(this),

				onFailure: function(payload) {
					Mojo.Log.error("getSlideList failed: " + Object.toJSON( payload ) );
				}
			});
	},


	showSlideText: function() {

		this.controller.stageController.pushScene('slidetext', this.docAssistant);
	}
});

