// FlickrFont v01
// http://www.greg-hill.id.au
// This work is licensed under a Creative Commons Attribution-ShareAlike 2.1 Australia License.

// Converts text in elements with class "FlickrFont" to images drawn from http://www.flickr.com

// http://ghill.customer.netspace.net.au/flickrfont

FlickrFont = new Array();

Q = 0;								// outstanding requests

function FlickrFontClass()
{

Q = 0;								// reset Q

// loop through all elements looking for those with class="FlickrFont"

var els=document.getElementsByTagName('*');  		// all elements
FFels = new Array(); 						// FlickrFont elements
string = new Array();
outHTML = new Array();

for(var i=0; i<els.length; i++) 
	if (els[i].className.search('FlickrFont')==0)
		FFels.push(els[i]);

for(var i=0; i<FFels.length; i++)
{
	string[i] = FFels[i].innerHTML;

	// load place holder HTML

	FFels[i].innerHTML = '<img 	src="http://ghill.customer.netspace.net.au/snake_transparent.gif" /><br/><b>FlickrFont:</b><br/> Images loading ...';
}	

buildFont();

if(Q==0) outputClass();					// if done loading, write results

return;
}

function buildFont()
{

// gather all the strings together and, if required, fetch images for each character

var index;
allChars = string.join().toUpperCase().replace(/drops/g,'');

for(var i=0; i<allChars.length; i++)
{
	currChar = allChars.substr(i,1);
	index = allChars.indexOf(currChar);
	if (index>-1 && index<i)			// if we've already dealt with this, skip
		continue;

	if (typeof(FlickrFont[currChar])=='undefined')
		fetchImages(currChar);
}
return;
}
	
function outputClass()
{

// writes out resulting HTML into appropriate elements

for(var i=0; i<FFels.length; i++)
{
	outHTML[i] = Flickrfy(string[i]);

	FFels[i].innerHTML = outHTML[i];
	FFels[i].className = "Flickred";			// mark elements as done

	if (FFels[i].id.search('FF_demo')==0)		// only used for demo page
	{
		document.getElementById('static').value = outHTML[i];	// put HTML into static demo textarea
		FFels[i].className = "FlickrFont";	// mark element as FlickrFont for next cycle
	}
}

return;
}

function Flickrfy( str )
{

// takes a string and returns the HTML for image tile version
// assumes the font is already built

var re = /http:\/\/static[^&]+/i;				// regular expression for capturing url

var resize = /_m\./ig;

var writeHTML = "";						// HTML output to be returned

if (!str) return '';

// loop through all char in string

for (var i=0; i<str.length; i++)
{
	currChar = str.substr(i,1).toUpperCase();

	if (currChar.search(drops)>-1)				// skip unknown punctuation
		continue;

	if (currChar.search(blanks)>-1)				// deal with spaces
	{
		writeHTML+='<img src="http://www.flickr.com/images/spaceball.gif" width="'+imgX+'" height="'+imgY+'" alt="'+currChar+'" title ="'+currChar+'" style="'+imgStyle+'"/>';

		continue;
	}

	if (!FlickrFont[currChar])
		continue;						// no images available

	var count = FlickrFont[currChar].length;
	if (count==0)
		continue;						// no images available

	var num=count;						// select random image
	while(num>=count)
		num = Math.floor(Math.random()*(count));

	var FFimg = FlickrFont[currChar][num];

	var link = FFimg.link;					// build image tag
	var descr = FFimg.description;

	var url=re.exec(descr)[0].replace(resize, '_s.'); // resize to small images

	var imgTag = '<a href="'+link+'"><img src="'+url+'" width="'+imgX+'" height="'+imgY+'" alt="'+currChar+'" title ="'+currChar+'" style="'+imgStyle+'"/></a>';

	writeHTML += imgTag;
}

return writeHTML;
}

function fetchImages( currChar )
{

// build image request and append to head

var tags;

if (currChar.search(/[a-z]/i)>-1)						// test for letter
	tags = tagChar+','+currChar+currChar; 		
else
	if (currChar.search(/[0-9]/)>-1)					// test for digit
		tags = tagDigit+','+currChar;
	else
		if (typeof(marks[currChar])!='undefined')		// test for punctuation
			tags = tagPunct+','+marks[currChar];			
		else
			return;

var req_url = 'http://www.flickr.com/services/feeds/photos_public.gne?tags='+tags+'&format=json';

// build JSON request script

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = req_url;
document.getElementsByTagName("head")[0].appendChild(script);

Q++;										// queue count increment

return;
}

function jsonFlickrFeed( obj )
{
// callback - determines character and then adds it to font object

var title = obj.title;
var re = / ([a-z0-9]+) \-/i;
var tag;
currChar = "sentinel";

tag = re.exec(title)[1].toUpperCase();

Q--;										// queue count decrement

if (tag.length==0)
{
	if (Q==0) outputClass();
	return;								// error in feed
}

if (tag.length<3)
	currChar=tag.charAt(0);
else
	for(var i in marks)
		if (tag.toLowerCase().search(marks[i])>=0)
			currChar = i;

if (currChar=="sentinel")
{
	if (Q==0) outputClass();
	return;								// couldn't match
}

FlickrFont[currChar] = obj.items;

if (Q==0) outputClass();						// all done - process results

return;
}

function FlickrFontString( str, varstr )
{
// takes string, builds required font and puts HTML in specified variable

if (!varstr)
	varstr = "FFstr";

string[0] = str;

buildFont();

var script = document.createElement('script');
script.type = 'text/javascript';
script.text = varstr+'= Flickrfy("'+str+'");';
document.getElementsByTagName("head")[0].appendChild(script);

return;
}


