$(document).ready(function(){
	$(".close").click(function(){
		$("#msgBox").fadeTo(300, 0.0);
	});
	window.setTimeout(function(){
		$("#msgBox").fadeTo(300, 0.0);
		},10000); 
	
	$("#addButton").click(function(){
		$("#addBox").fadeTo(1000, 1.0, function(){$("#backButton").fadeIn(600);});
		$("#addButton").fadeOut(1000);
		$("#listBox").fadeOut(1000);
		return false;
	});
	$("#backButton").click(function(){
		$("#listBox").fadeTo(1000, 1.0, function(){$("#addButton").fadeIn(600);});
		$("#backButton").fadeOut(1000);
		$("#addBox").fadeOut(1000);
		return false;
	});
});

function newPage(link){
	var xhr=new XMLHttpRequest();
	var url=link.href;
	if(xhr){
		xhr.open("GET", url, true);
		xhr.onreadystatechange=function(){
				if(xhr.readyState<=3){
					document.getElementById("rightBox").innerHTML="loading...";
				}
				if(xhr.readyState==4 && xhr.status==200){
					document.getElementById("rightBox").innerHTML=xhr.responseText;
				}
			}
		xhr.send(null);
	} else {
		alert("Sorry, we could not make an XHR");
	}
	return false;
}
function playMusic(obj){
	var musicFile=obj.getAttribute('music');
	musicBox=document.getElementById('musicBox');
	musicBox.innerHTML="<embed id='musicPlayer' src="+musicFile+" autostart='true' width='200px' height='25px' kioskmode='true'></embed>";
	return false;
}

function searchCatalog(){
	var queryStr=document.getElementById("queryStr").value;
	var xhr=new XMLHttpRequest();
	if(queryStr==0||queryStr==null){
		alert("Sorry");
	} else {
		document.getElementById("resultBox").style.display="block";
		if(xhr){
			xhr.open("GET", "_php/searchCatalog.php?query="+queryStr, true);
			xhr.onreadystatechange=function(){
				if(xhr.readyState<=3){
					document.getElementById("resultBox").innerHTML="searching for '"+queryStr+"'...";
				}
				if(xhr.readyState==4 && xhr.status==200){
					document.getElementById("resultBox").innerHTML=xhr.responseText;
				}
			}
			xhr.send(null);
		} else {
			alert("Sorry, we could not make a XHR ");
		}
	}
	return false;
}
function hideResultBox(){
	document.getElementById("resultBox").style.display="none";
	return false;
}

function addFan(){
	var name=document.getElementById("name").value;
	var email=document.getElementById("email").value;
	if(!email || !name){
		alertBox("Please enter a name and email");
	} else {
		var xhr = new XMLHttpRequest();
		var url="_php/addFan.php";
		var params="name="+name+"&&email="+email;
		if(xhr){
			xhr.open("POST", url, true);
			xhr.onreadystatechange=function(){
				if(xhr.readyState<=3){
					document.getElementById("fanBox").innerHTML="sending...";
				}
				if(xhr.readyState==4 && xhr.status==200){
					document.getElementById("fanBox").innerHTML=xhr.responseText;
				}
			}
		xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xhr.send(params);
		} else {
			alert("Sorry, we could not make an XMLHttpRequest");
		}
	}
	return false;
}

function removeFan(){
	var email=document.getElementById("removeEmail").value;
	if(!email){
		alertBox("Please enter an email address");
	} else {
		var xhr = new XMLHttpRequest();
		var url="_php/removeFan.php";
		var params="email="+email;
		if(xhr){
			xhr.open("POST", url, true);
			xhr.onreadystatechange=function(){
				if(xhr.readyState<=3){
					document.getElementById("messageBox").innerHTML="sending...";
				}
				if(xhr.readyState==4 && xhr.status==200){
					document.getElementById("messageBox").innerHTML=xhr.responseText;
				}
			}
		xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xhr.send(params);
		} else {
			alert("Sorry, we could not make an XMLHttpRequest");
		}
	}
	return false;
}

function countChar(obj, limit){
	var charLimit=limit;
	document.getElementById("countBox").innerHTML=charLimit;
	obj.onkeyup=function(){
		var counter=document.getElementById("countBox").innerHTML;
		if(counter<=0){
			obj.value=obj.value.slice(0, charLimit);
		}
		var count=obj.value.length;
		document.getElementById("countBox").innerHTML=charLimit-count;
	}
	if(counter<=0){
			obj.value=obj.value.slice(0, charLimit);
	}
}

function checkForm(){
	alert("you're typying in the form");
}

function closeAlertBox(){
	document.getElementById("alertBox").style.display="none";
	return false;
}

function alertBox(msg){
	var alertBox=document.getElementById("alertBox");
	document.getElementById("alertBoxMsg").innerHTML=msg;
	alertBox.style.display="block";
}

function swapCovers(){
	var frontCover=document.getElementById("albumCoverBox").firstChild.src;	
	var backCover=document.getElementById("backCoverBox").firstChild.src;
	var x=document.getElementById("albumCoverBox").firstChild;
	var y=document.getElementById("backCoverBox").firstChild;
	x.src=backCover;
	y.src=frontCover;
	return false;
}
