$(document).ready(function() {
 
	$.backstretch(backgroundUrl,{speed: 500},function(){
		revealNews(0);
		
		fitContent();
		$("nav").show("puff",function(){ revealMenu(0); });
	});
	
 	distribute("footer","footer a");
 	
 	$("footer a").bind("mouseenter click", function(){
		var popup = bubble("brandPopUp_" + $(this).attr("id").substr(6),'Please wait - loading content...');
		var pos = $(this).offset();
		var left = (pos.left + Math.ceil($(this).width() / 2)) - 220;
		if (left < 0) {
			left = 0;	
		}
	
		if (left > ($(document).width() - 470)) {
			left = $(document).width() - 480;	
		}
		popup.hide();
		popup.css("bottom", 70 - $(document).scrollTop() + 'px').css("left", left + "px").show("fade");
		popup.children(".bubbleContent").addClass("distribution" + $(this).attr("id").substr(6));
		
		
		
		$("body").append(popup);
		
		getContent($(this).attr("href"));
		
		$(this).animate({
				opacity: 1	
			},
			200
		);
		
		return false;
	});
	
	$("footer a").mouseleave(function(){
		$(".bubble[id*='brandPopUp']").hide("fade",200,function(){$(this).remove(); });
		$(this).animate({
				opacity: .5	
			},
			200
		);
	});
	
	$("nav .menuItem").click(function(){
		$("#page #inner").remove();
		$("#page").html('<div id="inner" class="' + $(this).children("h3").children("a").attr("href").replace("/","") + '">Please wait loading content...</div>');
		getContent($(this).children("h3").children("a").attr("href"));
		$("#page #inner").css("height", ($(window).height() - $("header").height() - $("footer").height() - 36) + 'px');
		$("#page #inner").show("slide",{direction: "left"},500);
		
		$(".selected").removeClass("selected");
		$(this).addClass("selected",200);
		
	});
	
	$("nav a").click(function(){
		$(this).parent().parent().trigger("click");
		return false;
	});
	
	$("#subscribe form").submit(function(){
		if ($(this).children("input[name='email']").val()) {
			$.ajax({
				url: "/subscribe",
				data: $(this).serialize(),
				type: "POST",
				success: function(data) {
					$("#subscribe").html(data);
				}
			});
		}
		else
		{
			alert("Please enter your email address to subscribe to offers.");	
		}
		return false;
	});
	
	$(window).resize(function(){
		distribute("footer","footer a:visible");
		fitContent();
	});
	
	$(window).scroll(function(){
		$(".bubble[id*='brandPopUp']").css("bottom", 70 - $(document).scrollTop() + 'px');
	});
	
});

function fitContent() {
	var win_height = $(window).height();
	var win_width = $(window).width();
	$("nav .container").css("height", win_height - $("header").height() - $("footer").height() + 20 + 'px');
	$("#page #inner").css("height", (win_height - $("header").height() - $("footer").height() - 36) + 'px');
	if (win_width < 1300) {
		$("aside").css("width","0px").css("display","none");
		$("#page").css("width","65%").css("left","35%");
		$("nav").css("width","35%");	
	}
	else
	{
		$("aside").css("width","25%").css("display","block");
		$("#page").css("width","50%").css("left","25%");
		$("nav").css("width","25%");		
	}
	
	if (win_height < 700) {
		$(".menuContent p").hide();	
	}
	else
	{
		$(".menuContent p").show();	
	}
}

function bubble(id,content) {
	var bubble = $("#bubble").clone();
	bubble.attr("id", id);
	bubble.children(".bubbleContent").html(content);
	return bubble;
}

var content = new Object;

function getContent(url) {
	if (content.hasOwnProperty(url)) {
		$("." + url.replace("/", "").replace("/", "")).html(content[url]);
	}
	else
	{
		$.get(url, function(data) {
			url = $(this)[0].url + '';
			$("." + url.replace("/", "").replace("/", "")).html(data);
			content[url] = data;
		});
	}
}

function revealNews(index) {
	$("#news div:eq(" + index + ")").show("drop",{},200,function(){
		if ($(this).index() < $("#news div").length) {
			revealNews($(this).index() + 1);	
		}
	});
}

function revealGame(index) {
	$("#games div.game:eq(" + index + ")").show("slide",{},100,function(){
		if ($(this).index() < $("#games div.game").length) {
			revealGame($(this).index() + 1);	
		}
	});
}

function revealMenu(index) {
	$("nav .container div:eq(" + index + ")").show("slide",{direction: 'up'},100,function(){
		if ($(this).index() < ($("nav .container div").length - 1)) {
			revealMenu($(this).index() + 1);	
		}
		else
		{
			$("#page div.reveal").show("slide",{direction: "left"},200);	
			 revealGame(0);
		}
	});
}

function distribute(container, links) {
	var containerWidth = $(container).width();
	var listWidth = 0;
	$(links).each( function () {
	listWidth = listWidth + $(this).width(); 
	} );
	var distance = containerWidth - listWidth;
	i = 1;
	$(links).each( function () {
	var oneDistance = Math.floor( distance/($(links).length - i) );
	if (oneDistance < 0) {
		oneDistance = 0;	
	}
	var marginRight = 0;
	var marginLeft = 0;
	if (oneDistance > 0) {
		marginRight = Math.floor(oneDistance / 2);	
		marginLeft = Math.floor(oneDistance / 2);	
	}
	$(this).css("margin-right", marginRight).css("margin-left", marginLeft);
	distance = distance - oneDistance;
	i++;
	} );
}

