jQuery(document).ready(function() {
    
    // Fix IE7 z-indexing
    $(function() {
        var zIndexNumber = 1000;
        $('li').each(function() {
            $(this).css('zIndex', zIndexNumber);
            zIndexNumber -= 10;
        });
    });
    
	// Hide dynamic elements
	$('[id^=menu], #fixed-footer, .products div, li .answer, .bean-to-cup a span, .history .detail, li.country ul').hide();
    
    // Display sub menus	    
    $('ul.main-nav li.sub-menu').hoverIntent({
        over: showMenu, 
        timeout: 250, 
        out: hideMenu
    });    
    function showMenu() {
        $(this).addClass('over');
        $('#menu-' + this.id).slideDown('fast', function() {});
    }
    function hideMenu() {
        $(this).removeClass('over');
        $('#menu-' + this.id).slideUp('fast', function() {});
    }
    
    // Show country dropdowm
    $('li.country').hoverIntent(function () {
        $('li.country ul').slideDown('fast', function() {});
    }, function () {
        $('li.country ul').slideUp('fast', function() {});
    });
    
    // Show and hide homepage footer
	$(".display-footer").mouseover(function() {		
		$("#fixed-footer").slideToggle("fast", function() {});
		if($(this).hasClass("show-footer")){
			$(this).addClass("hide-footer");
			$(this).removeClass("show-footer");
		}else{
			$(this).addClass("show-footer");
			$(this).removeClass("hide-footer");
		}
	});
    
    // Show food info	    
    $('ul.food li').hoverIntent(function () {
        $('a', this).addClass('selected');
        $('div', this).css('display', 'block');
    }, function () {
        $('a', this).removeClass('selected');
        $('div', this).css('display', 'none');
    });
    
    // Show coffe info	    
    $('ul.coffee li').hoverIntent(function () {
        $('a', this).addClass('selected');
        $('div', this).slideDown('fast', function() {});
    }, function () {
        $('a', this).removeClass('selected');
        $('div', this).slideUp('fast', function() {});
    });
    
    // Show bean to cup info	    
    $('.bean-to-cup a').hoverIntent(function () {
        $('.info', this).slideDown('fast', function() {});
    }, function () {
        $('.info', this).slideUp('fast', function() {});
    });
    
    // Toggle FAQ
	
	/* This code can be DELETED
	$('.q-and-a li').click(function() {
		if ($('.answer', this).is(':visible')) {
			$('.answer', this).slideUp('fast', function() {});
            $('a', this).removeClass('open');
		} else {
			$('.answer').slideUp('fast', function() {});
            $('.q-and-a li a').removeClass('open');
			$('.answer', this).slideToggle('fast', function() {});
            $('a', this).addClass('open');
		};
		return false;
	});
	*/

 $('.q-and-a li .question').click(function() {
    if ($(this).next('.answer').is(':visible')) {
        $(this).next('.answer').slideUp('fast', function() {});
        $(this).removeClass('open');
    } else {
      $('.answer').slideUp('fast', function() {});
            $('.q-and-a li a').removeClass('open');
            $(this).next('.answer').slideDown('fast', function() {});
            $(this).addClass('open');
    };
    return false;
  });





    // History timeline
    $(function() {
		if( $( "#slider" ).is("*") ) {
			var showDelay = 250;
			var showTimer = null;
			
			$( "#slider" ).slider({
				range: "min",
				value:0,
				min: 0,
				max: 4956,
				step: 708,
				slide: function( event, ui ) {
					//$('.timeline-container').scrollTo( ui.value, 300 );
					if (showTimer)
					clearTimeout(showTimer);
					showTimer = setTimeout(function() {
						$('.timeline-container').scrollTo( ui.value, 300 );
						$('.detail').slideUp('fast', function() {});
						$('.show-detail').show('fast', function() {});
					}, showDelay);
				}        
			});
			$('.timeline-container').scrollTo( $( "#slider" ).slider( "value" ), 300 );
			$('.detail').slideUp('fast', function() {});
			$('.show-detail').show('fast', function() {});
		}
    });
    
    // Toggle history extra detail
	$('.show-detail').click(function() {
		$('#detail-' + this.id).slideDown('fast', function() {});
		$(this).hide('fast', function() {});
		return false;
	});
	$('.hide-detail').click(function() {
		$('.detail').slideUp('fast', function() {});
		$('.show-detail').show('fast', function() {});
		return false;
	});
    
    // Meet the baristas
	$('.baristas ul li a').click(function() {
		$('.baristas img').attr('src', 'images/baristas/barista_' + this.id + '.jpg');
        return false;
	});
    
    // Show form help
    $('.show-help').hoverIntent(function () {
        $('.help').show('fast', function() {});
    }, function () {
        $('.help').hide('fast', function() {});
    });
    
    // Prevent click through to landing pages when js is enabled
	$('.no-click').click(function() {
		return false;
	});
    
});
