/* JAVASCRIPT LIBRARY */



var image = new Array;
var v = new Array;

//declare all images
image[0] = "../images/home_over.gif";
image[1] = "../images/media_over.gif";
image[2] = "../images/bio_over.gif";
image[3] = "../images/lyrics_over.gif";
image[4] = "../images/blog_over.gif";

//set image object src
for(var i=0; i < image.length; i++) {
  v[i] = new Image();
  v[i].src = image[i];
}






function loadPosts() {
//	alert('loading');
	var results = $.get("php/getPosts.php", function(data) {
	
		$("#posts").html(data);
		
		var t = setTimeout("loadPosts()", 240000)
	});
	
	
}

function newPost() {
	
	document.getElementById('submitPost').disabled = true;
	var post = document.getElementById('newPostText').value;
	
	
	$.post("php/newPost.php", {text:post}, function(data) {
		document.getElementById('submitPost').disabled = false;
		document.getElementById('newPostText').value = "";
		$("#post_window").css("display","none");

		loadPosts();
	});
	
}









function toggleWindow(name) {
	
	$("#login_window").css("display","none");
	$("#signup_window").css("display","none");
	$("#post_window").css("display","none");
		
	$("#"+name+"_window").css("display","block");

}

function closeWindow(name) {
	$("#"+name+"_window").css("display","none");
}








function loginUser() {
	
	var uLogin = document.getElementById("user_login").value
	var uPass = document.getElementById("user_pass").value
	
//	checkEmail(uEmail);
//	checkPass(uPass);

	$.post("php/login.php", {login:uLogin, pass:uPass}, function(data) {
		
		if(data == "true")
			location.reload();//changeLoginState()
		else
			alert(data);
	});
}


function logoffUser() {
	
	$.post("php/logout.php", function(data) {
		location.reload();
	});
}


function signupUser() {
	
	document.getElementById('submitSignup').disabled = true;
	
	var uEmail = document.getElementById("signup_email").value
	var uLogin = document.getElementById("signup_login").value
	var uPass = document.getElementById("signup_pass").value
	
	
	
	if(checkEmail(uEmail) == false)
		return false;
	if(checkPass(uLogin,"login") == false)
		return false;
	if(checkPass(uPass,"password") == false)
		return false;
	
	
	
	
	$.post("php/signup.php", {email:uEmail, login:uLogin, pass:uPass}, function(data) {
		
		$("#signup_window").css("display","none");
		
		document.getElementById("signup_email").value = "";
		document.getElementById("signup_login").value = "";
		document.getElementById("signup_pass").value = "";
		document.getElementById('submitSignup').disabled = false;
		
		alert(data);
	});
	
}







function checkEmail(val) {
	
	if( !val.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/) ) {
		alert("Sorry invalid email, please double check your email address for mistakes.");
		document.getElementById('submitSignup').disabled = false;
		return false;
	}
	
	
}

function checkPass(val, name) {
	
	if( !val.match(/^[a-zA-Z0-9_]+$/) || val.length < 4) {
		alert("Sorry invalid "+name+", please use more then four letters and or numbers without spaces.");
		document.getElementById('submitSignup').disabled = false;
		return false;
	}
	
}













function changeLoginState() {
	
	$("#login_window").css("display","none");
	$("#add_post").css("display","block");

	$("#login_link").html("Log Off");
	$("#login_link").click(logoffUser);
	
}