$(function () {
    "use strict";
    var homeHeight = $(window).height(),
        apiKey = '1acfbe7b8dc36fc88d831d5ab630c8ce';
    $("header nav ul li").hoverIntent(function () {
		$('ul', this).filter(':not(:animated)').slideDown();
    }, function () {
		$('ul', this).slideUp();
    });
	$("body.home").css("min-height", homeHeight);
    if ($(".liqueur-orange").length > 0) {
        $("li.selected li:first").addClass("selected");
    }
	if ($(".recipes").length > 0) {
        $("header nav li:nth-child(4):not(li ul li:nth-child(4))").addClass("selected");
    }
	$(".bottle").hover(function () {
		$(".new-bottle").fadeOut();
		$(".old-bottle").fadeIn();
	}, function () {
		$(".new-bottle").fadeIn();
		$(".old-bottle").fadeOut();
	});
	$("input, textarea").placehold();
	$(".timeline-page .image-slider").flexslider({
        animation: "slide",
        slideshow: false,
	    animationDuration: 800,
		directionNav: true
    });
	$(".timeline-page .flex-control-nav li:nth-child(1) a").text("1809");
	$(".timeline-page .flex-control-nav li:nth-child(2) a").text("1828");
	$(".timeline-page .flex-control-nav li:nth-child(3) a").text("1834");
	$(".timeline-page .flex-control-nav li:nth-child(4) a").text("1842");
	$(".timeline-page .flex-control-nav li:nth-child(5) a").text("1848");
	$(".timeline-page .flex-control-nav li:nth-child(6) a").text("1850");
	$(".timeline-page .flex-control-nav li:nth-child(7) a").text("1853");
	$(".timeline-page .flex-control-nav li:nth-child(8) a").text("1860");
	$(".timeline-page .flex-control-nav li:nth-child(9) a").text("1870");
	$(".timeline-page .flex-control-nav li:nth-child(10) a").text("1871");
	$(".timeline-page .flex-control-nav li:nth-child(11) a").text("1879");
	$(".timeline-page .flex-control-nav li:nth-child(12) a").text("1889");
	$(".timeline-page .flex-control-nav li:nth-child(13) a").text("1917");
	$(".timeline-page .flex-control-nav li:nth-child(14) a").text("1966");
	$(".timeline-page .flex-control-nav li:nth-child(15) a").text("2009");
	$(".recipe-slider").cycle({
		fx:     'scrollHorz',
		speed:  700,
		timeout: 0,
		next:   '.next-recipe',
		prev:   '.prev-recipe'
	});
	$(".history-page .slides").cycle({
		fx:     'scrollHorz',
		speed:  400,
		timeout: 0,
		next:   '.next',
		prev:   '.prev'
	});
	$('.next-slide').click(function (e) {
		$(".history-page .slides").cycle('next');
		e.preventDefault();
	});
	$(".slide-1 .frame .no-list-style").cycle({
		fx:     'fade',
		timeout: 8000,
		pager: '.pager-wrap-1'
	});
	$(".slide-2 .frame .no-list-style").cycle({
		fx:     'fade',
		timeout: 8000,
		pager: '.pager-wrap-2'
	});
	$(".slide-3 .frame .no-list-style").cycle({
		fx:     'fade',
		timeout: 8000,
		pager: '.pager-wrap-3'
	});
	$(".slide-4 .frame .no-list-style").cycle({
		fx:     'fade',
		timeout: 8000,
		pager: '.pager-wrap-4'
	});
	$(".recipe-content .highlight-list br").remove();
	if ($(".flickr-images-1").length > 0) {
        $.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + apiKey + '&photoset_id=72157628350654693&format=json&jsoncallback=?',
            function (data) {
                $.each(data.photoset.photo, function (i, item) {
                    var photoURL = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_m.jpg',
                        photoID = item.id,
                        imgLarge = photoURL.split('m.jpg')[0] + 'b.jpg';
                    $.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photos.getInfo&api_key=' + apiKey + '&photo_id=' + photoID + '&format=json&jsoncallback=?',
                        function (data) {
                            if (data.photo.tags.tag !== '') {
                                var tagsArr = new Array(),
                                    tags = tagsArr.join(', ');
                                $.each(data.photo.tags.tag, function (j, item) {
                                    tagsArr.push('<a href="http://www.flickr.com/photos/tags/' + item._content + '">' + item.raw + '</a>');
                                });
                            }
                            var imgCont = '<a href="' + imgLarge + '" rel="gallery" title="' + data.photo.description._content + '"><img src="' + photoURL + '" /></a>';
                            $(imgCont).appendTo('.flickr-images-1');
                            $(".photo-gallery a").fancybox({titlePosition: 'inside'});
                        });
                });
            });
    }
		if ($(".flickr-images-2").length > 0) {
	        $.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + apiKey + '&photoset_id=72157628350604487&format=json&jsoncallback=?',
	            function (data) {
	                $.each(data.photoset.photo, function (i, item) {
	                    var photoURL = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_m.jpg',
	                        photoID = item.id,
	                        imgLarge = photoURL.split('m.jpg')[0] + 'b.jpg';
	                    $.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photos.getInfo&api_key=' + apiKey + '&photo_id=' + photoID + '&format=json&jsoncallback=?',
	                        function (data) {
	                            if (data.photo.tags.tag !== '') {
	                                var tagsArr = new Array(),
	                                    tags = tagsArr.join(', ');
	                                $.each(data.photo.tags.tag, function (j, item) {
	                                    tagsArr.push('<a href="http://www.flickr.com/photos/tags/' + item._content + '">' + item.raw + '</a>');
	                                });
	                            }
	                            var imgCont = '<a href="' + imgLarge + '" rel="gallery" title="' + data.photo.description._content + '"><img src="' + photoURL + '" /></a>';
	                            $(imgCont).appendTo('.flickr-images-2');
	                            $(".photo-gallery a").fancybox({titlePosition: 'inside'});
	                        });
	                });
	            });
	    }
			if ($(".flickr-images-3").length > 0) {
		        $.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + apiKey + '&photoset_id=72157628394611295&format=json&jsoncallback=?',
		            function (data) {
		                $.each(data.photoset.photo, function (i, item) {
		                    var photoURL = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_m.jpg',
		                        photoID = item.id,
		                        imgLarge = photoURL.split('m.jpg')[0] + 'b.jpg';
		                    $.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photos.getInfo&api_key=' + apiKey + '&photo_id=' + photoID + '&format=json&jsoncallback=?',
		                        function (data) {
		                            if (data.photo.tags.tag !== '') {
		                                var tagsArr = new Array(),
		                                    tags = tagsArr.join(', ');
		                                $.each(data.photo.tags.tag, function (j, item) {
		                                    tagsArr.push('<a href="http://www.flickr.com/photos/tags/' + item._content + '">' + item.raw + '</a>');
		                                });
		                            }
		                            var imgCont = '<a href="' + imgLarge + '" rel="gallery" title="' + data.photo.description._content + '"><img src="' + photoURL + '" /></a>';
		                            $(imgCont).appendTo('.flickr-images-3');
		                            $(".photo-gallery a").fancybox({titlePosition: 'inside'});
		                        });
		                });
		            });
		    }
});

