$(function(){
	
	$('#quicksearch').placeholderInput();
	
	$('#logos').contentSlider();
	
	$('#gallery').contentSlider();
	
	$('.slider').slideOpen();
	
	$('#subject_experts').subjectExperts();
	
	$('.matrix').caseStudyLink();
	
	$('#client_filter').clientFilter();

	$("form#contactform, form#ask_experts").validate({
		errorElement: "span"
	});
	
	//$('.video_preview').videoPlay();
	$('#videoContainer').hide();
	videoloaded = true;
	
	if ($('#video img').length != 0) {
		
		var moogaloop = false;
	
		var swf_id = 'videoContainer';
		var video_id = $('#video img').attr("id").replace('video_', '');

		var flashvars = {
			clip_id: video_id,
			server: 'vimeo.com',
			show_portrait: 0,
			show_byline: 0,
			show_title: 0,
			fullscreen: 1,
			autoplay: 1,
			color: 'DD2E28',
			js_api: 1, // required in order to use the Javascript API
			js_onLoad: 'vimeo_player_loaded', // moogaloop will call this JS function when it's done loading (optional)
			js_swf_id: 'videoContainer' // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
		};
		var params = {
			allowscriptaccess: 'always',
			allowfullscreen: 'true',
			wmode:'transparent'
		};
		var attributes = {};

		$('#video img').click(function() { 
				$('#video img').hide();
				$('#videoContainer').show();
				swfobject.embedSWF("http://vimeo.com/moogaloop.swf", 'videoContainer', "440", "248", "9.0.0","expressInstall.swf", flashvars, params, attributes);
		});
	
	}
	
		
});


$.fn.placeholderInput = function(){
	return this.each(function(){
		$(this).find('input.text').each(function(){
			var input = $(this);
			var label = input.prev('label').text();
			
			input.val(label).addClass('placeholder').focus(function(){
				if (input.val() == label) input.removeClass('placeholder').val('');
			}).blur(function(){
				if (input.val() == '') input.val(label).addClass('placeholder')
			});
		});
	});
}

function on_finish(swf_id) {
	//document.getElementById('state').innerHTML = 'Paused';
	$('#video img').show();
	$('#videoContainer').hide();
}

function vimeo_player_loaded(swf_id) {
	moogaloop = document.getElementById(swf_id);
	//moogaloop.api_addEventListener('onPause', 'on_finish');
	moogaloop.api_addEventListener('onFinish', 'on_finish');

	if (!videoloaded) {
		$('#video img').show();
		$('#videoContainer').hide();
		moogaloop.api_play();
		videoloaded = true;
	}
}

$.fn.contentSlider = function(){
	return this.each(function(){
		var SCROLL_SPEED = 500;
		var container = $(this);
		var slides = $('>div', container);
		var ul = $('>ul',slides);
		if(container.attr('id') == 'gallery') {
			var scrollAmount = 1065;
		} else {
			var scrollAmount = 940;
		}
		
		var items = $('>li',ul);
		var itemsWidth = items.outerWidth(true) * items.length;
		
		items.clone().appendTo(ul);
		
		var slide = function(e){
			if(!slides.is(':animated')) {
				var currentScroll = slides.scrollLeft();

				if (e.data.direction == 'next') {
					slides.animate({scrollLeft: currentScroll+scrollAmount},{duration: SCROLL_SPEED, complete:function(){
						if (slides.scrollLeft()>=itemsWidth) slides.scrollLeft(slides.scrollLeft()-itemsWidth);
					}});
				} else {
					slides.animate({scrollLeft: currentScroll-scrollAmount},{duration: SCROLL_SPEED, complete:function(){
						if (slides.scrollLeft()<=0) slides.scrollLeft(itemsWidth);
					}});
				}
			}
		}
		
		// initialise slideshow
		slides.css({
			height: items.outerHeight()
		});
		ul.css({width: itemsWidth * 2 +'px'});
		
		$('<button class="slider_prev"><img src="/images/button_prev.png" alt="Previous" /></button>').bind('click',{direction:'prev'},slide).appendTo(container);
		$('<button class="slider_next"><img src="/images/button_next.png" alt="Next" /></button>').bind('click',{direction:'next'},slide).appendTo(container);
		
		if(container.attr('id') == 'gallery') {
			function slideContent(e) {
			  var currentScroll = slides.scrollLeft();
				slides.animate({scrollLeft: currentScroll+scrollAmount},{duration: SCROLL_SPEED, complete:function(){
					if (slides.scrollLeft()>=itemsWidth) slides.scrollLeft(slides.scrollLeft()-itemsWidth);
				}});
					
			}
			var slideInterval = setInterval(slideContent, 15000);
			function clearSlide() {
				clearInterval(slideInterval);
			}
			$('.slider_next, .slider_prev').click(function(){
				clearSlide();
				return false;
			});
		}

	});
}

