;
(function () {
	const
		transfer = require('transfer')
		, EventEmitter = require('myevents')
		, app_cache = require('app-cache')
		, uploadTask = require('uploadtask')
		, downTask = require('downtask')
		, taskStatus = require('taskstatus')
		, myStorage = require('local-storage')
		, execFile = require('child_process').execFile
		;
	transfer['window_load.complete']();
	transfer['window_load.drag'](document, global);
	transfer['window_load.tray'](nwWindow);
    transfer['window_load.task']();

	EventEmitter.on('logout', function () {

		app_cache.cur_user = null;
		delete app_cache.cur_user;

		let local_users = myStorage.get('local_users');
		if (local_users.length === 0) {
			return logout();
		}
		local_users.forEach(function (user) {
			user.auto = false;
		});
		myStorage.set('local_users', local_users);
		logout();
	});

	function logout() {
		stopUpdate();
		stopDown();

		// uploadTask.uploadloadingTasks = {};
		// downTask.downloadingTasks = {};
		//var win = nw.Window.get();

		// execFile(nw.process.execPath, (error) => {
		// 	if (error) {
		// 		throw error;
		// 	} 
		// });nw.process.exit();
		win.hide();
		nw.Window.open("./web_content/login.html", {
			frame: false,
			"width": 680,
			"height": 400,
			show: false
		}, (new_win) => {
			new_win.on('loaded', () => {
				//	new_win.location.reload();
				new_win.show(true);
				win.close();
			});
		});
	};

	function stopUpdate() {
		let key = Object.keys(uploadTask.uploadloadingTasks);
		key.forEach(e => {
			uploadTask.uploadloadingTasks[e].options.status = taskStatus.pause;
			delete uploadTask.uploadloadingTasks[e]
		})
	}

	function stopDown() {
		let key = Object.keys(downTask.downloadingTasks);
		key.forEach(e => {
			downTask.downloadingTasks[e].options.status = taskStatus.pause;
			delete downTask.downloadingTasks[e]
		})
	}

})();