$(document).ready(function(){
	if($('p.morespace')){
		$('p.morespace').html('Hover over the boxes to find out more.');	
	}
						   
	$('#mobilenavbutton').toggle(function(){
		//console.log('click one');
		$('#header-wrap header nav ul').slideDown();
		$('#mobilenavbutton').addClass('active');
	}, function(){
		//console.log('click two');
		$('#mobilenavbutton').removeClass('active');
		$('#header-wrap header nav ul').slideUp();
	})
	

	$('.js.borderradius header nav ul li a:not(".active")').children('span').animate({"opacity":0.5}).css({"width":0});
	$('.js.borderradius header nav ul li a').mouseenter(function(){
	   var itemWidth = $(this).width();
	   if($(this).hasClass('active')){
		   $(this).children('span').stop().css({"backgroundColor":"rgba(255,255,255,0.2)"});
	   }else{
		   $(this).children('span').stop().animate({"opacity":1, "width": itemWidth}, 750);
	   }
	}).mouseleave(function(){
		if($(this).hasClass('active')){
			$(this).children('span').stop().css({backgroundColor: "rgba(255,255,255,0.52"});
		}else{
			$(this).children('span').stop().animate({"width": 0, "opacity":0}, 750);
		}
	});

	if($('#showcase')){
		$('#showcase h2').first().addClass('active');
		$('#showcase div').css({ opacity: 0 }).first().addClass('visible').css({ opacity: 1 });
		
		$('#showcase h2').click(function(){
			$('.pagination').fadeOut();
			$('#showcase h2.active').removeClass('active');
			$('#showcase div.visible').animate({"opacity":0}).removeClass('visible');
			$(this).addClass('active').next('div').addClass('visible').animate({"opacity":1}, function(){
				if ($(this).attr('id') == 'our-clients' && !$(this).data('scroller-loaded')) {
					$('#our-clients').scroller().data('scroller-loaded', 'trevor');
				}
			}).children('.pagination').fadeIn();
		});
	}


	if($(".market-sector")){
		$(window).load(function(){
			var marketSectorHeight = $('.market-sector').height() - 30;
			//console.log(marketSectorHeight);

			$('.market-sector .details').animate({ "top": "+="+marketSectorHeight });
			$('.market-sector').mouseenter(function() {
				$('.details', this).animate({"top": "-="+marketSectorHeight}, 250);
			}).mouseleave(function() {
				$('.details', this).animate({"top": "+="+marketSectorHeight}, 250);
			});
		})
	}

	if($('.expandable')){
		$('.expandable').removeClass('open').children('div').hide();
		$('.expandable span').toggle(function(){
			$(this).parents('.expandable').siblings('.expandable.open').removeClass('open').children('div').slideUp();
			$(this).parents('.expandable').removeClass('closed').addClass('open');
			$(this).siblings('div').slideDown();
		}, function(){
			$(this).parents('.expandable').removeClass('open').addClass('closed');
			$(this).siblings('div').slideUp();
		});
	}

	if($('#media-showcase')){
		$('#media-showcase ul li a:first').addClass('active');
		$('#media-showcase .images img').css({"opacity":0}).addClass('hidden');
		$('#media-showcase .images img:first').css({"opacity":1}).removeClass('hidden');

		$('#media-showcase ul li a').mouseenter(function(){
			var imgtoshow = "#img" + $(this).attr('id');
			if($(imgtoshow).hasClass('hidden')){
				$('#media-showcase .images img').addClass('hidden').css({"opacity":0});
				$(imgtoshow).removeClass('hidden').css({"opacity":1});
			}else{
				return
			}
			$('#media-showcase ul li a.active').removeClass('active');
			$(this).addClass('active');
		});
	}

	if($('.columns3')){		
		$('.columns3 ul li a').click(function (event) {
			event.preventDefault();
		});

		$('.columns3 ul li').hover(function () {
				var classnames = $(this).data('highlight').split(' ');
				jQuery.each(classnames,function() {
					$('.columns3 ul li.' + this).addClass('active');
				});
				$(this).addClass('active');
			},
		function () {
			$(this).removeClass('active');
			$('.columns3 ul li').removeClass('active');
		});
	}
});

// Copyright (c) 2010 TrendMedia Technologies, Inc., Brian McNitt. 
// All rights reserved.
//
// Released under the GPL license
// http://www.opensource.org/licenses/gpl-license.php
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
// **********************************************************************


// Break it down! additions by us, here at this desk.

$(window).load(function() {
//start after HTML, images have loaded

    var InfiniteRotator = 
    {
        init: function()
        {
            //where are the slides?
            var slideContainer = $('#slides');
            var slideElement = $('#slides img');

            //how long before we remove the loady loader animation?
            var containerBgRemoveDelay = 3000;

            //initial fade-in time (in milliseconds)
            var initialFadeIn = 1250;
            
            //interval between items (in milliseconds)
            var itemInterval = 6000;
            
            //cross-fade time (in milliseconds)
            var fadeTime = 5000;
            
            //count number of items
            var numberOfItems = $(slideElement).length;

            //set current item
            var currentItem = 0;

            //show first item
            $(slideElement).eq(currentItem).fadeIn(initialFadeIn);
            //remove loady loader animation
            $(slideContainer).delay(containerBgRemoveDelay).css({"backgroundImage": "none"});

            //loop through the items        
            var infiniteLoop = setInterval(function(){

                $(slideElement).eq(currentItem).fadeOut(fadeTime);

                if(currentItem == numberOfItems -1){
                    currentItem = 0;
                }else{
                    currentItem++;
                }
                
                $(slideElement).eq(currentItem).fadeIn(fadeTime);

            }, itemInterval);   
        }   
    };

    InfiniteRotator.init();
    
});


