// ---

/*

	JaS is developed by Robert Nyman, http://www.robertnyman.com

	For more information, please see http://www.robertnyman.com/jas

*/

var JaS = {

	// Customization parameters

	imagePath : "http://www.caymanbiodiversity.com/wp-content/themes/baplarge/pictures_birds/picturestanagers/",

	images : [
	
		["atanagorintro.jpg", "Tanagers, Bananabird, Orioles, Grackles, Starlings, Waxwing, Hummingbirds and similar", "Small, medium-small birds. Varied beak forms, from small triangular to more substantial,  elongate, with downward curve."],	

	

		["orioles_baltimoreoriole.jpg", "<a href=\"http://www.birds.cornell.edu/AllAboutBirds/BirdGuide/Baltimore_Oriole.html\" target=\"_blank\"><img src=\"http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif\" border=\"0\" align=\"right\" onMouseover=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logoclick.gif'\"  onMouseout=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif'\"></a>Baltimore Oriole (previously Northern Oriole)", "<i>Icterus galbula</i><br>Length 18-20cm (7-8\"). Very uncommon passage migrant and winter visitor to the Cayman Islands. Prefers edge habitat, dry forest,  coastal and urban areas."],
		
		["orioles_bobolink.jpg", "<a href=\"http://www.birds.cornell.edu/AllAboutBirds/BirdGuide/Bobolink.html\" target=\"_blank\"><img src=\"http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif\" border=\"0\" align=\"right\" onMouseover=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logoclick.gif'\"  onMouseout=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif'\"></a>Bobolink", "<i>Dolichonyx oryzivorus</i><br>Length 18.5cm (7.25\"). Fairly common passage migrant to the Cayman Islands. Prefers secondary swamps, mangrove, Typha wetlands, grasslands and agricultural land. The average distance travelled by a Boblink on migration is estimated at 20,000km -  that's half way around the world, every year."],
		
		["orioles_easternmeadowlark.jpg", "<a href=\"http://www.birds.cornell.edu/AllAboutBirds/BirdGuide/Eastern_Meadowlark.html\" target=\"_blank\"><img src=\"http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif\" border=\"0\" align=\"right\" onMouseover=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logoclick.gif'\"  onMouseout=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif'\"></a>Eastern Meadowlark", "<i>Sturnella magna	</i><br>Length 23cm (9\"). A very rare and unusual visitor (vagrant) to the Cayman Islands, known only from a single observation on Cayman Brac. The Eastern Meadowlark is not a true lark (family Alaudidae). It is a member of the family Icteridae, which includes blackbirds and orioles."],
		
		["orioles_greaterantilleangr.jpg", "Greater Antillean Grackle (Ching-ching, Blacksmith)", "<i>Quiscalus niger	</i><br>Length 25-30cm (10-12\"). Common breeding resident in Grand Cayman and Little Cayman. Extirpated from Cayman Brac: last record in 1911. Our local subspecies <i>Quiscalus niger caymanensis</i> is endemic to Grand Cayman, and <i>Quiscalus niger bangsi<i/> is endemic to Little Cayman. Both are found nowhere else in the world. Prefers coastal areas and mangrove, where it breeds and roosts in large colonies. Breeds March - July, producing one or two clutches of 3-4 eggs. Takes its local name from its metallic, chiming call. Will take insects, lizards, and the eggs and chicks of other birds. Gregarious and inquisitive, Ching-chings appear quite fearless, especially when there is food about. Ching-chings are aggressive in defence of their nests, and will attack intruders much larger than themselves, including humans!"],
		
		["jamaicanoriole.jpg", "Jamaican Oriole", "<i>Icterus leucopteryx bardiÊÊÊ</i><br>Length 21cm (8\"). Last recorded 1965-1967 in George Town, the Jamaican Oriole is now extirpated in the Cayman Islands. Previously inhabiting woodland and gardens, the oriole was an endemic subspecies, Icterus leucopteryx bardi, unique to Cayman. Its loss underlines the fragility of our tiny island populations, and the importance of maintaining woodland areas and restoring native trees and vegetation to our gardens."],

		
		["hummingbirds_rubythroated.jpg", "<a href=\"http://www.birds.cornell.edu/AllAboutBirds/BirdGuide/Ruby-throated_Hummingbird.html\" target=\"_blank\"><img src=\"http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif\" border=\"0\" align=\"right\" onMouseover=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logoclick.gif'\"  onMouseout=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif'\"></a>Ruby-throated Hummingbird", "<i>Archilochus colubris		</i><br>Length 8-9.5cm (3-3.75\"). A very unusual visitor (vagrant) to the Cayman Islands. Known only from a couple of confirmed observations in Grand Cayman, including a long-staying individual in the Queen Elizabeth II Botanic Park. Reports of Hummingbirds are quite common in the Cayman Islands, but most observations turn out to be large, flower-feeding moths."],

		
		["orioles_shinycowbird.jpg", "<a href=\"http://www.birds.cornell.edu/AllAboutBirds/BirdGuide/Shiny_Cowbird.html\" target=\"_blank\"><img src=\"http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif\" border=\"0\" align=\"right\" onMouseover=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logoclick.gif'\"  onMouseout=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif'\"></a>Shiny Cowbird", "<i>Molothrus bonariensis	</i><br>Length 18-20cm (7-8\"). The status of the Shiny Cowbird in the Cayman Islands is currently unclear. There have been a couple of observations in Grand Cayman, in North Side, and it would appear likely that there has been at least one successful breeding attempt on island. The Shiny Cowbird has been expanding its range out of South America since 1900s, arriving in the United States in 1985. A nest parasite of other species, the spread of the Shiny Cowbird is a cause for serious conservation concern, and represents a serious threat to endangered local species. For this reason, the status of the Shiny Cowbird in the Cayman Islands will require careful monitoring and management, if our local birds are to be protected from this invasive species in the future."],
		
		["orioles_yelllowheadedblack.jpg", "<a href=\"http://www.birds.cornell.edu/AllAboutBirds/BirdGuide/Yellow-headed_Blackbird.html\" target=\"_blank\"><img src=\"http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif\" border=\"0\" align=\"right\" onMouseover=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logoclick.gif'\"  onMouseout=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif'\"></a>Yellow-headed Blackbird", "<i>Xanthocephalus xanthocephalus	</i><br>Length 21-28cm (8-11\"). A very rare and unusual visitor (vagrant) to the Cayman Islands, known only from a single observation on Grand Cayman."],
		
		["tanagers_bananaquit.jpg", "Bananaquit (Banana Bird)", "<i>Coereba flaveola	</i><br>Length 10-12.5cm (4-5\"). Common breeding resident in the Cayman Islands. Our local subspecies, <i>Coereba flaveola sharpei</i> is endemic: found only in the Cayman Islands and nowhere else in the world. Prefers shrubland, but forages and breeds in a variety of habitats, including urban areas. Probably our most common passerine, Bananabirds are a regular feature in Cayman\'s gardens. They are omnivorous, with a very broad diet, including insects, fruit, berries and nectar from flowers. Bananabirds breed throughout the year, and regularly tend to the construction of their small, round, side-entry nests. Their clutch of 2-3 young fledge after about 28 days."],
		
		
		
		["tanagers_cedarwaxwing.jpg", "<a href=\"http://www.birds.cornell.edu/AllAboutBirds/BirdGuide/Cedar_Waxwing.html\" target=\"_blank\"><img src=\"http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif\" border=\"0\" align=\"right\" onMouseover=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logoclick.gif'\"  onMouseout=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif'\"></a>Cedar Waxwing", "<i>Bombycilla cedrorum	</i><br>Length 18-18.5cm (7-7.25\"). Uncommon and irregular winter visitor and passage migrant to the Cayman Islands. Occasionally seen in flocks of up to sixty individuals, on Grand Cayman, often in coastal and urban areas. Observations appear to be linked to the availability of a good berry crop, especially Pepper Cinnamon <i>Canella winterana</i>. The Waxwing takes its name from its unusual wing appendages - the tips of the secondary flight feathers appearing to have been dipped in red wax."],
		
		["tanagers_europeanstarling.jpg", "<a href=\"http://www.birds.cornell.edu/AllAboutBirds/BirdGuide/European_Starling.html\" target=\"_blank\"><img src=\"http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif\" border=\"0\" align=\"right\" onMouseover=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logoclick.gif'\"  onMouseout=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif'\"></a>European Starling", "<i>Sturnus vulgaris	</i><br>Length 22cm (8.\"). A rare and unusual visitor (vagrant) to the Cayman Islands. Known only from a few observations on Cayman Brac and Little Cayman. European Starlings were brought to North America in the 1890s, by a group dedicated to introducing all the birds mentioned in the works of Shakespeare! From the original introduction of one hundred individuals, the population is now estimated at over 200 million. Like many species of birds, the plumage colouration of Starlings extends into the ultraviolet, beyond the spectrum of light visible to the human eye."],
		
		["tanagers_scarlettanager.jpg", "<a href=\"http://www.birds.cornell.edu/AllAboutBirds/BirdGuide/Scarlet_Tanager.html\" target=\"_blank\"><img src=\"http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif\" border=\"0\" align=\"right\" onMouseover=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logoclick.gif'\"  onMouseout=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif'\"></a>Scarlet Tanager", "<i>Piranga olivacea	</i><br>Length 18cm (7\"). Very uncommon winter visitor and passage migrant to the Cayman Islands, occasionally occurring in small flocks. Prefers dry forest, coastal and urban areas."],
		
		["tanagers_summertanager.jpg", "<a href=\"http://www.birds.cornell.edu/AllAboutBirds/BirdGuide/Summer_Tanager.html\" target=\"_blank\"><img src=\"http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif\" border=\"0\" align=\"right\" onMouseover=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logoclick.gif'\"  onMouseout=\"this.src='http://www.caymanbiodiversity.com/wp-content/themes/baplarge/logocornell.gif'\"></a>Summer Tanager", "<i>Piranga rubra	</i><br>Length 18-19.5cm (7-7.5\"). Uncommon winter visitor and passage migrant to the Cayman Islands, occasionally occurring in small flocks. Prefers dry forest, coastal and urban areas. Tanagers specialise in catching bees and wasps, rubbing the bodies on branches to remove the stinger, prior to eating them."],
		
		["tanagers_westernspindalis.jpg", "Western Spindalis (Bastard Cock, Sparrow, previously Stripe-headed Tanager)", "<i>Spindalis zena	</i><br>Length 15cm (5.75\"). Fairly common breeding resident on Grand Cayman. Absent from Cayman Brac and Little Cayman, though a single pair were present at Tarpon Lake until 1985. Our local subspecies, <i>Spindalis zena salvini</i> is endemic: found only in the Cayman Islands and nowhere else in the world. Prefers woodland and shrubland. Breeds April-August. Laying 2-3 spotted eggs, in a very small cup nest."]
		

				
	],

	fadeContainerId : "jas-container",

	imageContainerId : "jas-image",

	imageTextContainerId : "jas-image-text",

	previousLinkId : "previous-image",

	nextLinkId : "next-image",

	imageCounterId : "image-counter",

	startSlideShowId : "start-slideshow",

	stopSlideShowId : "stop-slideshow",	

	dimBackgroundOverlayId : "jas-dim-overlay",

	dimBackgroundId : "dim-background",

	noDimBackgroundId : "no-dim-background",

	thumbnailContainerId: "jas-thumbnails",

	tagsContainerId: "jas-tags",

	tagsSelectAllId: "jas-select-all-tags",

	useImageText : true,

	useThumbnails : true,

	allowDimmedBackground : true,

	automaticallyDimBackgroundWhenSlideShow : true,

	useTags : true,

	useKeyboardShortcuts : true,	

	useFadingIn : true,

	useFadingOut : true,

	useFadeWhenNotSlideshow : false,

	useFadeForSlideshow : true,

	useFadeAtInitialLoad : false,

	fadeIncrement : 0.1,	

	fadeInterval : 100, // Milliseconds	

	timeForSlideInSlideshow : 5000, // Milliseconds

	

	// JaS function parameters

	allImages : null,

	currentImages : null,

	fadeContainer : null,

	imageContainer : null,

	imageTextContainer : null,

	previousLink : null,

	nextLink : null,

	dimBackgroundOverlay : null,

	dimBackgroundLink : null,

	noDimBackgroundLink : null,

	dimmingActivated : false,

	imageCounter : null,

	startSlideShowLink : null,

	stopSlideShowLink : null,

	thumbnailContainer : null,

	thumbnailCollection : [],

	currentThumbnailSelected : null,

	tagsContainer : null,

	tagsSelectAll : null,

	tagsList : null,

	tags : [],

	tagsCheckboxes : [],

	selectAllTags : true,

	imageText : null,

	imageText : "",

	imageSource : "",

	imageIndex : 0,

	fadingIn : true,

	fadeLevel : 0,

	fadeEndLevel : 1,

	fadeTimer : null,

	hasOpacitySupport : false,

	useMSFilter : false,

	useMSCurrentStyle : false,

	slideshowIsSupported : false,

	slideshowIsPlaying : false,

	functionAfterFade : null,

	isInitialLoad : false,

	

	init : function (){

    	if($){

			this.fadeContainer = $(this.fadeContainerId);

			this.imageContainer = $(this.imageContainerId);

			this.slideshowIsSupported = this.fadeContainer && this.imageContainer;

			if(this.slideshowIsSupported){

				this.allImages = this.images;

				this.currentImages = this.images;

				if(this.useImageText){

					this.imageTextContainer = $(this.imageTextContainerId);

					if(!this.imageTextContainer){

						this.useImageText = false;

					}

				}

				this.hasOpacitySupport = typeof this.fadeContainer.style.filter != "undefined" || typeof this.fadeContainer.style.opacity != "undefined";

				this.useMSFilter = typeof this.fadeContainer.style.filter != "undefined";

				this.useMSCurrentStyle = typeof this.fadeContainer.currentStyle != "undefined";

				

				this.previousLink = $(this.previousLinkId);

				this.previousLink.onclick = JaS.previousLinkClick;

				this.nextLink = $(this.nextLinkId);

				this.nextLink.onclick = JaS.nextLinkClick;

				this.imageCounter = $(this.imageCounterId);

				this.startSlideShowLink = $(this.startSlideShowId);

				if(this.startSlideShowLink){

					this.startSlideShowLink.style.display = "inline";

				}

				this.startSlideShowLink.onclick = JaS.startSlideShowClick;

				this.stopSlideShowLink = $(this.stopSlideShowId);

				if(this.stopSlideShowLink){

					this.stopSlideShowLink.style.display = "none";

				}

				this.stopSlideShowLink.onclick = JaS.stopSlideshowClick;

				

				if(this.allowDimmedBackground){

					this.dimBackgroundOverlay = $(this.dimBackgroundOverlayId);

					this.dimBackgroundLink = $(this.dimBackgroundId);

					this.noDimBackgroundLink = $(this.noDimBackgroundId);

					if(this.dimBackgroundOverlay && this.dimBackgroundLink && this.noDimBackgroundLink){

						this.dimBackgroundLink.onclick = JaS.dimBackgroundClick;

						this.noDimBackgroundLink.onclick = JaS.noDimackgroundClick;

						this.noDimBackgroundLink.style.display = "none";

						this.dimmingActivated = true;

					}

				}

				

				if(this.useKeyboardShortcuts){

					document.onkeydown = JaS.documentKeyDown;

				}

				

				this.thumbnailContainer = $(this.thumbnailContainerId);

				if(this.useThumbnails && this.thumbnailContainer){

					this.createThumbnails();

				}

				

				this.tagsContainer = $(this.tagsContainerId);

				if(this.useTags && this.tagsContainer){

					this.tagsSelectAll = $(this.tagsSelectAllId);

					if(this.tagsSelectAll){

						this.tagsSelectAll.onclick = JaS.tagsSelectAllClick;

						this.createTagList();

					}

				}

				

				this.isInitialLoad = true;

				this.setImage();

				this.isInitialLoad = false;

			}

		}

	},

	

	previousLinkClick : function(oEvent){

		var oEvent = (typeof oEvent != "undefined")? oEvent : event;

		JaS.preventDefaultEventBehavior(oEvent);

		JaS.previousImage();

	},

	

	nextLinkClick : function(oEvent){

		var oEvent = (typeof oEvent != "undefined")? oEvent : event;

		JaS.preventDefaultEventBehavior(oEvent);

		JaS.nextImage();

	},

	

	startSlideShowClick : function(oEvent){

		var oEvent = (typeof oEvent != "undefined")? oEvent : event;

		JaS.preventDefaultEventBehavior(oEvent);

		JaS.startSlideshow();

	},

	

	stopSlideshowClick : function(oEvent){

		var oEvent = (typeof oEvent != "undefined")? oEvent : event;

		JaS.preventDefaultEventBehavior(oEvent);

		JaS.stopSlideshow();

	},

	

	dimBackgroundClick : function(oEvent){

		var oEvent = (typeof oEvent != "undefined")? oEvent : event;

		JaS.preventDefaultEventBehavior(oEvent);

		JaS.dimBackground();

	},

	

	documentKeyDown : function(oEvent){

		var oEvent = (typeof oEvent != "undefined")? oEvent : event;

		JaS.applyKeyboardNavigation(oEvent);

	},

	

	tagsSelectAllClick : function (oEvent){

		JaS.tagsSelectAll = this.checked;

		JaS.markAllTags();

	},

	

	noDimackgroundClick : function(oEvent){

		var oEvent = (typeof oEvent != "undefined")? oEvent : event;

		JaS.preventDefaultEventBehavior(oEvent);

		JaS.noDimBackground();

	},

	

	setImage : function (){

		if(this.currentImages.length > 0){

			this.imageContainer.style.visibility = "visible";

			this.imageSource = this.currentImages[this.imageIndex][0];

			//ahmad edit to render description

			if(typeof(this.currentImages[this.imageIndex][2]) == "undefined"){
                this.currentImages[this.imageIndex][2] = "";
            }

			this.imageText = "<b>" + this.currentImages[this.imageIndex][1] + "</b><br><br>" + this.currentImages[this.imageIndex][2];

			if(this.useFadingOut && (this.slideshowIsPlaying && this.useFadeForSlideshow) || (!this.slideshowIsPlaying && this.useFadeWhenNotSlideshow) && (this.useFadeAtInitialLoad && this.isInitialLoad || !this.isInitialLoad)){

				this.fadeOut();

			}

			else{

				this.displayImageCount();

				this.imageContainer.setAttribute("src", (this.imagePath + this.imageSource));

				this.setImageText();

				this.previousLink.style.visibility = (this.imageIndex > 0)? "visible" : "hidden";

				this.nextLink.style.visibility = (this.imageIndex < (this.currentImages.length - 1))? "visible" : "hidden";

				if((this.useFadeAtInitialLoad && this.isInitialLoad || !this.isInitialLoad) && ((this.slideshowIsPlaying && this.useFadeForSlideshow) || (!this.slideshowIsPlaying && this.useFadeWhenNotSlideshow))){

					this.fadeIn();

				}

			}

			if(this.useThumbnails){

				this.markCurrentThumbnail();

			}

		}

		else{

			this.imageSource = "";

			this.imageText = "";

			this.displayImageCount();

			this.imageContainer.style.visibility = "hidden";

			this.setImageText();

		}

	},

	

	displayImageCount : function (){

    	if(this.imageCounter){

			this.imageCounter.innerHTML = (((this.currentImages.length > 0)? this.imageIndex : -1) + 1) + " / " + this.currentImages.length;

		}

	},

	

	nextImage : function (){

		if(this.imageIndex < (this.currentImages.length - 1)){

			++this.imageIndex;

			this.setImage();

		}

		else if(this.slideshowIsPlaying){

			this.stopSlideshow();

			this.imageIndex = 0;

			this.setImage();

		}

	},

	

	previousImage : function (){

		if(this.imageIndex > 0){

			--this.imageIndex;

			this.setImage();

		}         

	},



	setImageText : function (){

    	if(this.useImageText && typeof this.imageText == "string"){

			this.imageTextContainer.innerHTML = this.imageText;

		}

	},

	

	setDimBackgroundSize : function(){

         var oDimBackground = this.dimBackgroundOverlay.style;

         var intWidth = document.body.offsetWidth;

         var intXScroll = (typeof window.pageXOffset != "undefined")? window.pageXOffset : document.body.scrollLeft;

         var intHeight = (typeof window.innerHeight != "undefined")? window.innerHeight : (document.documentElement)? document.documentElement.clientHeight : document.body.clientHeight;

         var intYScroll = (typeof window.window.pageYOffset != "undefined")? window.window.pageYOffset : (document.documentElement)? document.documentElement.scrollTop : document.documentElement.scrollTop;

         oDimBackground.width = intWidth + intXScroll + "px";

         oDimBackground.height = intHeight + intYScroll + "px";

	},

	

	dimBackground : function (){

         this.setDimBackgroundSize();

         this.dimBackgroundOverlay.style.display = "block";

		 this.noDimBackgroundLink.style.display = "inline";		

	},

	

	noDimBackground : function (fromStopSlideshow){

		this.dimBackgroundOverlay.style.display = "none";

		this.noDimBackgroundLink.style.display = "none";

		if(!fromStopSlideshow){

			this.stopSlideshow();

		}

	},

		

	startSlideshow : function (){

		if(this.currentImages.length > 0){

			this.startSlideShowLink.style.display = "none";

			this.stopSlideShowLink.style.display = "inline";

			this.slideshowIsPlaying = true;

			this.fadeTimer = setTimeout("JaS.nextImage()", JaS.timeForSlideInSlideshow);

			if(this.dimmingActivated  && this.automaticallyDimBackgroundWhenSlideShow){

				this.dimBackground();

			}

		}

	},

	

	stopSlideshow : function (){

		if(this.currentImages.length > 0){

			this.startSlideShowLink.style.display = "inline";

			this.stopSlideShowLink.style.display = "none";

			this.slideshowIsPlaying = false;

			this.setFadeParams(false, 1, 0);

			this.setFade();

			clearTimeout(this.fadeTimer);

			if(this.dimmingActivated && this.automaticallyDimBackgroundWhenSlideShow){

				this.noDimBackground(true);

			}

		}

	},

	

	fadeIn : function (){

		this.setFadeParams(true, 0, 1);

		this.functionAfterFade = null;

		this.fade();

		if(this.slideshowIsPlaying){

			this.functionAfterFade = "this.startSlideshow()";

		}

	},

	

	fadeOut : function (){

		this.setFadeParams(false, 1, 0);

		this.functionAfterFade = "this.fadeOutDone()";

		this.fade();

	},

	

	fadeOutDone : function (){

        this.displayImageCount();

		this.imageContainer.setAttribute("src", (this.imagePath + this.imageSource));

		this.setImageText();

		if(this.useFadingIn){

			this.fadeIn();

		}

		else{

			this.fadeLevel = 1;

			this.setFade();

		}

	},

	

	fade : function (){

		if((this.fadingIn && this.fadeLevel < this.fadeEndLevel) || !this.fadingIn && this.fadeLevel > this.fadeEndLevel){

			this.fadeLevel = (this.fadingIn)? this.fadeLevel + this.fadeIncrement : this.fadeLevel - this.fadeIncrement;

			// This line is b/c of a floating point bug in JavaScript

			this.fadeLevel = Math.round(this.fadeLevel * 10) / 10;

			this.setFade();

			this.fadeTimer = setTimeout("JaS.fade()", this.fadeInterval);

		}

		else{

			clearTimeout(this.fadeTimer);

			if(this.functionAfterFade){

				eval(this.functionAfterFade);

			}

		}

	},

	

	setFade : function (){

		if(this.useMSFilter){

			this.fadeContainer.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (this.fadeLevel * 100) + ")";

		}

		else{

			this.fadeContainer.style.opacity = this.fadeLevel;

		}

	},

	

	setFadeParams : function (bFadingIn, intStartLevel, intEndLevel){

		this.fadingIn = bFadingIn;

		this.fadeLevel = intStartLevel;

		this.fadeEndLevel = intEndLevel;

	},

	

	createThumbnails : function (){

		this.thumbnailContainer.innerHTML = "";

		this.thumbnailCollection = [];

    	var oThumbnailsList = document.createElement("ul");

		var oListItem;

		var oThumbnail;

		var oCurrentImage;

		for(var i=0; i<this.currentImages.length; i++){

        	oCurrentImage = this.currentImages[i];

			oListItem = document.createElement("li");

			oThumbnail = document.createElement("img");

			oThumbnail.setAttribute("id", ("jas-thumbnail-" + i));

			oThumbnail.setAttribute("src", (this.imagePath + oCurrentImage[0]));

			oThumbnail.setAttribute("alt", oCurrentImage[1]);

			oThumbnail.setAttribute("title", oCurrentImage[1]);

			oThumbnail.onclick = JaS.thumbnailClick;

			this.thumbnailCollection.push(oThumbnail);

			oListItem.appendChild(oThumbnail);

			oThumbnailsList.appendChild(oListItem);			

        }

		this.thumbnailContainer.appendChild(oThumbnailsList);

		if(this.thumbnailCollection.length > 0){

			this.markCurrentThumbnail();

		}

		if(this.slideshowIsPlaying){

			this.stopSlideshow();

		}

	},

	

	thumbnailClick : function (oEvent){

		JaS.imageIndex = parseInt(this.getAttribute("id").replace(/\D*(\d+)$/, "$1"), 10);

		JaS.setImage();

	},

	

	markCurrentThumbnail : function (){

		if(this.currentThumbnailSelected){

	        this.currentThumbnailSelected.className = "";

			// Sometimes, in IE, the image loses its reference to its parent

			if(this.currentThumbnailSelected.parentNode){

				this.currentThumbnailSelected.parentNode.className = "";

			}

		}

		this.currentThumbnailSelected = this.thumbnailCollection[this.imageIndex];

		this.currentThumbnailSelected.className = "selected";

		this.currentThumbnailSelected.parentNode.className = "selected-parent";

	},

	

	createTagList : function (){

		var strCurrentTag;

		var arrCurrentTag;

		var oRegExp;

		for(var i=0; i<this.images.length; i++){

			arrCurrentTag = this.images[i][2].replace(/\s*(,)\s*/,  "$1").split(",");

			for(var j=0; j<arrCurrentTag.length; j++){

            	strCurrentTag = arrCurrentTag[j];

				oRegExp = new RegExp(strCurrentTag, "i");

				if(this.tags.toString().search(oRegExp) == -1){

					this.tags.push(strCurrentTag);

				}

            }

        }

		this.tagsList = document.createElement("ul");

		var oListItem;

		var oTagCheckbox;

		var oLabel;

		for(var k=0; k<this.tags.length; k++){

			oTag = this.tags[k];

			oListItem = document.createElement("li");

			oTagCheckbox = document.createElement("input");

			oTagCheckbox.setAttribute("type", "checkbox");

			oTagCheckbox.setAttribute("id", ("jas-" + oTag));

			oTagCheckbox.setAttribute("value", oTag);

			oTagCheckbox.checked = true;

			oTagCheckbox.onclick = JaS.tagCheckboxClick;

			oLabel = document.createElement("label");

			oLabel.setAttribute("for", ("jas-" + oTag));

			oLabel.innerHTML = oTag;

			this.tagsCheckboxes.push(oTagCheckbox);

			oListItem.appendChild(oTagCheckbox);

			oListItem.appendChild(oLabel);

			this.tagsList.appendChild(oListItem);

		}

		this.tagsContainer.appendChild(this.tagsList);

		// This loop is necessary since IE can only mark checkboxes as checked after they've been added to the document

		for(var l=0; l<this.tagsCheckboxes.length; l++){

			this.tagsCheckboxes[l].checked = true;		

		}

	},

	

	tagCheckboxClick : function (oEvent){

		JaS.applyTagFilter();

	},

	

	applyTagFilter : function (){

		this.currentImages = [];

		var arrCurrentTags = [];

		var oCheckbox;

		for(var i=0; i<this.tagsCheckboxes.length; i++){

        	oCheckbox = this.tagsCheckboxes[i];

			if(oCheckbox.checked){

				arrCurrentTags.push(oCheckbox.value);

			}

        }

		var oRegExp;

		var oImage;

		for(var j=0; j<this.images.length; j++){

        	oImage = this.images[j];

			for(var k=0; k<arrCurrentTags.length; k++){

				oRegExp = new RegExp(arrCurrentTags[k], "i");

				if(oImage[2].search(oRegExp) != -1){

					this.currentImages.push(oImage);

					break;

				}

			}

        }

		

		if(this.useThumbnails){

			this.createThumbnails();

		}

		this.imageIndex = 0;

		this.setImage();

	},

	

	markAllTags : function (){

		for(var i=0; i<this.tagsCheckboxes.length; i++){

			this.tagsCheckboxes[i].checked = this.tagsSelectAll;

        }

		this.applyTagFilter();

	},

	

	closeSession : function (oEvent){

		JaS = null;

		delete JaS;

	},

	

	applyKeyboardNavigation : function (oEvent){

    	var intKeyCode = oEvent.keyCode;

    	if(!oEvent.altKey){

			switch(intKeyCode){

				case 32:

					this.slideshowIsPlaying = (this.slideshowIsPlaying)? false : true;

					if(this.slideshowIsPlaying){

						this.startSlideshow();

					}

					else{

						this.stopSlideshow();

					}

					this.preventDefaultEventBehavior(oEvent);

					break;

				case 37:

				case 38:

					this.previousImage();

					this.preventDefaultEventBehavior(oEvent);

					break;

				case 39:

				case 40:

					this.nextImage();

					this.preventDefaultEventBehavior(oEvent);

					break;

			}

		}

	},

	

	preventDefaultEventBehavior : function (oEvent){

		if(oEvent){

			oEvent.returnValue = false;

			if(oEvent.preventDefault){

				oEvent.preventDefault();

			}

		}

	}

};

// ---

addEvent(window, "load", function(){JaS.init();}, false);

addEvent(window, "unload", function(){JaS.closeSession();}, false);

// ---

// Utility functions

function addEvent(oObject, strEvent, oFunction, bCapture){

	if(oObject){

		if(oObject.addEventListener){

			oObject.addEventListener(strEvent, oFunction, bCapture);

		}

		else if(window.attachEvent){

			oObject.attachEvent(("on" + strEvent), oFunction)

		}

	}

}

// ---

function $(strId){

	return document.getElementById(strId);

}

// ---

if(typeof Array.prototype.push != "function"){

	Array.prototype.push = ArrayPush;

	function ArrayPush(value){

		this[this.length] = value;

	}

}

// ---


