var songSelection=-1;
var hoversong = false; //if the mouse is hovering over the song suggestion popup

function setSearchPopup() {
	var posx = 0;
	var posy = 0;
	var element = document.getElementById("keywordSearch");
	while( element != null ) {
		posy += element.offsetTop;
		element = element.offsetParent;
	}
	var element = document.getElementById("keywordSearch");
	while( element != null ) {
		posx += element.offsetLeft;
		element = element.offsetParent;
	}
	document.getElementById("suggestion_popup").style.left = posx+"px";
	document.getElementById("suggestion_popup").style.top = (posy+20)+"px";
}

function clearSongPopup(element) {
	var suglist = element.getElementsByTagName("li");
	for (var x = 0; x < suglist.length; x++) {
		document.getElementById("song"+x).className = "unsearchselected";
	}	
}

function clearArtistPopup(element) {
	var suglist = element.getElementsByTagName("li");
	for (var x = 0; x < suglist.length; x++) {
		document.getElementById("artist"+x).className = "unsearchselected";
	}	
}

function getSearch(e,element) {
	if (e.keyCode == 40 || e.keyCode == 38) {
		var suglist = document.getElementById("suggestion_popup").getElementsByTagName("li");
		if (e.keyCode == 40) {
			songSelection+=1;
			
			if (songSelection >= suglist.length) {
				songSelection = 0;
			}
		} else {
			songSelection-=1;
			
			if (songSelection < 0) {
				songSelection = suglist.length-1;
			}
		}
			clearSongPopup(document.getElementById("suggestion_popup"));
			
			if (hoversong == false) {
				document.getElementById("song"+songSelection).className = "searchselected";
			}
	} else if (e.keyCode == 13) {
		if(songSelection == -1) {
			document.omega_search_header.submit();
		} else {
			clickedSearch(document.getElementById("song"+songSelection));
			hideSearchPopup(true);
			songSelection = -1;
		}
	} else {
		setSearchPopup();
		var ajax;
		var parameters = "search="+element.value;
		try {
			ajax = new XMLHttpRequest();
		}catch(e) {
			try {
				ajax = new ActiveXObject("Msxm12.XMLHTTP");
			} catch(e) {
				try {
					ajax = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {	
					document.getElementById("suggestion_list").innerHTML = "<li>You need to update your browser for the full functionality of this website.</li>";
					return false;
				}
			}	
		}
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				document.getElementById("suggestion_popup").innerHTML = ajax.responseText;
				hideSearchPopup(false);
			}
		}
		ajax.open("POST","includes/templates/apple_zen/ajax/search.php",true);
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.send(parameters);
	}
}

function clickedSearch(element) {
	var txt = element.innerHTML.replace("&amp;","&");
	txt = txt.replace("&quot;","\"");
	txt = txt.replace("&lt;","<");
	txt = txt.replace("&gt;","<");
	document.getElementById("keywordSearch").value = txt;
	hideSearchPopup(true);
}

function hideSearchPopup(bool) {
	if (bool) {
		songSelection = -1;
		document.getElementById("suggestion_popup").style.display = 'none';
	} else {
		document.getElementById("suggestion_popup").style.display = 'block';	
	}
}

function clickedArtist(element) {
	var txt = element.innerHTML.replace("&amp;","&");
	txt = txt.replace("&quot;","\"");
	txt = txt.replace("&lt;","<");
	txt = txt.replace("&gt;","<");
	document.getElementById("artistSearch").value = txt;
	hideArtistSearchPopup(true);
}

function hideArtistSearchPopup(bool) {
	if (bool) {
		songSelection=-1;
		document.getElementById("artist_suggestion_popup").style.display = 'none';
	} else {
		document.getElementById("artist_suggestion_popup").style.display = 'block';	
	}
}

function getArtistSearch(e,element) {
	if (e.keyCode == 40 || e.keyCode == 38) {
		var suglist = document.getElementById("artist_suggestion_popup").getElementsByTagName("li");
		if (e.keyCode == 40) {
			songSelection+=1;
			
			if (songSelection >= suglist.length) {
				songSelection = 0;
			}
		} else {
			songSelection-=1;
			
			if (songSelection < 0) {
				songSelection = suglist.length-1;
			}
		}
			clearArtistPopup(document.getElementById("artist_suggestion_popup"));
			
			if (hoversong == false) {
				document.getElementById("artist"+songSelection).className = "searchselected";
			}
	} else if (e.keyCode == 13) {
		if(songSelection == -1) {
			document.omega_search_header.submit();
		} else {
			clickedArtist(document.getElementById("artist"+songSelection));
			hideArtistSearchPopup(true);
			songSelection = -1;
		}
	} else {
		setArtistSearchPopup();
		var ajax;
		var parameters = "search="+element.value;
		try {
			ajax = new XMLHttpRequest();
		}catch(e) {
			try {
				ajax = new ActiveXObject("Msxm12.XMLHTTP");
			} catch(e) {
				try {
					ajax = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {	
					document.getElementById("artist_suggestion_popup").innerHTML = "<li>You need to update your browser for the full functionality of this website.</li>";
					return false;
				}
			}	
		}
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				document.getElementById("artist_suggestion_popup").innerHTML = ajax.responseText;
				hideArtistSearchPopup(false);
			}
		}
		ajax.open("POST","includes/templates/apple_zen/ajax/artist_search.php",true);
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.send(parameters);
	}
	
}

function setArtistSearchPopup() {
	var posx = 0;
	var posy = 0;
	var element = document.getElementById("artistSearch");
	while( element != null ) {
		posy += element.offsetTop;
		element = element.offsetParent;
	}
	var element = document.getElementById("artistSearch");
	while( element != null ) {
		posx += element.offsetLeft;
		element = element.offsetParent;
	}
	document.getElementById("artist_suggestion_popup").style.left = posx+"px";
	document.getElementById("artist_suggestion_popup").style.top = (posy+20)+"px";
}