/* The HWGFlickrFeed lib By Gavin Brogan 
Version 0.1.2
Please e-mail comments to me at heavyweightgeek@gmail.com
This library is freely distributable under the terms of an MIT-style license.
with no warranty to speak of.

basic usage adds 20 newly uploaded images to element with id = "images"
<script>
	feed = new HWGFlickrFeed;
	feed.getfeed(); 
</script>

or simiilar but a bit more involved

<div id="photos"></div>
<div id="design"></div>

<script src="HWGflickrfeed.js"></script>
<script>
	feed = new HWGFlickrFeed;
	feed.imageSize = "_s";
	feed.maxToAttach = 4;
	feed.getfeed(); 
	feed.tags = "design"
	feed.nodeId = "design";
	feed.getfeed(); 
</script>
*/

Object.extend = function(destination, source){
	 for (var property in source){
		 destination[property] = source[property];
	 }
		 return destination;
};

function HWGFlickrFeed() {}
HWGFlickrFeed.prototype = {
	/*user defined stuff*/
	feedaddress: "http://api.flickr.com/services/feeds/photos_public.gne",
	id: undefined,
	ids: undefined, 
	tags: undefined,  			//comma seperated
	tagmode: "all", 			// all, any
	nodeId: "photos",
	imageSize: "_m", 			// _s, _t, _m, -, _b, _o 
	maxToAttach: 20,
	flickrfeedresponse: {},
	success: function(ff) {		//user can define this but will default to adding images to div with id="photos"
		ff.attach();
	},
	failure: function(ff) {
		alert("no photos at the moment for that tag");
	},
	/*end of stuff users will typically want to overwrite*/
	callback: function(ff) {
		if (ff.flickrfeedresponse.items.length !== 0){
			this.success(ff);
		}else{
			this.failure(ff);
		}	
	},
	getfeed: function(){
		var feedobjectcopy = {};
		Object.extend(feedobjectcopy,this);
		FlickrFeedManager.feeds.push(feedobjectcopy);
		if (FlickrFeedManager.feeds.length === 1){
			FlickrFeedManager.feeds[0].create();
		}
	},
	create: function(){
		var frs = this.feedaddress +'?format=json';
		if ( this.id !== undefined){frs = frs + "&id=" + this.id;}
		if ( this.ids !== undefined){frs = frs + "&ids=" + this.ids;}
		if ( this.tags !== undefined){frs = frs + "&tags=" + this.tags;}
		if ( this.tagmode !== "all"){frs = frs + "&tagmode=any";}
		frs = encodeURI(frs);
		if (document.createElement && document.body.appendChild){
			var newScript = document.createElement("script");
			newScript.src = frs;
			document.body.appendChild(newScript);
		}
	},
	attach: function(nodeid){
		if(nodeid !== undefined){
			this.nodeId = nodeid;
		}
		if (document.getElementById(this.nodeId)){
			var nodetoappendto = document.getElementById(this.nodeId);
			var sourceadd = "";
			var j = this.maxToAttach;
			if (this.flickrfeedresponse.items.length < this.maxToAttach ){
				j = this.flickrfeedresponse.items.length;
			}
			var linktoflickr;
			var imageelem;
			for (var i = 0; i < j ; i++){
				if 	(document.createElement && document.appendChild){
					linktoflickr = document.createElement('a');
					imageelem = document.createElement('img');
					linktoflickr.href = this.flickrfeedresponse.items[i].link;
					imageelem.title = this.flickrfeedresponse.items[i].title;
					sourceadd = this.flickrfeedresponse.items[i].media.m;
					imageelem.src = sourceadd.replace(/_m/,this.imageSize);
					linktoflickr.appendChild(imageelem);
					nodetoappendto.appendChild(linktoflickr);
				}
			}
		}
	}
};

FlickrFeedManager = {
	feeds: []
};

function jsonFlickrFeed(jff){
	var ffmf = FlickrFeedManager.feeds;
	ffmf[0].flickrfeedresponse = jff;
	ffmf[0].callback(ffmf[0]);
	ffmf.reverse();				//feeds in manager should be a last on last off 
	ffmf.pop();
	ffmf.reverse();
	if (ffmf.length !== 0){
		ffmf[0].create();
	}
}