/**
 * @author John Noel <john.noel@rckt.co.uk>
 * @copyright rckt 2011 <http://www.rckt.co.uk>
 * @package Welbeck
 */

/**
 * Scroller plugin
 * Turns a list into a scroller / carousel / slider / whatever
 *
 * @author John Noel <john.noel@rckt.co.uk>
 * @copyright rckt http://www.rckt.co.uk/
 * @version 2.1
 * @package Welbeck
 * @todo Allow choosing of pagination CSS classes
 * @todo Allow external calling of advance / retreat
 */
(function($) {
	$.fn.scroller = function(option) { 
		option = $.extend({}, $.fn.scroller.option, option);
		
		return this.each(function() {
			var $this = $(this),
				transitioning = false,
				slideCount = $(option.slidesSelector, $this).length;
						
			$('<div class="pagination" />')
				.append('<span class="previous">Previous</span>')
				.append('<span class="next">Next</span>')
				.appendTo($this);
			
			// "square off" scroller
			if((slideCount % option.slidesPerScroll) != 0)
			{
				var p = $(option.containerSelector, $this);
				var toInsert = $(option.slidesSelector, $this)
					.slice(0 - ((Math.ceil(slideCount / option.slidesPerScroll) * option.slidesPerScroll) - slideCount))
					.each(function() {
						p.append(this.cloneNode(false)); // not a deep copy
					});
			}
			
			// absolutise
			var h = 0, rw = 0; // rolling width
			$(option.slidesSelector, $this).each(function(idx) {
				$(this).css({
					position: 'absolute',
					top: 0, left: rw+'px'
				});
				
				rw += $(this).outerWidth(true);
				h = ($(this).outerHeight(true) > h) ? $(this).outerHeight(true) : h;
			});
			
			$(option.containerSelector, $this).css({ position: 'relative', height: h+'px' });
			
			// advance
			var advance = function(count) {
				if(!transitioning)
				{
					transitioning = true;
					
					var slides = $(option.slidesSelector, $this);
					// grab the first slidesPerScroll slides
					var cloneableSlides = $(option.slidesSelector, $this).slice(0, option.slidesPerScroll * count);
					var w = slides.outerWidth(true);
					
					// calculate scroll items widths (rolling width)
					var rw = 0;
					slides.each(function() { rw += $(this).outerWidth(true); });
					
					// append them and set their left offset
					$(option.containerSelector, $this).append(cloneableSlides.clone(true).each(function(idx) {
						$(this).css({ left: rw+'px' });
						rw += $(this).outerWidth(true);
					}));
					
					// shouldn't use slides variable as slides have been added
					// TODO grab the first option.slidesPerScroll and calculate width
					$(option.slidesSelector, $this).animate({
						left: '-=' + ((w * option.slidesPerScroll) * count)
					}, 700, function() {
						transitioning = false;
						cloneableSlides.remove();
					});
				} // !transitioning
			}; // advance()
			
			// retreat
			var retreat = function(count) {
				if(!transitioning)
				{
					transitioning = option.slidesPerScroll;
					
					// grab the final slidesPerScroll slides
					var cloneableSlides = $(option.slidesSelector, $this).slice(0 - (option.slidesPerScroll * count));
					var w = cloneableSlides.outerWidth(true);
					
					// calculate cloneable width
					var rw = 0;
					cloneableSlides.each(function() { rw += $(this).outerWidth(true); });
					
					// clone and prepend them, setting their left offset correctly
					$(option.containerSelector, $this).prepend(cloneableSlides.clone(true).each(function(idx) {
						$(this).css({ left: (0 - rw)+'px' });
						rw -= $(this).outerWidth(true);
					}));
					
					// TODO calculate proper width rather than assuming all the same width
					$(option.slidesSelector, $this).animate({
						left: '+=' + ((w * option.slidesPerScroll) * count)
					}, 700, function() {
						transitioning = false;
						cloneableSlides.remove();
					});
				} // !transitioning
			}; // retreat()
			
			$('.pagination .next', $this).click(function(evt) { evt.stopPropagation(); advance(1); }).
				mousedown(function(evt) { evt.stopPropagation(); advance(1); $(this).data('timer', setInterval(function() { advance(1); }, 300)); }).
				mouseup(function(evt) { evt.stopPropagation(); clearInterval($(this).data('timer')); })
			$('.pagination .previous', $this).click(function(evt) { evt.stopPropagation(); retreat(1); }).
				mousedown(function(evt) { evt.stopPropagation(); retreat(1); $(this).data('timer', setInterval(function() { retreat(1); }, 300)); }).
				mouseup(function(evt) { evt.stopPropagation(); clearInterval($(this).data('timer')); });
		});
	};
	
	$.fn.scroller.option = {
		slidesPerScroll: 1, // how many slides per click
		slidesSelector: 'ul.slides li', // how to select the slides (scoped to the element)
		containerSelector: 'ul.slides'
	};
})(jQuery);

$(window).load(function() {

	if('#image-scroller'){
		$('#image-scroller').scroller();
	}
});
