$(function(){
	
	// STOP MOTION
	
	$('#projects-button').mouseenter(function(e){
		imgCounter = 0;
		$('#projects-animate').show();
		projectsImgChange();
		$(this).css({cursor: 'pointer'});
	});
	$('#projects-button').mouseleave(function(e){
		clearTimeout(p);
		imgCounter = 0;
		$('#projects-animate').hide();
	});
	
	$('#team-button').mouseenter(function(e){
		imgCounter = 0;
		$('#team-animate').show();
		teamImgChange();
		$(this).css({cursor: 'pointer'});
	});
	
	$('#team-button').mouseleave(function(e){
		clearTimeout(t);
		imgCounter = 0;
		$('#team-animate').hide();
	});
	
	$('#news-button').mouseenter(function(e){
		imgCounter = 0;
		$('#news-animate').show();
		newsImgChange();
		$(this).css({cursor: 'pointer'});
	});
	$('#news-button').mouseleave(function(e){
		clearTimeout(n);
		imgCounter = 0;
		$('#news-animate').hide();
	});
	
	$('#clients-button').mouseenter(function(e){
		imgCounter = 0;
		$('#clients-animate').show();
		clientsImgChange();
		$(this).css({cursor: 'pointer'});
	});
	$('#clients-button').mouseleave(function(e){
		clearTimeout(c);
		imgCounter = 0;
		$('#clients-animate').hide();
	});
	
	$('#contact-button').mouseenter(function(e){
		imgCounter = 0;
		$('#contact-animate').show();
		contactImgChange();
		$(this).css({cursor: 'pointer'});
	});
	$('#contact-button').mouseleave(function(e){
		clearTimeout(co);
		imgCounter = 0;
		$('#contact-animate').hide();
	});
	
	$('#about-button').mouseenter(function(e){
		imgCounter = 0;
		$('#about-animate').show();
		aboutImgChange();
		$(this).css({cursor: 'pointer'});
	});
	$('#about-button').mouseleave(function(e){
		clearTimeout(a);
		imgCounter = 0;
		$('#about-animate').hide();
	});
	
	function timeOut() {
		aboutImgChange();
		clearTimeout(a);
		projectsImgChange();
		clearTimeout(p);
		teamImgChange();
		clearTimeout(t);
		newsImgChange();
		clearTimeout(n);
		clientsImgChange();
		clearTimeout(c);
		contactImgChange();
		clearTimeout(co);
	};
	
	// IMAGE ROLLOVERS
	
	$('.image-roll').hover(function() {
		var hL = $(this).attr("rel");
		$('.'+hL).show();
	}, function() {
		var hL = $(this).attr("rel");
		$('.'+hL).hide();
	});
	
	// LINKS
	
	$(window).scroll(function() {
		if ( $.browser.mozilla) {
			currentScrollPos = document.documentElement.scrollTop;
		} else {
			currentScrollPos = document.body.scrollTop;
		};
		
		if (currentScrollPos <= 720) {
			$('#links').css({top: 770 - currentScrollPos + "px"});
		} else {
			$('#links').css({top: 50 + "px"});
		};
		
		if (popUpTrue != 0) {
			var docViewTop = $(window).scrollTop();
			var docViewBottom = docViewTop + $(window).height();
		
			var elemTop = $("#project-pop").offset().top;
			var elemBottom = elemTop + $("#project-pop").height();
			
			if ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)) {
			} else {
				$('#project-mesh').fadeOut("slow");
				$('#project-pop').fadeOut("slow", function() {
					$('#load-projects-container').remove();
				});
				popUpTrue = 0;
				window.history.replaceState("window", "writethisdown", "http://writethisdown.co.uk");
			};
		}
	
	});
	
	$(window).resize(function() {
		if( window.innerWidth < 1020 ) {
 			$('#links').css({'left':'540px'});
		} else {
  			$('#links').css({'left':'50%'});
		};
	});
	
	$('#links li').hover(function() {
		$(this).css({"background-position":"-55px 0px"});
	}, function() {
		$(this).css({"background-position":"0px 0px"});
	});
	
	$('.linkk').click(function(event) {
		var selectOR = $(this).attr('href');
		var targetOffset = $(selectOR).offset().top;
		$('html, body').stop().animate({scrollTop: targetOffset}, 400);
	});
	
	
	
	// CIRCLE LINK
	
	$('.c-over').hover(function() {
		$(this).css({"background-position": "140px 0px", "cursor": "pointer"});
	}, function() {
		$(this).css("background-position", "0px 0px");
	});
	
	$('.c-over').click(function() {
		$.scrollTo('#container',400);
	});
	
	
	// ABOUT
	
	$('.about-nav-links').hover(function(){
		$(this).addClass('about-nav-links-o');
	}, function() {
		$(this).removeClass('about-nav-links-o');
	});
	
	$('#about-services').click(function(){
		$(this).hide();
		$('#about-text').animate({left: '-800'}, 500);
		$('#services-text').animate({left: '0'}, 500, function(){
			$('#about-about').show();
		});
	});
	
	$('#about-about').click(function(){
		$(this).hide();
		$('#about-text').animate({left: '85'}, 500);
		$('#services-text').animate({left: '800'}, 500, function(){
			$('#about-services').show();
		});
	});
	
	// FOLIO

	function videoResize() {
		$('.single-film iframe, .single-film object, .single-film embed').each(function () {
			var w = $(this).attr('width');
			var h = $(this).attr('height');
			$(this).attr('width','600');
			$(this).attr('height',(h/w)*'600');
		});	
	};
	
	videoResize();
	
	var projectRowW = 922 - 60;
	
	$('.projects-row').each(function () {
		var rowCount = $('.project-row-title', this).attr('rel');
		$('.project-link', this).css({'width':(projectRowW / rowCount) - 2 +'px'});
	});
	
	$('.o-link').live('mouseenter', function(e) {
		$(this).css({"cursor":"pointer"});
		$('.o-link-over', this).show();
	});
	$('.o-link').live('mouseleave', function(e) {
		$('.o-link-over', this).hide();
	});
	
	var popUpTrue = 0;
	
	$('.project-link').live('click', function(e) {
		e.preventDefault();	
		$('#project-pop, #project-mesh').fadeIn("fast");
		var projectPos = $('#projects-container').offset().top;
		$('html, body').stop().animate({scrollTop: projectPos-145}, 400, function(){
			popUpTrue = 1;
		});
		var $link = $(this).attr('href');
		$('#project-loading').show();
		window.history.replaceState("", "Film Index", $link);
		var $linkk = $link + ' #load-projects-container';
		$('#project-load').load(($linkk),function(){
			twttr.widgets.load();
			FB.Share.renderPass();
			$('#project-loading').hide();
			videoResize();		
		});
	});
	
	$('.close, #project-mesh').live('click', function(e) {
		e.preventDefault();
		$('#project-mesh').fadeOut("slow");
		$('#project-pop').fadeOut("slow", function() {
			$('#load-projects-container').remove();
		});
		popUpTrue = 0;
		window.history.replaceState("", "writethisdown", "http://writethisdown.co.uk");
	});
	
	// TEAM
	
	var postNumber = $('#team-count').attr('rel');
	var height = 400;
	
	$('.team-thumb').css({height:( (height / 3) - 10 ) +"px"});
	
	$('.team-thumb').bind('click', function(e){
		e.preventDefault();
		$('#team-loader').show();
		var $teamLink = $(this).attr('href');
		$.get($teamLink, {}, function(data){
			var $response = $('<div />').html(data);
			var $teamLoad = $response.find('#load-team-container');
			$('#team-single-c').append($teamLoad);
			$('#team-loader').hide();
			$('#team-content-container').stop().animate({left: '-630'}, 500, function(){
				$('.team-single-back').show();
			});
		});
	});
	
	$('.team-single-infos a').live('click', function(event){
		event.preventDefault();
		$('.team-single-back').hide();
		$('#team-content-container').stop().animate({left: '0'}, 500, function(){	
				$('#load-team-container').remove();
		});
	});
	
	// CLIENTS
	
	$('.client-img').mousemove(function(e){
		var id = $(this).attr('rel');
		$('.client-highlight', this).show();
		$('.'+id).css({left: ((e.pageX) - ((window.innerWidth - 570) / 2)) + "px"}).show();
	});
	$('.client-img').mouseout(function(e){
		var id = $(this).attr('rel');
		$('.client-highlight', this).hide();
		$('.'+id).hide();
	});
	$('#client-scroll').scroll(function(){
		$('.client-highlight').hide();
		$('.client-info').hide();
	});
	
	// NEWS
	
	$('.news-item iframe, .news-item object, .news-item embed').each(function () {
		var w = $(this).attr('width');
		var h = $(this).attr('height');
		$(this).attr('width','355');
		$(this).attr('height',(h/w)*'355');
	});	
	
	var newsNumber = $('#news-count').attr('rel');
	
	$('#news-strip').css({width:(455 * newsNumber) +"px"});
	
	if (newsNumber > 2) {
		$('.news-older').show();
	};
	
	//$('.arrows').hover(function() {
	//	$(this).css({"filter":"alpha(opacity=40)","opacity":"0.4", "cursor": "pointer"});
	//}, function() {
	//	$(this).css({"filter":"alpha(opacity=100)","opacity":"1"});
	//});
	
	var animating = 0;
	var newsno = 1;
	
	$('.news-older').live('click', function(e){
		if (!animating) {
			animating = 1;
			$('#news-strip').animate({
				left: "-=455"
			}, 500, function() {
				animating = 0;
				newsno ++;
				if (newsno > (newsNumber - 2)) {
					$('.news-older').hide();
				};
				if (newsno != 1) {
					$('.news-newer').show();
				};
			});
		};
	});
	
	$('.news-newer').live('click', function(e){
		if (!animating) {
			animating = 1;
			$('#news-strip').animate({
				left: "+=455"
			}, 500, function() {
				animating = 0;
				newsno --;
				if (newsno < (newsNumber - 1)) {
					$('.news-older').show();
				};
				if (newsno == 1) {
					$('.news-newer').hide();
				};
			});
		};
	});

});
