// JavaScript Document

(function($) {
	var $$,tID;	
	
	$$ = $.fn.fsGallery = function(options) {
		var $options = {
			loadingText:'loading...',
			delay: 300,
			fade: 200,
			raster: true,
			onLoad : function(){}
		};
		$$.options = $.extend($options,options);
		$('#showCaption').find('#hideImg').hide();
		if($('body').find('#fsImage').length < 1) {
			$('body').prepend('<div id="fsImage"><div><table><tr><td><img src="" class="init" /></td></tr></table></div></div>');
		}
		return this.each(function() {
			$(this).addClass('fsGallery');	
			$$.preload($(this).find('li:first'));
		});
	
	};
	
	$$.next = function(element) {
		if (!element || element.length < 1 || element.siblings().length < 1) {
			return false;
		} else {
			$('#next').unbind();
			$('#prev').unbind();
			clearTimeout(tID);
			$$.show(element.next().length > 0 ? element.next() : element.siblings(':first'));
		}
	};
	
	$$.prev = function(element) {
		if (!element || element.length < 1 || element.siblings().length < 1) {
			return false;
		} else {
			$('#next').unbind();
			$('#prev').unbind();
			clearTimeout(tID);
			$$.show(element.prev().length > 0 ? element.prev() : element.siblings(':last'));
		}
	};
	
	$$.preload = function(element, keepPreloading) {
		if (keepPreloading === null) {
			keepPreloading=true;
		}
		var anchor  = element.find('a');
		var image   = $(new Image());
		if (anchor.text().length > 0) {
			anchor.text(anchor.text().replace(/ /,'\xa0'));
		}
		element.addClass('loading');
		image.load(function() {
			element.removeClass('loading');
			if(anchor.find('img').length > 0) {
				element.css({backgroundImage:'url('+anchor.find('img').attr('src')+')',backgroundRepeat:'no-repeat',backgroundPosition:'50% 50%'});
				$('#fsTitleDiv').show();
				$('#loadImg').hide();
			}
			element.addClass('loaded').click(
				function() {
					$$.show($(this));
					return false;
				}
			).hover(
				function() {
					$(this).addClass('hover');
					$('.fsGallery li a').css('display','none');
					anchor.css({
						marginLeft: ((anchor.width()/2))*-1 +'px',
						marginTop: (anchor.height() + 20)*-1+'px'
					});
					clearTimeout(tID);
					tID = setTimeout(function() {
						if ($$.options.fade > 1) {
							anchor.css({marginLeft: ((anchor.width()/2)+10)*-1 +'px'});
							anchor.fadeIn($$.options.fade,function() {
								$(this).css({display:'block'});
							});
						} else {
							if ($.browser.msie) {
								anchor.css({marginLeft: ((anchor.width()/2)+10)*-1 +'px'});
							}
							anchor.css('display','block');
						}
					},$$.options.delay);
				},
				function() {
					$(this).removeClass('hover');
					clearTimeout(tID);
					anchor.css('display','none');
				}
			);
			if ($('#fsImage td img:not(.init)').length < 1) { $$.show(element); }
			if (element.next().length > 0) { $$.preload(element.next()); }
			$(this).remove();
			
		}).attr('src',anchor.attr('href'));
	};
	
	$$.c = function(e) {
		return $(document.createElement(e));
	};
	
	$$.resize = function(image) {
		var ratio = Math.max($(window).width()/image.width(),$(window).height()/image.height());
		if ($(window).width() > $(window).height()) {
			image.css({width:image.width()*ratio,height:'auto'});
		} else {
			image.css({width:'auto',height:image.height()*ratio});
		}
	};
	
	$$.show = function(element) {
		element.siblings('.active').removeClass('active');
		element.addClass('active');
		var image = $(new Image()).attr({
			src: element.find('a').attr('href'),
			alt: element.find('a').attr('title'),
			id:  element.find('a img').attr('id')
		});
		
		$('#next').click(
			function() { $$.next(element); }
			);
		$('#prev').click(
			function() { $$.prev(element); }
			);
		var raster = $$.options.raster ? $$.c('span').addClass('raster') : false;
		$('#fsImage td').empty().append(image.css('display','block')).prepend(raster);
		if(($.browser.msie && $.browser.version < 7) || $.browser.safari) {
			$(window).resize(function(){ $$.resize($('#fsImage td img:not(.init)')); });
			$(function($){$$.resize(image);});
		}
		$$.options.onLoad();
	};
	
	$.fn.hoverClass = function() {
		return this.hover(
			function() { $(this).addClass('hover'); },
			function() { $(this).removeClass('hover'); }
		);
	};
	
	})(jQuery);
	
	jQuery(function($) {
		
		var search = null;
		var key = '9d98c0b6bb1490ecf4bba5a103812dbe';
		var queryString = window.location.search.replace(/\?q=([^\&]+)/,'$1');
		if(queryString.length > 2) {
			search = queryString;
		}
		$('#imageList').hide();
	
		$('.fsSearch').find('input[type=text]').focus(function() {
			if($(this).attr('value') == 'search') {
				$(this).attr('value','');
			}
		}).blur(function() {
			if($(this).attr('value') == '') {
				$(this).attr('value','search');
			}
		});
		
				
		$("#imageList").flickr({     
			api_key: key,  
			type: 'search',
			user_id: '51415477@N08',
			sort: 'date-taken-desc',
			size: 'o',
			per_page: 100,
			loadFirst: function() {
				$('#imageList ul').fsGallery({
					delay:0,
					fade:1,
					onLoad : function() {
						var c = $('#fsCaption').css('visibility','hidden');
						var t = $('#fsImage td img:not(.init)').attr('alt');
						$('#fsTitle').text(t);
						$.getJSON('http://api.flickr.com/services/rest/?format=json&jsoncallback=?&api_key='
						+ key
						+ '&method=flickr.photos.getInfo'
						+ '&photo_id='+$('#fsImage td img:not(.init)').attr('id'), function(info){
							//var content = info.photo.description._content.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi,"");
							var content = info.photo.description._content;
							if(content.length > 0) {
								c.html(content).css('visibility','visible');
							}
						});
					}
				});
			}
		});
	});

