PlaysRatings = {
	init: function() {
		Query.init();
	}
}

Results = {
	gameId: [],				//the array holding the id of the games
	gameRating: [],			//the array holding the rating of the games
	gamePlayCount: [],		//the array holding the play count of the games
	
	init: function(){
		
	},
	
	setupResults: function() {
		/* FOR LANDING PAGE */
		if ($('body').attr('id') == ('games-landing')) {
			$(".new-recommended-plays").each(function(){
				var gameId = $(this).closest('li').attr('id');
				var indexOfGame = jQuery.inArray(gameId, Results.gameId);
				
				$(this).text(Utilities.commafy(Results.gamePlayCount[indexOfGame]) + ' Plays');
			})
			
			$(".new-recommended-rating").each(function(){
				var gameId = $(this).closest('li').attr('id');
				var indexOfGame = jQuery.inArray(gameId, Results.gameId);
				
				$(this).text('Rating: ' + Utilities.lengthify(Results.gameRating[indexOfGame]));
			})
			
			$(".most-popular-plays").each(function(){
				var gameId = $(this).closest('li').attr('class').replace(' has-facebook', '');
				var indexOfGame = jQuery.inArray(gameId, Results.gameId);
				
				$(this).text(Utilities.commafy(Results.gamePlayCount[indexOfGame]) + ' Plays');
			})
			
			$(".most-popular-rating").each(function(){
				var gameId = $(this).closest('li').attr('class').replace(' has-facebook', '');
				var indexOfGame = jQuery.inArray(gameId, Results.gameId);
				
				$(this).text('Game Rating: ' + Utilities.lengthify(Results.gameRating[indexOfGame]));
			})			
		}
		/* FOR ALL GAMES PAGE */
		else if ($('body').attr('id') == ('games-all')) {
			$(".game-plays").not('.facebook-stat').each(function(){
				var gameId = $(this).closest('li').attr('id');
				var indexOfGame = jQuery.inArray(gameId, Results.gameId);
				
				$(this).text(Utilities.commafy(Results.gamePlayCount[indexOfGame]) + ' Plays');
			})
			
			$(".game-rating").not('.facebook-stat').each(function(){
				var gameId = $(this).closest('li').attr('id');
				var indexOfGame = jQuery.inArray(gameId, Results.gameId);
				
				$(this).text('Game Rating: ' + Utilities.lengthify(Results.gameRating[indexOfGame]));
			})
		}
		/* FOR ALL PLAY PAGES */
		else {
			var gameId = directory;
			var indexOfGame = jQuery.inArray(gameId, Results.gameId);
			
			$("#game-plays .game-stat").text(Utilities.commafy(Results.gamePlayCount[indexOfGame]))
			$("#game-rating .game-stat").text(Utilities.lengthify(Results.gameRating[indexOfGame]));
			
			$(".new-recommended-plays").each(function(){
				var gameId = $(this).closest('li').attr('id');
				var indexOfGame = jQuery.inArray(gameId, Results.gameId);
				
				$(this).text(Utilities.commafy(Results.gamePlayCount[indexOfGame]) + ' Plays');
			})
			
			$(".new-recommended-rating").each(function(){
				var gameId = $(this).closest('li').attr('id');
				var indexOfGame = jQuery.inArray(gameId, Results.gameId);
				
				$(this).text('Rating: ' + Utilities.lengthify(Results.gameRating[indexOfGame]));
			})
			
			$(".most-popular-plays").each(function(){
				var gameId = $(this).closest('li').attr('class').replace(' has-facebook', '');
				var indexOfGame = jQuery.inArray(gameId, Results.gameId);
				
				$(this).text(Utilities.commafy(Results.gamePlayCount[indexOfGame]) + ' Plays');
			})
			
			$(".most-popular-rating").each(function(){
				var gameId = $(this).closest('li').attr('class').replace(' has-facebook', '');
				var indexOfGame = jQuery.inArray(gameId, Results.gameId);
				
				$(this).text('Game Rating: ' + Utilities.lengthify(Results.gameRating[indexOfGame]));
			})
		}
		
		$("#rate-game .rating").click(function() {
			Query.averageRating($(this));
			Cookie.appendCookie($(this).text());
		})
	}
}

