var selectedItems = new Array();
var availableItems = new Array();
var availableCocktails = new Array();
var hits = 0;
var xml;

// wenn es einen Hash, aber keine selectedItems gibt, wird selectedItems anhand vom Hash aktualisiert
function updateFromHash() {
	var zutaten = unescape(window.location.hash.replace('#', '')).split(';');
	
	if(window.location.hash != '' && selectedItems.length == 0 && window.location.hash != '#empty') {
		
		// Array leeren
		selectedItems.splice(0, selectedItems.length);
		for(var i = 0; i < zutaten.length; i++) {
			selectedItems.push(zutaten[i]);
		}
		
		refreshSelectedList();
		getZutatenAndCocktails();
	}
}

// aktualisiert den Hash anhand des Arrays selectedItems
function refreshHash() {
	var hash = selectedItems.join(';');
	if(hash == '') {
		hash = 'empty';
	}
	window.location.hash = hash;
}

// sucht nach passen Zutaten oder Cocktails anhand des Suchfelds
function searchItem() {
	
	q = $('q').value;
	
	if(xml) {
		xml.abort();
	}
	
	if(q.length < 2) {
		$('searchresults').innerHTML = '';
		$('load1').innerHTML = '';
	}
	else {
		xml = new Ajax.Request('ajax/cocktailmixer.php', {
			method: 'GET',
			parameters: { action: 'search', zutat: escape(q) },
			onLoading: function() {
				//$('load1').innerHTML = '<img src="images/load.gif" alt="" width="16" height="16" />';
			},
			onSuccess: function(t) {
				$('load1').innerHTML = '';

				if($('searchresults').innerHTML != unescape(t.responseText)) {
						$('searchresults').innerHTML = '<p>Suchergebnisse:</p>' + unescape(t.responseText);
				}				
			}
		});
	}
	
	return false;
}

// Fügt ein ausgewähltes Tag der Liste hinzu
function addToList(text) {
	var i = 0;
	var duplicate = 0;
	
	while(selectedItems[i]) {
		if(selectedItems[i] == text) {
			duplicate = 1;
		}
		i++;
	}
	
	if(duplicate != 1) {
		selectedItems.push(text);
	}
	
	$('q').focus();
	$('q').value = '';
	
	$('searchresults').innerHTML = '';
	$('wrapper').scrollTo();
	refreshHash();
	refreshSelectedList();
	getZutatenAndCocktails();
}

// Entfernt ein Element aus der Liste
function removeFormList(text) {
	var i = 0;
	while(selectedItems[i]) {
		if(selectedItems[i] == text) {
			selectedItems.splice(i,1);
		}
		i++;
	}
	
	refreshHash();
	refreshSelectedList();
	getZutatenAndCocktails();
}

// aktualisiert die Liste der ausgewählten Tags
function refreshSelectedList() {
	var i = 0;
	var text = '';
	var cocktail = '';
	
	while(selectedItems[i]) {
		
		if(selectedItems[i] != 'undefined') {
			cocktail = selectedItems[i].split(',');
			text = text + '<li>' + cocktail[1] + '&nbsp;&nbsp;<a href="#" class="delete" onclick="removeFormList(\'' + cocktail[0] + ',' + cocktail[1] + '\');return false;" title="Diese Auswahl wieder entfernen">entfernen</a></li>';
		}
		i++;
	}
	$('selectedItems').innerHTML = text;
}

// füllt die Arrays availableCocktails und availableItems
function getZutatenAndCocktails() {

	var zutaten = selectedItems.join(';')
    
    new Ajax.Request('ajax/cocktailmixer.php', {
    	method: 'GET',
    	parameters: { action: 'list', zutaten: zutaten },
    	onLoading: function() {
			//$('load2').innerHTML = '<img src="images/load.gif" alt="" width="16" height="16" />';
			//$('load3').innerHTML = '<img src="images/load.gif" alt="" width="16" height="16" />';
		},
		onSuccess: function(t) {
			var r = t.responseText.split("\n");
						
			if(r.length == 2) {
				$('availableCocktails').innerHTML = unescape(r[0]);
				$('availableItems').innerHTML = unescape(r[1]);	
			}
			else {
				$('availableCocktails').innerHTML = '';
				$('availableItems').innerHTML = '';
			}
			
			$('load2').innerHTML = '';
			$('load3').innerHTML = '';	
			
		}
    });
    
    return false;
}

// macht die Liste der Cocktails und Zutaten sichtbar
function showList() {
	document.forms.cocktailform.q.focus();
}

