
       // JS Constants:
	var supportedPostTypes = ["regular","photo","video","link"];
	var unsupportedPostTypes = ["quote","chat","audio"];
	var numFeaturedEntries = 1;
	var numEntriesOnPage = 3;
	var postsEntered = [];
	var numNonFeaturedEntries = (numEntriesOnPage - numFeaturedEntries);
	var maxTags = 4;
	var gArrFillerImages = new Array(5);
	gArrFillerImages[0] = "/images/media/art012_thumbnail.jpg";
	gArrFillerImages[1] = "/images/media/art011_thumbnail.jpg";
	gArrFillerImages[2] = "/images/media/art010_thumbnail.jpg";
	gArrFillerImages[3] = "/images/media/art005_thumbnail.jpg";
	gArrFillerImages[4] = "/images/media/art009_thumbnail.jpg";
	gArrFillerImages[5] = "/images/media/art015_thumbnail.jpg";
	gArrFillerImages[6] = "/images/media/art018_thumbnail.jpg";
	
	var gArrCreaturePics = new Array(5);
	gArrCreaturePics[0] = "/images/what001.png";
	gArrCreaturePics[1] = "/images/media/what001.png";
	gArrCreaturePics[2] = "/images/media/what002.png";
	gArrCreaturePics[3] = "/images/media/what003.png";
	gArrCreaturePics[4] = "/images/media/what004.png";
	gArrCreaturePics[5] = "/images/media/what005.png";
	gArrCreaturePics[6] = "/images/media/what006.png";
	gArrCreaturePics[7] = "/images/media/what007.png";










function decideToPopulateFeed(tumblrURL){
	// if there is an api response, populate the feed, otherwise leave the default content and make it visible.
	if (typeof(tumblr_api_read) != "undefined"){
		populateTumblrFeed(tumblrURL);
	}
	var announcementsEl = returnObjById('announcements');
	announcementsEl.style.visibility = 'visible';
}
    



function populateTumblrFeed(tumblrURL)
{
	var isFeatured;
	//for as many *featured* entries as there will be on the page 
	for (var j = 0; j < numFeaturedEntries; j++)
	{
		isFeatured = true;
		var entryToPopulateTable =  findNextPostOfApprovedTypes(isFeatured);
		var entryToPopulate = entryToPopulateTable.entryKey;
		var currentPostType = entryToPopulateTable.currentPostTypeKey;
		var currentEntryID = "Entry" + j;
		populateEntry(currentEntryID, entryToPopulate,isFeatured,currentPostType,tumblrURL);
	}
	//for as many *remaining non featured entries* as there will be on the page 
	for (var k = 1; k <= numNonFeaturedEntries; ++k)
	{
		isFeatured = false;
		var entryToPopulateTable =  findNextPostOfApprovedTypes(isFeatured);
		var entryToPopulate = entryToPopulateTable.entryKey;
		var currentPostType = entryToPopulateTable.currentPostTypeKey;
		var currentEntryID = "Entry" + k;
		populateEntry(currentEntryID, entryToPopulate,isFeatured,currentPostType,tumblrURL);
	}
}

function populateEntry(currentEntryID, entryToPopulate, isFeatured, currentPostType,tumblrURL)
{
	var isFeatured = isFeatured;
	var link = tumblr_api_read.posts[entryToPopulate]['url-with-slug'];
	switch (currentPostType)
	{
		case "regular": case "photo": case "video": case "link":
		{
			fillInTitle(entryToPopulate,currentEntryID,link,currentPostType);
			fillInSummary(entryToPopulate,currentEntryID,currentPostType);
			fillInReadMoreLink(entryToPopulate,currentEntryID,link,currentPostType);
			fillInDate(entryToPopulate,currentEntryID);
			fillInTags(entryToPopulate,currentEntryID,tumblrURL);
			fillInThumb(entryToPopulate,currentEntryID, link, currentPostType, isFeatured);
		}
		break;
	}
}

