var gallery =
{
	
	divs: [],
	
	smallImages: [],
	bigImages: [],
	className: "sc_gallery",
	
	start: function(divId)	{
				
		gallery.divs[gallery.divs.length] = document.getElementById(divId);
		
		if(document.addEventListener)	{
			window.addEventListener("load", gallery.getImages, false);
		}
		else if(document.attachEvent)	{
			window.attachEvent("onload", gallery.getImages);
		}
		else	{
			alert("This browser does not support Gallery.js");	
		}
		
	},
	
	windowWidth: function()	{
		return document.documentElement.offsetWidth;
	},
		
	windowHeight: function()	{
		if(window.innerHeight)	{
      var windowHeight = (window.innerHeight + window.scrollMaxY) || document.documentElement.offsetHeight;
		}
		else if(document.body.clientHeight)	{
			var windowHeight = document.body.scrollHeight;	
		}
		return windowHeight;
	},
	
	getImages: function()	{
	
		for(var b = 0; b < gallery.divs.length; b++)	{
			for(var a = 0; a < gallery.divs[b].getElementsByTagName("img").length; a++)	{
				
				var image = gallery.divs[b].getElementsByTagName("img")[a];
				
				var bigImage = new Image();
				
				if(image.alt != "")	{
					bigImage.src = image.alt;
					bigImage.alt = bigImage.src;
					bigImage.title = image.title;
					image.alt = bigImage.src;
				}
				else	{
					bigImage.src = image.src;
					bigImage.alt = bigImage.src;
					bigImage.title = image.title;
					image.alt = bigImage.src;					
				}
				
				image.onclick = function()	{
					gallery.showGallery(this.title);	
				}
				
				gallery.bigImages[gallery.bigImages.length] = bigImage;
				gallery.smallImages[gallery.smallImages.length] = image;
					
			}
		
		}
		
		gallery.createShowcase();
		
	},
	
	createShowcase: function()	{
		
		gallery.gallery = document.createElement("div");
		gallery.background = document.createElement("div");
		gallery.frame = document.createElement("div");
		gallery.showcase = document.createElement("div");
		gallery.image = document.createElement("img");
		
		gallery.gallery.id = "sc_gallery";
		gallery.gallery.onclick = gallery.hideGallery;
		gallery.background.id = "sc_gallery_background";
		gallery.frame.id = "sc_gallery_frame";
		gallery.showcase.id = "sc_gallery_showcase";
		gallery.image.onclick = function(event)	{
			if(typeof(event) == "undefined")	{
				event = window.event;
			}
			try	{
				event.stopPropagation();	
			}
			catch(e)	{
				event.cancelBubble = true;	
			}
		};
		
		// NAVIGATION
		gallery.imageTitle = document.createElement("p");
		gallery.closeLink = document.createElement("a");
		gallery.navigation = document.createElement("div");
		gallery.picList = document.createElement("p");
		gallery.nextLink = document.createElement("a");
		gallery.prevLink = document.createElement("a");
		
		gallery.imageTitle.id = "sc_gallery_title";
		gallery.navigation.id = "sc_gallery_navigation";
		gallery.closeLink.id = "sc_gallery_close";
		gallery.nextLink.id = "sc_gallery_next";
		gallery.prevLink.id = "sc_gallery_prev";
		
		if(document.addEventListener)	{
			gallery.nextLink.addEventListener("click", gallery.seeNext, false);
			gallery.prevLink.addEventListener("click", gallery.seePrevious, false);
		}
		else if(document.attachEvent)	{
			gallery.nextLink.attachEvent("onclick", gallery.seeNext);
			gallery.prevLink.attachEvent("onclick", gallery.seePrevious);
		}
		
		var closeText = document.createTextNode("Close");
		var nextText = document.createTextNode("Next");
		var prevText = document.createTextNode("Prev");
				
		// APPEND CHILD
		gallery.closeLink.appendChild(closeText);
		gallery.nextLink.appendChild(nextText);
		gallery.prevLink.appendChild(prevText);
		
		gallery.navigation.appendChild(gallery.prevLink);
		gallery.navigation.appendChild(gallery.picList);
		gallery.navigation.appendChild(gallery.nextLink);
		var end = gallery.bigImages.length;
		for(var a = 0; a < end; a++)	{
			var aNode = document.createElement("a");
			var textNode = document.createTextNode((a+1));
			aNode.onclick = function(event)	{
				if(!event)
					event = window.event;
				try	{
					event.stopPropagation();	
				}
				catch(e)	{
					event.cancelBubble = true;	
				}
				gallery.seeOne(gallery.bigImages[parseInt(this.innerHTML) - 1].title);
			}
			
			aNode.appendChild(textNode);
			gallery.picList.appendChild(aNode);
		}
		// do not display title with popup window
		//gallery.showcase.appendChild(gallery.imageTitle);
		gallery.showcase.appendChild(gallery.closeLink);
		gallery.showcase.appendChild(gallery.image);
		gallery.showcase.appendChild(gallery.navigation);
		
		gallery.frame.appendChild(gallery.showcase);
		gallery.gallery.appendChild(gallery.background);
		gallery.gallery.appendChild(gallery.frame);
		document.body.appendChild(gallery.gallery);
		
		gallery.styleShowcase();
		
	},
	
	styleShowcase: function()	{
		
		if(navigator.appName == "Microsoft Internet Explorer")	{
			gallery.gallery.style.position = "absolute";
		}
		else	{
			gallery.gallery.style.position = "absolute";
		}
		gallery.gallery.style.top = 0;
		gallery.gallery.style.left = 0;
		gallery.gallery.style.display = "none";
		gallery.gallery.style.zIndex = 1000000;
		gallery.gallery.className = gallery.className;
		
		gallery.background.style.width = gallery.windowWidth()+"px";
		gallery.background.style.height = gallery.windowHeight()+"px";
		
		gallery.frame.style.position = "absolute";
		gallery.frame.style.top = 0;
		gallery.frame.style.left = 0;
		gallery.frame.style.width = gallery.windowWidth()+"px";
		gallery.frame.style.height = "0px";
		
		gallery.showcase.style.position = "relative";
		gallery.showcase.style.margin = "auto";
		gallery.showcase.style.marginTop = "40px";
		
	},
	
	showGallery: function(title)	{
		
		for(var a = 0; a < gallery.bigImages.length; a++)	{
			if(gallery.bigImages[a].title == title)	{
				var bigImage = gallery.bigImages[a];
			}
		}
		try	{
			if(bigImage.width == 0 || bigImage.height == 0)	{
				return;	
			}
		}
		catch(e) {}
		
		gallery.styleShowcase();
		
		gallery.intervalIndex = 0;
		gallery.gallery.style.display = "block";
		try	{
			gallery.imageTitle.innerHTML = bigImage.title;

		gallery.image.src = bigImage.src;
		gallery.image.width = bigImage.width;
		gallery.image.height = bigImage.height;
		gallery.image.title = bigImage.title;
		gallery.showcase.style.width = bigImage.width+"px";
		gallery.showcase.style.height = bigImage.height+"px";
}
		catch(e) {}


		
		var end = gallery.bigImages.length;
		while(end--)	{
			if(gallery.bigImages[end].src == gallery.image.src)
				gallery.picList.getElementsByTagName("a")[end].className = "picSelected";
			else
				gallery.picList.getElementsByTagName("a")[end].className = "";
		}
		
		gallery.showcase.style.position = 'absolute';
		gallery.showcase.style.left = (document.documentElement.offsetWidth / 2) - (gallery.showcase.offsetWidth / 2) + 'px';
		
		gallery.showGalleryInterval();
		
	},
	
	showGalleryInterval: function()	{
		
		gallery.intervalIndex++;
		gallery.gallery.className = gallery.className + " " + gallery.className + gallery.intervalIndex;
		if(gallery.intervalIndex < 10)	{
			gallery.showInterval = setTimeout("gallery.showGalleryInterval()", 40);
		}
		
	},
	
	hideGallery: function(event)	{
				
		if(typeof(gallery.showInterval) != "undefined")	{
			clearTimeout(gallery.showInterval);
		}
		gallery.hideGalleryInterval();
				
	},
	
	hideGalleryInterval: function()	{
		
		gallery.intervalIndex--;
		gallery.gallery.className = gallery.className + " " + gallery.className + gallery.intervalIndex;
		if(gallery.intervalIndex > 0)	{
			setTimeout("gallery.hideGalleryInterval()", 40);
		}
		else	{
			gallery.gallery.style.display = "none";
			gallery.gallery.className = gallery.className;
		}
				
	},
	
	seeNext: function(event)	{
		
		try	{
			event.stopPropagation();	
		}
		catch(e)	{
			event.cancelBubble = true;	
		}
		
		for(var a = 0; a < gallery.bigImages.length; a++)	{
			if(gallery.bigImages[a].title == gallery.image.title)	{
				if((a+1) == gallery.bigImages.length)	{
					gallery.nextImage = gallery.bigImages[0];	
				}
				else	{
					gallery.nextImage = gallery.bigImages[a+1];	
				}
			}
		}
		
		gallery.swapIntervalIndex = 0;
		gallery.currentWidth = gallery.image.width;
		gallery.currentHeight = gallery.image.height;
		gallery.widthPerInterval = Math.round((gallery.nextImage.width - gallery.image.width) / 10);
		gallery.heightPerInterval = Math.round((gallery.nextImage.height - gallery.image.height) / 10);
		
		gallery.swapImageInterval();
		gallery.image.src = gallery.nextImage.src;
		gallery.image.title = gallery.nextImage.title;
		gallery.imageTitle.innerHTML = gallery.nextImage.title;
		var end = gallery.bigImages.length;
		while(end--)	{
			if(gallery.bigImages[end].src == gallery.image.src)
				gallery.picList.getElementsByTagName("a")[end].className = "picSelected";
			else
				gallery.picList.getElementsByTagName("a")[end].className = "";
		}
		//gallery.image.style.opacity = 0;
		//gallery.image.style.filter = "alpha(opacity=0)";
		
	},
	
	seePrevious: function(event)	{
		
		try	{
			event.stopPropagation();	
		}
		catch(e)	{
			event.cancelBubble = true;	
		}
		
		for(var a = 0; a < gallery.bigImages.length; a++)	{
			if(gallery.bigImages[a].title == gallery.image.title)	{
				if(a == 0)	{
					gallery.nextImage = gallery.bigImages[gallery.bigImages.length-1];	
				}
				else	{
					gallery.nextImage = gallery.bigImages[a-1];	
				}
			}
		}
		
		gallery.swapIntervalIndex = 0;
		gallery.currentWidth = gallery.image.width;
		gallery.currentHeight = gallery.image.height;
		gallery.widthPerInterval = Math.round((gallery.nextImage.width - gallery.image.width) / 10);
		gallery.heightPerInterval = Math.round((gallery.nextImage.height - gallery.image.height) / 10);
		
		gallery.swapImageInterval();
		gallery.image.src = gallery.nextImage.src;
		gallery.image.title = gallery.nextImage.title;
		gallery.imageTitle.innerHTML = gallery.nextImage.title;
		var end = gallery.bigImages.length;
		while(end--)	{
			if(gallery.bigImages[end].src == gallery.image.src)
				gallery.picList.getElementsByTagName("a")[end].className = "picSelected";
			else
				gallery.picList.getElementsByTagName("a")[end].className = "";
		}
				
		//gallery.image.style.opacity = 0;
		//gallery.image.style.filter = "alpha(opacity=0)";
	
	},
	
	seeOne: function(which)	{
		
		for(var a = 0; a < gallery.bigImages.length; a++)	{
			if(gallery.bigImages[a].title == which)	{
				gallery.nextImage = gallery.bigImages[a];	
			}
		}
		
		gallery.swapIntervalIndex = 0;
		gallery.currentWidth = gallery.image.width;
		gallery.currentHeight = gallery.image.height;
		gallery.widthPerInterval = Math.round((gallery.nextImage.width - gallery.image.width) / 10);
		gallery.heightPerInterval = Math.round((gallery.nextImage.height - gallery.image.height) / 10);
		
		gallery.swapImageInterval();
		gallery.image.src = gallery.nextImage.src;
		gallery.image.title = gallery.nextImage.title;
		gallery.imageTitle.innerHTML = gallery.nextImage.title;
		var end = gallery.bigImages.length;
		while(end--)	{
			if(gallery.bigImages[end].src == gallery.image.src)
				gallery.picList.getElementsByTagName("a")[end].className = "picSelected";
			else
				gallery.picList.getElementsByTagName("a")[end].className = "";
		}
		
		//gallery.image.style.opacity = 0;
		//gallery.image.style.filter = "alpha(opacity=0)";
	
	},
	
	swapImageInterval: function()	{
		
		gallery.swapIntervalIndex++;
		gallery.currentWidth += gallery.widthPerInterval;
		gallery.currentHeight += gallery.heightPerInterval;
		gallery.image.width = gallery.currentWidth;
		gallery.image.height = gallery.currentHeight;
		//gallery.image.style.opacity = gallery.swapIntervalIndex / 10;
		//gallery.image.style.filter = "alpha(opacity="+gallery.swapIntervalIndex * 10+")";
		gallery.showcase.style.width = gallery.currentWidth+"px";
		gallery.showcase.style.height = gallery.currentHeight+"px";
		
		if(gallery.swapIntervalIndex < 10)	{
			setTimeout("gallery.swapImageInterval()", 40);
		}
		
	}
	
}