$j.ajaxSetup({
	url: 'ajax.php',
	type: 'post',
	dataType: 'json',
	error: function (XMLHttpRequest, textStatus, errorThrown) {
		//template.overlay.message.error("server");
		template.overlay.parse({
			"content": XMLHttpRequest.responseText
		});
	}
});

var ajax = {
	"activeRequests": 0,
	// Login bar at the top of the site
	"login": {
		"user": "",
		"pass": "",
		"response": "",
		"check": function(u, p) {
			this.user = u;
			this.pass = p;
				
			template.login.showLoading("Checking details...");
				
			if ((this.user.length < 3) || (this.pass.length < 4) || !(template.login.clearedUsername)) {
				template.login.showLoaded('Invalid username/password<br /><a href="register.php?mode=forgotpass">Lost your password?</a>');
				return;
			}

			$j.ajax({
				data: {
					'mode':'login',
					'action':'check',
					'user':this.user,
					'pass':this.pass,
					'sid':Math.random()
				},
				success: function(json){
					if (json.success) {
						window.location = "index.php?login=login&user=" + json.user + "&pass=" + json.pass; // Pass from json as it's md5'd
					} else {
						if(json.invalid) {
							template.login.showLoaded('Invalid username/password<br /><a href="register.php?mode=forgotpass">Lost your password?</a>');
						}
					}
				}
			});
		}
	},
	// Search bar
	"search": {
		"suggestions": {
			"response": "",
			"lastCheck": 0,
			"find": function(s) {

				$j.ajax({
					data: {
						'mode':'search_suggest',
						'search':s,
						'sid':Math.random()
					},
					global: false,
					success: function(json){
						if (json.success) {
							var matches = new Array();
							matches = json.matches;
							
							template.searchSuggest.show(matches);
						} else {
							template.overlay.message.error("server");
						}
					}
				});
			}
		}
	},

	// Forums
	"forums": {
		"inp_tpc_id": "",
		"inp_text": "",
		"addPost": function(inp_tpc_id, inp_text) {
			this.inp_tpc_id = inp_tpc_id;
			this.inp_text = inp_text;
		
			$j.ajax({
				data: {
					'mode':'forums',
					'action':'add_post',
					'inp_tpc_id':this.inp_tpc_id,
					'inp_text':this.inp_text,
					'sid':Math.random()
				},
				success: function(json){
					if (json.success) {
						window.location = ("topic.php?t=" + json.tpc_id + "&p=" + json.tpc_num_pages + "#p" + json.pst_id);
					}
				}
			});
		}
	},
	
	"game": {
		"favourite": function(itm_id) {
			template.status.message.parse({
				"content": "Item added to your favourites"
			});
		},
		"score": function(itm_id, score) {	
			$j.ajax({
				data: {
					'mode':'game',
					'action':'score',
					'itm_id':itm_id,
					'score':score,
					'sid':Math.random()
				},
				success: function(json){
					if (json.success) {
						template.details.scoreSlider.success(json.newScoreF);
					}
				}
			});
		},
		"review": {
			"add": function(itm_id, text) {
				$j.ajax({
					data: {
						'mode':'game',
						'action':'review_add',
						'rev_itm_id':itm_id,
						'rev_text':text,
						'sid':Math.random()
					},
					success: function(json){
						template.overlay.parse({
							"content": "Review added successfully"
						});
					}
				});
			},
			"edit": function(itm_id, text) {
				$j.ajax({
					data: {
						'mode':'game',
						'action':'review_edit',
						'rev_itm_id':itm_id,
						'rev_text':text,
						'sid':Math.random()
					},
					success: function(json){
						if (json.success) {
							template.status.message.parse({
								"content": "Review updated successfully"
							});
						} else {
							alert(json.sql);
						}
					}
				});
			}
		}
	}
}