$(document).ready(function($) {
	$('a#arrow-up').click(function () {	
		if($('#toppanel').is(':visible')) {	
		$('#toppanel').slideToggle('slow');
			$(this).removeClass('arrow-up');
			$(this).addClass('arrow-up-down');	
		} else {
			$('#toppanel').slideToggle('slow');
			$(this).addClass('arrow-up');
			$(this).removeClass('arrow-up-down');	
				
			}
		return false;
	});
    	$(".popup").colorbox({width:"734", height:"350", inline:true, href:"#popup"});
    
});
function add_favorite(game_id) {
        $.post("js/calls.php", {'call[]' : ["add_favorite", game_id]}, function(data){
					if(data == 1) {
                        alert("You have already added this game to your favorites!"); 
                    } else if(data == 2) {
                        alert("You need to be logged in to perform this action!"); 
                    } else if(data == 3) {
                        alert("Added to your favorites, please view your profile to see it."); 
                    }
				});
    }
function add_comment(game_id, comment) {
					//add comment//
        			$.post("js/calls.php", {'call[]' : ["add_comment", comment, game_id]}, function(data2){
                    if(data2 == "2") {
                        alert("You need to be logged in to perform this action!"); 
                    } else if(data2 == "3") {
                        alert("You have already submited this comment");
                    } else {
                    	//get total number of pages
                    	$.post("js/calls.php", {'call[]' : ["last_page", game_id]}, function(last_page){
                    		total_pages = last_page;
	                    	//load spinner
		                    $("#comments_inner").html("<center><img src='images/load.gif' /></center>");
		               		//load up comments with the right page
							$.post("js/calls.php", {'call[]' : ["get_page", last_page, game_id, per_page]}, function(data){
				                    $("#comments_inner").html(data);
				                });
				    		current_page = total_pages;
		                   /// $('#comments_inner').append(data2);
		                    //load correct peggination
		                     $(".pagination").html("<center><img src='images/load.gif' /></center>");
							$.post("js/calls.php", {'call[]' : ["game_pagination", game_id]}, function(data3){
				                    $(".pagination").html(data3);
				                });
				            //scrolling, etc//
		                    var offsettop = parseInt($("body").css("height"));
							window.scrollTo(0,offsettop);
							$('#submitbutton').attr("disabled", true);
							var timer;
							timer = setTimeout(function() {
								$('#submitbutton').attr("disabled", false);
							}, 5000);
							$.scrollTo('#feedback');
						});
                    }
				});
    }
function add_rating(game_id, rating) {
        $.post("js/calls.php", {'call[]' : ["add_rating", game_id, rating]}, function(data){
				if(data == 2) {
                        alert("You need to be logged in to perform this action!"); 
                    } else if (data == 3) {
                    	alert("You have already rated this game!");
                    } else {
                    	$("#rating-bloc").html(data);
                    }
                });
    }
function delete_game(game_id) {
        $.post("js/calls.php", {'call[]' : ["delete_game", game_id]}, function(data){
                });
    }
function delete_message(message_id) {
        $.post("js/calls.php", {'call[]' : ["delete_message", message_id]}, function(data){
        			alert("Delete complete");
                });
    }
function add_friend(user_id) {
        $.post("js/calls.php", {'call[]' : ["add_friend", user_id]}, function(data){
				if(data == 2) {
                        alert("You need to be logged in to perform this action!"); 
                    } else if (data == 3) {
                    	alert("You have already added this person as a friend!");
                    } else {
                    	alert("Your friend request has been sent to this user.");
                    }
                });
    }
function report_game(game_id, msg) {
        $.post("js/calls.php", {'call[]' : ["report_game", game_id, msg]}, function(data){
				if(data == 2) {
                        alert("You need to be logged in to perform this action!"); 
                } else {
                    $("#report_inner").html("<p>Your report has been submited, thank you!</p>");
                }
                });
    }
var current_page = 1;
var total_pages = 1;
var per_page = 15;
function load_paggination(total, pp) {
	total_pages = total;
	per_page = pp;
}

