/*
systym: the NIH-syndrome javascript library
copyright (c) 2009 Chris Rose-Mathew
*/

//===================================================
// class: Gallery
//===================================================
// Expected Parameters:
// 0: Text id of gallery element (usually div)
// 1-n: Text url of full-size images
//===================================================
function Gallery(containerElement)
{
	if (!containerElement)
	{
		alert("ERROR - no container DOM element passed to Gallery creation function");
		return;
	}
	this.container = containerElement;
	
	this.element = document.createElement("div");
	if (!this.element)
	{
		alert("ERROR - Failed to create gallery element within container element");
		return;
	}
	this.container.appendChild(this.element);
	
	//=======================================
	// Gallery::viewImage()
	//=======================================
	// This function is the event handler for clicks on gallery images.
	// Of course, when run as such, "this" does not refer to the owning
	// Gallery object.
	//=======================================
	this.viewImage = function(e)
	{
		var clickedElement = getEventElement(e, true);
		//alert("Click on image <" + clickedElement.nodeName + ">");
		
		// Find the link (a) element containing the image
		var linkElement = null;
		if (clickedElement.nodeName == "IMG")
		{
			// onclick event handler was attached to thumbnail img within link
			linkElement = clickedElement.parentNode;
			//alert("linkelement: <" + linkElement.nodeName + ">");
		}
		else if (clickedElement.nodeName == "A")
		{
			// onclick event handler was attached to link containing thumbnail img
			linkElement = clickedElement;
			//alert("linkelement: <" + linkElement.nodeName + ">");
		}
		else if (clickedElement.nodeName == "P")
		{
			// onclick event handler was attached to paragraph containing link and thumbnail image
			linkElement = clickedElement.firstChild;
			//alert("linkelement: <" + linkElement.nodeName + ">");
		}
		else
		{
			alert("Image gallery onclick function attached to wrong element.");
			if (scene.currentPopupImage)
				delete scene.currentPopupImage;
			return false;
		}
		//alert("Click on image <" + linkElement.href + ">");
		
		// Create the Sprite for the image popup
		// Initially it is not attached to the document.
		if (scene.currentPopupImage)
			delete scene.currentPopupImage;
		scene.currentPopupImage = new Sprite();
		scene.currentPopupImage.deferGeometryChanges = false;
		scene.currentPopupImage.absolute();
		
		// choose a size and position for the popup base on specified image size -
		// either "wide" or "tall"
		var linkPrefix = linkElement.href;
		if (!linkPrefix)
		{
			delete scene.currentPopupImage;
			return false;
		}
		var imageSize = linkPrefix.substring(linkPrefix.length - 8, linkPrefix.length);
		if (imageSize == "portrait")
		{
			scene.currentPopupImage.size(265,435);
			scene.currentPopupImage.position((scene.viewport.width / 2) - 125, 320, 21);
		}
		else if (imageSize == "andscape")
		{
			scene.currentPopupImage.size(624,435);
			scene.currentPopupImage.position((scene.viewport.width / 2) - 302, 320, 21);
		}
		else
		{
			scene.currentPopupImage.size(230,385);
			scene.currentPopupImage.position((scene.viewport.width / 2) - 125, 320, 21);
		}
		
		// centered
		// scene.currentPopupImage.position((scene.viewport.width / 2) - 302, (scene.viewport.height / 2) - 201, 21);
		
		scene.currentPopupImage.setOpacity(0.04);
		scene.currentPopupImage.useImage(linkElement.href + ".jpg");
		scene.currentPopupImage.element.style.overflow = "hidden";
		scene.currentPopupImage.element.style.textAlign = "center";
		scene.currentPopupImage.element.style.padding = "10px";
		scene.currentPopupImage.backgroundColor("#999999");
		scene.currentPopupImage.element.className = "galleryImagePopup";
		
		// Use scene.blackout for lightbox-style effect
		scene.blackout.setOpacity(0.01);
		scene.blackout.backgroundColor("#000000");
		scene.blackout.element.style.display = "block";
		
		// Add annotations to image
		var annotationElement = document.createElement("p");
		var annotationText = document.createTextNode("--== Black Orchid Burlesque ==--");
		annotationElement.appendChild(annotationText);
		
		var annotationCloseLink = document.createElement("a");
		annotationCloseLink.href="javascript:closePopup()";
		var annotationCloseLinkText = document.createTextNode("[close]");
		annotationCloseLink.appendChild(annotationCloseLinkText);
		
		// Attach annotation and close link to image popup
		scene.currentPopupImage.element.appendChild(annotationElement);
		scene.currentPopupImage.element.appendChild(annotationCloseLink);
		
		// var bodyElement = document.getElementById("theBody");
		var bodyElement = getBodyElement();
		if (!bodyElement)
		{
			alert("ERROR - Failed to get the document body element");
			return false;
		}
		
		// Attach the gallery image to the document
		bodyElement.appendChild(scene.currentPopupImage.element);
		scene.currentPopupImage.image.onclick = closePopup;
		scene.currentPopupImage.image.oncontextmenu = closePopup;
		
		// set up fade-in for popup background frame and image itself
		if (scene.blackoutTimer)
		{
			clearInterval(scene.blackoutTimer);
		}
		scene.blackoutTimer = setInterval(fadeInPopup, 25);
		
		// Return false to disable default onclick processing
		return false;
	}
	
	// Check for no images specified
	if (arguments.length <= 1)
	{
	
		while (this.element.firstChild)
			this.element.removeChild(this.element.firstChild);

		var panel = document.createElement("div");
		var para = document.createElement("p");
		var paraText = document.createTextNode("Coming Soon...");
		
		para.appendChild(paraText);
		panel.appendChild(para);
		this.element.appendChild(panel);
		
		panel.style.textAlign = "center";
	}
	else
	{
		// Build the gallery DOM elements from the list of image paths given to
		// the Gallery constructor
		this.imageList = new Array();
		this.imageUrlList = new Array();
		this.imageThumbUrlList = new Array();
		for (var i = 1; i < arguments.length; i++)
		{
			//alert(arguments[i]);
			this.imageList.push(arguments[i]);
			this.imageUrlList.push(arguments[i] + ".jpg");
			this.imageThumbUrlList.push(arguments[i] + "_thumb.jpg");
			
			// The following structure is built for each gallery thumbnail
			// * div class="imageContainer"
			//    * p
			//       * a onclick=this.viewImage
			//         * img 80px 80px thumburl
			//         * br
			//         * annotation text
			
			// Create imageContainer div
			var imageContainerElement = document.createElement("div");
			imageContainerElement.className = "imageContainer";
			
			// Create container paragraph that includes link, img and annotation
			var paragraphElement = document.createElement("p");
			paragraphElement.onclick = this.viewImage
			
			// Create link for each thumbnail
			var linkElement = document.createElement("a");
			linkElement.href = this.imageList[i-1];
			linkElement.rel="PhotoBox";
			// linkElement.onclick = this.viewImage;
			
			// Create thumbnail for each link
			var linkThumbElement = document.createElement("img");
			linkThumbElement.src = this.imageThumbUrlList[i-1];
			linkThumbElement.className = "galleryImagePopup";
			
			// Create link break between image thumb and annotation
			var lineBreakElement = document.createElement("br");
			
			// Create annotation
			var annotationElement = document.createTextNode("");
			
			// Build the structure out of the piece
			linkElement.appendChild(linkThumbElement);
			linkElement.appendChild(lineBreakElement);
			linkElement.appendChild(annotationElement);
			
			paragraphElement.appendChild(linkElement);
			imageContainerElement.appendChild(paragraphElement);
			
			this.element.appendChild(imageContainerElement);
			
			if (i % 4 == 0)
			{
				var galleryBreak1 = document.createElement("br");
				var galleryBreak2 = document.createElement("br");
				this.element.appendChild(galleryBreak1);
				this.element.appendChild(galleryBreak2);
			}
		}
	}
}