$(window).load(function () {
    "use strict";
    var homeFadeTarget = $(".opacity .home .main,.opacity .home footer,.opacity .home .container"),
        fadeTarget =	 $(".opacity .subpage .main,.opacity .subpage footer,.opacity .subpage .container");
	if ($(".age-verification-wrap").length) {
		$(homeFadeTarget).delay(500).animate({
		    opacity: 1
		}, 'slow');
		$('.image-slider').flexslider({
			slideshow: false,
	        animation: "slide",
			animationLoop: false,
		    directionNav: true,
	        slideshowSpeed: 6000,
		    animationDuration: 600,
		    controlNav: false,
		    pauseOnAction: true,
		    start: function (slider) {
				$(".yes").click(function (e) {
					$(".age-verification-wrap").fadeOut();
					$(".bw-img").delay(1500).fadeOut(1200);
					slider.resume();
					e.preventDefault();
				});
		    }
	    });
	} else {
		$(homeFadeTarget).delay(200).animate({
		    opacity: 1
		}, 'slow');
	    $(fadeTarget).delay(200).animate({
	        opacity: 1
	    });
		$('.image-slider').flexslider({
	        animation: "slide",
			animationLoop: false,
		    directionNav: true,
	        slideshowSpeed: 6000,
		    animationDuration: 600,
		    controlNav: false,
		    pauseOnAction: true,
		    start: function () {
			    $(".bw-img").delay(2500).fadeOut(1500);
		    }
	    });
	}	
	$(".new-bottle").delay(2500).fadeOut("slow");
	$(".old-bottle").delay(2500).fadeIn("slow",function () {
		$(".old-bottle").delay(1500).fadeOut("slow");
		$(".new-bottle").delay(1500).fadeIn("slow");
	});
});
