Cufon.replace("h1, h2.title, h3, h4", {letterSpacing : "-1px"});
Cufon.replace("#navigation li a, #play, .archiveLink", {hover : "true", letterSpacing : "-1px"});
Cufon.replace("h2 a.pageTitle", {hover : "true", letterSpacing : "-1px"});

function ready(player_id) {
	// Keep a reference to Froogaloop for this player                    
	var froogaloop = Froogaloop(player_id);                  
	
	froogaloop.addEvent('play', function () {
		$('#topbar, #projectSingleListContainer, #footer, #projectsDescriptionNarrow, #projectsSingleContainer p, #projectsSingleContainer li.pImage, .projectPage hr, h2').fadeTo(500, 0.1);
		$('li.videoItem').find('iframe').not('#' + froogaloop.element.id).fadeTo(500, 0.1);
		froogaloop.api('play');
	});
	
	froogaloop.addEvent('pause', function () {
		$('#topbar, #projectSingleListContainer, #footer, #projectsDescriptionNarrow, #projectsSingleContainer p, #projectsSingleContainer li.pImage, .projectPage hr, h2').fadeTo(500, 1);
		$('li.videoItem').find('iframe').fadeTo(500, 1);
		froogaloop.api('pause');
	});
	
	froogaloop.addEvent('finish', function () {
		$('#topbar, #projectSingleListContainer, #footer, #projectsDescriptionNarrow, #projectsSingleContainer p, #projectsSingleContainer li.pImage, .projectPage hr, h2').fadeTo(500, 1);
		$('li.videoItem').find('iframe').fadeTo(500, 1);
		froogaloop.api('pause');
	});
}

function projectLink() {
	$('.projectLink').click(function(e){
		var projLink = $(this),
			cat = projLink.prev('.eCat').val(),
			all = "",
			year = "";
			
		//Is the parent a category or a year?
		if (!cat) {
			all = "yes";
			cat = "";
			year = "";
		} else {
			year = "";
			all = "";
		}
		
		$.ajax({
			"url" : "http://manvsmachine.co.uk/scripts/sessions.php",
			"type" : "post",
			"data" : {"year": year, "cat": cat, "all": all},
			"success" : function (data) {
				var target = projLink.attr('href');
				
				$('#wrapper, #footer').fadeOut(300, function () {
					window.location = target;
				});
			}
		});
		return false;
	});
}

function moreNews() {
	var offset = $('#bulletinList li:last-child').attr("rel"),
		totalEntries = $('#bulletinList').attr("rel");
		
	$('#bulletinList li:last-child').removeClass('lastEntry');
	
	//LOAD IN ENTRIES
	$.ajax({
		"url" : "http://manvsmachine.co.uk/index.php?/bulletin/more-news",
		"type" : "post",
		"data" : "offset="+offset,
		"success" : function (data) {
			$("#bulletinList").append(data);
			Cufon.replace("h3");
			$('.new' + offset).fadeIn(500);
			var newOffset = $('#bulletinList li:last-child').attr("rel");
			
			if (newOffset >= totalEntries) {
				$('#bulletinMore').html("There are no more Bulletin entries").addClass('greyText').removeClass('greenText');
			}
		}
	});
}

function overlays () {
	$('.overlayLink').hover(function () {
		var thisLink = $(this),
			id = thisLink.attr("id");
			
		thisLink.children('.overlay').fadeIn(500);
		$('.projectsArchiveList li a[rel=' + id + ']').attr("id", "active");
	}, function () {
		var thisLink = $(this),
			id = thisLink.attr("id");
		
		thisLink.children('.overlay').fadeOut(300);
		$('.projectsArchiveList li a[rel=' + id + ']').attr("id", "");
	});
	
	$('.indexTitleNav').hover(function () {
		var thisLink = $(this),
			id = thisLink.attr("rel");
			
		$('#' + id).children('.overlay').fadeIn(500);
	}, function () {
		var thisLink = $(this),
			id = thisLink.attr("rel");
			
		$('#' + id).children('.overlay').fadeOut(300);
	});
}

function layouts () {
	var curr = $(window).width(),
		x = $('body').attr('id');
		
	if (curr > 1210 && x == "narrow") {
		$("#wrapper, #topbar, #footer").fadeOut(100, function () {
			$('body').attr("id", "wide");
			Cufon.replace("h1, h2, h3", {letterSpacing : "-1px"});
			$("#wrapper, #topbar, #footer").fadeIn(1000);
		});		
	} else if (curr < 1209 && x == "wide") {
		$("#wrapper, #topbar, #footer").fadeOut(100, function () {
			$('body').attr("id", "narrow");
			Cufon.replace("h1, h2, h3", {letterSpacing : "-1px"});
			$("#wrapper, #topbar, #footer").fadeIn(1000);
		});		
	}
}

//EXTERNAL SITE LINKS
function eLinks() {
	var a = new RegExp('/' + window.location.host + '/');
	$('a').each(function () {
		if (!a.test(this.href)) {
			$(this).attr("target", "_blank");
		}
	});
}