function fillInTitle(entryToPopulate, currentEntryID,link,currentPostType)
{
	var elTitle = returnObjById('title' + currentEntryID );
	var title;
	switch (currentPostType)
	{
		case "regular":
		{
			title = tumblr_api_read.posts[entryToPopulate]['regular-title'];
		}
		break;
		case "photo":
		{
			title = "New Picture";
		}
		break;
		case "video":
		{
			title = "New Movie";
		}
		break;
		case "link":
		{
			title = tumblr_api_read.posts[entryToPopulate]['link-text'];
		}
		break;
	}
	elTitle.innerHTML = '<a class="blogTitleTxt" href="' + link + '">' + title + '</a>';
	
}

function fillInSummary(entryToPopulate, currentEntryID, currentPostType)
{
	var elSummary = returnObjById('summary'+ currentEntryID );
	var summary;
	switch (currentPostType)
	{
		case "regular":
		{
			summary = tumblr_api_read.posts[entryToPopulate]['regular-body'];
		}
		break;
		case "photo":
		{
			summary = tumblr_api_read.posts[entryToPopulate]['photo-caption'];
		}
		break;
		case "video":
		{
			summary = tumblr_api_read.posts[entryToPopulate]['video-caption'];
		}
		break;
		case "link":
		{
			summary = tumblr_api_read.posts[entryToPopulate]['link-description'];
		}
		break;
	}
	var strippedSummary = stripHTML(summary);
	elSummary.innerHTML = strippedSummary;
}

function fillInReadMoreLink(entryToPopulate,currentEntryID,link,currentPostType)
{
	var elReadMoreLink = returnObjById('readMoreLink'+ currentEntryID );
	switch (currentPostType)
	{
		case "regular": case "link":
		{
			summary = tumblr_api_read.posts[entryToPopulate]['regular-body'];
			elReadMoreLink.innerHTML = '<a class="blogReadMoreLink" href=' + link + '><img src="/images/getmore.png"></a>';
		}
		break;
		case "photo": case "video":
		{
			summary = tumblr_api_read.posts[entryToPopulate]['regular-body'];
			elReadMoreLink.innerHTML = '<a class="blogReadMoreLink" href=' + link + '><img src="/images/getmore.png"></a>';
		}
		break;
	}
}

function fillInDate(entryToPopulate,currentEntryID)
{
	var date = tumblr_api_read.posts[entryToPopulate]['date'];
	var elTimeAgo = returnObjById('timeAgo'+ currentEntryID);
	elTimeAgo.innerHTML = date;
}

function fillInTags(entryToPopulate,currentEntryID,tumblrURL)
{
	var elTags = returnObjById('tags'+ currentEntryID);
	elTags.innerHTML  = "";
	if (tumblr_api_read.posts[entryToPopulate]['tags'] != undefined)
	{
		var tags = tumblr_api_read.posts[entryToPopulate]['tags'];
		//loop through all tags to display each one, with comma seperation
		for (var i = 0; i < tags.length &&   i <  maxTags; ++i)
		{
			var currentTag = tags[i];
			

			var tagText = ' , <a class="blogTagsTxt" href='+ tumblrURL +'/tagged/'+currentTag+'>'+currentTag+'</a>';
			if (currentTag != "Featured")
			{
				elTags.innerHTML  += tagText;
			}
		}
	}
}

function fillInThumb(a_entryToPopulate,a_currentEntryID, a_link, a_currentPostType, a_isFeatured)
{
	var entryToPopulate = a_entryToPopulate;
	var currentEntryID = a_currentEntryID;
	var link = a_link;
	var currentPostType = a_currentPostType; 
	var isFeatured = a_isFeatured;
	
	
	var elThum = returnObjById('thum'+ currentEntryID);
	var imageElID = 'thumPic' + currentEntryID;
	elThum.innerHTML = '<a href="'+link+'"><img id="'+imageElID+'"></img></a>';
	var image_source;
	var image =  new Image();
	image.onload = function () 
	{
		imgOnLoadFunction(imageElID,isFeatured,currentEntryID,link,image);
	}
	
	switch (currentPostType)
	{
		case "regular":
		{
			var summary = tumblr_api_read.posts[entryToPopulate]['regular-body'];
			image_source = parseForImageSrc(summary);
			
		}
		break;
		case "photo":
		{
			image_source = tumblr_api_read.posts[entryToPopulate]['photo-url-400'];
			
		}
		break;
		case "video":
		{
			var summary = tumblr_api_read.posts[entryToPopulate]['video-caption'];
			image_source = parseForImageSrc(summary);
			
		}
		break;
		case "link":
		{
			var summary = tumblr_api_read.posts[entryToPopulate]['link-description'];
			image_source = parseForImageSrc(summary);
			
		}
		break;
	}
	image.src = image_source;
	
}