fadeInPopup = function()
{
	if (!scene.currentPopupImage)
		return;

	if (scene.blackout.opacity < 0.75)
	{
		if (scene.lowCPU)
			scene.blackout.setOpacity(scene.blackout.opacity + 0.2);
		else
			scene.blackout.setOpacity(scene.blackout.opacity + 0.1);
	}
			
	if (scene.lowCPU)
		scene.currentPopupImage.setOpacity(scene.currentPopupImage.opacity + 0.21);
	else
		scene.currentPopupImage.setOpacity(scene.currentPopupImage.opacity + 0.11);
	if (scene.currentPopupImage.opacity >= 1.0)
	{
		clearInterval(scene.blackoutTimer);
	}
}

closePopup = function(e)
{
	if (!scene.currentPopupImage)
		return;
	
	if (!e)
	{
		e = window.event;
	}
			
	var theBody = getBodyElement();
	theBody.removeChild(scene.currentPopupImage.element);
	delete scene.currentPopupImage;
	scene.currentPopupImage = null;
	
	// remove blackout effect
	scene.blackout.element.style.display = "none";
	
	if (e)
	{
		// only return false if actually called as event handler
		return false;
	}
}

//==============================================
// showGallery(galleryNumber)
//==============================================
/* Show an individual gallery, replacing the hidden gallery collection */
//==============================================
showGallery = function(galleryNumber)
{
	//return;
	
	
	// First we must hide the gallery collection thumbails
	var galleryCollectionElement = document.getElementById("galleryCollection");
	if (!galleryCollectionElement)
	{
		alert("ERROR - Failed  to find galleryCollection element");
		return;
	}
	// Hide the gallery collection
	galleryCollectionElement.style.display = "none";
	
	// Set up the selected gallery
	var galleryContainer = document.getElementById("tabPage3");
	if (!galleryContainer)
	{
		alert("ERROR - Failed  to find gallery container element (tabPage3)");
		return;
	}
	
	//==============================
	// Create gallery based on gallery number
	//==============================
	
	if (galleryNumber == -1)
	{
		scene.currentGallery = new Gallery(galleryContainer); // default "coming soon" gallery
	}
	else if (galleryNumber == 0) // Artistes/DivaDisaSta
	{
		scene.currentGallery = new Gallery(galleryContainer, 
			"images/Photos/Galleries/Artistes/DivaDisaStar/DivaDisaStar_portrait",
			"images/Photos/Galleries/Artistes/DivaDisaStar/DivaDisaStarChicagoXmas_landscape",
			"images/Photos/Galleries/Artistes/DivaDisaStar/MistressDisaStarr_portrait",
			"images/Photos/Galleries/Artistes/DivaDisaStar/DivaStarrz_portrait",
			"images/Photos/Galleries/Artistes/DivaDisaStar/DecadentDiva_landscape",
			"images/Photos/Galleries/Artistes/DivaDisaStar/BlackOrchidBurlesqueStar_portrait",
			"images/Photos/Galleries/Artistes/DivaDisaStar/NecromanticCircus_portrait",
			"images/Photos/Galleries/Artistes/DivaDisaStar/Metal4Africa_portrait",
			"images/Photos/Galleries/Artistes/DivaDisaStar/TheatreOfTemptations_portrait",
			"images/Photos/Galleries/Artistes/DivaDisaStar/DarkMistressOfTheCircus_portrait",
			"images/Photos/Galleries/Artistes/DivaDisaStar/CowGalDiva_portrait",
			"images/Photos/Galleries/Artistes/DivaDisaStar/Star_portrait"
			);
	}
	else if (galleryNumber == 1) // Artistes/ScarLitHearts
	{
		scene.currentGallery = new Gallery(galleryContainer, 
			"images/Photos/Galleries/Artistes/ScarLitHearts/ScarLitHearts_landscape",
			"images/Photos/Galleries/Artistes/ScarLitHearts/Timeless_portrait",
			"images/Photos/Galleries/Artistes/ScarLitHearts/BackStage_portrait",
			"images/Photos/Galleries/Artistes/ScarLitHearts/VegasDancer_portrait",
			"images/Photos/Galleries/Artistes/ScarLitHearts/MechanicalDoll_portrait",
			"images/Photos/Galleries/Artistes/ScarLitHearts/FrenchMaidInChicago_portrait",
			"images/Photos/Galleries/Artistes/ScarLitHearts/TipTop_portrait",
			"images/Photos/Galleries/Artistes/ScarLitHearts/FrenchPose_portrait",
			"images/Photos/Galleries/Artistes/ScarLitHearts/OffItComes_portrait",
			"images/Photos/Galleries/Artistes/ScarLitHearts/ScarLitHeartz_portrait",
			"images/Photos/Galleries/Artistes/ScarLitHearts/LivingDeadGirl_landscape"
		);
	}
	else if (galleryNumber == 3) // Artistes/MizzDevillinHell
	{
		scene.currentGallery = new Gallery(galleryContainer, 
			"images/Photos/Galleries/Artistes/MizzDevillinHell/MizzDevillinHell_small",
			"images/Photos/Galleries/Artistes/MizzDevillinHell/MizzDevillinHellMetal4Africa_portrait",
			"images/Photos/Galleries/Artistes/MizzDevillinHell/MizzDevillinHell_landscape",
			"images/Photos/Galleries/Artistes/MizzDevillinHell/WomensDay_portrait",
			"images/Photos/Galleries/Artistes/MizzDevillinHell/TamingTheBeast_portrait",
			"images/Photos/Galleries/Artistes/MizzDevillinHell/ShaBang_portrait",
			"images/Photos/Galleries/Artistes/MizzDevillinHell/MizzDevill_portrait",
			"images/Photos/Galleries/Artistes/MizzDevillinHell/InTheMoment_portrait",
			"images/Photos/Galleries/Artistes/MizzDevillinHell/WatchOut_portrait"
			);
	}
	else if (galleryNumber == 4) // General
	{
		// Create the gallery object
		scene.currentGallery = new Gallery(galleryContainer, 
			"images/Photos/Galleries/General/DivaDisaStar01_portrait",
			"images/Photos/Galleries/General/ProfileDivaDisaStar2_small",
			"images/Photos/Galleries/General/ProfileLadyMagnolia2_small",
			"images/Photos/Galleries/General/ProfileScarLitHearts_small",
			"images/Photos/Galleries/General/AtTheBar_portrait",
			"images/Photos/Galleries/General/Tall01_portrait"
		);						
	}
	else if (galleryNumber == 5) // 1_MagicAndMystery_2008
	{
		scene.currentGallery = new Gallery(galleryContainer, 
			"images/Photos/Galleries/Events/1_MagicAndMystery_2008/MagicAndMysteryInMayPoster_portrait",
			"images/Photos/Galleries/Events/1_MagicAndMystery_2008/Introductions_landscape",
			"images/Photos/Galleries/Events/1_MagicAndMystery_2008/MagicalVenue_landscape",
			"images/Photos/Galleries/Events/1_MagicAndMystery_2008/RasselDassel_portrait",
			"images/Photos/Galleries/Events/1_MagicAndMystery_2008/PoleMoves_landscape",
			"images/Photos/Galleries/Events/1_MagicAndMystery_2008/CharmingChairMoves_landscape",
			"images/Photos/Galleries/Events/1_MagicAndMystery_2008/AngiesLineup_portrait",
			"images/Photos/Galleries/Events/1_MagicAndMystery_2008/AngiesAtticCollection_landscape",
			"images/Photos/Galleries/Events/1_MagicAndMystery_2008/NowThatsMagic_landscape",
			"images/Photos/Galleries/Events/1_MagicAndMystery_2008/TheMysteriousNotKnot_portrait",
			"images/Photos/Galleries/Events/1_MagicAndMystery_2008/TheMagicMan_landscape",
			"images/Photos/Galleries/Events/1_MagicAndMystery_2008/Tada_landscape"
		);
	}
	else if (galleryNumber == 6) // 2_TheatreOfTemptations_2008
	{
		scene.currentGallery = new Gallery(galleryContainer, 
			"images/Photos/Galleries/Events/2_TheatreOfTemptations_2008/TheatreOfTemptationsPoster_portrait",
			"images/Photos/Galleries/Events/2_TheatreOfTemptations_2008/WelcomeToTheTheatre_landscape",
			"images/Photos/Galleries/Events/2_TheatreOfTemptations_2008/TemptatiousDelights_landscape",
			"images/Photos/Galleries/Events/2_TheatreOfTemptations_2008/ScarLitBurlesque_portrait",
			"images/Photos/Galleries/Events/2_TheatreOfTemptations_2008/DecadentDiva_landscape",
			"images/Photos/Galleries/Events/2_TheatreOfTemptations_2008/BirdOfParadise_portrait",
			"images/Photos/Galleries/Events/2_TheatreOfTemptations_2008/CanCan_landscape",
			"images/Photos/Galleries/Events/2_TheatreOfTemptations_2008/FlyingHigh_portrait",
			"images/Photos/Galleries/Events/2_TheatreOfTemptations_2008/ShooWow_portrait",
			"images/Photos/Galleries/Events/2_TheatreOfTemptations_2008/LadyWithTheVoice_landscape",
			"images/Photos/Galleries/Events/2_TheatreOfTemptations_2008/LightWorks_portrait",
			"images/Photos/Galleries/Events/2_TheatreOfTemptations_2008/TheDivaDisplay_landscape"
		);
	}
	else if (galleryNumber == 7) // 3_VivaLasVegas_2008
	{
		scene.currentGallery = new Gallery(galleryContainer, 
			"images/Photos/Galleries/Events/3_VivaLasVegas_2008/VivaLasVegasPoster_portrait",
			"images/Photos/Galleries/Events/3_VivaLasVegas_2008/CowGalDiva_portrait",
			"images/Photos/Galleries/Events/3_VivaLasVegas_2008/ScarLitMagic_landscape",
			"images/Photos/Galleries/Events/3_VivaLasVegas_2008/VegasGirls_landscape",
			"images/Photos/Galleries/Events/3_VivaLasVegas_2008/StarryFans_landscape",
			"images/Photos/Galleries/Events/3_VivaLasVegas_2008/OnTheMoney_landscape",
			"images/Photos/Galleries/Events/3_VivaLasVegas_2008/TopsAndNippleCaps_landscape"
		);
	}
	else if (galleryNumber == 8) // 4_ChristmasInChicago_2009
	{
		scene.currentGallery = new Gallery(galleryContainer, 
			"images/Photos/Galleries/Events/4_ChristmasInChicago_2009/ChristmasInChicagoPoster_portrait",
			"images/Photos/Galleries/Events/4_ChristmasInChicago_2009/GlitzAndGlamVenue_portrait",
			"images/Photos/Galleries/Events/4_ChristmasInChicago_2009/ChicagoTopsAndTails_portrait",
			"images/Photos/Galleries/Events/4_ChristmasInChicago_2009/DivaDisaStarFanDance_portrait",
			"images/Photos/Galleries/Events/4_ChristmasInChicago_2009/DidItMyWay_portrait",
			"images/Photos/Galleries/Events/4_ChristmasInChicago_2009/ShadowSeduction_portrait",
			"images/Photos/Galleries/Events/4_ChristmasInChicago_2009/JailBird_portrait",
			"images/Photos/Galleries/Events/4_ChristmasInChicago_2009/BangBang_landscape",
			"images/Photos/Galleries/Events/4_ChristmasInChicago_2009/ScarLitHeartsFrenchMaid_landscape",
			"images/Photos/Galleries/Events/4_ChristmasInChicago_2009/GracefulMoves_portrait",
			"images/Photos/Galleries/Events/4_ChristmasInChicago_2009/JessicaRabbit_portrait",
			"images/Photos/Galleries/Events/4_ChristmasInChicago_2009/TheDolls_landscape"
		);
	}
	else if (galleryNumber == 9) // Beasties
	{
		scene.currentGallery = new Gallery(galleryContainer, 
			"images/Photos/Galleries/Beasties/BeastiesLogo_portrait",
			"images/Photos/Galleries/Beasties/Starrlicious_landscape",
			"images/Photos/Galleries/Beasties/SwingThang_portrait",
			"images/Photos/Galleries/Beasties/MistressDisaStarr_portrait",
			"images/Photos/Galleries/Beasties/ScarLitHeartz_portrait",
			"images/Photos/Galleries/Beasties/MizzDevillinHell_landscape",
			"images/Photos/Galleries/Beasties/FlyingSparks_portrait",
			"images/Photos/Galleries/Beasties/MistressMoves_landscape",
			"images/Photos/Galleries/Beasties/TamingTheBeast_portrait",
			"images/Photos/Galleries/Beasties/MilitryMarch_landscape",
			"images/Photos/Galleries/Beasties/DevillinMotion_portrait",
			"images/Photos/Galleries/Beasties/BeastiesBurlesque_portrait"
		);
	}
	
	// Show the new gallery
	scene.currentGallery.element.style.display = "block";
	scene.currentGallery.element.className="galleryCollection";
}


