/*
a flickr-powered javascript gallery
v 0.5 - 11/27/2006 by Lucas J. Shuman
more info: http://fsviewr.lackadaisical.com/
*/

var FJS = {
	
	/* ------ PROPS -------- */
	
	api_key: "782fde498c7158a90fae4f14bf7b8a5a",
	data: null,
	current: null,
	source_type: null,
	source_id: null,
	set_owner: null,
	image_size: "",
	update_browser: false,
	tag: "",
	pics:6,
	navClass:"nav_flickr_back",
	navClass2:"nav_flickr_next",		
	/* ------- SETUP -------- */
	ls:null,nav_current:1,	
	init: function(source_type, source_id, image_size, update_browser,tag) {
		
		this.source_type = source_type.toLowerCase();
		this.source_id = source_id;
		this.update_browser = update_browser;
		this.tag = tag;
		
		if (image_size != "" && image_size != null)
			this.image_size = image_size.toLowerCase();
		//alert(this.tag);
		this.requestPhotoData();
		
	},
	
	/*	init: function(source_type, source_id, image_size, update_browser) {	
		this.source_type = source_type.toLowerCase();
		this.source_id = source_id;
		this.update_browser = update_browser;
		
		if (image_size != "" && image_size != null)
			this.image_size = image_size.toLowerCase();
		this.requestPhotoData();
	},*/
	
	/* ------- JSON REQUEST -------- */
	
	requestPhotoData: function() {
	//http://api.flickr.com/services/rest/?format=json&jsoncallback=FJS.handleResponse&api_key=782fde498c7158a90fae4f14bf7b8a5a&method=flickr.photos.search&user_id=16075235@N00&tags=lonavla;
	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;
			case 'tag_and_user':
				flickr_api_path += "&method=flickr.photos.search&user_id=" + this.source_id + "&tags=" + this.tag;
				break;

	
		}
	//alert(flickr_api_path);	
		document.write("<script type=\"text/javascript\" src=\"" + flickr_api_path + "\"></script>" + "\n");
		
	},
	
	/* ----- 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.openDiv("fjs-flickr");
		this.writeDiv("fjs-flickrphoto");
		this.writeDiv("fjs-flickrphototitle");
		this.writeDiv("fjs-flickrphotonav"); 

		this.writeDiv("fjs-flickrlink");
		this.closeDiv();

		this.writeDiv("fjs-flickrnav_strip");

		this.current = 0;
		
		if (location.hash != "") {
			var i = this.getPhotoIndexById(location.hash.substr(1));
			if (i > -1) this.current = i;
		}
		this.setNavStrip();		
		this.loadCurrentImage();
		
	},
	
	initErrorDisplay: function(err) {
		
		this.openDiv("fjs-flickr");
		this.writeDiv("fjs-flickrerror");
		this.closeDiv();
		this.setError(err);
	
	},
	
	/* ------ DIVS -------- */
	
	openDiv: function(div_id) {
		document.write("\n" + "<div id=\"" + div_id + "\">");
	},
	
	closeDiv: function(div_id) {
		document.write("</div>");
	},
	
	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.setCurrentTitle();
		this.setCurrentNav();
		this.setCurrentLink();	
		this.setCurrentPhoto();
		if (this.update_browser) this.updateBrowserLocation();
	},

	setNavStrip: function() {
	var output='',temp='';
	var image_count = this.data.length;
	var photourl;

	for(i=0 ; i < image_count; i++) {
	  photourl = this.getNavStripPhoto(i);
	  output+= "<li style=\"display:none;\"><a href=\""+ photourl + "\" title=\"" + this.data[i].title + "\" onclick=\"FJS.show("+ i + "); $('#toggleable div , #toggleableMain div').hide();	$('#fjs-flickr, #fjs-flickr div, #fjs-flickrnav_strip').show(); return false\"><img src=\"" + photourl +"\" alt=\""+this.data[i].title+"\" /></a></li>" + "\n";

	  //alert(photourl);
	}

	var prevlink="<li class='"+ this.navClass+"'><a href='#' onclick='FJS.navigate(-1);return false'>" 
	              + "<img src=\"/img/find_prev.gif\" />" + "</a></li>" + "\n";
	output=prevlink + output; //create previous link and appened

	var nextlink="<li class=\""+this.navClass2+"\"><a href=\"#\" onclick=\"FJS.navigate(1);return false\">" 
	              + "<img src=\"/img/find_next.gif\" />" + "</a></li>" + "\n";
	output+=nextlink;  //create next link and appened
	output = "<ul id=\"nav_strip\">" + output;
	output+="</ul>"

	this.updateDiv("fjs-flickrnav_strip",output);
	//alert(output);
	this.navigate(0);
  },

	show:function(image_no) {
	this.current = image_no;	
	this.loadCurrentImage();
  },

	navigate:function(offset) {
	//alert("navigate");
	//1. Get element 
	var image_count = this.data.length;
	var doc = document.getElementById("fjs-flickrnav_strip");
	for(var i=1;i<=image_count;i++)
	{
		doc.getElementsByTagName('li')[i].style.display='none';
	}

	var change=offset;
	//alert("nav_current : " + this.nav_current)
	this.nav_current+=change;
	//alert("change : " + change)
	//alert("no of pics : " + this.pics)
	for(var j=this.nav_current;j<this.nav_current+this.pics;j++){
	  //alert(j)
	  if(doc.getElementsByTagName('li')[j]){
		doc.getElementsByTagName('li')[j].style.display='inline';
	  }
	}
	var fl=doc.getElementsByTagName('a')[0];
	fl.style.display=this.nav_current==1?'none':'inline';
	var ll=doc.getElementsByTagName('a')[image_count+1];
	ll.style.display=this.nav_current > image_count-this.pics?'none':'inline';
  },


	getNavStripPhoto: function(i) { 
	var photo = this.data[i];
	var server = photo.server;
	var id = photo.id;
	var secret = photo.secret;
	var photo_url = "http://static.flickr.com/" + server + "/" + id + "_" + secret + "_s" + ".jpg";
	return photo_url;
  },
	
	/* -------- 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 ------- */
	
	setCurrentNav: function() {
		var html_prev = "<a href=\"#\" title=\"Prev\" onclick=\"FJS.goPreviousPhoto(); return false;\">&larr;</a>";
		var html_next = "<a href=\"#\" title=\"Next\" onclick=\"FJS.goNextPhoto(); return false;\">&rarr;</a>";
		this.updateDiv("fjs-flickrphotonav", html_prev + " " + this.getCurrentCount() + " " + html_next);
	},
	
	setCurrentTitle: function() {
		this.updateDiv("fjs-flickrphototitle", this.getPhotoTitle(this.current));
	},
	
	setCurrentPhoto: function() {
	var html = "";
	html += "<script type=\"text/javascript\" src=\"" + this.getPhotoDescriptionUrl(this.current) + "\"></script>";
	html += "<a href=\"#\" title=\"Next\" onclick=\"FJS.goNextPhoto(); return false;\">";
	html += "<img src=\"" + this.getPhotoUrl(this.current) + "\" alt=\"\" />";
	html += "</a>" 	+ "\n";;
	this.updateDiv("fjs-flickrphoto", html);
	},
	
	setCurrentLink: function() {
		var html = "<a href=\"" + this.getFlickrUrl(this.current) + "\" title=\"View at Flickr\" target=\"_blank\">View on Flickr</a>";
		this.updateDiv("fjs-flickrlink", 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;
     },
	
	getPhotoDescription: function(i) {
		var description;
    },

	getPhotoDescriptionUrl: function(i) {
	var flickr_api_path = "http://api.flickr.com/services/rest/?format=json&jsoncallback=FJS.handlePhotoResponse";
	var photo = this.data[i];
	var id = photo.id;
	flickr_api_path+=('&api_key=' + this.api_key);
	flickr_api_path+=('&method=flickr.photos.getInfo&photo_id=' + id);
	return flickr_api_path;
  },

	handlePhotoResponse: function(rsp){
	alert('hi');
	alert(rsp);
  },
	

	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) + "/" + this.data.length;
	},
	
	/* ---- BROWSER ---- */
	
	updateBrowserLocation: function() {
		location.hash = "#" + this.getPhotoId(this.current);
		document.title = this.getPhotoTitle(this.current);
	}
	
};



