$(function(){
	
	//Global Variables
	var animating = false;
	var width = 0;
	

	//Changed styling based on wether Javascript is enabled
	$('body').removeClass('no-js').addClass('js');
	
	
	
	
	
	
	
	//Hacked function to get the secondary navigation to center
	$('#primary-nav li.current-menu-item .sub-menu li').each(function() {
		width += ($(this).width());
	});
	$('#primary-nav li.current-menu-item .sub-menu').css({'margin-left' : -width/2, 'width' : width, 'visibility' : 'visible'});
	
	
	$('#primary-nav li.current-menu-ancestor .sub-menu li').each(function() {
		width += ($(this).width());
	});
	$('#primary-nav li.current-menu-ancestor .sub-menu').css({'margin-left' : -width/2, 'width' : width});
	$('#primary-nav li.current-menu-ancestor .sub-menu').css('visibility', 'visible');
	
	//Check if poll results are being shown and if so animate the bars
	var pollComplete = false
	var animatePoll = setInterval(function() {
		if($(".pollbar").length && (pollComplete != true)) {
			$('.pollbar').each(function() {
				var pollWidth = $(this).attr('rel');
				$(this).css('width', (2.7 * pollWidth.match(/\d+/)));
				$(this).show('slide', {direction: 'left'}, 1000);
			});	
			pollComplete = true;
		}
		clearInterval(animatePoll);
	}, 100);


	//Start orbit on the home page
	$('#featured').orbit({
		captions: true,
		animationSpeed: 1000
	});
	
	//IE 8 and lower fix for nth-child
	$('.landing-item:nth-child(even)').css('margin-left', '21');
	
	//Open the breadmakers drop down if user clicks on bakery products
	if (window.location.hash == '#breadmakers') {
		$('.breadmakers').find('h2').addClass('expanded')
		$('.breadmakers').find('div').slideToggle();
	}
	

		
	
	//Glossary page
	$('.drop-down').each(function() {
		if ($(this).find('h2').attr('rel') == 'a') {
			$(this).show();
		}
	});
	$('#glossary-nav a').click(function() {
		var category = $(this).attr('rel');
		$('.drop-down').hide();
		$('.drop-down').each(function(index) {			
			if ($(this).find('h2').attr('rel') == category) {
				$(this).show();
			}
		});
		$('#glossary-nav a').css('color', '#717171');
		$(this).css('color', '#E34205');
		return false;
			
	});
	
	
	
	//Image based menu
	$('#cooking-advice-menu li a').hover(function () {
		$('#cooking-advice-menu').parent().find('img').attr('src', $(this).attr('rel'));
		
	}, function() {
		
	}).click(function () {
		
		var a = $(this).attr('id');
		a = '.' + a;
		$('.tab-content').hide();
		$(a).show();
		
		$('#cooking-advice-menu li a').removeClass('active');
		$(this).addClass('active');
		
		return false;
	});
	
	
	// Drop down shizzle 
	$('.drop-down h2').click(function() {
		$(this).toggleClass('expanded');
		$(this).parent().find('div').slideToggle();
		return false;
	});
	
	//Show advanced recipe search options
	$('#recipe-search-advanced').click(function () {
		$('#recipe-search-hidden').stop().slideToggle();
		return false;
	});
	
	
	
	
	
	
	
	
	/* SLIDER FUNCTIONS (COOK AND BAKERY PAGES) */
	$('#slider-left').click(function () {
		tik_slide('left');
	});
	
	$('#slider-right').click(function () {
		tik_slide('right');
	});
	
	
	function tik_slide (direction) {
		
		//Lets exit if we are already sliding
		if (animating == true) {
			return;
		}
		
		//Find out what slide we are on and how many slides we have
		slideNo = $('#slider').find('.active').attr("rel");
		slideMax = $('#slider ul li').length;
	
		switch (direction)
		{
			case 'left':
				animating = true;
				slideNo = (slideNo == 1) ? slideMax : --slideNo; //If on last slide then go back to start
				$('#slider').find('.active').removeClass('active').hide('slide', {direction: "right"}, 1000); //Hide active slide and remove class
				$('#slider').find('.slide'+slideNo).css("left", "-280px").addClass("active").show().animate({left: 0}, 1000, function (){animating = false;});
				$('.slider-caption').hide();
				$('.caption'+slideNo).show();
				break;
			case 'right':
				animating = true;
				slideNo = (slideNo == slideMax) ? 1 : ++slideNo; //If on last slide then go back to start
				$('#slider').find('.active').removeClass('active').hide('slide', {direction: "left"}, 1000); //Hide active slide and remove class
				$('#slider').find('.slide'+slideNo).css("left", "280px").addClass("active").show().animate({left: 0}, 1000, function (){animating = false;});
				$('.slider-caption').hide();
				$('.caption'+slideNo).show();
				break;
		}
		
	}
	
	

	

	 
});



