$(document).ready(function() {
	GamesIndividual.init();
	GametapModule.init();
	SexyLinks.init();
})

GamesIndividual = {
	gameArray: [],
	
	init: function(){
		GamesIndividual.positioning();
		GamesIndividual.gameButtons();
		
		Cookie.getCookie();
	},
	
	positioning: function() {
		var recommendedHeight = $("#new-recommended-games").outerHeight(true);
		var mostPopularHeight = $("#most-popular-games").outerHeight(true);
		var mobileHeight = $("#mobile-games").outerHeight(true);
		
		//Utilities.log('recommendedH: ' +  recommendedHeight + ' :: mobileH: ' + mobileHeight + ' :: mostPopularH: ' + mostPopularHeight)
		
		if	(recommendedHeight >= mostPopularHeight && recommendedHeight >= mobileHeight) {
			//Utilities.log('recommended is tallest');
			$("#most-popular-games").css('height', recommendedHeight - 4); 
			$("#mobile-games").css('height', recommendedHeight - 4);
		}
		else if (mostPopularHeight >= mobileHeight && mostPopularHeight >= recommendedHeight) {
			//Utilities.log('most popular is tallest');
			$("#new-recommended-games").css('height', mostPopularHeight - 4);
			$("#mobile-games").css('height', mostPopularHeight - 4);
		}
		else if (mobileHeight >= mostPopularHeight && mobileHeight >= recommendedHeight) {
			//Utilities.log('mobile is tallest');
			$("#new-recommended-games").css('height', mobileHeight - 4);
			$("#most-popular-games").css('height', mobileHeight - 4); 
		}
	},
	
	gameArea: function(width, height) {
		var gameAreaWidth = width;
		var gameAreaHeight = height;
		
		$("#game").width(gameAreaWidth);
		$("#game").height(gameAreaHeight);
		$("#game").show();
	},
	
	gameButtonsPosition: function() {
		var buttonsWidth = 0;
		
		$(".game-button").each(function(i) {
			buttonsWidth += $(this).outerWidth(true);
		})
		
		$("#game-buttons").css('width', (buttonsWidth + 6) + 'px')
		
	},
	
	gameButtons: function() {
		$("#pick-any-game").click(function() {
			$(this).hide();
			$("#pick-any-game-expanded").show();
			
			$(".ie7 #game-swf, .ie8 #game-swf").hide();
			
			//stops click events when clicking in expanded box
			$("#pick-any-game-expanded").click(function(event) { 
				var target = $(event.target);
				if (target.is('a')) {
					//let it ride
				}
				else {
					//whoa, whoa, whoa
					return false; 
				}
			});
			
			//one time click event for whole document
			$(document).one("click", function() { 
				$("#pick-any-game-expanded").hide(); 
				$("#pick-any-game").show();
				if ($("#prerollBlock").length == 0) {
					$(".ie7 #game-swf, .ie8 #game-swf").show();
				}
			});
			
			//prevents $(document).one() from firing
			return false;
		})
		
		$("#pick-any-game-expanded p").click(function() {
			$("#pick-any-game-expanded").hide();
			$("#pick-any-game").show();
			if ($("#prerollBlock").length == 0) {
				$(".ie7 #game-swf, .ie8 #game-swf").show();
			}
		})
		
		$("#dim-lights-button").click(function() {
			if ($(this).hasClass('lights-off')) {
				 //When you turn the lights back on fade out the "dim" layer over 500 milliseconds and then remove it
       			$('#fade').fadeOut(250, function() { 
					$("#fade").remove(); 
				});
				$(this).text('Dim the lights')
				$(this).removeClass('lights-off')
			}
			else {
				$('body').append('<div id="fade"></div>'); //When you turn the lights off add the "dimmed" layer to the page
				$('#fade').fadeIn(250); //Fade in the "dim" layer over 250 milliseconds
				$(this).text('Bring up the lights')
        		$(this).addClass('lights-off')
			}
		})
		
		/* GAME BUTTONS */
		
		$("#comments-button").click(function(e) {
			$(window).scrollTop($("#comments").offset().top - 10);
		}); 
		
		$("#instructions-button, .email").click(function(event) {
			var target = $(event.target);
			var type = $(this).attr('id').replace("-secondary-button", "").replace("-button", "").replace("-main-share-icon", "");
			
			if (target.is("#" + type + "-button") || target.is("#" + type + "-main-share-icon") || target.is("#" + type + "-secondary-button")) {
		       event.stopPropagation();               
		       event.preventDefault();
		    }
			
			if ($("#" + type + "-box").css('display') != "block") {
				$(".on").removeClass('on');
				
				$("#email-box").hide();
				$("#instructions-box").hide();
				$("#share-box").hide();
				
				$("#" + type + "-box").show();
				
				if ($(this).hasClass('game-button')) {
					//add the new active class to the new active tab
					$(this).addClass('on');
				}
				else {
					$("#share-button").addClass('on');
				}
			}
		})
		
		$(".facebook, .twitter, .reddit").click(function(event) {
			var target = $(event.target);
			
			if (target.is($(this))) {
		       event.stopPropagation();               
		       event.preventDefault();
		    }
			
			window.open($(this).attr('title'), "_blank");
		});
		
		$("#high-scores-button").click(function(e) {
			if(e.ctrlKey) {
				window.open("http://highscores.adultswim.com/highscores/index.jsp?game=" + highscoresID + "&filter=0&itemsPerPage=50", "_blank");
			}
			else {
				window.open("http://highscores.adultswim.com/highscores/index.jsp?game=" + highscoresID + "&filter=0&itemsPerPage=50", "_self");
			}
		})
		
		$("#email-button").click(function(e) {
			if(e.ctrlKey) {
				if (dboard.isLoggedIn()){
					window.open("https://login.adultswim.com/services/aswim/flow/editUser?referrer=" + escape(location), "_blank");
		
				} else {
					window.open("https://login.adultswim.com/services/aswim/flow/editUser?referrer=" + "http://login.adultswim.com/Community/Account", "_blank");
				}
				window.open("https://login.adultswim.com/services/aswim/flow/editUser?referrer=" + escape(location), "_blank");
			} 
			else {
				if (dboard.isLoggedIn()){
					window.open("https://login.adultswim.com/services/aswim/flow/editUser?referrer=" + escape(location), "_self");
		
				} else {
					window.open("https://login.adultswim.com/services/aswim/flow/editUser?referrer=" + "http://login.adultswim.com/Community/Account", "_self");
				}
			}
		}); 
		
		$(".close-button").click(function() {
			//remove the active classes from the current active button
			$(".on").removeClass('on');
			
			$(this).closest('.hidden-box').hide();
		});
	},
	
	formValidation: function() {
		if ($("#your-name").val() == "" || $("#your-email").val() == "" || $("#friends-name").val() == "" || $("#friends-email").val() == "") {
			if ($("#your-name").val() == "") {
				$("#your-name-label").addClass("no-email");
			}
			else {
				$("#your-name-label").removeClass("no-email");
			}
			
			if ($("#your-email").val() == "") {
				$("#your-email-label").addClass("no-email");
			}
			else {
				$("#your-email-label").removeClass("no-email");
			}
			
			if ($("#friends-name").val() == "") {
				$("#friends-name-label").addClass("no-email");
			}
			else {
				$("#friends-name-label").removeClass("no-email");
			}
			
			if ($("#friends-email").val() == "") {
				$("#friends-email-label").addClass("no-email");
			}
			else {
				$("#friends-email-label").removeClass("no-email");
			}
			
			$("#required").removeClass("required-sent");
			$("#required").addClass("required-error");
			
			$("#required").text("All Fields Required")
			
			return;
		}
		else {
			$("#required").removeClass("required-error");
			$("#required").addClass("required-sent");
			
			$("#required").text("Your Message has been sent")
			
			$("#your-name-label").removeClass("no-email");
			$("#your-email-label").removeClass("no-email");
			$("#friends-name-label").removeClass("no-email");
			$("#friends-email-label").removeClass("no-email");
			
			$("#email-form").submit();
			
			$("#your-name").val() = "";
			$("#your-email").val() = "";
			$("#friends-name").val() = "";
			$("#friends-email").val() = "";
		}
	}
}

function endAd() {
	$("#game-ad").remove();
	$("#prerollBlock").show();
}

function endPreroll() {
	$("#prerollBlock").remove();
	GamesIndividual.gameArea(gameWidth, gameHeight);
	
	if (flashVars == '') {
		$("#game-swf").show();
	}
	else {
		$("#game-swf").empty();
		document.getElementById('game-swf').innerHTML = gameEmbedCode;
		$("#game-swf").show();
	}
	
	$('#game-stats').show();
	Query.incrementPlayCount();
}
