// remap jQuery to $
(function($){})(window.jQuery);


/* trigger when page is ready */
$(document).ready(function (){
	
	$('ol li').wrapInner('<span> </span>').addClass('green-bullets');
	initRotator();
	
	$(".main-navigation li.sub-navigation").hover(function(){
		$(this).addClass("hover").children("ul").stop(true,true).fadeIn(300);
		if ($(window).width() <767) {$(this).children("ul").hide();}
	},function(){
		$(this).removeClass("hover").children("ul").stop(true,true).fadeOut(300);
	});
	$(".quicklinks li:last-child").addClass("last-item");
	
	
});

//ROTATOR

//variables
var t
var slideDelay = 3000
var currSlide = ".current-slide"

//Initialise rotator

function initRotator(){
	$(".rotator ul").each(function(){
		//Set first and last slides
		$(this).children("LI:first").addClass("first").addClass("current-slide");
		$(this).children("LI:nth-child(2)").addClass("next-slide");
		$(this).children("LI:last").addClass("last");
		//Give each slide an ID
		var i = 1
		$(this).children("LI").each(function(){
			var slideID = "slide"+i
			$(this).attr("id", slideID).hide();
			i++;
		});
		$(".rotator .current-slide").show();
		$(".rotator .next-slide").show();

	});
	//Start auto rotate timer
	t = setTimeout("autoRotate();",slideDelay);
	
	
}

//Auto-Rotate
function autoRotate(){
	//Reset timer
	clearTimeout(t);
	var nextSlide
	//If current slide is the last the next slide will be the first, Otherwise the nest slide is the next numerically
	if ($(currSlide).hasClass("last")){
		nextSlide ="#slide1"
	}else{
		var nextSlide = $(currSlide).next();
	}
	//Rotate to the next slide
	rotate(nextSlide);
}

//Rotate
function rotate(nextSlide){
	//Give the next slide the class "next"
	$(".rotator .next-slide").removeClass("next-slide");
	$(nextSlide).addClass("next-slide").show();
	//Fade out current slide
	$(currSlide).fadeOut(800, function(){
		//Rename the next slide to be the current slide after previous slide fades out
		$(currSlide).removeClass("current-slide").hide();;
		$(nextSlide).addClass("current-slide").removeClass("next-slide");

		//Restart auto Rotate timer
		t = setTimeout("autoRotate();",slideDelay);
	});
}

function equalise(){
	$(".info-and-links-list li a").height("auto");
	if ($(window).width() >= 480 && $(window).width() <=767) {
		$(".info-and-links-list li:nth-child(odd)").each(function(){
			var thisHeight = $(this).children("a").height();
			var nextHeight = $(this).next("li").children("a").height();
			if (thisHeight > nextHeight){
				$(this).children("a").height(thisHeight);
				$(this).next("li").children("a").height(thisHeight);
			} else if (nextHeight > thisHeight){
				$(this).children("a").height(nextHeight);
				$(this).next("li").children("a").height(nextHeight);
			}
		});
	}
}


/* optional triggers*/

$(window).load(function() {
	equalise();
	
});

$(window).resize(function() {
	equalise();
});


