
function toggleOverflow(element, height, more) {

	if (element.style.overflow != "visible") {
		element.style.height = "auto";
		element.style.overflow = "visible";
		$(more).update("&ndash;");
	} else {
		element.style.height = height;
		element.style.overflow = "hidden";
		$(more).update("+");
	}

}

function openLogin(url) {
	window.open(url, 'login', 'width=400,height=250,resizable=no');
}

function searchMedia(event, keywords, elementID) {

	consoleLog.info("searchMedia: " + keywords);
	
	if (event.keyCode == 13) {
		Router.queryMedia(keywords, function (events) {
			consoleLog.info("found: " + events.length);
	
			// Clear the list
			if ($(elementID).childNodes != null) {
				$A($(elementID).childNodes).each(function(child){
					$(elementID).removeChild(child);
				});
			}
			
			// Add the items
			events.each(function(eventFull) {
				consoleLog.info("found: " + eventFull.media.name);
				
				var display = "(" + eventFull.dateRange.time + ") " +
					eventFull.media.name.unescapeHTML();
				$(elementID).options[$(elementID).options.length] =
					new Option(display, eventFull.media.mediaID, false, false);
			});
		});
	}
		
}

function searchYouTube(event, keywords, elementID) {

	consoleLog.info("searchMedia: " + keywords);
	
	if (event.keyCode == 13) {
		Router.searchYouTube(keywords, function (events) {
			consoleLog.info("found: " + events.length);
	
			// Clear the list
			if ($(elementID).childNodes != null) {
				$A($(elementID).childNodes).each(function(child){
					$(elementID).removeChild(child);
				});
			}
			
			// Add the items
			events.each(function(eventFull) {
				consoleLog.info("found: " + eventFull.media.name);
				var display = eventFull.media.name.unescapeHTML();
				var value = eventFull.media.mediaURL + " " +
					eventFull.media.name.unescapeHTML();
				$(elementID).options[$(elementID).options.length] =
					new Option(display, value, false, false);
			});
		});
	}
		
}

function selectAll(elementID) {
	$A($(elementID).options).each(function(option){
		option.selected = true;
	});
}

function copySelected(sourceListID, destListID) {
	// Create a map of the destination
	var destListMap = new Object();
	$A($(destListID).childNodes).each(function(child){
		destListMap[child.value] = child.text;
	});

	// Copy the selected items that do not exist in the destination
	$A($(sourceListID).childNodes).each(function(child){
		if (child.selected && destListMap[child.value] == undefined) {
			$(destListID).options[$(destListID).options.length] =
				new Option(child.text, child.value, false, false);
		}
	});
}

function removeSelected(listID) {
	if (!$Fstring(listID).empty()) {
		var selectedIndex = $(listID).selectedIndex;
		$($(listID).options[selectedIndex]).remove();
		if ($(listID).options.length > 0) {
			if ($(listID).options.length == selectedIndex) {
				--selectedIndex;
			}
			$(listID).options[selectedIndex].selected = true;
		}
	}
}

function getLastOption(listID) {
	return $(listID).options[$(listID).options.length - 1];
}

function moveSelectedToTop(listID) {
	var selectedIndex = $(listID).selectedIndex;
	if (selectedIndex > 0) {
		var prevOption = $(listID).options[0];
		var option = $($(listID).options[selectedIndex]).remove();
		$(prevOption).insert( { before: option } );
	}	
}

function moveSelectedUp(listID) {
	var selectedIndex = $(listID).selectedIndex;
	if (selectedIndex > 0) {
		var prevOption = $(listID).options[selectedIndex - 1];
		var option = $($(listID).options[selectedIndex]).remove();
		$(prevOption).insert( { before: option } );
	}	
}

function moveSelectedDown(listID) {
	var selectedIndex = $(listID).selectedIndex;
	if (selectedIndex < $(listID).options.length - 1) {
		var nextOption = $(listID).options[selectedIndex + 1];
		var option = $($(listID).options[selectedIndex]).remove();
		$(nextOption).insert( { after: option } );
	}	
}

function moveSelectedToBottom(listID) {
	var selectedIndex = $(listID).selectedIndex;
	if (selectedIndex < $(listID).options.length - 1) {
		var nextOption = $(listID).options[$(listID).options.length - 1];
		var option = $($(listID).options[selectedIndex]).remove();
		$(nextOption).insert( { after: option } );
	}	
}

function selectView(parentID, selectID) {
	$(parentID).childElements().each(function(child){
		if ($(child).match("#" + selectID)) {
			$(child).show();
		} else {
			$(child).hide();
		}
	});
}

function switchTab(parentID, selectID, className) {
	$$(parentID).each(function(child){
		if ($(child).match("#" + selectID)) {
			$(child).addClassName(className);
		} else {
			$(child).removeClassName(className);
		}
	});
}

function selectMultiple(listID, value) {
	if ($(listID).options.length > 0) {
		$(listID).value = value;
		$A($(listID).options).find(function (option) {
			return option.value == value;
		}).selected = true;
	}
}

