/* Feeds */

var FJS = new ajax_widget();
FJS.initWidget = function()
	{
	this.container=document.getElementById('flickr');
	this.name='slideshow';
	this.content=document.getElementById(this.name);
	this.api_key='625c4e77af956ca9b1d7de89f4896587';
	};
FJS.initConfig = function(values)
		{
		this.config['source_type'] = (values[0]?values[0].toLowerCase():'photoset');
		this.config['source_id'] = (values[1]?values[1].toLowerCase():'72157602145341690');
		this.config['image_size'] = (values[2]?values[2].toLowerCase():'');
		this.config['zoom_size'] = (values[3]?values[3].toLowerCase():'');
		this.config['update_browser'] = (values[4]&&values[4]!='false'?values[4]:false);
		};
FJS.requestData = function()
		{
		var path = "http://api.flickr.com/services/rest/?format=json&jsoncallback=FJS.handleResponse&api_key=" + this.api_key;
		switch (this.config['source_type']) {
			case 'photoset':
				path += "&method=flickr.photosets.getPhotos&photoset_id=" + this.config['source_id'];
				break;
			case 'user':
				path += "&method=flickr.people.getPublicPhotos&user_id=" + this.config['source_id'];
				break;
			case 'contacts':
				path += "&method=flickr.photos.getContactsPublicPhotos&user_id=" + this.config['source_id'];
				break;
			case 'favorites':
				path += "&method=flickr.favorites.getPublicList&user_id=" + this.config['source_id'];
				break;
			case 'group':
				path += "&method=flickr.groups.pools.getPhotos&group_id=" + this.config['source_id'];
				break;
			case 'interesting':
				path += "&method=flickr.interestingness.getList";
				break;
			case 'tag':
				path += "&method=flickr.photos.search&tags=" + this.config['source_id'];
				break;
		}
		this.requestJSonData(path);
		};

FJS.handleResponse = function(rsp)
		{
		if (rsp.stat.toLowerCase() == "fail")
			{
			this.error(rsp.message);
			}
		else
			{
			switch (this.config['source_type'])
				{
				case 'photoset':
					if(rsp.photoset.total>0)
						{
						this.set_owner = rsp.photoset.owner;
						this.data = rsp.photoset.photo;
						break;
						}
					else
						{
						this.notice("Pas de photo pour le moment !");
						}
				default:
					if(rsp.photos.total>0)
						{
						this.data = rsp.photos.photo;
						}
					else
						{
						this.notice("Pas de photo pour le moment !");
						}
					break;
				}
			if(this.data)
				{
				this.content.innerHTML='<p><span id="'+this.name+'_nav"><a href="#" id="'+this.name+'_prev">&lt;</a> <span id="'+this.name+'_count">0/0</span> <a href="#" id="'+this.name+'_next">&gt;</a></span><br /><a href="#" onclick="window.open(this.href); return false;" id="'+this.name+'_link"><strong id="'+this.name+'_title"></a></strong><br /><a href="#" id="'+this.name+'_zoom" onclick="image_popup(this.href); return false;"><img src="" id="'+this.name+'_img" /></a></p>';
				this.setFirstItem();
				}
			}
		};

FJS.loadCurrentItem = function()
		{
		// title
		document.getElementById(this.name+'_title').innerHTML=this.data[this.current].title;
		// navigation
		document.getElementById(this.name+'_prev').setAttribute('href','javascript:FJS.goPreviousItem();');
		document.getElementById(this.name+'_next').setAttribute('href','javascript:FJS.goNextItem();');
		document.getElementById(this.name+'_count').innerHTML=this.getCurrentCount();
		// zoom
		document.getElementById(this.name+'_zoom').setAttribute('href',this.getZoomedPhotoUrl(this.current));
		// flickr link
		document.getElementById(this.name+'_link').setAttribute('href',this.getFlickrUrl(this.current));
		// image
		document.getElementById(this.name+'_img').setAttribute('src',this.getPhotoUrl(this.current));
		};

FJS.getPhotoUrl = function(i) {
		var photo = this.data[i];
		var server = photo.server;
		var id = photo.id;
		var secret = photo.secret;
		var s = (this.config['image_size'] == "") ? "" : "_" + this.config['image_size'];
		var url = "http://static.flickr.com/" + server + "/" + id + "_" + secret + s + ".jpg";
		return url;
	};

FJS.getZoomedPhotoUrl = function(i) {
		var photo = this.data[i];
		var server = photo.server;
		var id = photo.id;
		var secret = photo.secret;
		var s = (this.config['zoom_size'] == "") ? "" : "_" + this.config['zoom_size'];
		var url = "http://static.flickr.com/" + server + "/" + id + "_" + secret + s + ".jpg";
		return url;
	};
	
FJS.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;
	};

function image_popup(url,title)
	{
	var image = new Image;
	image.src = url;
	image.setAttribute('title', title);
	image.onload=function()
		{ //this.title au lieu de ''
		var popup = window.open(this.src, '' , 'height='+this.height+', width='+this.width+', top='+(window.screen.height/2-image.height/2)+', left='+(window.screen.width/2-image.width/2)+', toolbar=no, menubar=no, location=no, resizable=yes, status=no'); //, scrollbars=yes
		popup.onload=function()
			{
			popup.resizeTo(this.width+((popup.innerWidth?popup.innerWidth:popup.document.body.clientWidth)-this.width),this.height+((popup.innerHeight?popup.innerHeight:popup.document.body.clientHeight)-this.height));
			popup.focus();
			};
		};
	}

if(window.addEventListener)
	{
	window.addEventListener('load', function() { FJS.init(); }, false);
	}
else if(window.attachEvent)
	{
	window.attachEvent('onload', function() { FJS.init(); }, false);
	}