function loadImagesFor(sectionID)
{
//	$('#photo_div').load('../admin/loadstructure.php?id=' + sectionID,null,imagesLoaded,'xml');
	$.post('../admin/loadstructure_json.php?id=' + sectionID,null,imagesLoaded,'json');
}
function imagesLoaded(tree,blabla)
{
	var structure = eval(tree);
	var page 			= null;
	var paragraph = null;
	var image			= null;
	var imageHTML = "";
	
  var photoDIVName = "photo_div";
	for(var i=0;i<tree.length;i++)
	{
		page = tree[0];
		photoDIVName = "photo_div_" + page.data.id;
		//alert('photo div: ' + photoDIVName);
		for(var j=0;j<page.children.length;j++)
		{
			paragraph = page.children[j];
//			alert(paragraph.children.length);
			for(var k = 0;k<paragraph.children.length;k++)
			{
				image = paragraph.children[k];
				imageHTML += "<div class='photo_frame'>" + formatImageHTML(image,240,240,true,true) + "</div>";
//				imageHTML += "<img src='../ImageDir/" + image.data.filename +"' width='" + image.data.width + "' height='" + image.data.height + "' />";
			}
		}
	}


	$('#' + photoDIVName).html(imageHTML);
	
	
	$('.photo_frame').click(
		function(e)
		{
			var jImg = $(this).find('img');
			top.clickPolaroid(jImg.attr('src'));
		}
	);
}
function ShowImage(State)
{
	if(State)
	{
		$('#image_viewer_background_overlay_div').removeClass('hidden');
		var imageLeft = ($(window).width()-$('#image_viewer_img').width())/2;
		var imageTop = ($(window).height()-$('#image_viewer_img').height())/2;
		$('#image_viewer_div').css({left:imageLeft,top:imageTop}).removeClass('hidden');
	}
	else
	{
		$('#image_viewer_background_overlay_div').addClass('hidden');
		$('#image_viewer_div').addClass('hidden');
	}
}
function formatImageHTML(image,targetWidth,targetHeight,scaleProportional,fitWithin)
{
	var retval 			= "";
	var widthToUse 	= 0;
	var heightToUse = 0;
	
	if(scaleProportional==undefined)
		scaleProportional = true;
	
	if(fitWithin==undefined)
		fitWithin 				= false;
	
	
	if(!scaleProportional)
	{
		widthToUse 	= targetWidth;
		heightToUse = targetHeight;
	}
	else
	{
		var scaleW = targetWidth/image.data.width;
		var scaleH = targetHeight/image.data.height;
//		alert(scaleW + "," + scaleH + ',');
		if(fitWithin)
		{
			if(scaleW>scaleH)
			{
				widthToUse 	= image.data.width*scaleH;
				heightToUse = targetHeight;
			}
			else
			{
				widthToUse 	= targetWidth;
				heightToUse = image.data.height*scaleW;
			}
		}
		else
		{
			if(scaleW<scaleH)
			{
				widthToUse 	= image.data.width*scaleH;
				heightToUse = targetHeight;
			}
			else
			{
				widthToUse 	= targetWidth;
				heightToUse = image.data.height*scaleW;
			}
		}
	}
	
	retval = "<img src='/image_dir/" + image.data.filename +"' width='" + widthToUse + "' height='" + heightToUse + "' />";
	return retval;
}