
function getLoggerFromDocumentURL()
{
	var logLevel = Log.NONE;
	var document_url = "" + document.URL;
	if (document_url.indexOf("?") != -1)
	{
		//document url has an argument on the end of it..
		var index = document_url.indexOf("?");
		var arg = document_url.substring(index);
		if (arg == "?DEBUG")
		{
			logLevel = Log.DEBUG;
		}

	}	
	
	return new Log(logLevel, Log.popupLogger);
}

function setDivZIndex(div, id)
{
	if (div == null)
	{
		alert("setDivZIndex div is null");
	}
	div.style.zIndex = id;
}


function ImageInformation(file, title, description, etsyLink)
{
	this.file = file;
	this.title = title;
	this.description = description;
	this.etsyLink = etsyLink;
	
	this.toString = function() 
	{
		return "ImageInformation {file: " + this.file + ", title: " + this.title + 
				", description: " + this.description + ", etsyLink: " + etsyLink + "}";
	};
}


/* thanks to this awesome tutorial for this code: http://www.javascriptkit.com/javatutors/static2.shtml */

function getBrowserScrollOffsetY()
{
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	//var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
	var dsoctop=document.all? iebody.scrollTop : pageYOffset;
	return dsoctop;
}

function moveDiv(div, offsetX, offsetY)
{
	if (offsetX != null)
	{
		div.style.left = offsetX + "px";
	}
	
	if (offsetY != null)
	{
		div.style.top = offsetY + "px";
	}
}


function ImageManager(name, images)
{
	this.logger = getLoggerFromDocumentURL();
	this.name = name;
	this.images = images;
	this.divPictureBox = getDivById('picturebox');
	this.divPictureTitle = getDivById('picturetitle');
	this.imgPictureBoxImage = getDivById('theactualpicture');
	//this.divPictureDescription = getDivById('picturedescription');
	//this.aEtsyLink = getDivById('etsylink');
	
	this.lastImageIndex = 0;
	
	this.showImage = function(imageIndex)
	{
		this.logger.debug("showImage(index: " + imageIndex + 
			", picture box visibility: " + this.imgPictureBoxImage.style.visibility + 
			", picture box zindex: " + getDivById('picturebox').style.zIndex +
			")");
		var imageInfo = this.images[imageIndex];
		
		if (imageInfo == null)
		{
			alert("could not find image w/ index " + imageIndex);
			return;
		}
		
		showOrHideDiv(false, 'theactualpicture');		

		var newImage = new Image();
		newImage.imageIndex = imageIndex;
		newImage.imageInfo = imageInfo;
		newImage.imageManager = this;
		newImage.onload = this.renderImage;
		newImage.src = imageInfo.file;
		
		setDivZIndex(getDivById('picturebox'), 1000);
		setDivZIndex(getDivById('loading'), 1001);
		setDivZIndex(getDivById('theactualpicture'), 1002);

		moveDiv(this.divPictureBox, null, getBrowserScrollOffsetY() + 10);
		
		if (this.divPictureBox.style.visibility != 'visible')
		{	
			this.logger.debug("showImage() fading picture box in");
			showOrHideDiv(true, 'picturebox');
			//fadeDiv(true, 'picturebox');
		}

	};
	
	this.renderImage = function()
	{
		var imageManager = this.imageManager;
	
		imageManager.logger.debug("renderImage(" + this.imageIndex + ")");
	
		var imageInfo = this.imageInfo;
		
		imageManager.divPictureTitle.innerHTML = imageInfo.title;
		//imageManager.divPictureDescription.innerHTML = imageInfo.description;
		//imageManager.aEtsyLink.href = imageInfo.etsyLink;
		imageManager.imgPictureBoxImage.onload = imageManager.showLoadedImage;
		imageManager.imgPictureBoxImage.src = this.src;
		imageManager.lastImageIndex = this.imageIndex;	
	};
	
	this.showLoadedImage = function()
	{
		showOrHideDiv(true, 'theactualpicture');
	}
	
	this.showNextImage = function()
	{
		var imageIndex = this.lastImageIndex + 1;
		if (imageIndex == this.images.length)
		{
			imageIndex = 0;
		}
		this.showImage(imageIndex);
	};
	
	this.showPreviousImage = function()
	{
		var imageIndex = this.lastImageIndex - 1;
		if (imageIndex < 0)
		{
			imageIndex = this.images.length - 1;
		}
		this.showImage(imageIndex);
	};
}