function imgOnLoadFunction(b_imageElID,b_isFeatured,b_currentEntryID,b_link,b_image)
{
	// must resize and crop the image
	var imageElID = b_imageElID;
	var isFeatured = b_isFeatured;
	var currentEntryID = b_currentEntryID;
	var link = b_link;
	var image = b_image;
	
	var thumbWidth;
	var thumbHeight;
	if (isFeatured == true)
	{
		targetWidth= 227;
		targetHeight = 153;
	}
	
	else
	{
		targetWidth= 158;
		targetHeight = 158;
	}
	
	var imageBigEnough = checkImageBigEnough(image,targetWidth, targetHeight);
	if (imageBigEnough == false)
	{
		var randomImage =  new Image();
		randomImage.onload = function () 
		{
			randomImageOnLoadFunction(imageElID,isFeatured,currentEntryID,link,randomImage);
		}
		randomImage_source = provideRandomImg();
		randomImage.src = randomImage_source;
		return;
	}
	var docimage = document.getElementById(imageElID);
	docimage.src = image.src;
	
	var testImage = image;
	var domImage = document.getElementById('thumPic'+ currentEntryID);
	
	smartResizeImage(testImage, domImage,targetWidth, targetHeight);
	smartCropImage(domImage,targetWidth, targetHeight);
	
	//link thumbframe to appropriate post
	if (isFeatured == true)
	{


		var elFrame = returnObjById('frame'+ currentEntryID);
		elFrame.onclick = function() { window.location.href =  link; }
		elFrame.onmouseover = function() { this.style.cursor='pointer';}

	}		
	else 
	{
	//do nothing, since the image already links and there is no frame element
	}
}

function randomImageOnLoadFunction(imageElID,isFeatured,currentEntryID,link,randomImage)
{
	var docimage = document.getElementById(imageElID);
	docimage.src = randomImage.src;
	var thumbWidth;
	var thumbHeight;
	if (isFeatured == true)
	{
		targetWidth= 227;
		targetHeight = 153;
	}
	else
	{
		targetWidth= 158;
		targetHeight = 158;
	}
	
	var testImage = randomImage;
	var domImage = document.getElementById('thumPic'+ currentEntryID);
	
	smartResizeImage(testImage, domImage,targetWidth, targetHeight);
	smartCropImage(domImage,targetWidth, targetHeight);
	
	
	
	//link thumbframe to appropriate post
	if (isFeatured == true)
	{
		var elFrame = returnObjById('frame'+ currentEntryID);
		elFrame.attributes['onclick'].value="location='"+link+"';";
	}		
	else 
	{
	//do nothing, since the image already links and there is no frame element
	}
}



    
function findNextPostOfApprovedTypes(isFeatured){
	var postIsUsed;
	// for each post in the api feed...
	for (var i = 0; i < tumblr_api_read.posts.length;++i)
	{
		// test to see if it happens to be a supported Type by running through all supported types and matching them against the post we're testing
		for (var j = 0; j < supportedPostTypes.length; ++j) 
		{
			//if it is a supported type
			if  (tumblr_api_read.posts[i].type == supportedPostTypes[j])
			{
				//record the current post type
				var currentPostType = tumblr_api_read.posts[i].type;
				// if we're looking for a featured post type
				if (isFeatured == true)
				{
					//if there are tags
					if (typeof(tumblr_api_read.posts[i].tags) != "undefined")
					{
						// run through every tag in the post we're testing to check if it says feaatured
						for (var k = 0; k < tumblr_api_read.posts[i].tags.length; ++k)
						{
							// if the current tag we're testing on the current post we're testing is featured
							if (tumblr_api_read.posts[i].tags[k] == "Featured")
							{
								//choose this post, and mark it as entered
								markPostAsEntered(i);
								return {entryKey: i, currentPostTypeKey: currentPostType};
							}
						}
					}
				}
				// for non-featured entry:
				else
				{
					// be sure the entry hasn't already been used by testing every post already entered to see if it matches the current post being tested
					for (var l = 0; l < postsEntered.length; ++l )
					{
						if (postsEntered[l] == i)
						{
							// the entered post tested has already been used.  No need to continue testing all Entered Posts.  Skip out of this loop and mark the post as used.
							var postIsUsed = true;
							break;
						}
						// if the post wasn't used, remove post is used
						postIsUsed = false;
					}
					// if the post hasn't been used, use it and mark as used.
					if (postIsUsed != true)
					{
						markPostAsEntered(i);
						return {entryKey: i, currentPostTypeKey: currentPostType};
					}
				}
			}
		}
	}
	// if every post has been exhausted and nothing has passed all the test, return -1 for no matching posts
	return -1;
}