$(document).ready(function() {
	//IPAD QUIRKS
	var orig = $(window).width();

	$('#loading').fadeOut(100);
	$("#wrapper, #footer").fadeIn(600);

	//TITLE OVERLAYS
	if ($('.overlay').length > 0) {
		overlays();
	}
	
	//IE 6 DROPDOWN
	if (jQuery.browser.msie) {
		if (parseInt(jQuery.browser.version, 10) === 6) {
			var dropdown = "<div id='ieDropdown'><h2>Warning</h2><div id='closeIeDropdown'><a href='#'>close</a></div><p>This site is not optimised for Internet Explorer 6. We cannot guarantee that all the features of this site will look and function properly. Please upgrade your browser to the <a href='http://www.microsoft.com/windows/internet-explorer/worldwide-sites.aspx'>latest version here</a>.<br />Using the latest version will be more secure, faster and you will be able to experience better functionality across the internet.<br /><br />Alternatively you can download a different browser. We recommend <a href='http://www.google.com/chrome'>Chrome</a>, <a href='http://www.mozilla-europe.org/en/firefox/'>Firefox</a> or <a href='http://www.apple.com/safari/'>Safari</a>.</p></div>";
			$('body').append(dropdown);
			$('#closeIeDropdown a').click(function () {
				$(this).parent().parent().slideUp(500);
			});
		}
	}

	//EXTERNAL LINKS
	eLinks();

	//CENTRE ANIMATED GIFS IN CONTAINER
	if ($('.animatedGifContainer').length > 0) {
		var img = $('.animatedGifContainer').children(),
			imgW = img.width();
			
		img.css({ 'marginLeft' : (550 - imgW) / 2});
	}
	
	//CENTRE ANIMATED GIFS IN CONTAINER
	if ($('.animatedGifContainerProjects').length > 0) {
		var gifImg = $('.animatedGifContainerProjects').children(),
			gifImgW = gifImg.width();
			
		gifImg.css({ 'marginLeft' : (710 - gifImgW) / 2});
	}
	
	// Vimeo Videos
	if ($('.vimeo_video').length > 0) {
		// Set-up the video interactive bits
		$('iframe').each(function (i) {
			$(this).attr('id', 'vimeo_' + i);
			$(this).attr('src', $(this).attr('src') + '&player_id=vimeo_' + i);
		
			Froogaloop(this).addEvent('ready', ready);
		});
	}
	
	//WINDOW WIDTH
	if (orig < 1209) {
		$('body').attr("id", "narrow");
	} else {
		$('body').attr("id", "wide");
	}

	//WINDOW RESIZING
	$(window).resize(function () {
		layouts();
	});

	//SITE FADE NAV
	$('a.siteNav').click(function () {
		var target = $(this).attr('href');
		
		$('#wrapper, #footer').fadeOut(300, function () {
			$('#loading').fadeIn(400);
			window.location = target;
		});
		
		return false;
	});
	
	//READ MORE
	if ($('#readMoreLink').length > 0) {
		$('#readMoreLink').click(function () {
			var readLink = $(this);
			readLink.parent().prev().slideToggle(500);
			
			if (readLink.text() === "Read More...") {
				readLink.text("Close");
			} else {
				readLink.text("Read More...");
			}		
			return false;
		});
	}
	
	//ARCHIVE
	if ($('.archiveMasterLink').length > 0) {
		$('.archiveMasterLink').click(function () {
			var mLink = $(this),
				category = mLink.attr("rel"),
				year = "";
				
			if (mLink.hasClass('mArchiveOpen') === true) {
				mLink.parent().parent().next().next('.projectsArchiveList').slideUp(500);
				mLink.parent().parent().next('hr').animate({"width" : 100 + "%"});
				mLink.removeClass('mArchiveOpen').addClass('mArchiveClosed');
			} else {
				$("#projectListGrid").fadeOut(500);
				$('.mArchiveOpen').parent().parent().next().next('.projectsArchiveList').slideUp(500);
				$('.mArchiveOpen').parent().parent().next('hr').animate({"width" : 100 + "%"});
				$('.mArchiveOpen').removeClass('mArchiveOpen').addClass('mArchiveClosed');
				mLink.parent().parent().next('hr').animate({"width" : 30 + "px"});
				mLink.parent().parent().next().next('.projectsArchiveList').slideDown(500);
				mLink.addClass('mArchiveOpen').removeClass('mArchiveClosed');
				
				//LOAD IN ENTRIES
				$.ajax({
					"url" : "http://manvsmachine.co.uk/projects/filtered-content",
					"type" : "post",
					"data" : {"year": year, "category": category},
					"success" : function (data) {
						$("#projectListGrid").html(data);
						Cufon.replace("h1");
						$("#projectListGrid").fadeIn(500);
						overlays();	
						projectLink();
					}
				});
			}
			return false;
		});
		
		//SESSION MANAGEMENT
		$('.indexTitleNav').click(function (e) {
			var titleLink = $(this),
				parent = titleLink.parent().parent().prev().prev('.margin10').children().children('.archiveMasterLink'),
				cat = parent.attr("rel"),
				all = "",
				year = "";
				
			//Is the parent a category or not?
			if (!cat) {
				all = "yes";
				cat = "";
				year = "";
			} else {
				year = "";
				all = "";
			}
			
			$.ajax({
				"url" : "http://manvsmachine.co.uk/scripts/sessions.php",
				"type" : "post",
				"data" : {"year" : year, "cat" : cat, "all" : all},
				"success" : function (data) {
					var target = titleLink.attr('href');
					$('#wrapper, #footer').fadeOut(300, function () {
						window.location = target;
					});
				}
			});
			return false;
		});	
		projectLink();	
	}	
	
	//MORE NEWS
	if ($('#moreNewsLink').length > 0) {
		$('#moreNewsLink').click(function () {
			moreNews();
			return false;
		});
	}
	
	//BACK TO TOP LINK 
	if ($('#toTopLink').length > 0) {
		$('#toTopLink').click(function (e) {
			e.preventDefault();
			$('html, body').animate({'scrollTop' : 0}, 500, 'swing');
		});	
	}
});
