$(document).ready(function(){
	
	/* fix issue with panes breaking back button */
	if($('.tabs').length) {
		$('.tabs a').click(function(e) {
			location.hash = $(this).attr('class');
		});
	}
	
	if($('.message').length) {
		setTimeout(function () {
			$('.message').slideUp('slow');
		}, 5000);
	}
	
	// accessibility
	$("body").addClass("js-enabled");
	if ($(".index").length) {
		//removes the tabindex for preventing bad tabbing on scrollable items
		$(".index #items a").attr("tabindex","-1");
	}
	
	scrolltop();
	
	// custom easing from george mcginley's jquery easing v1.3
	$.easing.easeInOutExpo = function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
	
	// content-main scrollable
	$(".index .content-main").scrollable({
		size: 1,
		clickable: false,
		keyboard: 'static',
		easing: 'easeInOutExpo', 
		speed: 1200,
		loop: true
		}).navigator({
			navi: "#navigator"
		}).autoscroll({
			interval: 12000,
			autopause: true
	}); 
	
	// testimonial rotational fade
	$('.testimonial').innerfade({
		speed: 'slow',
		timeout: 9000,
		type: 'sequence',
		containerheight: '16em'
	}); 
	
	// form labels
	$("label").inFieldLabels(); 
	
}); 

// fade transition on panes
$(function() {
	if ($("body").hasClass("no-fade")) {
		$("ul.tabs").tabs("div.panes > div", {
			current: 'current'
		});
	} else {
		$("ul.tabs").tabs("div.panes > div", {
			effect: 'fade',
			fadeInSpeed: 500,
			current: 'current'
		});
	}
});

// scroll to top
function scrolltop() {
	$('a[rel="self"]').click(function() {
		$.scrollTo( '#wellbeing-ac-uk', 500 );
		return false;
	});
}

// sorting filter
(function($) {
	$.fn.sorted = function(customOptions) {
	var options = {
	reversed: false,
	by: function(a) { return a.text(); }
	};
	$.extend(options, customOptions);
	$data = $(this);
	arr = $data.get();
	arr.sort(function(a, b) {
	var valA = options.by($(a));
	var valB = options.by($(b));
	if (options.reversed) {
	return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;				
	} else {		
	return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;	
	}
	});
	return $(arr);
	};
})(jQuery);

// DOMContentLoaded
$(function() {

	// bind radiobuttons in the form
	var $filterType 	= $('#filter_default input[name="type"]');
	var $filterSort 	= $('#filter_default input[name="sort"]');

	// get the first collection
	var $applications = $('ul#downloads_default');

	// clone applications to get a second collection
	var $data = $applications.clone();

	// attempt to call Quicksand on every form change
	$filterType.add($filterSort).change(function(e) {
		if ($($filterType+':checked').val() == 'all') {
			var $filteredData = $data.find('li');
		} else {
			var $filteredData = $data.find('li[data-type=' + $($filterType+":checked").val() + ']');
		}

		// if sorted by document
		if ($('#filter_default input[name="sort"]:checked').val() == "document") {
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('span[data-type=document]').text();
				}
			});
		} else {
			// if sorted by name
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('h4').text().toLowerCase();
				}
			});
		}   

		// finally, call quicksand
		$applications.quicksand($sortedData, {
			duration: 800,
			easing: 'easeInOutQuad'
		});

	});

});

// The big picture
$(function() {

	// bind radiobuttons in the form
	var $filterType 	= $('#filter_the_big_picture input[name="type"]');
	var $filterSort 	= $('#filter_the_big_picture input[name="sort"]');
	
	// get the first collection
	var $applications = $('ul#downloads_the_big_picture');

	// clone applications to get a second collection
	var $data = $applications.clone();

	// attempt to call Quicksand on every form change
	$filterType.add($filterSort).change(function(e) {
		if ($('#filter_the_big_picture input[name="type"]:checked').val() == 'all') {
			var $filteredData = $data.find('li');
		} else {
			var $filteredData = $data.find('li[data-type=' + $('#filter_the_big_picture input[name="type"]:checked').val() + ']');
		}
	
		// if sorted by document
		if ($('#filter_the_big_picture input[name="sort"]:checked').val() == "document") {
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('span[data-type=document]').text();
				}
			});
		} else {
			// if sorted by name
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('h4').text().toLowerCase();
				}
			});
		}   

		// finally, call quicksand
		$applications.quicksand($sortedData, {
			duration: 800,
			easing: 'easeInOutQuad'
		});

	});

});

