$(document).ready(function(){

	$('#main').hide();
		
	$('#sitenav a').click(function() {
		$('#main').show();
		$('#intro').hide();
		$('#sitenav').addClass('sticky');
		$('html,body').animate({ scrollTop: $($(this).attr('href')).offset().top - 105 });
		return false;
	});

	/* wine & news */

	var wineCurrentPosition = 0;
	var newsCurrentPosition = 0;
	
	var slideWidth = 860;
	
	var wineSlides = $('.wine-slide');
	var newsSlides = $('.news-slide');
	
	var wineSlideCount = wineSlides.length;
	var newsSlideCount = newsSlides.length;
	
	$('#wine-slideshow').css('overflow', 'hidden');
	$('#news-slideshow').css('overflow', 'hidden');
	
	wineSlides.wrapAll('<div id="wine-slideshow-inner"></div>').css({
		'float' : 'left',
		'width' : slideWidth
	});
	newsSlides.wrapAll('<div id="news-slideshow-inner"></div>').css({
		'float' : 'left',
		'width' : slideWidth
	});
	
	$('#wine-slideshow-inner').css('width', slideWidth * wineSlideCount);
	$('#news-slideshow-inner').css('width', slideWidth * newsSlideCount);
	
	$('#wine-slideshow').prepend('<span class="slideshow-control" id="wine-slideshow-left">Move left</span>').append('<span class="slideshow-control" id="wine-slideshow-right">Move right</span>');
	$('#news-slideshow').prepend('<span class="slideshow-control" id="news-slideshow-left">Move left</span>').append('<span class="slideshow-control" id="news-slideshow-right">Move right</span>');
	
	$('#wine-slideshow-left').bind('click', function(){
		wineCurrentPosition = wineCurrentPosition - 1;
		if (wineCurrentPosition < 0) {
			wineCurrentPosition = wineSlideCount - 1;
		}
		$('#wine-slideshow-inner').animate({
			'marginLeft' : slideWidth * (-wineCurrentPosition)
		});	
	});
	$('#wine-slideshow-right').bind('click', function(){
		wineCurrentPosition = wineCurrentPosition + 1;
		if (wineCurrentPosition >= wineSlideCount) {
			wineCurrentPosition = 0;
		}
		$('#wine-slideshow-inner').animate({
			'marginLeft' : slideWidth * (-wineCurrentPosition)
		});	
	});
	
	$('#news-slideshow-left').bind('click', function(){
		newsCurrentPosition = newsCurrentPosition - 1;
		if (newsCurrentPosition < 0) {
			newsCurrentPosition = newsSlideCount - 1;
		}
		$('#news-slideshow-inner').animate({
			'marginLeft' : slideWidth * (-newsCurrentPosition)
		});	
	});
	$('#news-slideshow-right').bind('click', function(){
		newsCurrentPosition = newsCurrentPosition + 1;
		if (newsCurrentPosition >= newsSlideCount) {
			newsCurrentPosition = 0;
		}
		$('#news-slideshow-inner').animate({
			'marginLeft' : slideWidth * (-newsCurrentPosition)
		});	
	});

	
	// contact
	
	$("#contact-from button").click(function(){
		$(".formError").hide();
	});

	$("#contact-form form").validationEngine();

	$("#contact-form form").submit(function(e){
		$('#loading').css('visibility','visible');
		$.post('submit.php',$(this).serialize()+'&ajax=1',
			function(data){
				$("#contact-form form").hide('slow').after('<h1>Thank you!</h1>');				
				$('#loading').css('visibility','hidden');
			}
		
		);
		e.preventDefault();
	})
    
});

$(window).load(function() {
    $('#intro-slider').nivoSlider({
            effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
            pauseTime: 3000, // How long each slide will show
            startSlide: 0, // Set starting Slide (0 index)
            directionNav: false, // Next & Prev navigation
            directionNavHide: true, // Only show on hover
            controlNav: false, // 1,2,3... navigation
            controlNavThumbs: false, // Use thumbnails for Control Nav
            keyboardNav: false, // Use left & right arrows
            pauseOnHover: false, // Stop animation while hovering
            manualAdvance:false // Force manual transitions
        });
});
