/*global jQuery, $, setTimeout */

// Equal height home panels
jQuery(function() { 
	jQuery().equalHeights && jQuery("#footer-container-bg .widget").equalHeights(); 
});

//vimeo sidebar, launch in new tab
jQuery(function() { 
	jQuery('.vimeoBadge a').attr('target', '_blank'); 
});

// Homepage tabs
jQuery(function() {
	jQuery('#home-tab1').hide();
	jQuery('#home-tab2').hide();
	jQuery('#home-tab3').hide();
	jQuery('.selected-on-load').show();
	
	if (!jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7) {
		jQuery('#home-tabs').show();
	}
	
	jQuery('.button-1').click(function() {
			jQuery('#home-tab1').show();
			jQuery('#home-tab2').hide();
			jQuery('#home-tab3').hide();
			jQuery('.button-1, .button-2, .button-3').removeClass('current-item');
			jQuery('.button-1').addClass('current-item');
	});
		
	jQuery('.button-2').click(function() {
			jQuery('#home-tab1').hide();
			jQuery('#home-tab3').hide();
			jQuery('#home-tab2').show();
			jQuery('.button-1, .button-2, .button-3').removeClass('current-item');
			jQuery('.button-2').addClass('current-item');
	});
	
	jQuery('.button-3').click(function() {
			jQuery('#home-tab1').hide();
			jQuery('#home-tab2').hide();
			jQuery('#home-tab3').show();
			jQuery('.button-1, .button-2, .button-3').removeClass('current-item');
			jQuery('.button-3').addClass('current-item');
	});
});

//Vimeo Video Selector on the Messages of Support page
jQuery(function($){
	
	var pane = $('#mcs4_container .content');

	pane.jScrollPane({
		showArrows: false,
		animateScroll: true,
		mouseWheelSpeed: 10,
		verticalDragMaxHeight: 11,
		verticalDragMinHeight:11
	});

	var pane_height = pane.height(),
		li_height = $('li', pane).height(),
		scroller = $('.jspPane');


	var api = pane.data('jsp');
	
	function clear_playing_classes() {
		//Remove the playing class from any li's that might already have it UPDATE: And the new above-playing class
		$('.playing, .above-playing', pane).removeClass('playing above-playing');
	}

	//When a link in an li is clicked
	$('.listed-video-supporter', pane).mousedown(function(e) {
		// required for WebKit
		api.scrollToElement(this);

		if ($(this).is('.playing')) {
			// don't reload an already playing video
			return false;
		}

		clear_playing_classes();

		//We need to have "this" as a variable so this function will take it, as we need to delay it so that the arrow doesn't
		// show up before the thing has finished scrolling
		var this_link = $(this);

		var top = this_link.position().top;

		var visible_top = 0 - scroller.position().top, 
			visible_bottom = visible_top + pane_height;

		// do green arrow immediately
		var timeout = 0;
		
		if (top < visible_top || top + 2 * li_height > visible_bottom) {
			// need to scroll so wait a bit
			timeout = 200;
		}

		// give it time to scroll
		setTimeout(function() {
			if ($('.playing', pane).length) {
				// user has clicked on another link within 200ms
				return;
			}
			//And add class to the closest li to this a tag so we can do some active state business
			this_link.addClass('playing');
			//We're also going to add the above-playing class to the thing above it, to fix an issue with borders between li's
			this_link.prev('.listed-video-supporter').addClass('above-playing');
		
			//Take the video ID from the URL
			//The URL used on the page (added by clients) is a regular vimeo link, which we want to change with javascript into an embed one)
			var vid_url =  $.trim($('a', this_link).attr("href").match(/[\d]+$/));

			// Change the video after this event returns so UI seems faster
			setTimeout(function() {
				//Set the SRC of the iframe on the page to an embed link containing our video ID
				//If some of these settings don't work, it's probably because they're a Plus member and they can override them on vimeo.com
				$('#video-area')
					.children('.supporter-video')
					.attr("src", "http://player.vimeo.com/video/" + vid_url + "?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1");				
			}, 20);

		}, timeout);

		return false;
	}).click(function() {
		//Return false on the link so that it doesn't actually load the link
		//When JS is disabled, none of this will take place so the user will just be linked to the vimeo video.
		return false;
	});
	
	
	//Last but not least, when someone is scrolling we want to remove the playing classes
	////////////// Click 
	$('.jspTrack, .jspDrag, .jspArrow').mousedown(clear_playing_classes);
	///////// Mousewheel scroll
	$('.video-supporter-list').mousewheel(clear_playing_classes);
	
	$('.container').mousewheel(function() {
		return false;
	});

});

// Facebook Fancybox
jQuery(function() {
	jQuery(".fbPhoto").fancybox({
	  'titleShow'		: false,
	  'transitionIn'	: 'none',
	  'transitionOut'	: 'none'
	});
			
	jQuery(".fancybox").fancybox();
	
	jQuery(".other-organisations").fancybox({
		padding: 5,
		showNavArrows:false
	});
});

//Custom easing for sliders on charity, organisations and supporter listers.
jQuery.easing.custom = function (x, t, b, c, d) {
	var s = 1.70158; 
	if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
	return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
}

	/* Initialise Slider */	
	$('.slider') 
		.cycle({
		fx: 'scrollHorz',
		pager: '.nav',
		cleartypeNoBg: true,
		backwards:false,
		easing:  'custom',
		speedIn:  1500, 
		speedOut: 1500,
		timeout:0,
		startingSlide: 0
	});
	
//jQuery(function() {
		//jQuery('.page-template-supporters-v2 .content img').lazyload();
	//});

// Facebook fix
jQuery(function() { 
	jQuery('.widget_FacebookLikeBox').addClass('facebookfix_pt1');
	
	jQuery('iframe').load(function(){
		jQuery('iframe').addClass('facebookfix_pt2');
	});
	
});

// GAT Delay Click
function recordOutboundLink(link, category, action) {
	// track event
	_gat._getTrackerByName()._trackEvent(category, action);

	// go to the link's URL after 100ms
	// setTimeout(function() {
	//  document.location = link.href;
	// }, 100);
}

// Creating custom :external selector
jQuery.expr[':'].external = function(obj){
	return !obj.href.match(/^mailto\:/)
			&& (obj.hostname != location.hostname);
};

jQuery(function($) { 
	// Add 'external' CSS class to all external links
	$('a:external').addClass('external').click(function() {
		recordOutboundLink(this, 'Outbound Link Clicked', $(this).attr('href')); 
		//return false;
	});

	// Add event to facebook link
	$('a.facebook-link').click(function() {
		recordOutboundLink(this, 'Facebook Link Clicked', $(this).attr('href')); 
		//return false;
	});
});
	
