	//IE6 background image flicker fix
	try { document.execCommand( "BackgroundImageCache", false, true); } catch(e) {};
	
	Cufon.replace('#access a,#site-description,.FLICKR .entry-title, h3.widget-title, #content .entry-title');
	
	var AutoTimer = false;
	var ScTOP = 0;
	
	$(function(){
		var $box = $('body.FLICKR');
		if ($box.length) {
		
			if ( $('body.home').length ) {
				var feed = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=3939cd89701ac5dae5bebe8acd75609c&photoset_id=72157626396757823&format=json&extras=url_l,url_sq&jsoncallback=?";
				ScTOP = 240;
			} else {
				//Special Offers
				var feed = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=3939cd89701ac5dae5bebe8acd75609c&photoset_id=72157626577177997&format=json&extras=url_l,url_sq&jsoncallback=?";
				ScTOP = 144;
			}
			
			
			
			$.ajaxSetup({ cache: true });
			$.getJSON(feed,function(data){
				$ul = $('<ul/>').attr('id','homegallery');
				
				$.each(data.photoset.photo, function(i,item){
					if (item.url_l) {
						var url = item.url_l;
					} else {
						var url = 'http://farm'+item.farm+'.static.flickr.com/'+item.server+'/'+item.id+'_'+item.secret+'.jpg';
					}
					
					var thuburl = item.url_sq;
					
					lurl = '/wp-content/themes/devagents/images/thumb.php?w=699&zc=1&h=380&src='+url;
					turl = '/wp-content/themes/devagents/images/thumb.php?w=70&zc=1&h=60&src='+thuburl;
					
					$ul.append('<li><a href="'+lurl+'" class="thumbimg"><img src="'+turl+'" width="70" height="60" alt="'+item.title+'" title="'+item.title+'"/></a></li>');
				});
				
				
				
				$imgbox = $('<div/>').attr('id','imgbox');
				
				$('a',$ul).click(function(){
					clearTimeout(AutoTimer);
					$('.on',$ul).removeClass('on');
					var $e = $(this);
					$e.append('<span class="loading"></span>');
					var $oldimg = $('img',$imgbox).attr('src');
					var $newimg = $('<img/>').attr('src',$e.attr('href')).load(function(){
						if ($oldimg) {
							$imgbox.css({
								backgroundImage: 'url('+$oldimg+')'
							});
						}
						$imgbox.html('');
						$imgbox.append($newimg);
						$newimg.hide().fadeIn('slow',function(){
							$imgbox.css('background-image','none');
							$('.thumbimg',$ul).not($e).fadeTo('fast',0.7);
							$e.fadeTo('fast',1).addClass('on');
							$('.loading',$e).remove();
							
							AutoFrontpage();
						});
					});
					
					return false;
				});
				
				$('.thumbimg:first',$ul).trigger('click');
				
				if (data.photoset.photo.length > 9) {
					$ulcntrls = "<div id='homegallery_ctrl'><a href='#prev' class='prev'>Prev</a><a href='#next' class='next'>Next</a></div>";
				} else {
					$ulcntrls = "";
				}
				
				$('body.FLICKR #content').prepend($ul).prepend($imgbox).prepend($ulcntrls);
				
				if (data.photoset.photo.length > 9) {
					$('a.prev').click(function(){
						$ul.animate({scrollTop : '-='+ScTOP+'px'},'slow');
						return false;
					});
					$('a.next').click(function(){
						$ul.animate({scrollTop : '+='+ScTOP+'px'},'slow');
						return false;
					});
				}
				
				$ul.hide().fadeIn();
				
				$ul.animate({scrollTop : '0px'},'fast');
				
			});
			
			
		}
	});
		
	
	function AutoFrontpage()
	{
		clearTimeout(AutoTimer);
		AutoTimer = setTimeout(function(){
			
			var $li = $('#homegallery a.on').parents('li:first');
			var $ul = $li.parents('ul:first');
			
			var $next = $li.next('li');
			if (!$next.length) {
				$next = $ul.find('li:first');
			}
			
			$('a',$next).trigger('click');
			
		},5000);
	}
		
