var flickrJson;
var flickrCycle = function ( div, seconds, i ) {
	if (	i >= flickrJson.items.length ) {
		i = 0;
	}
	$(div).slice(0,1).html( flickrJson.items[i].description.replace( /&lt;/gi, "<" ).replace( /&gt;/gi, ">" ).replace( /&quot;/gi, "\"" ));
	timeout = window.setTimeout('flickrCycle("' + div + '", ' + seconds + ',' + ( i + 1 ) + ')', seconds * 1000 );
};
$( function () {
	var tag = $('div.tags').length ? $('div.tags').text() : $('meta[name=Keywords]').attr('content');
	// window.console && console.log( tag );
	if (	tag ) {
		var timeout;
		$('div.flickr').empty().slice(1).remove();
		if (	$('div.flickr').length && ! $('div.map').length && ! $('div.gbbs img').not('.google').length ) {
			var url = 'http://api.flickr.com/services/feeds/photos_public.gne?id=77361130@N00&tagmode=any&tags=' + tag + '&lang=en-us&format=json&jsoncallback=?';
			$.getJSON( url, function ( js ) {
				if (	js.items.length ) {
					flickrJson = js; // not sure I can pass it in the timeout otherwise
					if (	! $('div.flickr h2').length ) {
						$('div.flickr').prepend('<h2></h2><div class="content"></div>');
					}
					$('div.flickr h2').slice(0,1).html('Some pictures');
					flickrCycle( 'div.flickr .content', 10, 0 );
				}
				else {	$('div.flickr').remove();
				}
			} );
		}
		else {	$('div.flickr').remove();
		}
	}
} );