// Health
$(function() {

	// bind radiobuttons in the form
	var $filterType 	= $('#filter_health input[name="type"]');
	var $filterSort 	= $('#filter_health input[name="sort"]');
	
	// get the first collection
	var $applications = $('ul#downloads_health');

	// clone applications to get a second collection
	var $data = $applications.clone();

	// attempt to call Quicksand on every form change
	$filterType.add($filterSort).change(function(e) {
		if ($('#filter_health input[name="type"]:checked').val() == 'all') {
			var $filteredData = $data.find('li');
		} else {
			var $filteredData = $data.find('li[data-type=' + $('#filter_health input[name="type"]:checked').val() + ']');
		}
	
		// if sorted by document
		if ($('#filter_health input[name="sort"]:checked').val() == "document") {
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('span[data-type=document]').text();
				}
			});
		} else {
			// if sorted by name
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('h4').text().toLowerCase();
				}
			});
		}   

		// finally, call quicksand
		$applications.quicksand($sortedData, {
			duration: 800,
			easing: 'easeInOutQuad'
		});

	});

});

// Communications and events
$(function() {

	// bind radiobuttons in the form
	var $filterType 	= $('#filter_communications_and_events input[name="type"]');
	var $filterSort 	= $('#filter_communications_and_events input[name="sort"]');
	
	// get the first collection
	var $applications = $('ul#downloads_communications_and_events');

	// clone applications to get a second collection
	var $data = $applications.clone();

	// attempt to call Quicksand on every form change
	$filterType.add($filterSort).change(function(e) {
		if ($('#filter_communications_and_events input[name="type"]:checked').val() == 'all') {
			var $filteredData = $data.find('li');
		} else {
			var $filteredData = $data.find('li[data-type=' + $('#filter_communications_and_events input[name="type"]:checked').val() + ']');
		}
	
	console.log($('#filter_communications_and_events input[name="type"]:checked').val());
		// if sorted by document
		if ($('#filter_communications_and_events input[name="sort"]:checked').val() == "document") {
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('span[data-type=document]').text();
				}
			});
		} else {
			// if sorted by name
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('h4').text().toLowerCase();
				}
			});
		}   

		// finally, call quicksand
		$applications.quicksand($sortedData, {
			duration: 800,
			easing: 'easeInOutQuad'
		});

	});

});

// Leadership and management
$(function() {

	// bind radiobuttons in the form
	var $filterType 	= $('#filter_leadership_and_management input[name="type"]');
	var $filterSort 	= $('#filter_leadership_and_management input[name="sort"]');
	
	// get the first collection
	var $applications = $('ul#downloads_leadership_and_management');

	// clone applications to get a second collection
	var $data = $applications.clone();

	// attempt to call Quicksand on every form change
	$filterType.add($filterSort).change(function(e) {
		if ($('#filter_leadership_and_management input[name="type"]:checked').val() == 'all') {
			var $filteredData = $data.find('li');
		} else {
			var $filteredData = $data.find('li[data-type=' + $('#filter_leadership_and_management input[name="type"]:checked').val() + ']');
		}
	
	console.log($('#filter_leadership_and_management input[name="type"]:checked').val());
		// if sorted by document
		if ($('#filter_leadership_and_management input[name="sort"]:checked').val() == "document") {
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('span[data-type=document]').text();
				}
			});
		} else {
			// if sorted by name
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('h4').text().toLowerCase();
				}
			});
		}   

		// finally, call quicksand
		$applications.quicksand($sortedData, {
			duration: 800,
			easing: 'easeInOutQuad'
		});

	});

});

// Organisational culture
$(function() {

	// bind radiobuttons in the form
	var $filterType 	= $('#filter_organisational_culture input[name="type"]');
	var $filterSort 	= $('#filter_organisational_culture input[name="sort"]');
	
	// get the first collection
	var $applications = $('ul#downloads_organisational_culture');

	// clone applications to get a second collection
	var $data = $applications.clone();

	// attempt to call Quicksand on every form change
	$filterType.add($filterSort).change(function(e) {
		if ($('#filter_organisational_culture input[name="type"]:checked').val() == 'all') {
			var $filteredData = $data.find('li');
		} else {
			var $filteredData = $data.find('li[data-type=' + $('#filter_organisational_culture input[name="type"]:checked').val() + ']');
		}
	
	console.log($('#filter_organisational_culture input[name="type"]:checked').val());
		// if sorted by document
		if ($('#filter_organisational_culture input[name="sort"]:checked').val() == "document") {
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('span[data-type=document]').text();
				}
			});
		} else {
			// if sorted by name
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('h4').text().toLowerCase();
				}
			});
		}   

		// finally, call quicksand
		$applications.quicksand($sortedData, {
			duration: 800,
			easing: 'easeInOutQuad'
		});

	});

});
