function MediaViewer(media_dir, admin_url, path)
{
	this.Path = path;
	this.AdminURL = admin_url;
	this.MediaDirectory = media_dir;
	this.Conn = new AjaxConnection(this.Path + "/media_reader.php");
	this.CurrentMode = "null";
	this.NumberOfAlbums = 0;
	this.NumberOfAlbumThumbnailsLoaded = 0;
	this.NumberOfItems = 0;
	this.NumberOfItemThumbnailsLoaded = 0;
	this.ClassName = "MediaViewer";
	this.FirstItemID = -1;
	this.Items = new Array();
	this.PendingSelectID = -1;
	this.CurrentID = -1;
	
	this.Initialize = function()
	{
		/*
		var thumbnails_div = document.getElementById("thumbnails");
		if (thumbnails_div.addEventListener)
		{
			thumbnails_div.addEventListener('DOMMouseScroll', this.OnMouseWheel, false);
		}
		thumbnails_div.onmousewheel = this.OnMouseWheel;
		*/
	}
	
	this.OnMouseWheel = function(evt)
	{
		if(evt.preventDefault)
		{
			evt.preventDefault();
		}
		evt.returnValue = false;
	}
	
	this.GetItems = function(id)
	{
		this.FirstItemID = -1;
		this.Items = new Array();
		
		var obj = new AjaxObject(this);
		obj.Parameters.push("action=list_items");
		obj.Parameters.push("album_id=" + id);
		obj.Callback = this.GetItemsCallback;
		
		this.Conn.DoAjax(obj);
	}
	
	this.GetItemsCallback = function(response)
	{
		var out = "";
		this.Owner.NumberOfItems = response.childNodes.length;
		this.Owner.Items = new Array();
		this.Owner.NumberOfItemThumbnailsLoaded = 0;
		for(var i = 0; i < response.childNodes.length; i++)
		{
			var currentItem = response.childNodes[i];
			var id 			= currentItem.childNodes[0].firstChild.nodeValue;
			var album_id	= currentItem.childNodes[1].firstChild.nodeValue;
			var caption 	= currentItem.childNodes[2].firstChild.nodeValue;
			var file 		= currentItem.childNodes[3].firstChild.nodeValue;
			var enabled 	= currentItem.childNodes[4].firstChild.nodeValue;
			var created 	= currentItem.childNodes[5].firstChild.nodeValue;
			
			var item = new Array();
			item["id"] = id;
			item["album_id"] = album_id;
			item["caption"] = caption;
			item["file"] = file;
			
			this.Owner.Items.push(item);
			
			if(this.Owner.FirstItemID == -1)
			{
				this.Owner.FirstItemID = id;
			}
			
			out += "<a id=\"item_" + id + "\" href=\"javascript:void(0)\" onclick=\"viewer.SelectItem(" + id + ")\" >";
				out += "<img onload=\"viewer.ItemThumbnailLoaded()\" src=\"" + this.Owner.AdminURL + "/__include/image.php?f=" + this.Owner.MediaDirectory + "/" + file + "&w=160&h=120&q=90\" />";
			out += "</a>";
		}
		
		document.getElementById("thumbnails_container").innerHTML = out;
	}
	
	this.ItemThumbnailLoaded = function()
	{
		this.NumberOfItemThumbnailsLoaded++;
		
		if(this.NumberOfItemThumbnailsLoaded == this.NumberOfItems)
		{
			$("#thumbnail_loading").fadeOut("fast");
			$("#thumbnails_container").fadeIn("fast");
			
			this.SelectItem(this.FirstItemID);
		}
	}
	
	this.NextItem = function()
	{
		for(var i = 0; i < this.Items.length; i++)
		{
			var item = this.Items[i];
			var divItem = document.getElementById("item_" + item["id"]);
			if(item["id"] == this.CurrentID)
			{
				if(i < this.Items.length - 1)
				{
					this.SelectItem(this.Items[i + 1]["id"]);
				}
			}
		}
	}
	
	this.PreviousItem = function()
	{
		for(var i = 0; i < this.Items.length; i++)
		{
			var item = this.Items[i];
			var divItem = document.getElementById("item_" + item["id"]);
			if(item["id"] == this.CurrentID)
			{
				if(i > 0)
				{
					this.SelectItem(this.Items[i - 1]["id"]);
				}
			}
		}
	}
	
	this.SelectItem = function(id)
	{
		if(this.CurrentID == id)
		{
			return;
		}
		
		this.PendingSelectID = id;
	
		$("#large_size_loading").fadeIn("fast");
		$("#large_size_image").fadeOut("fast", viewer.SelectItemReady);
	}
	
	this.SelectItemReady = function()
	{
		var index = -1;
		var id = viewer.PendingSelectID;
		viewer.CurrentID = id;

		for(var i = 0; i < viewer.Items.length; i++)
		{
			var item = viewer.Items[i];
			var divItem = document.getElementById("item_" + item["id"]);
			if(item["id"] == id)
			{
				index = i;
				divItem.className = "selected";
			}
			else
			{
				divItem.className = "";
			}
		}

		var img = document.getElementById("large_size_image");
		img.src = viewer.AdminURL + "/__include/image.php?f=" + viewer.MediaDirectory + "/" + viewer.Items[index]["file"] + "&w=600&h=450&q=90";
		img.onload = viewer.LargeImageLoaded;
		
		viewer.ScrollTo(id);
	}
	
	this.ScrollTo = function(id)
	{
		for(var i = 0; i < this.Items.length; i++)
		{
			var item = this.Items[i];
			var divItem = document.getElementById("item_" + item["id"]);
			if(item["id"] == id)
			{
				index = i;
				var target = (-1 * index * 150) + 150;
				setTimeout("viewer.ScrollToHelper(" + id + ", " + target + ")", 10);
				break;
			}
		}
	}
	
	this.ScrollToHelper = function(id, target)
	{
		var container = document.getElementById("thumbnails_container");
		var diffY = (target - container.offsetTop) / 2;
		container.style.top = (container.offsetTop + diffY) + "px";
		
		if(Math.abs(container.offsetTop - target) > 2)
		{
			setTimeout("viewer.ScrollToHelper(" + id + ", " + target + ")", 10);
		}
	}
	
	this.LargeImageLoaded = function()
	{
		$("#large_size_loading").fadeOut("fast");
		$("#large_size_image").fadeIn("fast");
		
		var largeImage = document.getElementById("large_size_image");
		largeImage.style.marginTop = (-1 * Math.round(largeImage.offsetHeight / 2)) + "px";
		largeImage.style.marginLeft = (-1 * Math.round(largeImage.offsetWidth / 2)) + "px";
	}
	
	this.ShowMainLoading = function()
	{
		$("#main_loading").fadeIn("fast");
	}
	
	this.HideMainLoading = function()
	{
		$("#main_loading").fadeOut("fast");
	}
	
}
