
var FC = {

    showHide: function () {
        $('.show-hide').hide();
        //when the checkbox is clicked, show the information

        $('.hideShowChkbox').bind('click', function () {

            if (this.checked) {
                $('.show-hide').slideDown(100);
            }

            else {
                //hide the div
                $('.show-hide').slideUp(100);
            }

        });
    },


    productDetail: function()
    {
    		$('body').addClass('js');
    		
		var details = $('.detail');
		details.each
		(
			function()
			{
				$(this).prepend($(this).find('h2 a').addClass('marker'));
			}
		)
		$('.marker').each
		(
			function()
			{
				$(this).bind
				(
					'mouseover focus',
					function()
					{
						$(this).next().css('display', 'block');
					}
				)
				$(this).bind
				(
					'mouseout blur',
					function()
					{
						$(this).next().css('display', 'none');
					}
				)
			}
		)

    },

	/* Added by RW: 01/10/10
	 * Wrap the instructions in a block-level element to prevent float issues with content.
	 * Ideally this should be done on the back-end.
	 */
	recipeInstructions : function(){

		$('ul.instructions-lozenge').wrap('<div />');
	},

	/*
	 * Homepage hero JS 
	 * /Rather than fix this I rewote in a couple of mins.
	 */
    promo: function () {
		
		var $promo = $('div.promo');
		var $menuList = $promo.find('ol.promo-list');
		var $menuListItems = $menuList.find("li");
        var $contentList = $promo.find('div.content ol');
        var $contentListItems = $contentList.find('li');
		
		//hide all but one - this should be done with CSS
		$contentListItems.hide();
		$contentListItems.eq(0).show();
		
		$menuListItems.bind("mouseover",menuItemOverHandler);
		
		function menuItemOverHandler(e) {
			var $this = $(this);
			
			//highlight menu item
			$menuListItems.removeClass("current");
			$this.addClass("current");
			
			//show correct slide
			$contentListItems.hide();
			$contentListItems.eq($menuListItems.index(this)).show();
			
		}
		
		//Set height of images
		var heightImage = $promo.find('.promo-wrapper').height() + 6;
		$promo.find('.image img').height(heightImage);
		
		/*
        var $promoPar = $('div.promo');
        var $clickedPar = $promoPar.find('ol.promo-list');
        var $contentPar = $promoPar.find('div.content ol');

        //fade all items out and then show the first one
        $contentPar.find('li').hide;
        $contentPar.find('li:first-child').stop().fadeIn('1');


        $('div.promo ol.promo-list a').bind('click', function () {
            var el = this;
            var $contentItem; //left hand side content
            var $currentItem = $clickedPar.find('li.current'); // right hand side menu
            var $currentContentItem = $contentPar.find('li.current');
            var index = $clickedPar.find('a').index(el);


            //move the current class to the item that's just been selected
            $currentItem.removeClass('current');
            $(el).parent().addClass('current');

            //hide the content of the currently selected item and then show the content of the item that's just been selected
            $currentContentItem.fadeOut('slow', function () {
                $currentContentItem.removeClass('current');
                $contentItem = $contentPar.find('li').eq(index);
                $contentItem.addClass('current');
                $contentItem.fadeIn('slow');
                $currentContentItem = $contentItem;
            });

            return false;
        });
		*/
    }
}
$(function () {

    if ($('.show-hide').length) {
        FC.showHide();
    }

    if ($('div.promo').length) {
        FC.promo();
    }
    
    FC.productDetail();

	FC.recipeInstructions();
    
    
	//LIST HEIGHT
	$('.sub-section').removeClass('accessibility-hide').css('visibility', 'hidden');
	var height=0;
	$('.sub-section ul').each
	(
		function()
		{
			if($(this).height() > height)
			{
				height = $(this).height();
			}
    		}
	)
	var ref = $('ul.main-navigation > li:last');
	var p = ref.position();
	var top = p.top + ref.height();
	$('.sub-section').css('top', top);
	$('.header-wrapper').css('height', top + height);
	$('.sub-section').css('visibility', 'visible');


	//CLEAR LINKS
	function clearLinks()
	{
		$('ul.main-navigation > li > a').removeClass('active');
	}

	//HIDE SUB-SECTIONS
	function hideSubsections()
	{
		$('.sub-section').addClass('accessibility-hide');
	}
	hideSubsections();


	//ACTIVATE CURRENT SECTION
	function activateCurrent()
	{
		if ($('ul.main-navigation > li > a.current').length>0)
		{
			clearLinks();
			hideSubsections();
			$('ul.main-navigation > li > a.current').addClass('active').next().removeClass('accessibility-hide');
		}
       	}
       	activateCurrent();
       	
	$('ul.main-navigation > li > a').each
	(
		function()
		{
			$(this).bind
			(
				'mouseenter focus',
				function()
				{
					clearLinks();
					$(this).addClass('active');
					hideSubsections();
					$(this).next('.sub-section').removeClass('accessibility-hide');
				}
			);
		}
	)
	
	$('ul.main-navigation').bind
	(
		'mouseleave',
		function()
		{
			activateCurrent();
		}
	);
	
	$('.page a:eq(0)').bind
	(
		'focus',
		function()
		{
			activateCurrent();
		}
	);
	
	//Add classes to .instructions ol li to show numbering
	var count = 1;
	$('.instructions ol > li').each(function() {  
	    $(this).addClass('item-0'+count).addClass('clear');	
	    count++;
	});
	
	//Add classes to .instructions ol ul to show sub-list lozenge and then add 'first' class to first list item
	$('.instructions ol ul').each(function() {  
	    $(this).addClass('instructions-lozenge').addClass('clear');	
	    $('.instructions-lozenge li:first').addClass('first');
	});
});



