//=======================
// Font Sizer
//=======================

$(document).ready(function(){
	$('#fontsizer').jfontsizer({applyTo: '#container, nav.primary_nav section', changemed: '2', changelarge: '4', expire: 30});
});

//=======================
// News Ticker
//=======================

$(document).ready(function() {

	/* A class of accessible is added to the ticker which 'hides' it.
	This is because before the news ticker JS kicks in, all the list items are displayed.
	The line below removes the class */
	if ($("nav.ticker").length > 0){
		$("nav.ticker").removeClass("accessible");

		var options = {
			newsList: "nav.ticker ul",
			startDelay: 500,
			loopDelay: 5000,
			tickerRate: 40,
			placeHolder1: " -",
			placeHolder2: "",
			controls: false,
			stopOnHover: false
		}
		$().newsTicker(options);
	}
});


//=======================
// Accordions
//=======================
$(document).ready(function()
{
	var c = 0;
	var panel = $(".expander .expander_content");
	var currentId = $(".expander").attr('id');
	var l = $('.expander .trigger').length - 1;
	$(".expander .trigger").addClass("active");

	/* Set panels which should always be open on page load
	/* Note, the format is, id + panelx where x is the panel number */

	$.cookie('search_filterpanel0','open0', null);
	$.cookie('resources_expanderpanel0','open0', null);
	$.cookie('violations_expanderpanel0','open0', null);

	// default to all panels closed
	$(".expander .expander_content").addClass('hide_nav');

	// add inactive class to all triggers
	$('.expander .trigger').removeClass('active').addClass('inactive');

	// loop through panels and act upon cookies
	$(".expander .trigger").each(function(c)
	{
		// if the expanding content was user created, don't retrieve cookie - we have no way of applying unique ID to
		// user created content.
		if($(this).hasClass('wysiwyg') == false) {
			var cvalue = $.cookie(currentId + 'panel' + c);
			//console.log('Found cookie: "'+currentId+'panel'+c+'" Value: "'+cvalue+'"');

			if ( cvalue == 'closed' + c )
			{
				accordianClose($(this));
			};

			if ( cvalue == 'open' + c )
			{
				accordianOpen($(this));
			};
		};
	});

	function accordianToggle(trigger, isOpening)
	{
		// if the panel is already in the correct position, do nothing
		if((trigger.hasClass('active') && isOpening) || (trigger.hasClass('inactive') && !isOpening)) return false;


		var myPanel = trigger.next();

		// show or hide?
		if(isOpening) myPanel.removeClass('hide_nav');
		else myPanel.addClass('hide_nav');

		// swap the classes
		trigger.toggleClass('active inactive');

		// set the cookie
		var num = $(".expander .trigger").index(trigger);
		var cooky = currentId + 'panel' + num;
		var value = (isOpening ? 'open' : 'closed')+num;

		$.cookie(cooky, value, null);
	}

	function accordianOpen(trigger)
	{
		accordianToggle(trigger, true);
	}

	function accordianClose(trigger)
	{
		accordianToggle(trigger, false);
	}

	$(".expander .trigger").click(function()
	{
		// are we opening or closing
		if($(this).hasClass('inactive'))
		{
			// opening
			accordianOpen($(this));
		}
		else
		{
			// closing
			accordianClose($(this));
		}

	});

});

//=======================
// Top Login menu
//=======================

$(document).ready(function() {

// Remove nojs classes which open the Login on hover event (graceful fallback)
	$("div.flyout").removeClass("nojs");
	$("a.signin").removeClass("nojs");

// Set the click trigger element and function
	$("nav.account a.signin").click(function()

	{

		var dd = $("div.flyout");
// We don't want to use 'hide' here, as it's no accessible, instead, we check for the default absolute top value
		if(dd.css("top") == "-10000px")
		{
// jsopen sets the position of the menu from -10000px to 30px
			dd.addClass("jsopen");
// change the class of our trigger element
			$("a.signin").addClass("open");
// Lets be extra nice to user and focus cursor on the first input field
			$("#username").focus();
		}
		else
		{
// Remove our classes
			dd.removeClass("jsopen");
			$("a.signin").removeClass("open");
		}

		return false;
	});

// Prevents clicking only flyout 'bubbling' to document level
	$('nav.account div.flyout.login').click(function(e) {
		e.stopPropagation();
	});

// If we click outside our menu, remove our classes to hide the menu.
	$(document).click(function()
	{
		$("div.flyout").removeClass("jsopen");
		$("a.signin").removeClass("open");
	});
});


//=======================
// Date Picker
//=======================

$(function() {
		$( ".datepicker" ).datepicker({
			changeMonth: true,
			changeYear: true,
			showOn: "button",
			buttonImage: "/assets/images/datepicker.png",
			buttonImageOnly: true
		});
		$( ".datepicker" ).datepicker( "option", "dateFormat", "dd/mm/yy" );
	});


//=======================
// VIDEO
//=======================


ukad = {

	init: function() {

		$("html").removeClass("no-js").addClass("js");

		this.check_for_video();

		// external links
		$("a.external").click(function(ev){
			ev.preventDefault();
			window.open($(this).attr("href"))
		}).each(function(){
			$(this).attr("title","Link opens in new window")
		})

		// clear buttons
		$("input[type='reset']").click(function(){
			u = $(this).data('clear-url');
			if(u) {
				window.location = u;
			}
		})
	},

	// only load video assets if we really need them
	check_for_video: function() {

		vids = $("#main_content video");

		// no vids, nothing to do
		if(!vids.size()) {

			return;
		}
		// load JW Player only when we need it
		yepnope({
			load : '/assets/jwplayer/jwplayer.js',
			callback: function () {
				vids.each(function(){

					jwplayer($(this).attr("id")).setup(
						{
							flashplayer: "/assets/jwplayer/player.swf",
							skin: "/assets/jwplayer/skins/modieus/modieus.zip",
							events:{
								onPlay: function(){
									if(typeof ukad_current_video != "undefined") {
										$.get('/resources/video-view-log/'+ukad_current_video);
									}
								}
							}
						}
					)
				});
			}
		});
	}

};

$(function(){

	ukad.init();

})


//===========================
// H1 Random Background Image
//===========================

$(document).ready(function(){
	var numRand = Math.floor(Math.random()*3);
	if(!$('body.athletes h1').hasClass('myadams')) {
	$('body.athletes h1').css('background-image', 'url(/assets/images/athletes/header' + numRand + '.jpg)');
	}
});

$(document).ready(function() {

//=============================================
// Email Signup link in footer - fancybox modal
//=============================================

	 $(".signup").fancybox({
  'width': 600,
  'height': 670,
 'overlayColor'  	: '#005484',
 'overlayOpacity'	: 0.9,
  'type': 'iframe',
  'scrolling'	: 'auto'
 });

});
