function getPhotos()
{
	new fetchFlickrPix({
		method: 'flickr.photos.search',
		user_id: '29024202@N04',
		jsoncallback: 'initGallery'
		});
}

function initGallery(res)
{
	new geckoGalleryLite(res);
}


function fetchFlickrPix(args) {
	// Set the properties
	this.apiKey = "acc0692942a8b7d07dfb18f6de35ea3e";
	this.baseUrl = "http://www.flickr.com/services/rest/?";
	this.format = "json";
	this.args = args;

	// Create the script tag
    this.scriptTag = document.createElement("script");
    this.scriptTag.setAttribute("type", "text/javascript");
    this.scriptTag.setAttribute("src", this.buildRestUrl());
	
	// Append to head
	document.getElementsByTagName("head").item(0).appendChild(this.scriptTag);
}

fetchFlickrPix.prototype.buildRestUrl = function()
{
	// Add the extra args 
	this.args.api_key = this.apiKey;
	this.args.format = this.format;
	
	var queryString = [];
	for (var property in this.args) queryString.push(encodeURIComponent(property) + '=' + encodeURIComponent(this.args[property]));
	return this.baseUrl + queryString.join('&');
}

function geckoGalleryLite(res)
{
	this.photoList = res.photos.photo;
	this.galleryContainer = document.getElementById("galleryContainer");
	this.buildThumbnails();
};

geckoGalleryLite.prototype.buildThumbnails = function()
{
	for(var i in this.photoList)
	{
		var thumbContainer = document.createElement('div');
		thumbContainer.className = "thumbcontainerlite";
		this.galleryContainer.appendChild(thumbContainer);
		
		var thumb = document.createElement('img');
		thumb.src = this.buildFlickrImgUrl(this.photoList[i],this.imgSize.square);
		thumb.setAttribute("title", this.photoList[i].title);
		thumb.setAttribute("alt",  this.photoList[i].title);
		//thumb.setAttribute("rel", "lightbox");
		thumb.className = "thumbnail";
		
		var hlink =  document.createElement("a");
		hlink.setAttribute("rel", "lightbox");
		hlink.setAttribute("href",  this.buildFlickrImgUrl(this.photoList[i],this.imgSize.medium));
		hlink.setAttribute("title", this.photoList[i].title);
		hlink.appendChild(thumb);
		
		thumbContainer.appendChild(hlink);
	}
	initLightbox();
}

geckoGalleryLite.prototype.imgSize = {
	thumb: "_t",
	square: "_s",
	small: "_m",
	medium: "",
	large: "_b"
}

geckoGalleryLite.prototype.buildFlickrImgUrl = function (photoData, imgSize)
{
	return 'http://static.flickr.com/'+photoData.server+'/'+photoData.id+'_'+photoData.secret+imgSize+'.jpg';
};