// ---
/*
	JaS is developed by Robert Nyman, http://www.robertnyman.com
	For more information, please see http://www.robertnyman.com/jas
		
	Insert link in legend using the following syntax: 				
	<a class=a1 target=_blank href='http://my link.dk'>Klik</a>				
	S.W. Rasmussen 22. November 2011
*/
 
var JaS = {
	// Customization parameters
	imagePath : "",
	images : [
["P1000514-1.jpg","Jeg ville have forsvoret, at et lille firbenet væsen i form af en hund kunne have så megen indflydelse på vores dagligdag. Men Stoffer har faktisk haft meget stor indflydelse på Ninas og min tilværelse: Vi kommer ud at gå ture, render rundt og kaster bolde og pinde - og nyder det, altsammen takket været Stoffer.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000514-1.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["img_00.jpg","Det hele startede med pasning af Futte i tre uger mens min kusine Bente og hendes mand Volker var på ferie.  Futte var en en Bishon Havanese hund. Nina og jeg har altid haft katte og havde ikke store forventning til Futte. Men vi blev klogere, meget klogere i de tre uger Futte boede hos os.<br><font style='font-size:9px'><i>FileInfo: </i><b>IMG_00.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000892.jpg","Resultatet blev, at Nina ledte på nettet efter en Futte-hund. De er ikke så almindelige, så vi måtte helt til Bredebro i Sønderjylland for at købe en Bichon Havanais.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000892.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000906.jpg","Så vi drog til Sønderjylland og hentede Futte-hunden, som blev døbt Stoffer efter vores sidste kat samt inspireret af Steen & Stoffer tegneserien, som vi begge holder meget af. Otte uger og fuld af nysgerrighed, og drive...<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000906.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000898.jpg","Vore naboer skulle jo vide hvad sådan en fætter havde kostet og var ved at falde om, da de hørte prisen. På Lolland plejer man vist ikke at betale så formidable summer for en hund. Men vi er jo Københavnere/tilflyttere, og folk fra København er jo ikke rigtig kloge i forvejen, så af dem kan man jo vente sig hvad som helst!. Vi lolikker vil jo helst have alting gratis...<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000898.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000902.jpg","Sjovt nok viste det sig, at også naboerne - efter de havde mødt og havde lært Stoffer at kende - begyndte at sige: Han var godt nok meget dyr - men er sandelig alle pengene værd. Og det er <i>meget</i> store ord i en lolliks mund: Både at indrømme en fejltagelse <i>og</i> at erkende, at det indimellem kan være en god ide, at ofre penge på ting, der ikke umiddelbart betaler sig...<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000902.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000903.jpg","For Nina og mig var det som at have fået et nyt barn som i stedet for bleen tissede på gulvet rundt omkring. Heldigvis er vore gulvtæpper ikke noget at skrive hjem om, så det var til at leve med.<br><br>Mere vigtigt var det, at alting efter kort tid kom til at dreje sig om Stoffer. Sammenlignet med vore katte fyldte Stoffer dagligdagen på en helt ny og anderledes måde.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000903.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000907.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000907.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000909.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000909.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000926.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000926.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000933.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000933.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000934.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000934.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000941.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000941.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000942.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000942.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000944.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000944.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010270.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010270.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010271.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010271.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010363.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010363.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010364.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010364.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010365.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010365.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010369.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010369.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010391.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010391.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000617.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000617.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000618.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000618.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000631.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000631.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1000638.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1000638.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010484.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010484.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010485.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010485.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010519.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010519.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010553.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010553.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010635.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010635.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010665.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010665.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1010671.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1010671.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1020876.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1020876.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1030008.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1030008.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1030454.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1030454.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1030462.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1030462.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1030636.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1030636.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1040393.jpg","Stoffer hund.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1040393.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1060251.jpg","Stoffers tilkomne: Trisse, ca. en måned gammel, februar 2010.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1060251.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1060255.jpg","Stoffers tilkomne: Trisse, ca. en måned gammel, februar 2010.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1060255.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""],
["P1060256.jpg","Stoffers tilkomne: Trisse, ca. en måned gammel, februar 2010.<br><font style='font-size:9px'><i>FileInfo: </i><b>P1060256.JPG</b><i>, 2011-01-10 17:32:25</i></font>", ""]
],
	fadewrapperId : "jas-wrapper",
	imagewrapperId : "jas-image",
	imageTextwrapperId : "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",
	thumbnailwrapperId: "jas-thumbnails",
	tagswrapperId: "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 : 1500, // Milliseconds	
	
	// JaS function parameters
	allImages : null,
	currentImages : null,
	fadewrapper : null,
	imagewrapper : null,
	imageTextwrapper : null,
	previousLink : null,
	nextLink : null,
	dimBackgroundOverlay : null,
	dimBackgroundLink : null,
	noDimBackgroundLink : null,
	dimmingActivated : false,
	imageCounter : null,
	startSlideShowLink : null,
	stopSlideShowLink : null,
	thumbnailwrapper : null,
	thumbnailCollection : [],
	currentThumbnailSelected : null,
	tagswrapper : 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.fadewrapper = $(this.fadewrapperId);
			this.imagewrapper = $(this.imagewrapperId);
			this.slideshowIsSupported = this.fadewrapper && this.imagewrapper;
			if(this.slideshowIsSupported){
				this.allImages = this.images;
				this.currentImages = this.images;
				if(this.useImageText){
					this.imageTextwrapper = $(this.imageTextwrapperId);
					if(!this.imageTextwrapper){
						this.useImageText = false;
					}
				}
				this.hasOpacitySupport = typeof this.fadewrapper.style.filter != "undefined" || typeof this.fadewrapper.style.opacity != "undefined";
				this.useMSFilter = typeof this.fadewrapper.style.filter != "undefined";
				this.useMSCurrentStyle = typeof this.fadewrapper.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.thumbnailwrapper = $(this.thumbnailwrapperId);
				if(this.useThumbnails && this.thumbnailwrapper){
					this.createThumbnails();
				}
				
				this.tagswrapper = $(this.tagswrapperId);
				if(this.useTags && this.tagswrapper){
					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.imagewrapper.style.visibility = "visible";
			this.imageSource = this.currentImages[this.imageIndex][0];
			this.imageText = this.currentImages[this.imageIndex][1];
			if(this.useFadingOut && (this.slideshowIsPlaying && this.useFadeForSlideshow) || (!this.slideshowIsPlaying && this.useFadeWhenNotSlideshow) && (this.useFadeAtInitialLoad && this.isInitialLoad || !this.isInitialLoad)){
				this.fadeOut();
			}
			else{
				this.displayImageCount();
				this.imagewrapper.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.imagewrapper.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 (){
		this.imageTextwrapper.setAttribute("alt", this.imageText);
    	if(this.useImageText && typeof this.imageText == "string"){
			this.imageTextwrapper.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.imagewrapper.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.fadewrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (this.fadeLevel * 100) + ")";
		}
		else{
			this.fadewrapper.style.opacity = this.fadeLevel;
		}
	},
	
	setFadeParams : function (bFadingIn, intStartLevel, intEndLevel){
		this.fadingIn = bFadingIn;
		this.fadeLevel = intStartLevel;
		this.fadeEndLevel = intEndLevel;
	},
	
	createThumbnails : function (){
		this.thumbnailwrapper.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.thumbnailwrapper.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.tagswrapper.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;
	}
}
// ---

