// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
	"use strict";

	var app = WinJS.Application;
	var activation = Windows.ApplicationModel.Activation;

	var myData = new WinJS.Binding.List();

	var publicMembers =
		{
			itemList: myData
		};
	WinJS.Namespace.define("DataExample", publicMembers);

//	var channel = new Windows.Foundation.Diagnostics.LoggingChannel("UserMarksChannel");

	app.onactivated = function (args) {
		if (args.detail.kind === activation.ActivationKind.launch) {
			if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
				// TODO: This application has been newly launched. Initialize
				// your application here.
			} else {
				// TODO: This application has been reactivated from suspension.
				// Restore application state here.
			}

			listView.addEventListener("selectionchanged", function (eventInfo) {
//				channel.logMessage("selectionchanged start", Windows.Foundation.Diagnostics.LoggingLevel.information);
				if (listView.winControl.selection.count() > 0) {
					listView.winControl.selection.getItems().then(function (items) {
						// Clear the template container
						details.innerHTML = "";
						// Render the template
						if(items[0].data.fullsize == null)
						{
							Windows.Storage.StorageFile.getFileFromPathAsync(items[0].data.path).then(function(image) {
								items[0].data.fullsize = URL.createObjectURL(image);
								detailsTemplate.winControl.render(items[0].data, details);
							});
						}
						else
							detailsTemplate.winControl.render(items[0].data, details);
					});
				}
//				channel.logMessage("selectionchanged end", Windows.Foundation.Diagnostics.LoggingLevel.information);
			});
			getPics();
			args.setPromise(WinJS.UI.processAll());
		}
	};

	app.oncheckpoint = function (args) {
		// TODO: This application is about to be suspended. Save any state
		// that needs to persist across suspensions here. You might use the
		// WinJS.Application.sessionState object, which is automatically
		// saved and restored across suspension. If you need to complete an
		// asynchronous operation before your application is suspended, call
		// args.setPromise().
	};

	app.start();

	function getPics() {
//		channel.logMessage("getPics start", Windows.Foundation.Diagnostics.LoggingLevel.information);
		Windows.Storage.KnownFolders.picturesLibrary.getFolderAsync("PerfDiagHub").then(function (folder) {
			return folder.getFilesAsync();
		}).then(function (files) {
			files.forEach(function (file) {
				getFile(file);
			});
		}).done(function() {
//			channel.logMessage("getPics end", Windows.Foundation.Diagnostics.LoggingLevel.information);
		});

	}

	function getFile(file) {
//		channel.logMessage("getFile start", Windows.Foundation.Diagnostics.LoggingLevel.information);
		Windows.Storage.StorageFile.getFileFromPathAsync(file.path).then(function (image) {
//			return file.getThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode);
//		}).then(function (image) {
			var imgSrc = URL.createObjectURL(image);
			myData.push({ title: file.name, date: file.dateCreated, picture: imgSrc, path: file.path, fullsize: null});
		}).done(function() {
//			channel.logMessage("getFile end", Windows.Foundation.Diagnostics.LoggingLevel.information);
		});

	}
})();