function pagination(fun, page, game_id) {

	if(page > 0) {
		if(fun == "normal") {
			$("#comments_inner").html("<center><img src='images/load.gif' /></center>");
			$.post("js/calls.php", {'call[]' : ["get_page", page, game_id,per_page]}, function(data){
		                    $("#comments_inner").html(data);
		                });
		    current_page = page;
	    } else if(fun == "next") {
	    	if(current_page < total_pages) {
	    	$("#comments_inner").html("<center><img src='images/load.gif' /></center>");
	    	current_page++;
			$.post("js/calls.php", {'call[]' : ["get_page", current_page, game_id,per_page]}, function(data){
		                    $("#comments_inner").html(data);
		                });
		    }
	    } else if(fun == "previous") {
	    	if(current_page > 1) {
	    	$("#comments_inner").html("<center><img src='images/load.gif' /></center>");
	    	current_page--;
			$.post("js/calls.php", {'call[]' : ["get_page", current_page, game_id,per_page]}, function(data){
		                    $("#comments_inner").html(data);
		                });    
			}
	    }
    }
}
function delete_comment(comment_id) {
	 $.post("js/calls.php", {'call[]' : ["delete_comment", comment_id]}, function(data){
                });
}
function delete_comment_profile(comment_id) {
	 $.post("js/calls.php", {'call[]' : ["delete_comment_profile", comment_id]}, function(data){
                });
}
function verify_email(user_id) {
	 $.post("js/calls.php", {'call[]' : ["verify_email", user_id]}, function(data){
	 alert(data);
                });
}
function favorite_remove(game_id) {
	 $.post("js/calls.php", {'call[]' : ["favorite_remove", game_id]}, function(data){
                });
}
function reject_friend(friendship_id) {
	 $.post("js/calls.php", {'call[]' : ["reject_friend", friendship_id]}, function(data){
                });
}
function accept_friend(friendship_id) {
	 $.post("js/calls.php", {'call[]' : ["accept_friend", friendship_id]}, function(data){
                });
}
function login(username, password) {
	$("#loginform").html("<center><img src='images/load.gif' /></center>");
			$.post("js/calls.php", {'call[]' : ["login", username, password]}, function(data){
		                   	if(data == 2) {
		                   		$("#loginform").html('<p style="color: rgb(228, 115, 32); font-size: 12px; font-weight:bold;">Incorrect Username or Password</p><form id="loginform" name="loginform" method="post"><p><label for="username">Username:</label> <input type="text" name="username" id="username" onblur="if(this.value==\'\') this.value=\'Username\'" onfocus="if(this.value ==\'Username\' ) this.value=\'\'" value="Username" /></p><p><label for="password">Password:</label> <input type="password" name="password" id="password" onblur="if(this.value==\'\') this.value=\'Password\'" onfocus="if(this.value ==\'Password\' ) this.value=\'\'" value="Password" /></p><div class="loginbtn"><input type="checkbox" name="remember" id="remember" /><label id="remember-lab" for="remember">Remember Me</label> <input type="image" src="images/login-btn2.png" /></div><p align="right" class="forgot-pass"><a href="forgot-password/">Forgot Password?</a></p></form>');

		                    } else if(data != 2) {
		                    	$(location).attr('href',data);
		                    }
		             	});

}
 $(document).ready(function(){
            $("#shadow").css("height", $(document).height()).hide();
            $(".lightSwitcher").click(function(){
                $("#shadow").toggle();
                if ($("#shadow").is(":hidden")) {
                	$(".lightSwitcher").css("color", "#000");
                    $(this).html("Turn Off Lights").removeClass("turnedOff");
                } else {
                 	$(".lightSwitcher").css("color", "#FFFF00");
                    $(this).html("Turn On Lights").addClass("turnedOff");
                }
            });
            $("#shadow").click(function() {
            	$(this).css('display', 'none');
            	if ($("#shadow").is(":hidden")) {
                	$(".lightSwitcher").css("color", "#000");
                    $(this).html("Turn Off Lights").removeClass("turnedOff");
                } else {
                 	$(".lightSwitcher").css("color", "#FFFF00");
                    $(this).html("Turn On Lights").addClass("turnedOff");
                }
            });
            
        });
//user page peggination//
function pagination_profile(fun, page, profile_id) {

	if(page > 0) {
		if(fun == "normal") {
			$("#comments_inner").html("<center><img src='images/load.gif' /></center>");
			$.post("js/calls.php", {'call[]' : ["get_page_profile", page, profile_id,per_page]}, function(data){
		                    $("#comments_inner").html(data);
		                });
		    current_page = page;
	    } else if(fun == "next") {
	    	if(current_page < total_pages) {
	    	$("#comments_inner").html("<center><img src='images/load.gif' /></center>");
	    	current_page++;
			$.post("js/calls.php", {'call[]' : ["get_page_profile", current_page, profile_id,per_page]}, function(data){
		                    $("#comments_inner").html(data);
		                });
		    }
	    } else if(fun == "previous") {
	    	if(current_page > 1) {
	    	$("#comments_inner").html("<center><img src='images/load.gif' /></center>");
	    	current_page--;
			$.post("js/calls.php", {'call[]' : ["get_page_profile", current_page, profile_id,per_page]}, function(data){
		                    $("#comments_inner").html(data);
		                });    
			}
	    }
    }
}
function add_comment_profile(profile_id, comment) {
					//add comment//
        			$.post("js/calls.php", {'call[]' : ["add_comment_profile", comment, profile_id]}, function(data2){
                    if(data2 == "2") {
                        alert("You need to be logged in to perform this action!"); 
                    } else if(data2 == "3") {
                        alert("You have already submited this comment");
                    } else {
                    	//get total number of pages
                    	$.post("js/calls.php", {'call[]' : ["last_page_profile", profile_id]}, function(last_page){
                    		total_pages = last_page;
	                    	//load spinner
		                    $("#comments_inner").html("<center><img src='images/load.gif' /></center>");
		               		//load up comments with the right page
							$.post("js/calls.php", {'call[]' : ["get_page_profile", last_page, profile_id, per_page]}, function(data){
				                    $("#comments_inner").html(data);
				                });
				    		current_page = total_pages;
		                   /// $('#comments_inner').append(data2);
		                    //load correct peggination
		                     $(".pagination").html("<center><img src='images/load.gif' /></center>");
							$.post("js/calls.php", {'call[]' : ["profile_pagination", profile_id]}, function(data3){
				                    $(".pagination").html(data3);
				                });
				            //scrolling, etc//
		                    var offsettop = parseInt($("body").css("height"));
							window.scrollTo(0,offsettop);
							$('#submitbutton').attr("disabled", true);
							var timer;
							timer = setTimeout(function() {
								$('#submitbutton').attr("disabled", false);
							}, 5000);
							$.scrollTo('#commentform');
						});
                    }
				});
    }
    
    
