$(document).ready(function(){



	/*
	 * append class to last item in list
	*/
	$('.cols-3 li:last-child').addClass("last");
	
	
	
	/*
	 * stop twitter widget title (and others) being a link. Title bar should just alow expand/collapse.
	*/
	$('#sidebar h2 a').each(function() {
		$(this).parent().html($(this).text());
	})




	/*
	 * Talent page rollovers
	*/
	$('.talentContent .pagecontent li .img a').append("<div class='mask'></div>");
	
	$('.talentContent .pagecontent li a').not('.popup a').click(function() {
		if($('.talentContent .pagecontent li.poppedUp').length == 0) {
			$(this).closest('li').addClass('poppedUp').find('.popup').animate({opacity: 'show'}, {duration: 300, easing: "linear"});
			$('body').addClass("talentPopup");
		}
		return false;
	})
	
	$('.talentContent .popup a.close').click(function() {
		$(this).closest('.popup').animate({opacity: 'hide'}, {duration: 300, easing: "linear"}).closest('li').removeClass('poppedUp');
		$('body').removeClass("talentPopup");
		return false;
	})
	
	
	
	
	
	
	/*
	 * Homepage showreel reveal
	*/
	$('#showreelReveal_button').click(function() {
			$('#showreelReveal_button_wrap').animate({height: 'hide'}, {duration: 300, easing: 'easeOutQuad', complete: function() {
					$('#showreelReveal').animate({height: 'show'}, {duration: 500, easing: 'easeInQuad', complete: function() {
							$('#showreelReveal .inner').css('visibility', 'visible');																								  
					}})
					}
			});
	});
	
	
	
	
	
	
	
	/*
	 * Global search button replace
	*/
	$('#searchsubmit').addClass("hide");
	$('#searchsubmit').after('<div id="searchsubmit_js"></div>');
	$('#searchsubmit_js').click(function() {
		$(this).parent('form').submit();
	});	
	
	$('#searchform').hover(function() {
		$(this).addClass("hover");
	},
	function() {
		$(this).removeClass("hover");
	});
	
	var searchStartVal = "search";
	$('#s').val(searchStartVal);
	
	$('#s').focus(function() {
		if ($(this).val() == searchStartVal) {
			$(this).val('');
		}
	})
	
	$('#s').blur(function() {
		if ($(this).val() == searchStartVal || $(this).val() == "") {
			$(this).val(searchStartVal);
		}
	});
	
	
	
	
	
	
	/*
	 * Widget expand/collapse
	*/
	$('.widget h2').click(function() {
		
		if ($(this).parent().hasClass('widgetCollapse')  || $(this).parent().hasClass('widgetCollapseInitial')) {			
		
			$(this).parent().removeClass('widgetCollapse').find('.inner').animate({height: 'show'}, {duration: 300, easing: 'easeOutQuad', complete:function() {				
				$(this).parent().removeClass('widgetCollapseInitial');
				if ($(this).parent().attr('id').indexOf('se-contact-us') != -1) {
					// initialise google maps as it doesn't like initialising when it's collapsed
					gmaps_initialize_widget();
				}
			}});
		} else {
			$(this).parent().addClass('widgetCollapse').find('.inner').animate({height: 'hide'}, {duration: 300, easing: 'easeOutQuad'});
		}
	});
	
	
	
	
	
	
	
	/*
	 * slideshow functionality for both main slideshow and widget slideshow
	*/
	var iteration = [];
	
	function slideshow(el, oldSlide, newSlide) {
		$(el).parents('.slideshowWrap').find('ul.slideshow li:eq('+oldSlide+')').animate({opacity: 'hide'}, {duration: 100, easing: 'linear', complete: function() {
			$(el).parents('.slideshowWrap').find('ul.slideshow li:eq('+newSlide+')').animate({opacity: 'show'}, {duration: 100, easing: 'linear'});
		}});
	}
				
	$('ul.slideshow').not('.slideshow-singlePost').each(function(i) {
				if ($(this).hasClass('minislideshow')) {
					$(this).wrap('<div class="minislideshowWrap slideshowWrap"></div>');
					if ($(this).parents('li.widget').hasClass('carousel-type-just-launched')) {
						var itemURL = $(this).closest('.slideshowWrap').find('.minislideshow li:eq(0) a.permLink').attr('href');
						$(this).parent().after('<div class="primary"><a class="standard" href="'+itemURL+'">More</a></div>');
					}
				} else {
					$(this).wrap('<div class="slideshowWrap"></div>');
				}	
				
				el = $(this).parent();	
	
				$(el).append('<div class="paginate"></div>');
				$(el).find('.paginate').html('<ul></ul>');
				
				$(el).find('ul.slideshow li').each(function(i) {
					$(this).parents('.slideshowWrap').find('.paginate ul').append('<li class="img img'+i+'">'+i+'</li>');
				});
				
				if (!$(el).hasClass('minislideshowWrap')) {
					$(el).find('.paginate ul').prepend('<li class="prev">previous</li>').append('<li class="next">next</li>');
				}
				
				// set first image to show
				$(el).find('ul.slideshow li').not(':eq(0)').css('display', 'none');
				$(el).find('.paginate li.img:eq(0)').addClass('active');	
				
				// fix the height of the carousel so it doesn't jump
				
				var thisHeight = $(el).find('ul.slideshow').height();
				/* debug */ console.log('thisHeight:' + thisHeight); /* enddebug */
				$(el).find('ul.slideshow').height(thisHeight);				
				/* debug */ var newHeight = $(el).find('ul.slideshow').height(); console.log('newHeight:' + newHeight); /* enddebug */
				
				
				// click an image button
				$(el).find('.paginate li.img').click(function() {
					if (!$(this).hasClass('active')) {
						var wrap = $(this).closest('.slideshowWrap');
						if ($(wrap).find('li:animated').length == 0) {
							var slideshowOld = $(wrap).find('.paginate li.active').prevAll('.img').length;
							var slideshowActive = $(this).prevAll('.img').length;
							$(this).addClass('active').siblings('.img').removeClass('active');
							slideshow(this, slideshowOld, slideshowActive);
							if ($(wrap).hasClass('minislideshowWrap')) {
								if ($(this).closest('li.widget').hasClass('carousel-type-just-launched')) {
									// if it's the "just launched" widget then we need to change the URL for the "More" button
									var itemURL = $(wrap).find('.minislideshow li:eq('+slideshowActive+') a.permLink').attr('href');
									$(wrap).next().find('a').attr('href', itemURL);
								}
							}
						}
					}
				});
				
				// previous button
				$(el).find('.paginate li.prev').click(function() {
					var wrap = $(this).closest('.slideshowWrap');
					if ($(wrap).find('li:animated').length == 0) {
						var numberOfImages = $(wrap).find('.paginate li.img').length;
						var slideshowOld = $(wrap).find('.paginate li.active').prevAll('.img').length;
						var slideshowActive = (slideshowOld == 0) ? numberOfImages-1 :  slideshowOld - 1;						
						$(wrap).find('.paginate li.img:eq('+slideshowActive+')').addClass('active').siblings('.img').removeClass('active');
						slideshow(this, slideshowOld, slideshowActive);
					}
				});
				
				// next button
				$(el).find('.paginate li.next').click(function() {	
					var wrap = $(this).closest('.slideshowWrap');
					if ($(wrap).find('li:animated').length == 0) {
						var numberOfImages = $(wrap).find('.paginate li.img').length;
						var slideshowOld = $(wrap).find('.paginate li.active').prevAll('.img').length;
						var slideshowActive = (slideshowOld == numberOfImages-1) ? 0 :  slideshowOld + 1;						
						$(wrap).find('.paginate li.img:eq('+slideshowActive+')').addClass('active').siblings('.img').removeClass('active');
						slideshow(this, slideshowOld, slideshowActive)
					}
				});
				
				
				// if hover, stop iterating
				$(el).parents('.inner').hover(function() {
					clearInterval(iteration[i]);
				}, function() {
					iteration[i] = setInterval(function() {
							iterate(i)
						}, 4000);
				});
				
				
				// setup auto-iterate
				if ($(this).hasClass('minislideshow')) {
					// set each up to sutochange
					iteration[i] = setInterval(function() {
							iterate(i)
						}, 4000);
				}
				
				$('ul.slideshow').css('display', 'block');
	
	});	
	
	
	// iterate widget
	function iterate(num) {		
		var el = $('.slideshow').eq(num);	
		var wrap = $(el).closest('.slideshowWrap');
		if ($(wrap).find('li:animated').length == 0) {
			var numberOfImages = $(wrap).find('.paginate li.img').length;
			var slideshowOld = $(wrap).find('.paginate li.active').prevAll('.img').length;
			var slideshowActive = (slideshowOld == numberOfImages-1) ? 0 :  slideshowOld + 1;
			$(wrap).find('.paginate li.img:eq('+slideshowActive+')').addClass('active').siblings('.img').removeClass('active');
			slideshow(el, slideshowOld, slideshowActive)
			if ($(wrap).hasClass('minislideshowWrap')) {
				if ($(wrap).closest('li.widget').hasClass('carousel-type-just-launched')) {
					// if it's the "just launched" widget then we need to change the URL for the "More" button
					var itemURL = $(wrap).find('.minislideshow li:eq('+slideshowActive+') a.permLink').attr('href');
					$(wrap).next().find('a').attr('href', itemURL);
				}
			}
		}
	}	
				
	
});

