KEYWORD = {
	//needle: new RegExp(needle,"g"),
}

KEYWORD.setNeedle = function(newNeedle) {
	this.needle = new RegExp(newNeedle,"gi");	
}

KEYWORD.getNeedle = function() {
	return this.needle;	
}

galleryLoader = {
	
	KEYWORD :{
		setNeedle:function(newNeedle) {
			this.needle = new RegExp(newNeedle,"gi");
		}
	},
	
	albumid: "",
	nextPhoto: "1",
	currentPhoto: "1",
	lastPhoto: "1",
	url:"",
	photoQuery:{
		album:{}
	},
	init:function(user, needle) {
		this.KEYWORD.setNeedle(needle);			
		//var that = this;
		this.user = user;
		this.albumQuery = "";
	
	},
	loadAlbums:function () {
		$.ajax({
			type:"GET",
			dataType:"jsonp",
			url: "http://picasaweb.google.com/data/feed/api/user/"+this.user+"?alt=json&kind=album",
			error: function(XMLHttpRequest, textStatus, errorThrown){
				$('<p></p>')
				.html("There was an error with your request")
				.appendTo('#errorPane');
				returnVal = false;
			},
			success: function(html) {
				galleryLoader.setAlbumQuery (eval(html));
			},
			complete:function () {
				galleryLoader.listAlbums();	
			}
		});		
	},
	listAlbums: function() {
		$("#albumList").html("");
		
		var albums = this.albumQuery.feed.entry;
		for (var i=0; i < albums.length; i++) {
			if (albums[i].title.$t.match(this.KEYWORD.needle)) {
				var displayTitle = albums[i].title.$t.replace(this.KEYWORD.needle,"");
				var albumIdentity = albums[i].gphoto$id.$t;
				var albumUser = albums[i].gphoto$user.$t;
				$("#albumList").append('<div><a id="'+albumIdentity+'" class="navlink" href="javascript:void(0)">'+displayTitle+'</a></div>');
				
				$("a#"+albumIdentity).attr("href", 'javascript:galleryLoader.loadPhotos("'+albumIdentity+'", 1);');
				
				
			}
		}
		if (parseInt($("#albumList").height()) > parseInt($("#albumGrid-wrapper").height())) {
			$('#sliderAlbum').show().slider({
				orientation:'vertical',
				min:-100,
				max:-1,
				value:-1,
				slide:function(event,ui) {
					var myScrollHeight = parseInt($("#albumList").height());
					var myDivHeight = parseInt($("#albumGrid-wrapper").height());
					var changeVal = (myScrollHeight-myDivHeight+20)*ui.value/100;
					$("#albumList").css("top",changeVal);
				}
			});
		}
	},
	
	setAlbumQuery:function(i) {
		this.albumQuery = i;	
	},
	
	loadPhotos:function(albumid, nextPhoto) {
		this.albumid = albumid;
		this.nextPhoto = nextPhoto;
		this.currentPhoto = nextPhoto;
		var runAjax = false;
		var url = "http://picasaweb.google.com/data/feed/api/user/"+this.user+"/albumid/"+this.albumid+"?alt=json&kind=photo&thumbsize=72c&imgmax=640&max-results=16&start-index="+this.nextPhoto;	
		if (typeof(this.photoQuery.album[this.albumid]) != "undefined") {
			if (typeof(this.photoQuery.album[this.albumid].page[this.currentPhoto]) != "undefined") {
				this.listPhotos();
				tb_init("a.thickbox");
			} else {
				runAjax = true;
			}
		} else {
			runAjax = true;
			
		}
		if (runAjax) {
			$.ajax({
				type:"GET",
				dataType:"jsonp",
				scriptCharset:"UTF-8",
				url: url,
				error: function(XMLHttpRequest, textStatus, errorThrown){
					$('<p></p>')
					.html("There was an error with your request")
					.appendTo('#errorPane');
				},
				success: function(html) {
					galleryLoader.setPhotoQuery(galleryLoader.albumid,galleryLoader.nextPhoto, eval(html));
					galleryLoader.listPhotos();
					
				},
				complete:function() {
					tb_init("a.thickbox");	
				}
			});
		}
		
			
	},
	
	setPhotoQuery:function(id,page,data) {
		if (!galleryLoader.photoQuery.album[id]){
			galleryLoader.photoQuery.album[id] = {
				page:{}
			};
		}
		galleryLoader.photoQuery.album[id].page[page] = data;
		//this.photoQuery.album[id].page[page] = data;
	},
	
	listPhotos:function() {
		$("#photoGrid").html("");
		var photoData = this.photoQuery.album[this.albumid].page[this.currentPhoto].feed;
		var photos = this.photoQuery.album[this.albumid].page[this.currentPhoto].feed.entry;
		
		this.nextPhoto = parseInt(photoData.openSearch$startIndex.$t)+parseInt(photoData.openSearch$itemsPerPage.$t);
		this.lastPhoto = parseInt(photoData.openSearch$startIndex.$t)-parseInt(photoData.openSearch$itemsPerPage.$t);
		
		for (var i=0; i < photos.length; i++) {
		    var photoTitle = photos[i].title.$t;
		    var photoURL = photos[i].media$group.media$content[0].url;
		    var photoThumb = photos[i].media$group.media$thumbnail[0].url;
		    
		    $("#albumTitle").html(photoData.title.$t.replace(this.KEYWORD.needle,""));
		    $("#photoGrid").append('<a href="'+photoURL+'" class="thickbox" title="'+photoTitle+'"><img border="0" class="picasaThumb" src="'+photoThumb+'" title="'+photoTitle+'" /></a>');
		    
		    /*$("a#"+albumIdentity).data("userID",albumUser).data("albumID",albumIdentity).click(function() {
		    	var photos = new albumLoader($(this).data("userID"), $(this).data("albumID"));										  
		    });*/
		    
		}
		
		
		//Create Next Button if Necessary
		$("#photosContent .controlButtons").html("");
		if ((parseInt(photoData.openSearch$startIndex.$t)+parseInt(photoData.openSearch$itemsPerPage.$t)) < parseInt(photoData.openSearch$totalResults.$t)) {
		    $("#photosContent .controlButtons").append("<div class='moreButton'><a id='picnext' class='navlink' href='javascript:void(0);'>Next</a></div>");
		    	
		    $("#picnext").attr("href", 'javascript:galleryLoader.loadPhotos("'+this.albumid+'",'+this.nextPhoto+')');
		}
		
		//Create Previous Button If Necessary
		if ((parseInt(photoData.openSearch$startIndex.$t)-parseInt(photoData.openSearch$itemsPerPage.$t)) > 0) {
		    $("#photosContent .controlButtons").append("<div class='moreButton'><a id='picprevious' class='navlink' href='javascript:void(0);'>Previous</a></div>");
		    
		    $("#picprevious").attr("href", 'javascript:galleryLoader.loadPhotos("'+this.albumid+'",'+this.lastPhoto+')');
		    	
		}	
		
	}

}
