$(document).ready(function () {

	if ( $("#gallery-wrap").length > 0 ) {
		$("#gallery-wrap").cycle('fade');
	};
	
	if ( $(".sub-gallery").length > 0 ) {
		$(".sub-gallery").cycle('fade');
	};	
	
	if ( $("a.features").length > 0 ) {
	    $("a.features").fancybox();
	};
	
	if ( $("#flickr-images").length > 0 ) {
				
				$("#loader").show(); 
								
        //assign your api key equal to a variable
        var apiKey = 'e291f1706209119848fd19102ce12bdd';

        //the initial json request to flickr
        $.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + apiKey + '&photoset_id=72157626944956395&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" title="' + data.photo.title._content + '"><img src="' + photoURL + '" /></a>';

                    //append the 'imgCont' variable to the document
										$("#loader").fadeOut(function() {
											$(imgCont).appendTo('#flickr-images');
										});
                    
										
																			
                        $("#flickr-images a").hover(
                          function () {
                            $(this).stop().animate({opacity: 1}, 400);
                          },
                          function () {
                            $(this).stop().animate({opacity: .8}, 200);
                          }
                        );

                        $("#flickr-images a").fancybox();

                			});
								

          });
					
        });

    };
	
});