// Bewertung von Cocktails
function cocktailVote(rating, cocktail) {
	
	new Ajax.Request('ajax/rating.php', {
		method: 'get',
		parameters: { action: 'cocktail_rating', rating: rating, cocktail: cocktail },
		onLoading: function() {
			$('ratingresult').innerHTML = '<li><img src="images/load_white.gif" alt="" width="16" height="16" /></li>';
		},
		onSuccess: function(t) {
			if(t.responseText == 'Du hast bereits eine Bewertung abgegeben.') {
				$('ratingresult').innerHTML = t.responseText;
			}
			else {
				var result = t.responseText.split(';');
				
				if(result[1] == 1) {
					$('ratingcountofcocktail').innerHTML = result[1] + ' Bewertung';
				}
				else {
					$('ratingcountofcocktail').innerHTML = result[1] + ' Bewertungen';
				}
				var rateofcocktail = result[0] / result[1];
				if(('' + rateofcocktail).length > 3) {
					rateofcocktail = ('' + rateofcocktail).substr(0, 3);
				}
				$('rateofcocktail').innerHTML = rateofcocktail;
				$('ratingresult').innerHTML = 'Danke für deine Bewertung.';
				$('widthofrating').style.width = (((result[0] / result[1]) / 5) * 146) + 'px';
			}
		}
	});	
}

// Cocktail vorschlagen, Zutat hinzufügen
function addZutat() {
	var obj = $('zutatenliste');
	var i = obj.getElementsByTagName('li').length;
	var li = document.createElement("li");
	li.innerHTML = '<input type="text" class="inputzutat1" name="menge' + i + '" /> <select class="inputzutat2" name="einheit' + i + '" size="1"><option></option><option>cl</option><option>ml</option><option>TL</option><option>BL</option><option>EL</option><option>l</option><option>Spritzer</option><option>Tropfen</option><option>Flasche</option><option>Kugel</option><option>Kugeln</option><option>Tasse</option><option>Scheibe</option><option>St&uuml;ck</option><option>Schuss</option></select> <input type="text" class="inputzutat3" name="zutat' + i + '" onkeyup="searchItemCocktailVorschlagen(this)" />';
	
	obj.appendChild(li);
	
	if(document.add.num_zutaten) {
		document.add.num_zutaten.value = (parseInt(document.add.num_zutaten.value) + 1);
	}
}

// Cocktailzutat Suchen
function searchItemCocktailVorschlagen(obj) {
		
	/*if(xml) {
		xml.abort();
	}*/
	
	q = obj.value;
		
	if(q.length < 2) {
		$('searchresults').innerHTML = '';
		$('load').innerHTML = '';
	}

	if(q.length > 1) {

		/*$('zutatensuche').style.display = "block";
	   
		sendxmlhttp.open("GET", 'include/httprequest.php?action=search&type=' + obj.name + '&zutat=' + escape(q), true);
		sendxmlhttp.onreadystatechange = function() {
			if(sendxmlhttp.readyState == 4 && sendxmlhttp.status == 200) {
				if($('searchresults').innerHTML != unescape(sendxmlhttp.responseText)) {
					$('searchresults').innerHTML = unescape(sendxmlhttp.responseText);
				}
				$('load').innerHTML = '';
			}
			else {
				$('load').innerHTML = '<img src="images/load_white.gif" alt="" width="16" height="16" />';
			}
		}
		sendxmlhttp.send(null);
		*/
				
		xml = new Ajax.Request('ajax/cocktailmixer.php', {
			method: 'get',
			parameters: { action: 'search', type: obj.name, zutat: escape(q) },
			onLoading: function() {
				$('load').innerHTML = '<img src="images/load_white.gif" alt="" width="16" height="16" />';
			},
			onSuccess: function(t) {
						
				$('searchresults').innerHTML = t.responseText;
				$('zutatensuche').style.display = 'block';
				
				$('load').innerHTML = '';
			}
		});		
		
	}
}

// Cocktailzutat hinzufügen
function addToListCocktailVorschlagen(value,inputname) {
	document.add.elements[inputname].value = value;
	$('zutatensuche').style.display = 'none';
	$('searchresults').innerHTML = '';
}

// Cocktail Suche
var suchexmlhttp = null;

function searchCocktails(q) {
	
	if(suchexmlhttp) {
		suchexmlhttp.abort();
		suchexmlhttp = null;
	}	
	
	if(q.length == 0) {	
		$('liste').style.display = 'block';	
	}
	
	if(q.length < 2) {
		$('searchresult').innerHTML = '';
		$('load').innerHTML = '';
	}
	
	if(q.length > 1) {

		$('searchresult').style.display = "block";
		// Mozilla
		if (window.XMLHttpRequest) {
			suchexmlhttp = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
			suchexmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}	

		suchexmlhttp.open("GET", 'include/httprequest.php?action=search&cocktail=' + escape(q), true);

		suchexmlhttp.onreadystatechange = function() {
			if(suchexmlhttp.readyState == 4 && suchexmlhttp.status == 200) {
				if($('searchresult').innerHTML != unescape(suchexmlhttp.responseText)) {
					$('searchresult').innerHTML = unescape(suchexmlhttp.responseText);
					$('liste').style.display = 'none';
				}
				$('load').innerHTML = '';
			}
			else {
				$('load').innerHTML = '<img src="images/load_white.gif" alt="" width="16" height="16" />';
			}
		}
		suchexmlhttp.send(null);
	}
}