$(document)
		.ready(
				function() {
					$('#mainMenu ul, #filter-work ul').each(function() {
						$(this).children('li:last').addClass('last')
					});
					var bodyClass = $('body');
					if (bodyClass.hasClass('single-award')) {
						$('#mainMenu li a:contains("Awards")').parent()
								.addClass('current_page_item')
					}
					;
					if (bodyClass.hasClass('single-work')
							|| bodyClass.hasClass('work-category')) {
						$('#mainMenu li a:contains("Our Work")').parent()
								.addClass('current_page_item')
					}
					;
					if (bodyClass.hasClass('archive')
							|| bodyClass.hasClass('single-post')
							|| bodyClass.hasClass('search')) {
						$('#mainMenu li a:contains("Trendspotting")').parent()
								.addClass('current_page_item')
					}
					;
					var contentWidth = $('#central').width();
					$('#header .wrap').width(contentWidth + 10);
					var homeCount = 1;
					$('#featuredPosts li').each(function() {
						if (homeCount % 5 == 0) {
							$(this).css('clear', 'left')
						}
						homeCount++
					});
					$('.thumb').each(function() {
						$(this).append('<span class="mask"></span>')
					});
					var awardListElements = $('#awardList li');
					for ( var i = 0; i < awardListElements.length; i += 20) {
						awardListElements.slice(i, i + 20).wrapAll(
								'<li class="slide"><ul></ul></li>')
					}
					var peopleListElements = $('#peopleList li');
					for ( var i = 0; i < peopleListElements.length; i += 6) {
						peopleListElements.slice(i, i + 6).wrapAll(
								'<li class="slide"><ul></ul></li>')
					}
					$('#peopleList li .thumb')
							.click(
									function(event) {
										event.preventDefault();
										var getPeopleParent = $(this).parent(
												'li');
										var workName = $(this).attr('title');
										var workRole = $(this).attr('rel');
										var workImg = $(this).attr('href');
										$(getPeopleParent).addClass(
												'currentItem');
										$('#peopleList li')
												.not(getPeopleParent)
												.removeClass('currentItem');
										$('#currentPerson')
												.html(
														'<span class="thumb"><img src="'
																+ workImg
																+ '" alt="" /><span class="mask"></span></span><h2>'
																+ workName
																+ '</h2><h3>'
																+ workRole
																+ '</h3>')
									});
					$('#peopleList li li:first').addClass('currentItem');
					$('#project-nav, #people-nav, #awards-nav').hide();
					var workTotalThumbs = $('#project-slider li').length;
					var peopleTotalThumbs = $('#peopleList li').length;
					var awardTotalThumbs = $('#awardList .slide').length;
					if (workTotalThumbs > 1) {
						$('#project-nav').show()
					}
					;
					if (peopleTotalThumbs > 6) {
						$('#people-nav').show()
					}
					;
					if (awardTotalThumbs > 1) {
						$('#awards-nav').show()
					}
					;
					$('#awardList .sliderWrap').simpleScroller( {
						getPrevId : '#awardPrev',
						getNextId : '#awardNext'
					});
					$('#project-slider .sliderWrap').simpleScroller( {
						getPrevId : '#workPrev',
						getNextId : '#workNext',
						getThumbsId : '#project-thumbs'
					});
					$('#peopleList .sliderWrap').simpleScroller( {
						getPrevId : '#peoplePrev',
						getNextId : '#peopleNext'
					});

					/* Added to reload videos when scrolling so they stop playing */
					getCurrentIndex = function() {
						// Find current index
						var left = parseInt($('#project-slider .sliderWrap').css('left'));
						var index = Math.floor(-left / 540);

						return index;
					}
					stopVideos = function(index) {
						// Get video from current index if video item
						$('#project-slider .sliderWrap li').eq(index).filter('.videoSlide').each(function() {
							// Reload the node to stop video if playing
							var node = $(this).children(':first');
							var tagName = node.get(0).tagName.toLowerCase();

							if(tagName == 'iframe') {
								node.get(0).src = node.get(0).src;
							}
							else {
								var node = $(this).children(':first').remove();
								$(this).append(node);
								//node.get(0).stopVideo();
								//var myPlayer = document.getElementById('playerid');
								//myPlayer.stopVideo();
							}
						});
					};

					$('#project-thumbs li a').click(function(event) {
						event.preventDefault();
						var projectSlideCoord = $(this).attr('rel');

						index = getCurrentIndex();

						$('#project-slider .sliderWrap').animate( {
							left : projectSlideCoord
						}, 800, null, function() { stopVideos(index) });
					});
					$('#project-thumbs li:first a').click(function() {
						$('#workPrev').animate( {
							opacity : 0.3
						}, 500, function() {
							$(this).css('display', 'block')
						});
						$('#workNext').animate( {
							opacity : 1
						}, 500, function() {
							$(this).css('display', 'block')
						})
					});
					$('#project-thumbs li:last a').click(function() {
						$('#workNext').animate( {
							opacity : 0.3
						}, 500, function() {
							$(this).css('display', 'block')
						});
						$('#workPrev').animate( {
							opacity : 1
						}, 500, function() {
							$(this).css('display', 'block')
						})
					});
					$('#project-thumbs li').not(':first, :last').click(
							function() {
								$('#workPrev, #workNext').animate( {
									opacity : 1
								}, 500, function() {
									$(this).css('display', 'block')
								})
							});
					var count = 1;
					$(
							'#featuredProjects li, #allProjects li, #clientList li, #peopleList .slide li')
							.each(function() {
								if (count % 3 == 0) {
									$(this).addClass('alt')
								}
								count++
							});
					var hasFlash = false;
					try {
						var fo = new ActiveXObject(
								'ShockwaveFlash.ShockwaveFlash');
						if (fo)
							hasFlash = true
					} catch (e) {
						if (navigator.mimeTypes["application/x-shockwave-flash"] != undefined)
							hasFlash = true
					}
					if (hasFlash == false) {
						$('#featuredPosts #flashSlider').hide();
						$('#featuredPosts .sliderWrap').show()
					} else {
						$('#featuredPosts #flashSlider').show();
						$('#featuredPosts .sliderWrap').hide()
					}
					var lbLink = $('a[rel="lBox"]');
					var lbImg = $(lbLink).html();
					lbLink
							.click(function(event) {
								event.preventDefault();
								$('body')
										.append(
												'<div class="lightbox"><div class="lightBox-holder">' + lbImg + '<span class="close"></span></div></div>');
								var getImgWidth = $('.lightBox-holder img')
										.width();
								$('.lightBox-holder').width(getImgWidth)
							});
					$('.lightBox-holder .close').live('click', function() {
						$(this).parents('.lightbox').remove()
					})
				});
