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

var Common = Class.create ({
	
	// handle tap on a single row in videos list
	videoSelected: function(event) {
		Mojo.Log.info("Youtube: common videoSelected");
		var targetRow = $(event.originalEvent.target);
		if (targetRow != undefined) {
			if (targetRow.hasClassName('list-item-thumbnail') || targetRow.hasClassName('list-item-thumbnail-frame')) {
				Mojo.Log.info("Youtube: common videoSelected PLAY");
				this.playVideo(event.item);
			}
			else {
				Mojo.Log.info("Youtube: common videoSelected SHOW DETAILS");
				// show details scene
				Mojo.Controller.stageController.pushScene('video', event.item);
			}
		}
    }, 
	
	playVideo: function(video) {
		if (video.link == "") {
			// if video has no "playable" link, don't try to play it 
			// and don't add it to history
			Mojo.Log.info("Youtube: common playVideo: Video link is empty: %s", video.title);
			Mojo.Controller.stageController.topScene().showAlertDialog({
				onChoose: function(){},
				message: $L("This is a restricted video. It can only be played in your computer’s web browser."),
				choices: [{label: $L("OK"), value: "ok"}]
			});
			return;
		}
		
		// add this video to history
		AppAssistant.history.insert(video);
	
		var params = {};
		params.target = video.link;
		params.title = video.title;
		params.thumbUrl = video.thumbnail_240_320;

		var VideoLibrary = MojoLoader.require({
			name: "metascene.videos",
			version: "1.0"
		})["metascene.videos"];

		Mojo.Log.info("Youtube: common playVideo params: %j", params);

		Mojo.Controller.stageController.pushScene({
			name: VideoLibrary.Nowplaying.Name,
			assistantConstructor: VideoLibrary.Nowplaying.Assistant,
			sceneTemplate: VideoLibrary.Nowplaying.Template
			}, params);
	},
	
	handleCommand: function(event) {
		if (event.type == Mojo.Event.command && event.command == "help") {
			Mojo.Controller.stageController.topScene().serviceRequest(
				"palm://com.palm.applicationManager", {
				method: "open",
			  	parameters: {
			    	id: 'com.palm.app.help',
			      	params: {
			        	target: "http://help.palm.com/youtube/index.html"
			      	}
			  	}
			});
			event.stop();
		}
	},

});