$.fn.slideOpen = function(){
	return this.each(function(){
		$(this).delegate('h2>a,strong>a,h3>a','click',function(){
			var content = $(this).parent().next();
			if (!content.is(':animated')) {
				if (content.is(':visible')) {
					content.slideUp(function(){
						content.parent().removeClass('open').addClass('closed');
					});
				} else {
					content.slideDown();
					content.parent().removeClass('closed').addClass('open');
				}
			}
			return false;
		}).children().addClass('closed').children('div').hide();
	});
}


$.fn.subjectExperts = function(){
	return this.each(function(){
		var container = $(this);
		var slides = container.children();
		var ul = $('>ul',slides);
		var images = ul.children('li');
		var controls = $('<ol class="pagination"></ol>').insertAfter(this);
		var timer;
		var scrollAmount = images.outerWidth(true);
		var position = 0;
		
		ul.css({width: images.outerWidth(true) * images.length + 'px'});
		slides.css({height: images.outerHeight()}).scrollLeft(0);

		var markers = $(images.map(function(i){
			return '<li data-position="'+i+'"><a href="#">'+(i+1)+'</a></li>';
		}).get().join(''));

		var setMarker = function(position){
			var current = markers.filter('.current');
			var currentText = current.text();
			var next = markers.eq(position);
			var nextText = next.text();
			current.children().replaceWith('<a href="#">'+currentText+'</a>');
			next.children().replaceWith('<em>'+nextText+'</em>');
			markers.removeClass('current');
			next.addClass('current');
		}

		var switchImage = function(action){
			if (!slides.is(':animated')) {
				var currentScroll = slides.scrollLeft();
				if (currentScroll >= images.length*scrollAmount) { // reset to beginning if we’ve gone around multiple cycles
					slides.scrollLeft(0);
					currentScroll = 0;
				}
				
				position = action.data('position');

				slides.animate({ scrollLeft: position*scrollAmount },500);

				if (position >= images.length) position = 0;
				setMarker(position);
			}
		}

		controls.prepend(markers).delegate('a','click',function(){
			var action = $(this).parent();
			switchImage(action);
			return false;
		});

		// init current marker
		setMarker(position);
		
	});
}


$.fn.caseStudyLink = function(){
	return this.each(function(){
		$(this).delegate('li','mouseenter',function(){
			$(this).children('a').animate({top:0},{duration:400,queue:false});
		}).delegate('li','mouseleave',function(){
			$(this).children('a').animate({top:143},{duration:400,queue:false});
		});
	});
}

$.fn.clientFilter = function(){
	return this.each(function(){
		var matrices = $('.matrix');
		var links = $(this).find('a');
		$(this).delegate('a','click',function() {
			matrices.hide().filter($(this).attr('href')).show();
			links.removeClass('active');
			$(this).addClass('active');
			return false;
		});

	});
}

/*$.fn.videoPlay = function(){
	return this.each(function(){
		$(this).click(function(){
			$(this).remove();
			$('#video').html('');
		});
	});
}*/

/*$.fn.newsItems = function() {
	$('#latest_news li').hide();
	$('#latest_news li:first-child').show();
	setInterval(function(){
		$('#latest_news li').filter(':visible').fadeOut("slow",function(){
			if($(this).next('li').size()){
				$(this).next().fadeIn("slow");
			}
			else{
				$('#latest_news li').eq(0).fadeIn("slow");
			}
		});
	},5000);
}*/
