preLoadImages = function() {
	var cache = [];
	var args_len = arguments.length;
	for (var i = args_len; i--;) {
		var cacheImage = document.createElement('img');
		cacheImage.src = arguments[i];
		cache.push(cacheImage);
	}
};

function getCategory(num) { 
/* 	add if first load */
	$("#category_title").fadeOut(10);
	$("#portfolio_items").fadeOut(10);
	$("#main_portfolio_image").fadeOut(10);
	var html = "";
	$("#portfolio_items").html(html);
	category_id = num * 1 + 1;
	$.getJSON('/lookup/category/' + category_id + '/', function(data) {	
		$.each(data, function(i, item) {
			var html = "";
			html += '<li class="portfolio_thumb"><img id="' + item.pk + '" src="/' + item.fields.alt_thumb + '" onClick="getPortfolioEntry(' + category_id + ',' + item.pk + ');"/></li>';
			$("#portfolio_items").append(html);
/* 			$("#preloadImages").css("background-image", "url(" + '/' + item.fields.picture + ")"); */
			preLoadImages('/' + item.fields.picture);
		});
		$("#portfolio_items").hide();
		var category = "";
		category += data[0].fields.category.fields.name;
		$("#category_title").html(category);
		$("#category_title").hide();
		$("#category_title").fadeIn(400);
		$("#portfolio_items").fadeIn(400);
		portfolio_id = data[0].pk;
		getPortfolioEntry(category_id,portfolio_id);
	});
};

function getPortfolioEntry(category_id, portfolio_id) {
	$("#main_portfolio_image").fadeOut(800);
	portfolio_id = portfolio_id * 1;
	old_height = $("#main_portfolio_image").height();
	$.getJSON('/lookup/category/' + category_id + '/portfolio/' + portfolio_id + '/', function(data) {
		var image = "";
		if (data[0].fields.category == '1') {
			image += '<a id="main_portfolio_image" href="' + data[0].fields.url + '"><img alt="' + data[0].fields.title + '" title="' + data[0].fields.title + '" src="/' + data[0].fields.picture + '"/></a>';
/* 			set image, then set change this to just wrap around image else image = image */
			}
		else {
			image += '<img id="main_portfolio_image" alt="' + data[0].fields.title + '" title="' + data[0].fields.title + '" src="/' + data[0].fields.picture + '"/>';
			};
		replacePortfolio(image,old_height);
		var category_id = "";
		var portfolio_id = "";
	});
};

function getNewSizes(old_height) {
	new_height = $("#main_portfolio_image").height();
	div_height = $("#in_content").height();
	old_height = old_height * 1;
	new_height = new_height * 1;
	div_height = div_height * 1;
	if (old_height < 450) {
		old_height = "450"
	}
	else {
		old_height = old_height;
	};
	var height_diff = "";
	var new_div_height = "";
	height_diff = (new_height - old_height);
	new_div_height = (div_height + height_diff);
   animateChanges(new_div_height);
};

function replacePortfolio(image,old_height) {
	$("#main_portfolio_image").replaceWith(image);
	$("#main_portfolio_image").hide();
	getNewSizes(old_height);
};

function animateChanges(new_div_height) {
	$(document).ready(function() {
		$("#in_content").animate( { 
			height: new_div_height
			}, 800 
		);
		$("#main_portfolio_image").fadeIn(800);
		
	});
}