/**
 * Warburtons.co.uk
 * /our-products/recipes/ javascript
 *
 */
 
 $(document).ready(function(){
 	/**
 	 * Show hide sections on click
 	 */
 	$('div.recipe').hide();
 	/* but make sure to show the currently selected one */
 	$('div.selected div.recipe').show();
 	 
 	showHideRecipe();
 	 
 	/**
 	 * Load recipes with AJAX via pagination click
 	 */
 	loadRecipe();
 });
 
 /* showHideRecipe */
 function showHideRecipe(){
 
 	
 	
 	/* make the headings links - linking to wrapping div #recipes to keep focus in place */
 	$('div#recipes div.section h2 span').each(
 		function(){
 			var html = $(this).html();
 			$(this).html('<a href="#PrimaryContent">'+html+'</a>');
 		}
 	);
	
	/* toggle the slide up/down */
	$('div#recipes div.section h2').click(
		function(){
			$('div.recipe').slideUp(); /*hide all other recipes*/
			$('div#recipes div.section h2').removeClass('selected');
			
			if($(this).next('div.recipe').css('display') == 'none'){
				$(this).next('div.recipe').slideDown('slow'); /*slide down appropriate*/
				$(this).addClass('selected');
			}
			else{
				$(this).next('div.recipe').slideUp('slow');
				$(this).removeClass('selected');
			}
		}
	);
 }
 
 /* loadRecipe */
 function loadRecipe(){

 	/* hide loading holder */
	$("img.ajax-loader").hide();
	/* load in new recipe on click */
 	$('ul.pagination li a').click(function(){
 		$(this).unbind();
		/* href value of clicked link */
		var URL = $(this).attr("href");
		/* hide current recipe */
		$(this).parent().parent().parent().hide();
		/* show loading GIF */
		$(this).parent().parent().parent().next('img.ajax-loader').show();
		/*grab new contents and put them into same location */
		$(this).parent().parent().parent().load(URL,'',
			function (responseText, textStatus, XMLHttpRequest){
			  /* make sure the <div> is shown for IE6 :( */
			  $(this).parent().children('div.recipe').show();
			  /* re-run function */			  
			  loadRecipe();
			}
		);
		/*stop link completing*/		
		return false;
 	});
 }
 