$(document).ready(function () {
	$("#[id*='sort-']").click(function(){
		$.ajax({
			type: 'POST',
			url: '/_functions.php',
			data: 'func=sort&mode='+this.id,
			cache: false,
			success: function(response){
				response = unescape(response);
				location.reload();
				return false;
			}
		});//END AJAX
	});
	// SEARCH BUTTON
	$("#searchbtn").click(function(){
		var keywords = $("#keywords").val();
		keywords = str_replace(" ","-",keywords);
		if(strlen(keywords)<3){
			$("#search-note").html("Please enter a keyword");
		}else{
			top.location="/search/"+keywords+'/1';
		}
	});
	// END SEARCH BUTTON
	// REPORT TUTORIAL
	$("#[id*='report-']").click(function(){

		$.showAkModal('/report.php?id='+this.id,'Report A Tutorial',630,350);
	});
	//END REPORT TUTORIAL
	
	//RATINGS
	$("#[id*='rate-']").click(function(){
		var rate = this.id;

		$.ajax({
			type: 'POST',
			url: '/_functions.php',
			data: 'func=rate&rate='+rate,
			cache: false,
			success: function(response){
				response = unescape(response);

				var response = response.split("|");
				
				if(response[0]=="error"){
					alert("You already rated this tutorial. You gave it a "+response[1]);	
				}else{
					$("#rbox-"+response[0]).hide().show().html(response[1]);
				}
				return false;
			}
		});//END AJAX	
   });
	//END RATINGS
	// SAVE A TUTORIAL
	$("#[id*='fav-']").click(function(){
		var save = this.id;
		$.ajax({
			type: 'POST',
			url: '/_functions.php',
			data: 'func=save&save='+this.id,
			cache: false,
			success: function(response){
				response = unescape(response);
				var error = response.split("|");
				if(error[0]=="error"){
					$(".popup").slideDown().html(error[1]).slideUp(2000);
				}else{
					$("#"+save).html(response);
				}
				
				return false;
			}
		});//END AJAX	
	});
	// END SAVE A TUTORIAL
	
	//LOGIN
	$(".loginbtn").click(function(){
		var username = $("#username").val();
		var password = $("#password").val();
		if(!(!username || !password)){
			$.ajax({
				type: 'POST',
				url: '/_functions.php',
				data: 'func=login&username='+username+'&password='+password,
				cache: false,
				success: function(response){
					response = unescape(response);
					response = response.split("|");
					if(response[0]=="success"){
						$(".loginform").html(response[1]);
					}else{
						$(".popup").slideDown().html(response[1]).slideUp(2000);
					}

				}
			});//END AJAX			
		}else{
			$(".popup").slideDown().html("Enter your email and password").slideUp(2000);
		}
	});
	// END LOGIN
	
	//LOGOUT
	$("#logoutbtn").click(function(){
		$.ajax({
			type: 'POST',
			url: '/_functions.php',
			data: 'func=logout',
			cache: false,
			success: function(response){
				$(".loginform").html("You are logged out");
			}
		
		});//END AJAX				
	});
	// END LOGOUT
	
	$('#bookmark').jFav();
	
}); 