$(document).ready(function() {
	var first = $('#thumbnails img:first').addClass('selected');
	setDescription(first);

	$('#thumbnails img').wrap('<a href="#" onclick="return false;" style="outline:none;"></a>');

	//$('#thumbnails img').hoverIntent(function(){swapImage($(this));},function(){}
	$('#thumbnails img').hover(function(){swapImage($(this));},function(){}
	).click(function(){swapImage($(this));});
	//).css('paddingLeft',function(){return ((60 - $(this).width()) / 2)+'px';}
	//).css('paddingRight',function(){return ((60 - $(this).width()) / 2)+'px';}
	//).css('paddingTop',function(){return ((40 - $(this).height()) / 2)+'px';}
	//).css('paddingBottom',function(){return ((40 - $(this).height()) / 2)+'px';});

	$('#dining,#kitchen,#dressing').hover(
		function(){areaHoverOver($(this));},
		function(){areaHoverOut($(this));});
});
 
function areaHoverOver(item){
	var coords = item.attr('coords').split(',');
	$('#'+item.attr('id')+'-hover').css({left:coords[0]+'px',top:coords[3]+'px'}).show();
}
function areaHoverOut(item){
	$('#'+item.attr('id')+'-hover').hide();;
}

function swapImage(item){
	$('#content img:first').attr({src:item.attr('src').replace('-thumb',''),alt:item.attr('alt'),title:item.attr('alt')});
	$('#thumbnails img').removeClass('selected');
	item.addClass('selected');
	setDescription(item);
}

function setDescription(item){
	$('#description').remove();
	$('#thumbnails').append('<div id="description">' + item.attr('alt') + '</div>');
}

function preload(){
	setTimeout(function() { 
		var cache = [];
		$('#thumbnails img').each(function(){
			var img = document.createElement('img');
			img.src = $(this).attr('src').replace('-thumb','');
			cache.push(img);
			//$('#content').append(img);
		});
	}, 500);
}

//var thumbs = [];
function preloadTEST(){
	var cache = [];
	// get references to image nodes into array
	$('#thumbnails img').each(function(){
		thumbs.push($(this).attr('src'));
	});
	while (thumbs.length > 0){
		setTimeout(function() { 
			var img = document.createElement('img');
			img.src = thumbs[thumbs.length].replace('-thumb','');
			cache.push(img);
			thumbs.pop();
		}, 500);
	}
}
