﻿var fixedPointGallery= new Object(
	{
	openPhoto		: 0,
	albums			: [],
	interfaceReady	: false,
	buttonmouseover	: false,

	bindToMother: function(openPhoto, albumXML)
		{
		if(openPhoto) this.openPhoto = openPhoto;
		fixedPointGallery.objectContainer = document.body; 
		var lightboxContainer = document.createElement("DIV");
		lightboxContainer.id = "lightbox";
		with(lightboxContainer.style)
			{
			display	= "block";
			position	= "fixed";
			top		= "0%";
			left 		= "0%";
			width 		= "100%";
			height		= "100%";
			overflow	= "hidden";
			}

		// Create background shadow
		var lightboxShadow = document.createElement("DIV");
		with(lightboxShadow.style)
			{
			position	= 'absolute';
			top		= '0px';
			left		= '0px'; 
			background 	= '#000000'; 
			opacity	= '0.5';
			filter 	= "Alpha(opacity=50)"; 
			width		= '100%';
			height		= '100%';
			}
		lightboxContainer.appendChild(lightboxShadow);
		document.body.appendChild(lightboxContainer);

		fixedPointGallery.objectContainer.appendChild(lightboxContainer);
		fixedPointGallery.objectContainer = lightboxContainer;
		fpTools.addNewStyle('/js/gallery/style.css');
		this.init(albumXML);
		},
	init: function(albumXML)
		{
		fixedPointGallery.galleryPath = '/js/gallery/';
		if(albumXML)
			{
			var album = albumXML;
			}
		else var album = document.getElementById("albumname").value;
		fixedPointAjaxFramework.addToLine([album, false, fixedPointGallery.openFeed, false, false]);
		},

	openFeed: function(loadData)
		{
		var rssData = loadData;
		galleryAlbums = rssData.getElementsByTagName("album");
		for(var y=0; y<galleryAlbums.length; y++)
			{
			var albumName = galleryAlbums[y].getAttribute("name");
			var albumPath = galleryAlbums[y].getAttribute("albumpath");

			var albumPhotos = [];
			var items = galleryAlbums[y].getElementsByTagName("photo");
			for (var x=0; x<items.length; x++)
				{
				albumPhotos.push([items[x].getAttribute("thumbnail"), items[x].getAttribute("large"), items[x].getAttribute("width"), items[x].getAttribute("height")]);
				}
			fixedPointGallery.albums[albumName] = {
				albumId	: albumName,
				albumPath	: albumPath,
				photos		: albumPhotos};
			}

			fixedPointGallery.openAlbum = albumName;
			fixedPointGallery.setupthumbnails();

		},
	
	setupthumbnails: function()
		{
// Fullsize image holder
		var imageContainer = document.createElement("IMG");

		imageContainer.style.padding = "10px";
		imageContainer.style.background = "#ffffff";
		imageContainer.style.position = "absolute";

		fixedPointGallery.objectContainer.appendChild(imageContainer);
		fixedPointGallery.imageContainer = imageContainer;

// Set up user interface layer (for z-indexing)
		var uiLayer = document.createElement("DIV");
		fixedPointGallery.uiLayer = uiLayer;
		with(uiLayer.style)
			{
			width		= "100%";
			height		= "100%";
			position	= "absolute";
			top		= "0px";
			left		= "0px";
			display	= "block";
			background	= "transparent";
			}
		fixedPointGallery.objectContainer.appendChild(fixedPointGallery.uiLayer);

		uiLayer.onclick = function()
			{
			fixedPointGallery.closeGallery();
			}

// Set up thumbnail container and load thumbnails
		var thumbnailContainer = document.createElement("DIV");
		thumbnailContainer.className = "thumbnailcontainer";
		fixedPointGallery.thumbnailContainer = thumbnailContainer;

		var thumbnailImageImageHolder = document.createElement("DIV");
		thumbnailImageImageHolder.className = "clearfix";
		
		for(var i=0; i < fixedPointGallery.albums[fixedPointGallery.openAlbum].photos.length; i++)
			{
			var thumbnailHolder = document.createElement("DIV");
			thumbnailHolder.className = "thumbnailImageHolder";

			var thumbnail = document.createElement("IMG");
			thumbnail.src = fixedPointGallery.albums[fixedPointGallery.openAlbum].albumPath+ fixedPointGallery.albums[fixedPointGallery.openAlbum].photos[i][0];
			thumbnail.title = i;


			thumbnail.onclick = function()
				{
				
				fixedPointGallery.openPhoto = this.title;
				if(!fixedPointGallery.interfaceReady)	
					{
					fixedPointGallery.setupInterface();
					}
				else	{
					var thumbnailsHeight = parseInt(fixedPointGallery.thumbnailContainer.scrollHeight);
					fpTools.animate.init(fixedPointGallery.thumbnailContainer, animate._y, 0, -thumbnailsHeight, 8, 30);
					}
				fixedPointGallery.prepareImageContainer();
				}
			
			thumbnailHolder.appendChild(thumbnail);
			thumbnailImageImageHolder.appendChild(thumbnailHolder);
			}
		thumbnailContainer.appendChild(thumbnailImageImageHolder);

		var topLevelButton = document.createElement("DIV");
		topLevelButton.className = "topLevelBtn";


		topLevelButton.onclick = function()
			{
			var thumbnailsHeight = parseInt(fixedPointGallery.thumbnailContainer.scrollHeight);

			if(parseInt(thumbnailContainer.style.top) == 0)
				{
				fpTools.animate.init(fixedPointGallery.thumbnailContainer, animate._y, 0, -thumbnailsHeight, 8, 30);				
				}
			else	{
				fpTools.animate.init(fixedPointGallery.thumbnailContainer, animate._y, -thumbnailsHeight, 0, 8, 30);
				}
			}

		thumbnailContainer.appendChild(topLevelButton);
		fixedPointGallery.objectContainer.appendChild(thumbnailContainer);
		fixedPointGallery.thumbnailContainer = thumbnailContainer;
		fixedPointGallery.prepareImageContainer();
		fixedPointGallery.setupInterface();
		},
		

	setupInterface: function()
		{

		fixedPointGallery.uiLayer.onmouseover = function()
			{
			fixedPointGallery.hideUI(false);
			clearTimeout(fixedPointGallery.dockHideTimeOut);
			}

		fixedPointGallery.uiLayer.onmouseout = function()
			{ 
			fixedPointGallery.dockHideTimeOut = setTimeout("fixedPointGallery.hideUI(true)", 3600);
			}

		var prevButtonHolder = document.createElement("DIV");
 		prevButtonHolder.className = "prevBtnHolder";

		var prevButton = document.createElement("DIV");
 		prevButton.className = "prevBtn";

		var nextButtonHolder = document.createElement("DIV");
 		nextButtonHolder.className = "nextBtnHolder";

		var nextButton = document.createElement("DIV");
 		nextButton.className = "nextBtn";

		prevButton.onmouseover = function(){fixedPointGallery.buttonmouseover = true;}
		nextButton.onmouseover = function(){fixedPointGallery.buttonmouseover = true;}
		prevButton.onmouseout = function(){fixedPointGallery.buttonmouseover = false;}
		nextButton.onmouseout= function(){fixedPointGallery.buttonmouseover = false;}

		
		prevButton.onclick = function()
			{
			fixedPointGallery.openPhoto--;
			if(fixedPointGallery.openPhoto < 0)
				{
				fixedPointGallery.openPhoto = fixedPointGallery.albums[fixedPointGallery.openAlbum].photos.length-1;
				}
			return fixedPointGallery.prepareImageContainer();
			}

		nextButton.onclick = function()
			{
			fixedPointGallery.openPhoto++;
			if(fixedPointGallery.openPhoto >= fixedPointGallery.albums[fixedPointGallery.openAlbum].photos.length)
				{
				fixedPointGallery.openPhoto = 0;
				}
			return fixedPointGallery.prepareImageContainer();
			}
		prevButtonHolder.appendChild(prevButton);
		nextButtonHolder.appendChild(nextButton);
		fixedPointGallery.uiLayer.appendChild(nextButtonHolder);
		fixedPointGallery.uiLayer.appendChild(prevButtonHolder);

// Loading indicator
		var loadingIndicatorContainer = document.createElement("DIV");
		loadingIndicatorContainer.className = "loadingIndicator";

		loadingIndicatorContainer.style.left = (fixedPointUiControl.screenWidth/2) - 42+"px";
		loadingIndicatorContainer.style.top = (fixedPointUiControl.screenHeight/2) - 52+"px";


		var loadIndicatorBgr = document.createElement("DIV");
		loadIndicatorBgr.className = "loadBgr";
		
		var loadIndicator = document.createElement("IMG");
		loadIndicator.src = fixedPointGallery.galleryPath+"images/loading.gif";
		
		loadingIndicatorContainer.appendChild(loadIndicatorBgr);
		loadingIndicatorContainer.appendChild(loadIndicator);

		fixedPointGallery.objectContainer.appendChild(loadingIndicatorContainer);
		fixedPointGallery.loadingIndicatorContainer = loadingIndicatorContainer;
		
		fixedPointGallery.dockHideTimeOut = setTimeout("fixedPointDashboardDock.hideDock(true)", 3600);

		var thumbnailsHeight = parseInt(fixedPointGallery.thumbnailContainer.scrollHeight);
		fpTools.animate.init(fixedPointGallery.thumbnailContainer, animate._y, 0, -thumbnailsHeight, 8, 30);
		fixedPointGallery.interfaceReady = true;

		var closeButtonContainer = document.createElement("DIV");
		closeButtonContainer.className = "closeBtnContainer";
		
		var closeButton = document.createElement("DIV");
		closeButton.className = "closeBtn";
		
		closeButtonContainer.appendChild(closeButton);
		fixedPointGallery.uiLayer.appendChild(closeButtonContainer);

		closeButton.onclick = function()
			{
			fixedPointGallery.closeGallery();
			}
		},

	hideUI: function(hide)
		{
		if(hide)
			{
			fpTools.animate.init(fixedPointGallery.uiLayer, animate._alpha, 100, 0, 8, 30);
			fixedPointGallery.uiLayerHide = true;
			}
		else 	{
			if(fixedPointGallery.uiLayerHide) 
				{
				fpTools.animate.init(fixedPointGallery.uiLayer, animate._alpha, 0, 100, 8, 30);
				fixedPointGallery.uiLayerHide = false;
				}
			}
		},

	prepareImageContainer: function()
		{
		fpTools.animate.init(fixedPointGallery.imageContainer, animate._alpha, 0, 0, 1, 30);
		setTimeout("fixedPointGallery.delayPerkele()", 300);
		},

	delayPerkele: function()
		{
		var galleryWidth = parseInt(fixedPointGallery.objectContainer.style.width);
		var galleryHeight = parseInt(fixedPointGallery.objectContainer.style.height);
		fixedPointGallery.loadingIndicatorContainer.style.left = (fixedPointUiControl.screenWidth/2) - 42+"px";
		fixedPointGallery.loadingIndicatorContainer.style.top = (fixedPointUiControl.screenHeight/2) - 52+"px";

		var imageUrl = fixedPointGallery.albums[fixedPointGallery.openAlbum].albumPath + fixedPointGallery.albums[fixedPointGallery.openAlbum].photos[fixedPointGallery.openPhoto][1];
		fpTools.animate.init(fixedPointGallery.loadingIndicatorContainer, animate._alpha, 0, 100, 8, 30);
		fixedPointAjaxFramework.addToLine([fixedPointGallery.galleryPath+'gallery.php', false, false, 'fixedPointGallery.changeImage("'+imageUrl+'")', 'file='+imageUrl], false);
		},
	
	changeImage: function(loadData)
		{
		fpTools.animate.init(fixedPointGallery.loadingIndicatorContainer, animate._alpha, 100, 0, 8, 30);
		var imageWidth = fixedPointGallery.albums[fixedPointGallery.openAlbum].photos[fixedPointGallery.openPhoto][2];
		var imageHeight = fixedPointGallery.albums[fixedPointGallery.openAlbum].photos[fixedPointGallery.openPhoto][3];

		fixedPointGallery.imageContainer.style.width = imageWidth;
		fixedPointGallery.imageContainer.style.height = imageHeight;
		fixedPointGallery.imageContainer.style.left = (fixedPointUiControl.screenWidth/2)-(imageWidth/2)-10+"px";
		fixedPointGallery.imageContainer.style.top = (fixedPointUiControl.screenHeight/2)-(imageHeight/2)-10+"px";

		setTimeout("fixedPointGallery.fadeIn('"+loadData+"')", 1000);
		},
	
	fadeIn: function(loadData)
		{
		fixedPointGallery.imageContainer.src = fixedPointGallery.galleryPath+'gallery.php?file='+loadData;
		fpTools.animate.init(fixedPointGallery.imageContainer, animate._alpha, 0, 100, 8, 30);
		},
	
	closeGallery: function()
		{
		if(fixedPointGallery.buttonmouseover) return false;
		var testi = document.getElementsByTagName("DIV");
		for (var i=0; i < testi.length; i++)
			{
			if(testi[i].id == 'lightbox')
				{
				document.body.removeChild(testi[i]);
				}
			}
		}
	});