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

var VideoAssistant = Class.create({
	
    initialize: function(vid, swapForHomeScene){
		Mojo.Log.info("Youtube: video-assistant initialize swapForHomeScene: %s", swapForHomeScene);
        this.video = vid;
		this.swapHomeSceneOnBack = swapForHomeScene;
    },
    
    setup: function() {
		// setup application menu
		this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, AppAssistant.appMenuModel);
		
		// setup bottom menus
		this.cmdMenuModel = {items: [{label: $L('More'), submenu:'more-menu'}, 
									{label: $L('Share'), submenu:'share-menu'}]};
									
		this.controller.setupWidget('more-menu', undefined, this.moreMenuModel);
		this.controller.setupWidget('share-menu', undefined, this.shareMenuModel);
		this.controller.setupWidget(Mojo.Menu.commandMenu, {menuClass: "no-fade"}, this.cmdMenuModel);
		
		// render video details template
        var info = Mojo.View.render({object: this.video, template: 'video/video-info'});
        $('video_info').update(info);
		
		$('play_video').observe(Mojo.Event.tap, 
			function(event){
				AppAssistant.common.playVideo(this.video);
			}.bind(this));
    },
	
	activate: function() {
		// in case of cross app launch we want to focus the app only after 
		// search scene is ready to go. This is so that user won't see a
		// home scene flash before we push the search scene
		// "Home scene flash" happens only if app was already running when 
		// it received the cross app launch request.
		Mojo.Log.info("Youtube: video activate: activate stage controller");
		this.controller.stageController.activate();
	},
	
	handleCommand: function(event) {
		if (event.type == Mojo.Event.back && this.swapHomeSceneOnBack == true) {
			Mojo.Log.info("Youtube: video cleanup: swap scene for scene HOME");
			event.stop();
			this.controller.stageController.swapScene("home");
		}
		else if (event.type == Mojo.Event.command) {
			if (event.command == 'more') {
				if (this.video.author && this.video.author != "") {
					Mojo.Controller.stageController.pushScene("search", this.video.author, "author");
				}
				event.stop(event);
			}
			else if (event.command == 'related') {
				if (this.video.related_url && this.video.related_url != "") {
					Mojo.Controller.stageController.pushScene("search", this.video.related_url, "related");
				}
				event.stop(event);
			}
			else if (event.command == 'email' && this.video.shareLink && this.video.shareLink != "") {
				this.controller.serviceRequest('luna://com.palm.applicationManager', {
				 		method: 'open',
				 		parameters: {
							id: 'com.palm.app.email',
							params: {text: this.video.shareLink}
						}});
				event.stop(event);
			}
			else if (event.command == 'msg' && this.video.shareLink && this.video.shareLink != "") {
				this.controller.serviceRequest('luna://com.palm.applicationManager', {	
						method: 'open',
						parameters: {
							id: 'com.palm.app.messaging',
							params: {messageText: this.video.shareLink}
						}})
				event.stop(event);
			}	
			else {
				AppAssistant.common.handleCommand(event);	
			}
		}
	},
    
	moreMenuModel: {label: $L('More'), 
					items: [
						{label: $L('More from This Author'), command:'more'}, 
						{label: $L('Related Videos'), command:'related' }
					]},
					
	shareMenuModel: {label: $L('Share'), 
					items: [
						{label: $L('Email'), command:'email'}, 
						{label: $L('Text Message'), command:'msg' }
					]},
	
});
