$(document).ready(function() {
	
	var game = window.location.hash.substring(1);
	if (game.length != 0) {
		showLightbox(game);
	}
	
	$(".input-text").placeholder();
	
	setupSlideshow();
	setupLightboxes();
	setupThumbnails();
	
	/* Homepage Slideshow */
	
	function setupSlideshow() {
		
		setInterval(advanceSlide, 7000);
		
		$("div.sub-slider ul li").click(function() {
			showSlide($(this).attr('id'));		
		});

		var sum = 0;
		
		$('div.sub-slider ul li').each( function(){ sum += $(this).outerWidth(); });
		$('div.sub-slider ul').width(sum);

		$("div.sub-slider span.right.arrow").click(function() {		
			slideTrayRight();
		});
		
		$("div.sub-slider span.left.arrow").click(function() {
			slideTrayLeft();
			
		});
		
	}
	
	function advanceSlide() {
		var currActive = $("div.sub-slider ul li.active");
		var nextActive = currActive.next("li");
		
		slideTrayRight();
		showSlide(nextActive.attr("id"));
	}
	
	function showSlide(gameName) {
		var currActiveButton = $("div.sub-slider ul li.active");
		
		var currSlide = $("#slideContainer li.active");
		var nextSlide = $("#" + gameName + "Slide");

		currActiveButton.removeClass("active");
		$("#" + gameName).addClass("active"); //Thumbnail in tray
		
		nextSlide.hide();
		nextSlide.addClass("transitioning active");
		
		nextSlide.fadeIn(1000, function() {
			currSlide.removeClass("active");
			nextSlide.removeClass("transitioning");
		});
	}
	
	var locked = false; //Wait until one animation is done first
	var slideWidth = $("div.sub-slider ul li:first-child").outerWidth();
	
	function slideTrayRight() {
		if (!locked) {
			locked = true;
			var slides = $("div.sub-slider ul");
			var leftSlide = $("div.sub-slider ul li:first-child");
	
			slides.animate( {
				"left": "-=" + slideWidth
				}, 700, function() {
				slides.append(leftSlide);
				slides.css("left", "0");
				locked = false;
			});
		}
	}
	
	function slideTrayLeft() {
		if (!locked) {
			locked = true;
			var slides = $("div.sub-slider ul");
			var rightSlide = $("div.sub-slider ul li:last-child");
			
			slides.prepend(rightSlide);
			slides.css("left", "-" + slideWidth + "px");
			
			slides.animate( {
				"left": "0" 
			}, 700, function() {
				locked = false;
			});
		}
	}
	

	
	
	
	function setupThumbnails() {
		$('div.thumbnails ul li img.small').click(function() {
			var bigVersion = $(this).next('.big');
			var featureBox = $(this).closest('div.thumbnails').children('div.featured');
			featureBox.children('*').replaceWith(bigVersion.clone());
			//mainImage.attr('src', bigImage.attr('src'));
		});
	}
	
	/* Lightboxes (used on games and jobs pages) */
	
	function setupLightboxes() {
		

		$('a.show-lightbox').click(function() {
			$('div#overlay').fadeIn();
			var lightbox = $("#" + $(this).attr('name') + "StoryLightbox");
			window.scrollTo(0, 0);
			lightbox.fadeIn();
			window.location.hash = lightbox.attr("id").slice(0, -8);
			lightbox.attr("tabindex",-1).focus(); 
			return false;
		});

		$('div.lightbox a.close, #overlay').click(function() {
			closeAll();
			return false;
		});

		$('div.controls a.prev').click(function() {
			rotateGame($(this).closest("div.lightbox"), true);
			return false;
		});

		$('div.controls a.next').click(function() {
			rotateGame($(this).closest("div.lightbox"), false);
			return false;
		});

		$("div.lightbox").keyup(function(e) {

			if (e.keyCode == 37) //Left
				rotateGame($(this), true);
			else if (e.keyCode == 39) //Right
				rotateGame($(this), false);
			else if (e.keyCode == 27) //Escape
				closeAll();
		});
	}
	
	function showLightbox(id)
	{
		$('div#overlay').fadeIn();
		gameToShow = $("div.lightbox#" + id + "Lightbox");
		gameToShow.fadeIn();
		gameToShow.attr("tabindex",-1).focus(); 
		
	}

	function rotateGame(currentGame, isPrev) {
		var newGame = isPrev ? currentGame.prev("div.lightbox") : currentGame.next("div.lightbox");
		if (newGame.length != 0) {
			currentGame.fadeOut(500);
			newGame.fadeIn();
			window.location.hash = newGame.attr("id").slice(0,-8);
			newGame.attr("tabindex",-1).focus(); 
		}
	}
	
	function closeAll() {
		$('div#overlay').fadeOut();
		$('div.lightbox').fadeOut();
		window.location.hash = "";
		return false;
	}
	
	
	
	
});

function highlightTab(i)
{
	$("ul.nav li").removeClass("active");
	$("ul.nav li:nth-child(" + i + ")").addClass("active");
}

function showAccountTransferDialog() {
    var iframe = document.getElementById('iframe')

        iframe.src = "./account_transfer.php";
    iframe.parentNode.style.display = 'block';
}

function hideAccountTransferDialog() {
    var iframe = document.getElementById('iframe')

        iframe.parentNode.style.display = 'none';
}


