$(document).ready(function () {
	
	// fade in content on window load
	  $(window).load(function () {
       $("#age-check").animate({opacity:1});
     });

	// fade in content on window load
	  $(window).load(function () {
       $("body").animate({opacity:1});
     });

	// age check hover

	$(".legal-nav a").hover(function(){

		$("span.visible-nav", this).stop().animate({ opacity: 0 });
		$("span.invisible-nav", this).stop().animate({ opacity: 1 });

	}, function() {

		$("span.invisible-nav", this).stop().animate({ opacity: 0 });
		$("span.visible-nav", this).stop().animate({ opacity: 1 });

	});
	
	$("#yes").click(function(e){
		$("#age-check").fadeOut();
		e.preventDefault();
	});
		

	// main nav hover
	
	$("#main-nav .hover-nav").hover(function(){
		
		$("span.visible-nav", this).stop().animate({ opacity: 0 });
		$("span.invisible-nav", this).stop().animate({ opacity: 1 });
		
	}, function() {
	
		$("span.invisible-nav", this).stop().animate({ opacity: 0 });
		$("span.visible-nav", this).stop().animate({ opacity: 1 });
		
	});

	// dropdown menu
	$("ul.dropdown li").hover(function(){

	        $(this).addClass("hover");
	        $('ul:first',this).css('visibility', 'visible');

	    }, function(){

	        $(this).removeClass("hover");
	        $('ul:first',this).css('visibility', 'hidden');
	});


	// twitter feed
	if ( $("#twitter").length > 0 ) {
		
			 $("#twitter").tweet({
		      username: "DomaineDeCanton",
		      count: 3,
		      loading_text: "loading tweets..."
		    });
	};
	
	// video popup
	if ( $(".video-link").length > 0 ) {	
	
		$(".video-link").fancybox({
			'width': 800,
			'height': 450,
			'padding': 0,
			'overlayColor': '#000',
			'overlayOpacity': .7,
			'showNavArrows': false
		});
	
	};
	
	// homepage drink slider
//	if ( $("#cocktail-rotator").length > 0 ) {
//		$("#cocktail-rotator").cycle({
//			timeout: 6000
//		});
//	};
	
	// newsletter popup
	
	$("#newsletter-button").fancybox({
		'overlayColor': '#000',
		'overlayOpacity': .6,
		'showNavArrows': false,
		'padding': 1,
		'width' : 1000,
		'height' : 500,
		'type'  : 'iframe'
	});
	
	// carousel setup
	if ( $(".slider").length > 0 ) {
		
		$('.slider').jcarousel({
			'scroll':  1,
			'speed':   'slow',
			'visible': 5,
			'wrap': 		'circular'
		});
		
		$('.jcarousel-item').hover(function() {

			$('.hover-info', this).filter(':not(:animated)').animate({ opacity: 1 });

		}, function() {

			$('.hover-info', this).animate({ opacity: 0 });

		});
		
	};
	
	// cycle setup
	if ( $(".drink-search-results .drink").length > 1 ) {

		$('.drink-search-results .webappsearchresults').before('<ul><li class="prev">Prev</li><li class="next">Next</li></ul>').cycle({
			fx:     'scrollHorz',
			timeout: 0,
			next: ".next",
			prev: ".prev",
			height: 400,
			width: 500,
			pager:  ".cycle-nav",
	    pagerAnchorBuilder: function (idx, slide) {
			return '<li><a href="#"><img src="' + jQuery(slide).children("img").eq(0).attr("src") + '" /></a></li>';
			}
		});
	};
	
	// carousel setup
	if ( $(".featured-bartender-list").length > 0 ) {

		$('.featured-bartender-list').jcarousel({
			'scroll':  1,
			'speed':   'slow',
			'visible': 6,
			'wrap': 		'circular'
		});
	};
	
	// recipe overlay setup
	if ( $(".click-target").length > 0 ) {
		
		$(".slider li a").fancybox({
			'cyclic':  true,
			'overlayColor': '#000',
			'overlayOpacity': .6,
			'padding': 1
		});
		
	};
	
	if ( $(".drink-list").length > 0 ) {
		
		$(".drink-list li a").fancybox({
			'cyclic':  true,
			'overlayColor': '#000',
			'overlayOpacity': .6,
			'padding': 1
		});
		
	};
	
	// BTOY Form setup
	$(".region-us").click(function(){
		
		$(".country-list").slideUp("fast");
		$(".city-list").slideDown("slow");
		
	});
	
	$(".region-international").click(function(){
		
		$(".city-list").slideUp("fast");
		$(".country-list").slideDown("slow");
		
	});
	
	
	
	// news cycle setup
	if ( $("#content-changer-wrap").length > 0 ) {
		
			$("#content-changer-wrap").organicTabs({
	        "speed": 200
	    });
		
	};
	
	// news page twitter feed
	if ( $("#twitter-holder").length > 0 ) {
		
			 $("#twitter-holder").tweet({
		      username: "DomaineDeCanton",
		      count: 10,
		      loading_text: "loading tweets..."
		    });
	};
	
	// flickr api feed
	if ( $("#flickr-holder").length > 0 ) {
		
        //assign your api key equal to a variable
        var apiKey = '466ee0414072ee01344c81828d0ef93a';

        //the initial json request to flickr
        $.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + apiKey + '&photoset_id=72157627494153228&format=json&jsoncallback=?',
        function(data){

            //loop through the results with the following function
            $.each(data.photoset.photo, function(i,item){

                //build the url of the photo in order to link to it
                var photoURL = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_m.jpg';

                //turn the photo id into a variable
                var photoID = item.id;

                    //use another ajax request to get the tags of the image
          	$.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photos.getInfo&api_key=' + apiKey + '&photo_id=' + photoID + '&format=json&jsoncallback=?',
                    function(data){

                        //if the image has tags
                        if(data.photo.tags.tag != '') {

                            //create an empty array to contain all the tags
                            var tagsArr = new Array();

                            //for each tag, run this function
                            $.each(data.photo.tags.tag, function(j, item){

                                //push each tag into the empty 'tagsArr' created above
                                tagsArr.push('<a href="http://www.flickr.com/photos/tags/' + item._content + '">' + item.raw + '</a>');

                            });

                            //turn the tags array into a string variable
                            var tags = tagsArr.join(', ');
                        }


                    var imgLarge = photoURL.split('m.jpg')[0] + 'b.jpg';
                    var imgCont = '<a href="' + imgLarge + '" rel="gallery"><img src="' + photoURL + '" /></a>';

                    //append the 'imgCont' variable to the document
                    $(imgCont).appendTo('#flickr-holder');
                                        
                        $("#flickr-holder a").hover(
                          function () {
                            $(this).stop().animate({opacity: 1}, 400);
                          },
                          function () {
                            $(this).stop().animate({opacity: .8}, 200);
                          }
                        );

                        $("#flickr-holder a").fancybox({
													'overlayColor': '#000',
													'overlayOpacity': .6,
													'padding': 1
												});

           });
        });
      });
    };

});
