/*
* Copyright (C) 2009 Joel Sutherland
* Licenced under the MIT license
* http://www.newmediacampaigns.com/page/
*/
(function($) {
	$.fn.nmcflickr = function(settings) {
		settings = $.extend({
			
		}, settings);

		
		$(this).each(function(){
			var countrytag = $(this).attr('id');
			
			$(this).find('.loadgallery').attr('href', 'http://www.flickr.com/photos/44802888@N04/tags/' + countrytag + '/');
		
			$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=44802888@N04&tags=" + countrytag + "&lang=en-us&format=json&jsoncallback=?", function(data){
				$.each(data.items, function(i,item){
					if(i < 6){
						
						var regex = /<p>(.*?)<\/p>/g;
						var input = item.description;
						var description = '';
						if(regex.test(input)) {
							description = input.match(regex)[2];
							/*if(description != '')
								description = description.replace('<p>','').replace('</p>','');*/
						}
					
						$("<img/>").attr("src", item.media.m.replace('_m','_s'))
								   .attr('alt', item.title)
								   .attr('width', '75').attr('height', '75')
								   .appendTo("#" + countrytag + " ul")
								   .wrap('<li></li>')
								   .wrap("<a title='" + description + "' rel='prettyPhoto[" + countrytag + "]' href='" + item.media.m.replace('_m', '') + "'></a>");
								   
								   
						$("a[rel^='prettyPhoto']").prettyPhoto();
					}
				});
			});
		});
	}
})(jQuery);

(function($) {
	$.fn.nmcflickrAll = function(settings) {
		settings = $.extend({
			
		}, settings);

		
		$(this).each(function(){
						
			$(this).find('.loadgallery').attr('href', 'http://www.flickr.com/photos/44802888@N04/');
		
			$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=44802888@N04&lang=en-us&format=json&jsoncallback=?", function(data){

				$.each(data.items, function(i,item){
					if(i < 6){
						
						var regex = /<p>(.*?)<\/p>/g;
						var input = item.description;
						var description = '';
						if(regex.test(input)) {
							description = input.match(regex)[2];
							/*if(description != '')
								description = description.replace('<p>','').replace('</p>','');*/
						}
					
						$("<img/>").attr("src", item.media.m.replace('_m','_s'))
								   .attr('alt', item.title)
								   .attr('width', '75').attr('height', '75')
								   .appendTo('#all ul')
								   .wrap('<li></li>')
								   .wrap("<a title='" + description + "' rel='prettyPhoto[]' href='" + item.media.m.replace('_m', '') + "'></a>");
								   
								   
						$("a[rel^='prettyPhoto']").prettyPhoto();
					}
				});
			});
		});
	}
})(jQuery);

(function($) {
	$.fn.nmcflickrSearch = function(settings) {
		settings = $.extend({
			
		}, settings);
		
		
		$(this).each(function(){
			var countrytag = $(this).attr('id').replace('-',',');
			$(this).find('.loadgallery').attr('href', 'http://www.flickr.com/photos/44802888@N04/');
		
			$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=44802888@N04&tags=" + countrytag + "&tagmode=any&lang=en-us&format=json&jsoncallback=?", function(data){
				$.each(data.items, function(i,item){
					if(i < 2){
						
						var regex = /<p>(.*?)<\/p>/g;
						var input = item.description;
						var description = '';
						if(regex.test(input)) {
							description = input.match(regex)[2];
							/*if(description != '')
								description = description.replace('<p>','').replace('</p>',''); */
						}
					
						$("<img/>").attr("src", item.media.m.replace('_m','_s'))
								   .attr('alt', item.title)
								   .attr('width', '90').attr('height', '90')
								   .appendTo('.searchMedia ul.photos')
								   .wrap('<li></li>')
								   .wrap("<a title='" + description + "' rel='prettyPhoto[]' href='" + item.media.m.replace('_m', '') + "'></a><h3>'"+item.title+"'</h3><p>'" + description + "'</p>");
								   
								   
						$("a[rel^='prettyPhoto']").prettyPhoto();
					}
				});
			});
		});
	}
})(jQuery);

(function($) {
	$.fn.nmcflickrAnniversary = function(settings) {
		settings = $.extend({
			
		}, settings);

		
		$(this).each(function(){
			var countrytag = '30th-anniversary';
			
			$(this).find('.loadgallery').attr('href', 'http://www.flickr.com/photos/44802888@N04/tags/' + countrytag + '/');
		
			$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=44802888@N04&tags=" + countrytag + "&lang=en-us&format=json&jsoncallback=?", function(data){
				$.each(data.items, function(i,item){
			
					if(i < 2){
					
						$("<img/>").attr("src", item.media.m.replace('_m','_s'))
								   .attr('alt', item.title)
								   .attr('width', '75').attr('height', '75')
								   .appendTo("#anniversary ul")
								   .wrap('<li></li>')
								   .wrap("<a rel='prettyPhoto[" + countrytag + "]' href='" + item.media.m.replace('_m', '') + "'></a><h3>'" + item.title +"'</h3>");
								   
								   
						$("a[rel^='prettyPhoto']").prettyPhoto();
					}
				});
			});
		});
	}
})(jQuery);


$(document).ready(function(){
	
	$('.gallery').nmcflickr();
	$('.photos div').nmcflickr();
	$('.photos #all').nmcflickrAll();
	$('.searchMedia .photos').nmcflickrSearch();
	$('#anniversary').nmcflickrAnniversary();
});