// Anzahl der maximal erlaubten Favoriten (muss in der favoriten.conf.php ebenfalls eingestellt werden!)
var maxFavoriten = 10;

function $(id) {
	return document.getElementById(id);
}

/**
 * Läd die Favoriten der entsprechenden Kategorie
 */
function ddZeigeLinks() {
	var kid = $('favHK').options[$('favHK').selectedIndex].value;
	$('favLinks').innerHTML = $('favLinks'+kid).innerHTML;
}

/**
 * Fügt einen Faforit der Auswahl hinzu
 */
function ddAddLink() {
	if($('favAuswahl').length >= 10) {
		alert('Du kannst nicht mehr als 10 Favoriten setzen!');
		return;
	}
	else if($('favLinks').selectedIndex == -1)
		return;
	
	var optionValue = $('favLinks').options[$('favLinks').selectedIndex].value;
	var optionAnzeige = $('favLinks').options[$('favLinks').selectedIndex].text;
	
	for(var i = 0; i < $('favAuswahl').length; i++) {
		if($('favAuswahl').options[i].value == optionValue) {
			alert('Link bereits ausgewählt!');
			return;
		}
	}
	
	newOption = new Option(optionAnzeige, optionValue, false, false);
	newOption.setAttribute('ondblclick', 'ddRemoveLink()');
	newOption.selected = 'selected';
	$('favAuswahl').options[$('favAuswahl').length] = newOption;
}

/**
 * Entfernt einen Favorit aus der Auswahl
 */
function ddRemoveLink() {
	if($('favAuswahl').length == 0 || $('favAuswahl').selectedIndex == -1)
		return;
		
	var selectedIndex = $('favAuswahl').selectedIndex;
	$('favAuswahl').options[selectedIndex] = null;
	if($('favAuswahl').options[selectedIndex] != null)
		$('favAuswahl').optionMAX_FAVORITENs[selectedIndex].selected = 'selected';
	else 
		$('favAuswahl').options[selectedIndex -1].selected = 'selected';
}

/**
 * Verschiebt einen Favorit in der Auswahl nach oben bzw. unten
 * @param param
 */
function ddSetPosition(param) {
	if($('favAuswahl').selectedIndex == -1)
		return;
	
	var selectedIndex = $('favAuswahl').selectedIndex;
	var nextIndex = selectedIndex + param;
	var selectedOption = $('favAuswahl').options[selectedIndex];
	var nextOption = $('favAuswahl').options[nextIndex];
	
	var selectedOptionValue = selectedOption.value;
	var selectedOptionText = selectedOption.text;
	var nextOptionValue = nextOption.value;
	var nextOptionText = nextOption.text;
	
	$('favAuswahl').options[nextIndex].value = selectedOptionValue;
	$('favAuswahl').options[nextIndex].text = selectedOptionText;
	$('favAuswahl').options[nextIndex].selected = 'selected';
	$('favAuswahl').options[selectedIndex].value = nextOptionValue;
	$('favAuswahl').options[selectedIndex].text = nextOptionText;
}

/**
 * Bereitet die Auswahlliste zur Speicherung vor
 */
function prepareFavList() {
	$('favAuswahl').setAttribute('multiple', 'multiple');
	for(i = 0; i < $('favAuswahl').length; i++) {
		$('favAuswahl').options[i].selected = 'selected';
	}
}

