jQuery(document).ready(function(){

	// Add VideoJS to all video tags on the page when the DOM is ready
	//var myManyPlayers = VideoJS.setup("All");
	jQuery('#sidebar li.video').click(function(e){
		e.preventDefault();
		var videoid = getID(jQuery(this).attr('id'));
		var relvid = jQuery('#sidebar-video-'+videoid+' .video-popup');
		var thisVideo = VideoJS.setup('video-js-'+videoid);
		
		//jQuery('.sidebar-video').fadeOut();
		jQuery('.video-popup').not(relvid).animate({
			height: '0',
			width: '0',
			padding: '0'
		});
		jQuery(relvid).animate({
			height: '290',
			width: '447',
			padding: '15'
		});
		
		jQuery('.video-popup .video-close').click(function(e){
			e.preventDefault();
			jQuery(relvid).animate({
				height: '0',
				width: '0',
				padding: '0'
			});
			thisVideo.pause();
		});
		
		jQuery('#sidebar li.video').not(this).click(function(e){
			e.preventDefault();
			jQuery(relvid).animate({
				height: '0',
				width: '0',
				padding: '0'
			});
			thisVideo.pause();
		});
		
	});
		
	/* Initialise common functions */
	
	jQuery(".equalheights").equalHeights();
	
	jQuery('#language-selector ul li').hover(
	function(e){
		jQuery('span', this).fadeIn();
	}, 
	function(e){
		jQuery('span', this).fadeOut();
	});
	
	/* Initialise the homepage js functions */
	if(jQuery('body'). hasClass('page-template-page-home-php')){
		homepageHero();
		whatwedoPanels();
		VideoJS.setupAllWhenReady();
	}
	
	
	/* Initialise the client index js functions */
	if(jQuery('body'). hasClass('page-template-template-client-index-php')){
		
		// Client overlays
		jQuery('.client').hover(
		function(e){
			jQuery('.overlay', this).fadeIn();
		}, 
		function(e){
			jQuery('.overlay', this).delay(300).fadeOut();
		});
	}
	
	/* Initialise the what we do js functions */
	if(jQuery('body'). hasClass('page-template-template-what-we-do-php')){
		whatwedoPanels();
	}
	
	
	/* Initialise the team index js functions */
	if(jQuery('body'). hasClass('page-template-template-who-we-are-php')){
		teamOverlays();
	}
	
	/* Initialise if tweet this present */
	if(jQuery('*').hasClass('social-media')){
		//alert('Yup it has the lass');
		// DSocial media rollovers
		jQuery('.social-media ul li a').hover(
		function(e){
			var text = jQuery(this).attr('title');
			jQuery('.overlay', this).fadeIn();
			jQuery('canvas.greyscale', this).fadeOut();
			jQuery('img.greyscale', this).fadeIn();
			jQuery(this).append('<span>'+text+'</span>');
			jQuery('span',this).fadeIn('fast');
		}, 
		function(e){
			jQuery('span', this).fadeOut(function(e){
				jQuery(this).remove();
			});
			jQuery('.overlay', this).fadeOut();
			jQuery('img.greyscale', this).fadeOut();
			jQuery('canvas.greyscale', this).fadeIn();
		});
	}	
	

});

/* Homepage hero */
function homepageHero(){
	
	var home = jQuery('body.home').size();
	if(home > 0){
		jQuery('#hero-viewer').cycle({
			fx: 'fade',
			delay: 6000,
			timeout: 9000,
			pager: '.hero-thumbs ul',	
			pagerAnchorBuilder: function(idx, slide) {
				        //var src = jQuery(slide).eq(0).attr('id');
				        var src = jQuery(slide).attr('id');
				        var title = jQuery('.hero-info h3', slide).text();
				        var desc = jQuery('img', slide).attr('alt');
				        return '<li><a href="#" title="'+title+'"><span>'+desc+'</span><img src="'+src+'" width="260" height="68" title="More..." alt="'+title+'" /></a></li>';
						}       	
		});
	}
}

function whatwedoPanels(){
	jQuery('.action-list li').click(function(e){
		e.preventDefault();
		var id = getID(jQuery(this).attr('id'));
		var linkclass = jQuery(this).attr('class');
		//alert(linkclass);
		jQuery(this).siblings().removeClass('active');
		jQuery(this).parent().siblings().removeClass('active');
		jQuery(this).addClass('active');
		jQuery('.reveal-'+linkclass).fadeOut();
		jQuery('#reveal-'+id).fadeIn();
	});
}

function teamOverlays(){
	 // Team overlays
	 jQuery('.team-member').hover(
	 function(e){
	 	jQuery('.overlay', this).fadeIn();
	 	jQuery('canvas.greyscale', this).fadeOut();
	 	jQuery('img.greyscale', this).fadeIn();
	 }, 
	 function(e){
	 	jQuery('.overlay', this).fadeOut();
	 	jQuery('img.greyscale', this).fadeOut();
	 	jQuery('canvas.greyscale', this).fadeIn();
	 });
	 
	 jQuery('.team-member').click(function(e){
	 	var teamid = getID(jQuery(this).attr('id'));
	 	jQuery('#team-info-container > div').slideUp();
	 	jQuery('#team-'+teamid).slideDown();
	 	jQuery('.mugshot').fadeOut();
	 	jQuery('.mugshot', this).fadeIn();
	 }); 
	 
	 jQuery('.greyscale').data('desaturate');	
	 
	 jQuery('#team-info-container').scrollFollow({
	 	offset: 40
	 });
}

// Return Id of string //
function getID(strID){
 return strID.replace(/[^0-9]/gi, '');
}

// Desaturate

var paircount = 0;

/* If you want to desaturate after page loaded - use onload event
* of image.
*/
function initImage(obj){
	obj.onload = null;
	var $newthis = jQuery(obj);
	if (jQuery.browser.msie){
	  // You need this only if desaturate png with aplha channel
	  $newthis = $newthis.desaturateImgFix();
	}
	// class for easy switch between color/gray version
	$newthis.addClass("pair_" + ++paircount);
	var $cloned = $newthis.clone();
	// reset onload event on cloned object
	$cloned.get(0).onload = null;
	// add cloned after original image, we will switch between
	// original and cloned later
	$cloned.insertAfter($newthis).addClass("color").hide();
	// desaturate original
	$newthis.desaturate();
};    
