var YouTubeImporter = {
	initialize: function () {

	},
	showDialog: function () {
		if (!user_name) {
			alert ('You have to log in first!');
			return;
		}
		Settings.closeBox();
		showBackground();
		$('import_from_youtube_dialog').style.display = 'block';
		$('importing_from_youtube').style.display = 'none';
	},
	hideDialog: function () {
		//hideBackground();
		Settings.openBox();
		$('import_from_youtube_dialog').style.display = 'none';
	},
	onClickOK: function () {
		if ($('youtube_import_username').value == '') {
			alert ('Please, enter your username!')
			return;
		}
		var playlists = ($('youtube_import_playlists').checked?'yes':'no');
		var favorites = ($('youtube_import_favorites').checked?'yes':'no');

		if (playlists == 'no' && favorites == 'no') {
			alert ('Please, select either playlists, favorites or both to import!')
			return;
		}
		urchinTracker('import_from_youtube/' + $('youtube_import_username').value);
		$('importing_from_youtube').style.display = 'block';
		this.params = $H({user:$('youtube_import_username').value, playlists:playlists, favorites:favorites});
		new Ajax.Request ('import_youtube_user_stuff.php', {
			method:'get',
			parameters: this.params.toQueryString(),
			onSuccess: this.result_handler.bind(this)});
	},
	onClickCancel: function () {
		this.hideDialog();
	},
	result_handler: function (ajaxResponse) {
		var t = ajaxResponse.responseText;
		var jsonObj = null;
		$('importing_from_youtube').style.display = 'none';
		try {
			eval ('jsonObj = ' + t);
			if (jsonObj.error)
				alert (jsonObj.error)
			else {
				if (jsonObj.vn > 0)
					SavedPlaylists.loadPlaylists();
				if (jsonObj.fn > 0)
					Favorites.reloadFavorites();

				var text = '';
				if (this.params.playlists == 'yes' && this.params.favorites == 'yes' && jsonObj.vn == 0 && jsonObj.fn == 0) {
					text = 'No Playlists or Favorites were imported';
				} else {
					if (this.params.playlists == 'yes') {
						/*text = jsonObj.vn + ' new ' + (jsonObj.vn == 1?'video':'videos') + ' have been added to ' +
							jsonObj.pn + ' new ' + (jsonObj.pn == 1?'playlist':'playlists');// + ' and ' + jsonObj.pu + (jsonObj.pu == 1?' was':' were') + ' old';*/
						if (jsonObj.vn == 0) {
							text = 'No Playlists were imported';
						} else
							text = jsonObj.pn + ' new ' + (jsonObj.pn == 1?'Playlist':'Playlists') + ' were added to your Playlists, totalling ' + jsonObj.vn + ' ' + (jsonObj.vn == 1?'video':'videos');
						if (this.params.favorites == 'yes') text += '\n';
					}

					if (this.params.favorites == 'yes') {
						if (jsonObj.fn == 0) {
							text += 'No Favorites were imported';
						} else
							text += jsonObj.fn + ' new Favorites' + (jsonObj.fn == 1?'':'s') + ' were added to your Favorites ';
					}
				}
				alert (text);

				this.hideDialog();
				if (rc.showFavorites && this.params.favorites == 'yes' && jsonObj.fn > 0)
					Favorites.show();
			}
		} catch (e) {
			alert ('This service is temporarily unavailable. Please, try again later');
		}
	},
	showQuickPlaylistImportDialog: function () {
		SavedPlaylists.closeList();
		showBackground();
		$('quick_playlist_import').style.display = 'block';
		$('quick_playlist_importing_from_youtube').style.display = 'none';
	},
	hideQuickPlaylistImportDialog: function () {
		hideBackground();
		$('quick_playlist_import').style.display = 'none';
		SavedPlaylists.openList();
	},
	onQuickPlaylistImportDialogOK: function () {
		var url = $('quick_youtube_import_playlist_url').value;
		if (!url) {
			alert ('Please, entery playlist URL!');
			return;
		}
		var r1 = /youtube\.com\/view_play_list\?p=(.{16})/;
		var r2 = /youtube\.com\/watch\?v=.*&p=(.{16})/;
		var playlistID = '';
		if (r1.test(url)) {
			var chunk = r1.exec(url);
			playlistID = chunk[1];
		} else if (r2.test(url)) {
			var chunk = r2.exec(url);
			playlistID = chunk[1];
		} else {
			alert ('Unrecognized YouTube playlist URL!');
			return false;
		}

		$('quick_playlist_importing_from_youtube').style.display = 'block';
		var params = $H({plid: playlistID});
		new Ajax.Request('/quick_playlist_import.php', {
			method:'post',
			parameters: params.toQueryString(),
			onSuccess: this.onQuickPlaylistImportDialogOK_handler.bind(this)
		});
		urchinTracker('quick_import_from_youtube/' + playlistID);
	},
	onQuickPlaylistImportDialogOK_handler: function (ajaxRequest) {
		$('quick_playlist_importing_from_youtube').style.display = 'none';
		var jsonObj = null;
		eval ('jsonObj = ' + ajaxRequest.responseText);
		if (!jsonObj) {
			alert ('Error! Please try again!');
		} else {
			if (jsonObj.errorid)
				alert (jsonObj.error);
			else {
				SavedPlaylists.loadPlaylists();
				alert ('Created a new playlist "' + jsonObj.name + '" with ' + jsonObj.vn + " videos in it");
				this.hideQuickPlaylistImportDialog();
			}
		}
	}
}