  $(function(){             
	  
	  var photosetID = querySt('id');
	  var title = querySt('t');
	  
	  //assign your api key equal to a variable
	  var apiKey = '8e9e3da506a2011602677c611f822112';
	  var photosetID = querySt('id');
	  var title = querySt('t');
	  var counter = 0;
	  var page = 0;
	  var picsPerPage = 20;
	  var totalPhotos = 0;
	  var pages = 0;
	  //the initial json request to flickr
	  //to get your latest public photos, use this request: http://api.flickr.com/services/rest/?&method=flickr.people.getPublicPhotos&api_key=' + apiKey + '&user_id=29096781@N02&per_page=15&page=2&format=json&jsoncallback=?
	  
	  $.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + apiKey + '&photoset_id=' + photosetID + '&format=json&jsoncallback=?',
	  function(data){
		
		if(data.stat == 'fail'){
			$('#error').html('Sorry, this gallery is not available right now because: '+data.message+'<br /> Please try another <a href="http://www.gagnonwelding.com/Gallery.html">gallery</a>');
			return false;	
		}
		title = title.replace(/%20/g, ' ');
		  $("#title").text(title);
		  totalPhotos = $(data.photoset.photo).length;	
		  var Data = data;
		  var pages = Math.ceil(totalPhotos / picsPerPage);
		  
		  if(pages > 1){
		  for(var i=0; i<pages; i++){
			 var pageLink = '<a href="#" class="pageChanger" id="' + i + '"> Page ' + (i+1) + '</a> ';
			 $('#pages').append(pageLink);
		  }
		  $('#pages a:first-child').addClass('on');
		  }
		  populatePage(Data,0);
		  
			 $('a.pageChanger').click(function(){
			    var targetPage = $(this).attr('id');
				$('#image-container').fadeOut(function(){populatePage(Data, targetPage);})
				$('#pages a').removeClass('on');
				$(this).addClass('on');
				return false;
			 });			 
		});
	    
	  function populatePage(data, page){
		$('#image-container').empty();
		for(var i=0; i<	picsPerPage && page*picsPerPage+i < totalPhotos ; i++){
		  var item = data.photoset.photo[i + page * picsPerPage];

		  //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 + '_s.jpg'
		  var bigPhotoURL = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '.jpg'
		  //turn the photo id into a variable
		  var photoID = item.id;
		  
			  //create an imgCont string variable which will hold all the link location, title, author link, and author name into a text string
			  var img = '<a href ="' + bigPhotoURL + ' "class="fancybox" rel="album" >';
			  img += '<img src="' + photoURL + '" alt="" />';
			  img += '</a>';
			  
			  //append the 'imgCont' variable to the document
			  $(img).appendTo('#image-container');
			  $('a.fancybox').fancybox(); 
		  }
		  $('#image-container').fadeIn();
		  $('a.fancybox').css({'opacity':.6});
		  $('a.fancybox').hover(function(){
					$(this).stop().animate({'opacity':1}, 250);	
		  },function(){
					$(this).stop().animate({'opacity':.6}, 250);							
		  });

	  }
	  });
	  
function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
	ft = gy[i].split("=");
	if (ft[0] == ji) {
	return ft[1];
}
}
}
                