Query = {
	//SINGLE_GAME_URL: "http://games.adultswim.com/adultswimdynamic/stats/oneGame.jsp?gameId=biblefight",
	//STATS_FILE: "http://games.adultswim.com/adultswimdynamic/stats/view.config",
	//RELOAD_FILE: "http://games.adultswim.com/adultswimdynamic/stats/reload.config",
	//sample_url: "http://games.adultswim.com/adultswimdynamic/stats/increment/as/games/spellmyfinger/tp/1/stat.process",
	AVERAGE: "/adultswimdynamic/stats/average/as/games/",
	INCREMENT: "/adultswimdynamic/stats/increment/as/games/",
	RATING: "/rtg",
	PLAYCOUNT: "/tp",
	PROCESS: "/stat.process",
	ALL_GAMES_URL: "/adultswimdynamic/stats/allGames.jsp",
	type: "GET",
	error: false,
	
	init: function(){
		Query.load(Query.ALL_GAMES_URL, "GET");
	},
	
	load: function(xmlUrl, type) {
		Query.type = type;
		
		$.ajax({
			type: Query.type,
			url: xmlUrl,
			dataType: "xml",
			contentType: "text/xml",
			error: function(xhr, desc){
				Utilities.log('Error: ' + xhr + ' - ' + desc + '\n');
				Query.error = true;
			},
			success: function(xml){
				Utilities.log('success');
				Query.error = false;
				
				if (Query.type == "GET") {
					Query.storeResults(xml);
					Results.setupResults();
					
					try {
						Sorting.sortBySetup("most-rated");
					}
					catch(e) {
						Utilities.log('not on the all games page')
					}
				}
			},
			complete: function(){
				Utilities.log('complete');
				if (Query.error == true) {
					Utilities.log('Error!!!');
				}
			}
		});
	},
	
	storeResults: function(xml) {		
		$(xml).find('game').each(function(i) {
			Results.gameId[i] = $(this).attr('name');
			Results.gameRating[i] = $(this).attr('rtg');
			Results.gamePlayCount[i] = $(this).attr('tp');
		})
		
	},
	
	incrementPlayCount: function() {
		Query.load(Query.INCREMENT + directory + Query.PLAYCOUNT + '/1' + Query.PROCESS, "POST");
	},
	
	averageRating: function(div) {
		var rating = div.text();
		
		Query.load(Query.AVERAGE + directory + Query.RATING + '/' + rating + Query.PROCESS, "POST");
		
		$("#game-stats .rated").each(function() {
			$(this).removeClass('rated');
		})
		div.addClass('rated');
	}
}

var Cookie = {
	NAME: "ratings",
	options: { path: '/', expires: 10000 },
	
	getCookie: function() {
		if ($.cookie(Cookie.NAME) == null) {  // no cookie exists
			Cookie.setOriginalCookie();
		}
		else { // cookie exists but this game hasn't been rated
			Cookie.useCookie();
		}
	},
	
	setOriginalCookie: function() {
		$.cookie(Cookie.NAME, '', Cookie.options) // set cookie
	},
	
	useCookie: function() {
		var gameName = directory;
		var cookieString = $.cookie(Cookie.NAME); 
		var gameFilter = new RegExp("#" + gameName + "=([0-9.][^#]*)");
		
		var match = cookieString.match(gameFilter); 
		
		//if the game is in the cookie
		if (match != null) {
			var userRating = match[1];
			
			//if the number the user clicked is not equal to the current rating and the text is a two or less digit number
			if (($("#rate-game .rating").text() != userRating) && ($("#rate-game .rating").text().length < 3)) {
				cookieString = cookieString.replace(gameFilter, gameName + "=" + div.text())
				
				$.cookie(Cookie.NAME, cookieString, Cookie.options);
			}
			
			//if there's not a rating already highlighted
			if ($(".rating").hasClass('rated') == false) {
				var classToFind = ".rating" + userRating;
				
				$("#game-stats " + classToFind).addClass('rated');
			}
		}
	},
	
	appendCookie: function(rating){
		var gameName = directory;
		var cookieString = $.cookie(Cookie.NAME); // get cookie
		var gameFilter = new RegExp("#" + gameName + "=([0-9.][^#]*)");
		
		var match = cookieString.match(gameFilter);
		
		if (match == null) {
			$.cookie(Cookie.NAME, cookieString + '#' + gameName + '=' + rating, Cookie.options)  // set cookie
		}
		else {
			cookieString = cookieString.replace(gameFilter, '#' + gameName + "=" + rating)
			
			$.cookie(Cookie.NAME, cookieString, Cookie.options);
		}
	}
}
