// a flickr-powered javascript gallery
// v 0.5 - 11/27/2006 by Lucas J. Shuman
// more info: http://fsviewr.lackadaisical.com/
// Customised by El Mysterioso for Ladybird Vintage 2007


// change these numbers for different photos
// NB 0 is the latest image, so make sure you count from 0 each time 

 myphotosGallery = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8");
 
 myphotosHome = new Array("0", "1", "2", "3", "4", "5");
 
// -------  DON'T EDIT BEYOND THIS! --------

var item_num = 1;
var FJS = {
	
	// ------ PROPS --------
	
	api_key: '625c4e77af956ca9b1d7de89f4896587',
	data: null,
	current: null,
	source_type: null,
	source_id: null,
	set_owner: null,
	image_size: "",
	update_browser: false,
	home_images: false,
	
	// ------- SETUP -------- 
	
	init: function(source_type, source_id, image_size, update_browser, home_images) {
		
		this.source_type = source_type.toLowerCase();
		this.source_id = source_id;
		this.update_browser = update_browser;
		this.home_images = home_images;
		
		if (image_size != "" && image_size != null)
			this.image_size = image_size.toLowerCase();
		
		this.requestPhotoData();
		
	},
	
	// ------- JSON REQUEST -------- 
	
	requestPhotoData: function() {
		
		var flickr_api_path = "http://api.flickr.com/services/rest/?format=json&jsoncallback=FJS.handleResponse&api_key=" + this.api_key;
		
		switch (this.source_type) {
			case 'photoset':
				flickr_api_path += "&method=flickr.photosets.getPhotos&photoset_id=" + this.source_id;
				break;
			case 'user':
				flickr_api_path += "&method=flickr.people.getPublicPhotos&user_id=" + this.source_id;
				break;
			case 'contacts':
				flickr_api_path += "&method=flickr.photos.getContactsPublicPhotos&user_id=" + this.source_id;
				break;
			case 'favorites':
				flickr_api_path += "&method=flickr.favorites.getPublicList&user_id=" + this.source_id;
				break;
			case 'group':
				flickr_api_path += "&method=flickr.groups.pools.getPhotos&group_id=" + this.source_id;
				break;
			case 'interesting':
				flickr_api_path += "&method=flickr.interestingness.getList";
				break;
			case 'tag':
				flickr_api_path += "&method=flickr.photos.search&tags=" + this.source_id;
				break;
		}
		
		document.write('<script type="text/javascript" src="' + flickr_api_path + '"></script>');
		
	},
	
// ----- JSON CALLBACK ----- 
	
	handleResponse: function(rsp) {
		
		if (rsp.stat.toLowerCase() == "fail") {
			this.initErrorDisplay(rsp.message);
			return;
		}
		
		switch (this.source_type) {
			case 'photoset':
				this.set_owner = rsp.photoset.owner;
				this.data = rsp.photoset.photo;
				break;
			default:
				this.data = rsp.photos.photo;
				break;
		}
		
		this.initDisplay();
	},
	
// ------- SETUP ------- 

	initDisplay: function() {
		//this.current = 0;
		
		if(this.home_images == true){
		myphotos = myphotosHome;
		} else {
		    myphotos = myphotosGallery
		}
		
		
		for (var x = 0; x < myphotos.length; x++)
		 {
		
		this.writeDiv("fjs-flickrphoto");
		this.closeDiv();
		
		this.current = myphotos[x];
		this.loadCurrentImage();
		
		item_num ++
		
		   }
	},
	
	initErrorDisplay: function(err) {
		
		this.openDiv("fjs-flickr");
		this.writeDiv("fjs-flickrerror");
		this.closeDiv();
		this.setError(err);
	
	},
	
	// ------ DIVS -------- 
	
	openDiv: function(div_id) {
		document.write("<li id=\"" + div_id + item_num + "\">");
	},
	
	closeDiv: function(div_id) {
		document.write("</li>");
	},
	
	writeDiv: function(div_id) {
		this.openDiv(div_id);
		this.closeDiv();
	},
	
	updateDiv: function(div_id, html) {
		document.getElementById(div_id).innerHTML = html;
	},
	
	// -------- IMGS -------- 
	
	loadCurrentImage: function() {
		this.setCurrentPhoto();
		//if (this.update_browser) this.updateBrowserLocation();
	},
	
	// -------- NAVIGATION ----- 
	
	goNextPhoto: function() {
		this.current++;
		if (this.current == this.data.length) this.current = 0;
		this.loadCurrentImage();
	},
	
	goPreviousPhoto: function() {
		this.current--;
		if (this.current < 0) this.current = this.data.length - 1;
		this.loadCurrentImage();
	},
	
	// ------- DISPLAY ------- 
	
	setCurrentPhoto: function() {
    
        if(this.home_images){
		var html = "<div><a href=\"Photos.aspx\" title=\"View our Gallery\">";
		html += "<img src=\"" + this.getPhotoUrl(this.current) + "\" alt=\"" + this.getPhotoTitle(this.current) + "\" title=\"" + this.getPhotoTitle(this.current) + "\" />";
		html += "</div></a>";
		} else {
		var html = "<div><a href=\"" + this.getFlickrUrl(this.current) + "\" title=\"View at Flickr\" target=\"_blank\">";
		html += "<img src=\"" + this.getPhotoUrl(this.current) + "\" alt=\"" + this.getPhotoTitle(this.current) + "\" title=\"" + this.getPhotoTitle(this.current) + "\" /></a>";
		html +="<br />";
		html += "<a href=\"" + this.getFlickrUrl(this.current) + "\" title=\"View at Flickr\" target=\"_blank\" class=\"textlink\">" + this.getPhotoTitle(this.current) + "</a>";
		html += "</div>";
		}
		this.updateDiv("fjs-flickrphoto" + item_num, html);
	},
	
	setError: function(err) {
		var html = "<span class=\"err-label\">Flickr Error:</span> <span class=\"err-message\">" + err + "</span>";
		this.updateDiv("fjs-flickrerror", html);
	},
	
	// -------- DATA ------ 
	
	getPhotoIndexById: function(id) {
		var i = this.data.length;
		while (i--) {
			if (this.data[i].id == id) return i;
		}
		return -1;
	},
	
	getPhotoId: function(i) {
		return this.data[i].id;
	},
	
	getPhotoTitle: function(i) {
		return this.data[i].title;
	},
	
	getPhotoUrl: function(i) {
		var photo = this.data[i];
		var server = photo.server;
		var id = photo.id;
		var secret = photo.secret;
		var s = (this.image_size == "") ? "" : "_" + this.image_size;
		var url = "http://static.flickr.com/" + server + "/" + id + "_" + secret + s + ".jpg";
		return url;
	},
	
	getFlickrUrl: function(i) {
		var photo = this.data[i];
		var owner = this.set_owner == null ? photo.owner : this.set_owner;
		var id = photo.id;
		var url = "http://www.flickr.com/photos/" + owner + "/" + id;
		return url;
	},
	
	getCurrentCount: function() {
		return (this.current + 1) + " of " + this.data.length;
		
	},
	
	
	// ---- BROWSER ----
	
	updateBrowserLocation: function() {
		//location.hash = "#" + this.getPhotoId(this.current);
		//document.title = this.getPhotoTitle(this.current);
	}
	
};
