var JaS = {
	// Customization parameters
	imagePath : "pictures/",
	images : [ // Separate multiple tags by a comma
			  
		["Custom-Platform,-SO102168.png",			"Custom <br />None <br />Assemble <br />Automotive <br />102168", "Custom,Assemble,Automotive"],
		["Custom-Base,-SO101546.png",				"Custom <br />None <br />Process <br />Medical Device <br />101546", "Custom,Process,Medical Device"],
		//["Custom-Base,-SO101682.png",				"Custom <br />None <br />Process <br />Fiber Optic <br />101682", "Custom,Process,Fiber Optic"],
		//["Custom-Frame,-SO101079.png",				"Custom <br />None <br />Measure <br />Disk Drive <br />101079", "Custom,Measure,Disk Drive"],
		//["Custom-Frame,-SO101500.png",				"Custom <br />None <br />Test <br />Electronics <br />101500",  "Custom,Test,Electronics"],
		//["Custom-Frame,-SO101802.png",				"Custom <br />None <br />Process <br />Medical Device <br />101802", "Custom,Process,Medical Device"],
		//["Custom-Guard,-SO101548.png",				"Custom <br />None <br />Dispense <br />Medical <br />101548", "Custom,Dispense,Medical"],
		//["Custom,-Gantry-Weldment,-SO100950.png",	"Custom <br />None <br />Inspect <br />Semiconductor <br />100950", "Custom,Inspect,Semiconductor"],
		//["Custom,-SO101896.png", 					"Custom <br />None <br />Test <br />Packiging <br />101896", "Custom,Test,Packaging"],
		//["Custom-Base-SO101947.png",				"Custom <br />None <br />None <br />None <br />101947", "Custom"],
		//["Custom-Base,-SO101986.png",				"Custom <br />None <br />None <br />None <br />101986", "Custom"],
		
		["DS-Platform,-Modified,-SO102155.png", 	"Modified <br />Designer Series <br />None <br />None <br />102155 ","Modified,Designer Series"],
		
		["AS-Platform,-Modified,-SO101425.png", 		"Modified <br />Automation Series <br />Assemble <br />Autimotive <br />101425", "Modified,Automation Series,Assemble,Automotive"],
		//["AS-Platform,-Modified,-SO101851.png", 		"Modified <br />Automation Series <br />Test <br />Solar <br />101851", "Modified,Automation Series,Test,Solar"],
		//["AS-Base,-Modified,-SO101070.png", 			"Modified <br />Automation Series <br />Process <br />Life Sciences <br />101070", "Modified,Automation Series,Process,Life Sciences"],
		//["AS-Platform,-Modified,-SO101600.png", 		"Modified <br />Automation Series <br />Process <br />Electronics <br />101600 ","Modified,Automation Series,Process,Electronics"],
		//["AS-Platform,-Modified,-SO101615.png", 		"Modified <br />Automation Series <br />Dispense <br />Electronics <br />101615 ","Modified,Automation Series,Dispense,Electronics"],
		//["AS-Platform,-Modified,-SO101731.png", 		"Modified <br />Automation Series <br />Process <br />Fiber Optic <br />101731 ","Modified,Automation Series,Process,Fiber Optic"],
		//["AS-Platform,-Modified,-SO102013.png", 		"Modified <br />Automation Series <br />None <br />None <br />102013 ","Modified,Automation Series"],
		//["AS-Platform,-Modified,-w-Hepa-SO101640.png",	"Modified <br />Automation Series <br />None <br />None <br />101640 ","Modified,Automation Series"],
		//["AS-Base-with-Modified-Gussets-SO101866.png",	"Modified <br />Automation Series <br />None <br />None <br />101866", "Modified, Automation Series"],
		
		["PR-Platform,-Modified,-SO101187.png", 		"Modified <br />Production Series <br />Inspect <br />Semiconductor <br />101187", "Modified,Production Series,Inspect,Semiconductor"],
		//["PR-Platform,-Modified,-SO101983.png",			"Modified <br />Production Series <br />Dispense <br />Electronics <br />101983", "Modified,Production Series,Dispense,Electronics"],
		//["Laser-Platform-Modified-PR-SO100997.png",		"Modified <br />Production Series <br />Process <br />Aerospace <br />100997", "Modified,Production Series,Process,Aerospace"],
		//["PR-Platform,-Modified,-SO101784.png",			"Modified <br />Production Series <br />Assemble <br />Autimotive <br />101784", "Modified,Production Series,Assemble,Autimotive"],
		
		["IS-Base-SO102169.png", 					"Modified <br />Industrial Series <br />Assemble <br />Defence <br />102169 ","Modified,Industrial Series,Assemble,Defence"]
		//["IS-Base,-Modified,-SO101773.png", 		"Modified <br />Industrial Series <br />Assemble <br />Construction <br />101773 ","Modified,Industrial Series,Assemble,Construction"],
		//["IS-Base,-Modified,-SO102170.png", 		"Modified <br />Industrial Series <br />Process <br />Pharmaceutical <br />102170 ","Modified,Industrial Series,Process,Pharmaceutical"],
		//["IS-Platform,-Modified,-SO100107.png", 	"Modified <br />Industrial Series <br />Inspect <br />Consumer Products <br />100107 ","Modified,Industrial Series,Inspect,Consumer Products"],
		//["IS-Base,-Modified-SO101993.png",			"Modified <br />Industrial Series <br />None <br />None <br />101993 ","Modified,Industrial Series"]
	],
	fadeContainerId : "jas-container",
	imageContainerId : "jas-image",
	imageTextContainerId : "jas-image-text",
	imageCounterId : "image-counter",
	thumbnailContainerId: "jas-thumbnails",
	tagsContainerId: "jas-tags",
	tagsSelectAllId: "jas-select-all-tags",
	useImageText : true,
	useThumbnails : true,
	useTags : true,
	useFadingIn : true,
	useFadingOut : true,
	useFadeAtInitialLoad : true,
	fadeIncrement : 0.1,	
	fadeInterval : 100, // Milliseconds	
	
	// JaS function parameters
	allImages : null,
	currentImages : null,
	fadeContainer : null,
	imageContainer : null,
	imageTextContainer : null,
	imageCounter : 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,
	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.imageCounter = $(this.imageCounterId);
				
				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;
			}
		}
	},
	
	
	tagsSelectAllClick : function (oEvent){
		JaS.tagsSelectAll = this.checked;
		JaS.markAllTags();
	},
	
	
	setImage : function (){
		if(this.currentImages.length > 0){
			this.imageContainer.style.visibility = "visible";
			this.imageSource = this.currentImages[this.imageIndex][0];
			this.imageText = this.currentImages[this.imageIndex][1];
			if(this.useFadingOut && (this.useFadeAtInitialLoad && this.isInitialLoad || !this.isInitialLoad)){
				this.fadeOut();
			}
			else{
				this.displayImageCount();
				this.imageContainer.setAttribute("src", (this.imagePath + this.imageSource));
				this.setImageText();
				if(this.useFadeAtInitialLoad && this.isInitialLoad || !this.isInitialLoad){
					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;
		}
	},
	
	

	setImageText : function (){
		this.imageTextContainer.setAttribute("alt", this.imageText);
    	if(this.useImageText && typeof this.imageText == "string"){
			this.imageTextContainer.innerHTML = this.imageText;
		}
	},
	

	
	fadeIn : function (){
		this.setFadeParams(true, 0, 1);
		this.functionAfterFade = null;
		this.fade();
	},
	
	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();
		}
	},
	
	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;
	},
	
	
	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;
	}
}
// ---