
function showLists(speciesId, contextPath, listaDomain) {
	myLightWindow.activateWindow(
		{href: '#selectDiv', type: 'inline', width: 520, height:305}
	);

	AddToLifeList.showLists(function(lifelists) {
		var listDiv = $$('.select-lifelist')[1];
		
		if (!listDiv) {
			alert('Please login');
			return false;
		}
		
		listDiv.innerHTML = '';
		
		if (!lifelists) {
			window.location.href = contextPath + '/profile/lifeListsForm.html?formAction=add&speciesId=' + speciesId;
		} else {
			$A(lifelists).each(function(lifelist) {
				var source = '';
				var block = Builder.node('div', {className : 'select-lifelist-block'}, '');
				
				if (lifelist.coverImage) {
					source = listaDomain + '/' + lifelist.coverImage;
				} else {
					source = contextPath + '/images/lifelist-cover.jpg';
				}
				
				var img = Builder.node('img', {src : source});
				var a1 = Builder.node('a', {href : "#"}, img);
				
				setSelectListEvent(a1, lifelist.id, speciesId);
				block.appendChild(a1);
				block.appendChild(Builder.node('br'));
				
				var a2 = Builder.node('a', {href : "#", className : 'title'}, lifelist.name);
				
				setSelectListEvent(a2, lifelist.id, speciesId);
				block.appendChild(a2);
				listDiv.appendChild(block);
			});
		}
	});
	
	return false;
}

function setSelectListEvent(node, listId, speciesId) {
	node.onclick = function() {
		AddToLifeList.addToList(listId, speciesId, function(data) {
			if (data) {
				$("audubonNotificationText").innerHTML = data;
				$("audubonNotification").show();
				new Effect.Highlight('audubonNotification', {startcolor: "#407437", restorecolor: "#ffffff"});
			}
			myLightWindow.deactivate();
		});
		return false;
	};
}

function addToWatchList(id) {
	WatchListDWRController.addSpeciesToWatchList(id, function(resultCode) {
		if (resultCode == 'ok') {
			//change button
			$("addToWatchListLink").hide();
			$("removeFromWatchListLink").show();
			//notification
			$("audubonNotificationText").innerHTML = "Species has been added to watchList";
			$("audubonNotification").show();
			new Effect.Highlight('audubonNotification', {startcolor: "#407437", restorecolor: "#ffffff"});
			
			
		} else {
			//notification
			$("audubonNotificationText").innerHTML = "Species has not been added";
			$("audubonNotification").show();
			new Effect.Highlight('audubonNotification', {startcolor: "#407437", restorecolor: "#ffffff"});
		}
	});
	return false;
}

function removeFromWatchList(id) {
	WatchListDWRController.removeSpeciesFromWatchList(id, function(resultCode) {
		if (resultCode == 'ok') {
			//change button
			$("removeFromWatchListLink").hide();
			$("addToWatchListLink").show();
			//notification
			$("audubonNotificationText").innerHTML = "Species has been removed from watchList";
			$("audubonNotification").show();
			new Effect.Highlight('audubonNotification', {startcolor: "#407437", restorecolor: "#ffffff"});
			
			
		} else {
			//notification
			$("audubonNotificationText").innerHTML = "Species has not been removed";
			$("audubonNotification").show();
			new Effect.Highlight('audubonNotification', {startcolor: "#407437", restorecolor: "#ffffff"});
		}
	});
	return false;
}

function reportAbuseContent(id, element) {
	ReportAbuseDWRController.reportAbuseContent(id, function(resultCode) {
		if (resultCode == 'ok') {
			$("audubonNotificationText").innerHTML = "Media item has been reported as abuse content";
			$("audubonNotification").show();
			new Effect.Highlight('audubonNotification', {startcolor: "#407437", restorecolor: "#ffffff"});
			element.hide();
			
		} else {
			$("audubonNotificationText").innerHTML = "Could not report abuse content";
			$("audubonNotification").show();
			new Effect.Highlight('audubonNotification', {startcolor: "#407437", restorecolor: "#ffffff"});
		}
	});
	return false;
}

/* Search Species */

function searchSpecies(autocompleter, token) {
	SimpleSearchDWRController.searchSpecies(token, function(data) {
		autocompleter.setChoices(data);
	});
}

function nameValueSelector(value) {
	return cutUnderLine(value.properties.Title + " (" + value.topCategory.name + ")");
}

function cutUnderLine(str) {
	while (str.charAt(0) == "_") {
		str = str.substring(1, str.length);
	}
	while (str.charAt(str.length - 1) == "_") {
		str = str.substring(0, str.length - 1);
	}
	return str;
}

