var main = {
	init : function(){
		main.menu(); // Init menu
		$("article:last-child, li:last-child").addClass('last-child'); // Apply last-child classes
	},
	
	menu : function() {
		var hoverState;
		$("#main-menu > ul > li > a").hover(function() {
			$(hoverState).removeClass("submenu");
			$(this).parent().addClass("submenu");
			hoverState = $(this).parent();
		});
		// If we go anywhere outside the menu area clear the hover.
		$("#main").hover(function() {
			$(hoverState).removeClass("submenu");
		});
	},

	carousel : function() {
		// Initialize carousel
		$("#carousel .scrollable").scrollable();

		// Delay load of presented images
		$("#carousel .presented li.preload").each(function(index){
			var $this = $(this);
			// Get BG image url
			var imageUrl = $($this).css('background-image');
			imageUrl = imageUrl.replace(/"/g,"").replace(/url\(|\)$/ig, "");
			imageUrl = imageUrl.split("?url=");
			// Load data.  Use Image() as cross-domain is not allowed for straight loading
			img = new Image();
			img.src = imageUrl[1];
			$(img).load(function(){
				$($this).css('background', 'url(' + imageUrl[1] + ') no-repeat scroll 0 0 transparent');
				$($this).removeClass('preload');
			});
		});
		
		$("#carousel .presented ul.items li:eq(0)").addClass('active');
		
		// Carousel click function
		$("#carousel .scrollable .items a").click(
				function() {

					var activePane = $("#carousel .presented ul.items li.item.active");

					// console.log(activePane);

					// Get LI zero based index
					var liIndex = $(this).parent().index();
					// Get zero based position in ULs and update LI index based
					// on this
					liIndex = liIndex
							+ (($(this).parent().parent().index()) * 5);

					// Activate item
					$("#carousel .scrollable .items a").removeClass("active");
					$(this).addClass("active");

					// Activate main image
					$(activePane).fadeOut(
							'slow',
							function() {
								$("#carousel .presented ul.items li.item:eq("+ liIndex + ")").fadeIn().addClass('active');
								// console.log($(".presented ul.items
								// li.item:eq("+liIndex+")"));
							}).removeClass("active");

					return false;

					// when page loads simulate a "click" on the first image
				});
	}

};



var home = {
	init : function() {
		main.init(); // Init the menu dropdowns etc.
		main.carousel(); // Init carousel
	}
};

var about = {
	init : function() {
		main.init();
	}
};

var blog = {
	init : function() {
		main.init();
		// Studio selector
		// var domain = document.domain;
		var selected = $("#studio-select").attr('class');
		$("#studio-select").val(selected);
		var url = "/blog?studioId=";
		$("#studio-select").change(function() {
			selected = $("#studio-select").val();
			url = url + selected;
			window.location = url;
		});
	}
};

var careers = {
		init : function() {
			var historyState = true;
			if ($('html').hasClass("ie7")){
				var historyState = false;
			}
			main.init();
			main.carousel();
			$(".col1 .navigator .items ul").tabs(".col2 .presented > div",{ history: historyState, effect: 'fade' });
}
	};
