	$(document).ready(function() {
	
		var pages = new Array;
		var pageTops = new Array;
		
		$(".page").each(function() {
			var id = $(this).attr('id');
			if(id=="footer") id = "press";
			pages.push(id);
			pageTops.push($(this).offset().top);
		});
		
		pages.push('press-articles');
		pageTops.push($("#footer").offset().top+20);
		
		var videoOpen = false;
		
		function closeVideo() {
			$("#videospace").slideUp(1000);
			$('#header').fadeIn(1000);
			$("div.row, div.page:not(#work)").animate({'opacity': '1.0'}, 1000, '', function() {
				$("#videospace").remove();
			});
			videoOpen = false;
		}
	
		$(window).scroll(function() {
								  
			if(ie) return;
			
			var userX = $(window).scrollTop();
			
			$.each(pageTops, function(key, value) {
				if((userX+100)>value&&(userX+100)<pageTops[key+1]) location.hash = "!"+pages[key];
			});
			
			if (userX==0) {
				$("#header").css({'height': '110px'});
				location.hash = "";
				if(videoOpen) closeVideo();
			};
		
		});
		

	    $('.menu a, .navmenu a').click(function() {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			var anchorName = this.hash.slice(1);
			if($target.length) {
				var targetOffset = $target.offset().top - 80;
				$('html,body').animate({scrollTop: targetOffset}, 1000, '', function() {
					location.hash = "!"+anchorName;
				});
		   		return false;
		  	}
	  	});
		
		$(".page:not(#work)").bind('click', function(ev) {
			if(videoOpen) {
				closeVideo();
				ev.preventDefault();
			}
		});
		
		$("#work .row .block:not(.intro) a").click(function(ev) {
			if(videoOpen) {
				closeVideo();
				ev.preventDefault();
				return;
			};
			var href = $(this).attr('href');
			var row = $(this).parent().parent();
			$.ajax({
				url: href + "?type=ajax",
				dataType: "json",
				success: function(data) {
					
					row.after('<div id="videospace"></div>');
					$("#videospace").slideUp(1);
					$("#videospace").html(data.html).slideDown(1000);
					
					var topDif = ($(window).height() - $("#videospace div").height() - 70) / 2;
					$('html,body').animate({"scrollTop": ($("#work-"+data.id).offset().top - topDif)}, 1000);
					$('#header').fadeOut(700);
					$("div.row, div.page:not(#work)").animate({'opacity': '0.6'}, 1000);
				},
				error: function() {
					window.location = href;
				}
			});
			videoOpen = true;
			ev.preventDefault();
		});
		
		$("a.close-work").live('click', function(ev) {
			closeVideo();
			$('html,body').animate({"scrollTop": ($("#work").offset().top - 80)}, 1000);
			ev.preventDefault();
		});
		
		if(location.hash!="") {
			var hash = location.hash;
			hash = hash.replace('#', '');
			if(hash.charAt(0)=="!") {
				hash = hash.replace('!', '');
				if($("#"+hash).length)
					$('html,body').animate({"scrollTop": ($("#"+hash).offset().top - 80)}, 700);
			}
		} else {
			setTimeout( function() { window.scrollTo(0, 1); } , 100);
		};
		
		var introblockHover;
		
		function closeIntroBlock(parent) {
				var infobox = $(".infobox", parent);
				infobox.animate({width: '50px'}, 250);
				$(".box", infobox).animate({width: '0px'}, 250, function() {
					infobox.animate({width: '0px'}, 50, function() {
						infobox.remove();
					});
				});
		};
		
		$(".block.intro a").mouseover(function() {
			if($(".infobox", this).length>0) { if(introblockHover) clearTimeout(introblockHover); return; };
			$(this).append('<div class="infobox"></div>');
			var infobox = $(".infobox", this);
			infobox.css({ 'top': '-70px', 'left': '82px', 'position': 'absolute' });
			infobox.css({'width': '0px'});
			infobox.append('<div class="arrow">&nbsp;</div><div class="box"><p>Simply mail us your script or idea and we can supply you with a tailored online reel, featuring commercials relevant to your brief.</p></div>');
			$(".box", infobox).css({'width': '0px'});
			//$(".box", infobox).animate({width: '36px'}, 100);
			infobox.animate({width: '51px'}, 50, function() {
				setTimeout(function() {
					infobox.animate({width: '250px'}, 250);
					$(".box", infobox).animate({width: '236px'}, 250);
				}, 10);
			});
		}).mouseout(function() {
			var parent = $(this);
			introblockHover = setTimeout(function() { closeIntroBlock(parent); }, 500);
		});
		
	});