function markPostAsEntered(i)
{
	postsEntered.push(i);
}
  
function returnObjById(id)
{
	var returnVar;
    if (document.getElementById)
        returnVar = document.getElementById(id);
    else if (document.all)
        returnVar = document.all[id];
    else if (document.layers)
        returnVar = document.layers[id];
    return returnVar;
}
  
function stripHTML(text)
{ 
	var stripped = text.replace(/(<([^>]+)>)/ig,""); 
	return stripped;
}

function parseForImageSrc(text)
{
	//need to get the image tag from the raw html in the post body
	var arrImg_src = text.match(/src="(.+?[\.jpg|\.gif|\.png])"/);
	
	var img_src;
	//if there was no image, choose one randomly from the given set
	if (arrImg_src == null)
	{
		img_src = provideRandomImg();
	}
	else
	{
		img_src = arrImg_src[1];
	}
	return img_src;
}


function parseForFrameSrc(text)
{
	//need to get the image tag from the raw html in the post body
	if (text.match(/.jpg,(.+?[\.jpg]),/)!= null)
	{
		var img_src = text.match(/.jpg,(.+?[\.jpg]),/)[1];
	}
	else
	{
		img_src = provideRandomImg();
	}
	
	
	return img_src;
}

function decodeURL(text){
	var lsRegExp = /\+/g;
	// Return the decoded string
	return unescape(String(text).replace(lsRegExp, " ")); 
}


function replaceDivBgImage(imgSrc, elementID,ParentID)
{
        //var imgPath = document.getElementById("elementID").style.backgroundImage;
	document.getElementById(elementID).style.backgroundImage = 'url('+imgSrc+')';
 }

 
 function checkImageBigEnough(image,targetWidth, targetHeight)
 {
	 var bigWidth = image.width;
	 var bigHeight = image.height;
	 var goalWidth = targetWidth;
	 var goalHeight = targetHeight;
	 
	 if (bigWidth < goalWidth || bigHeight < goalHeight)
	 {
		 return false;
	 }
	 else
	 {
		 return true;
	 }
 }
 
 function smartResizeImage(testImage, domImage,targetWidth, targetHeight)
 {
	 
	var goalAspectRatio = targetWidth/targetHeight;
	var imageAspectRatio = testImage.width/testImage.height;
	if (imageAspectRatio < goalAspectRatio)
	{
	// size to fit width
		domImage.width  = targetWidth
		domImage.height = targetWidth / imageAspectRatio;
	}
	else
	{
	// size to fit height
	domImage.width  = targetHeight * imageAspectRatio;
	domImage.height = targetHeight;
	}
 }
 
 
 function provideRandomImg()
 {
	var numRandomImagesLeft = gArrFillerImages.length;
	var randomNumber = (Math.round((Math.random()*(numRandomImagesLeft-1))))
	return img = gArrFillerImages.splice(randomNumber,1);
 }
 
 
 
 
 
 function stripPx(text)
 {
	var strippedNum = text.replace("px", ""); 
	 return strippedNum;
 }

  function smartCropImage(domImage,targetWidth, targetHeight)
 {
	var imageWidth = domImage.width;
	var imageHeight = domImage.height;
	if (imageWidth > targetWidth )
	{
	 var widthDifference = imageWidth - targetWidth;
	 var widthOffset = widthDifference/2;
	 domImage.style.marginLeft = '-'+widthOffset+'px';
	}
	 
	if (imageHeight > targetHeight )
	{
	 var heightDifference = imageHeight - targetHeight;
	 var heightOffset = heightDifference/2;
	 domImage.style.marginTop = '-'+heightOffset+'px';
	}
	 
 }

