PCGalleryFlip = function(settings) {
	var currentPage = 0;	
	var photosPerPage = 10;
	var numPhotos;
	var currentSide = "front";
	var locked = false;

	var loadManager = {
		queue: 0,
		reserve: function() {
		    this.queue++;
		},
		checkIn: function() {
		    this.queue--;
			if(this.queue==0) this.readyFunction();
		},
		reset: function() {
		    this.queue = 0;
		},
		setReadyFunction: function(func) {
		      this.readyFunction = func;
		}
	}
	
	var nextSet = function() {
		if(locked) return;
		locked = true;

		currentPage++;
		
		if(!(((numPhotos > currentPage - 1) && (numPhotos < currentPage * 10)) || (numPhotos > currentPage * 10)))	{
			currentPage = 1;
		}
		
		$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=de78dca5357ee9b73aa33fc0a3f595e3&photoset_id=72157626916299680&per_page=" + photosPerPage + "&page=" + currentPage + "&format=json&jsoncallback=?", loadSet);
	}

	var loadSet = function(data) {
		numPhotos = data.photoset.photo.length;

		currentSide = currentSide == "front" ? "back" : "front";
		
		cards = $(".flickr-div ."+ currentSide);
		cardContainers = $(".flickr-div");
			
		for(var i=0; i < photosPerPage; i++) {
			if(i < numPhotos) {
				var item = data.photoset.photo[i];
				var title = item.title;
				var imageSource = "http://farm" + item.farm + ".static.flickr.com/" + item.server + "/" + item.id + "_" + item.secret + "_" + "m.jpg";
				var imageLink = "http://www.flickr.com/photos/" + data.photoset.owner + "/" + item.id + "/";
				
				cardContainers.eq(i).addClass("deciding");
				
				cards.eq(i).find("a").attr({
					href: imageLink
				})
				
				loadManager.reserve();
				
				var image = $("<img/>");
				var imageContainer = cards.eq(i).find("a");
				
				//IE 7/8 requires a work around to get proper image dimensions, possibly due to transitional doctype
				image.load((function(imageContainer) {
					return function(){
						 //this timeout fixes an issue where ie7/8 occasionally fails to display an image per image source being set
						setTimeout((function(self){
							return function() {
								realImage = imageContainer.find("img");
								realImage.attr("src", $(self).attr("src"));
								realImage.removeClass("vertical").removeClass("horizontal");	
								self.width > self.height ? realImage.addClass("horizontal") : realImage.addClass("vertical");
								loadManager.checkIn();
							}
						})(this),1)
					}
				})(imageContainer)).error(function(){
					loadManager.checkIn();
				})
				
				image.attr({
					src: imageSource
				})
				
			} else {
				//when they images above get loaded, they'll switch to flipped, so we need to make sure we set 
				//empty thumbs to flipped as well to keep states in sync
				cardContainers.eq(i).addClass("empty").toggleClass("flipped");
			}
		}
		
		loadManager.setReadyFunction(function() {
			displaySet(numPhotos);
		})
	}
	
	var displaySet = function(count) {
		for(i=0;i<count;i++) {
			setTimeout((function(n){
				return function() {
					$('.flickr-div').eq(n).removeClass("deciding").toggleClass("flipped").removeClass("empty");
				}
			})(i),i*50);
		}
		setTimeout(function(){
			locked = false;
		},count*50)
	}	
	
	var preloadContainer = $("<img style='height:1px;overflow:hidden;position:absolute;top:0;left:0'/>")
	
	return {
		nextSet: nextSet,
		loadManager: loadManager,
		fillThumbContainers: fillThumbContainers
	}
}

function nextSet() {
	myPCGalleryFlip.nextSet();
}

function fillThumbContainers() {
	//myPCGalleryFlip.fillThumbContainers();
}
