var feedIndex=0;
var loadFlickr = function(feedIndex){
	$.getJSON(flickrFeed[feedIndex] + "&jsoncallback=?", function(data){
		var feedRSS = flickrFeed[feedIndex].replace('json','rss_200');
		var feedTitle = data.title.replace('Content from ','');																				
		$('#flickrcontent').append('<div id="photocontainer' + feedIndex + '" class="photocontainer"></div>'); //Main containers
		$('#photocontainer' + feedIndex).append('<a href="' + feedRSS + '"><img src="/images/common/feed-icon-14x14.png" alt="' + feedTitle + ' Flickr RSS Feed" title="' + feedTitle + ' Flickr RSS Feed" class="feedicon"/></a>'); //RSS feedicon
		$('#photocontainer' + feedIndex).append('<h3>' + feedTitle + '</h3>'); //Photoset title
		$('#photocontainer' + feedIndex).append('<div class="photobox" id="photobox' + feedIndex + '"/>'); //div that contains the thumbs
		if(data.items.length > 7) { //add a last class for last in the row
			$('#photocontainer' + feedIndex).append('<a href="#" class="viewphotos" onclick="viewAllPhotos(' + feedIndex + '); return false;" id="viewall' + feedIndex + '">View all Photos &gt;</a>');	
		}
		var thumbNum = 1;
		var thumbLast = '';
		$.each(data.items, function(i,item){
			photoPath = item.media.m;
			smallPhotoPath = photoPath.replace("_m.jpg","_s.jpg");
			largePhotoPath = photoPath.replace("_m.jpg",".jpg");
			$('#photobox'+ feedIndex).append('<a href="' + largePhotoPath + '" title="' + item.title + '" class="zoom"><img src="' + smallPhotoPath + '" alt="' + item.title + '"' + thumbLast + ' /></a>');
			if (thumbNum == 6) { 	
				thumbNum = 0;
				thumbLast = ' class="last"';
			}
			else { 
				thumbNum ++;
				thumbLast = '';
			}
		});
		
		flickrLastIndex = flickrFeedLength - 1;
		if(feedIndex == flickrLastIndex) {
			//All images have been set attach the zoom.
			$('.zoom').click(function(){
				lightBoxZoom(this);
				return false;
			});
		}
		if(feedIndex < flickrLastIndex) { 
			//Load up the next flickr feed in the array
			feedIndex++;
			loadFlickr(feedIndex);
		}
	});
}

var activePhotoNum= 'notset';
var viewAllPhotos = function(idNum){
	if (activePhotoNum == idNum){
		//close the open box
		$('#photobox' + activePhotoNum).css('height','80px');
		$('#viewall' + activePhotoNum).html('View All Photos &gt;');
		activePhotoNum = 'notset';
	}
	else if(activePhotoNum != 'notset'){
		$('#photobox' + activePhotoNum).css('height','80px');
		$('#viewall' + activePhotoNum).html('View All Photos &gt;');
		$('#photobox' + idNum).css('height','auto');
		$('#viewall' + idNum).html('View Most Recent Photos &gt;');
		activePhotoNum = idNum;
	}
	else { 
		$('#photobox' + idNum).css('height','auto');
		$('#viewall' + idNum).html('View Most Recent Photos &gt;');
		activePhotoNum = idNum;
	}
}




