//Allow IE4 to work with us.
  	if(!document.getElementById && document.all)
	  document.getElementById = function(id) {return document.all[id];}

	
	//slideshow constructor
	function slideshow(objName){
		
		//author: jeremy.wray@bbc.co.uk
		//required properties (must be set by user)
		
		/*name of created slideshow object (enables multiple slideshows in one page)
		passed in createSlideShow constructor*/
		this.name = objName;
		//array of images used by slideshow
		this.images = [];
		//id of the image on the pgae
		this.img_tag_id = "";
		
		//optional/default properties
		
		//aray of transitions, may be set for each image or one for all, the default here is a blend	
		this.transitions = ["blendtrans(duration=1.0)"];
		//speed at which frames change in ms, this is the default:
		this.speed = 3000;
		//slideshow loops by default otherwise plays once
		this.loop = true;
		//path to images, to keep the images array above, shorter	
		this.image_path = "";
		
		//non-user properties
		
		//image array index
		this.int_index_image = 0;
		//tansition array index
		this.int_index_transition = 0;
		//error message string placeholder
		this.str_err = "";
		//boolean for filters support (currently IE only) 
		this.supportsFilters = false;		
		//reference to setInterval
		this.interval_ref = "";
		
		//methods		
		
		//plays the slideshow forwards
		this.play = slideshowPlay;
		//plays the slideshow backwards
		this.reverse = slideshowReverse;
		//plays one frame forwards
		this.playOne = slideshowplayOne;
		//plays one frame backwards
		this.reverseOne = slideshowreverseOne;
		//plays one passed-in frame 
		this.playFrame = slideshowplayFrame;
		//stops the slideshow
		this.stop = slideshowStop;
		//handles transition of image/filter
		this.transition = slideshowTransition;
		//handles validation and error messages
		this.validation = slideshowValidation;
	}
	
	//slideshow methods
	
	//start slideshow playing
	function slideshowPlay(){
			
		//validate before attempting to play			
		if (! this.validation()){return false};
		//first stop running slideshow
		this.stop();
		//set playing
		this.interval_ref = setInterval(this.name+".transition('play')" , this.speed);		
	}
	
	//start slideshow playing in reverse
	function slideshowReverse(){	
			
		//validate before attempting to play			
		if (! this.validation()){return false};
		//first stop running slideshow
		this.stop();
		//set playing
		this.interval_ref = setInterval(this.name+".transition('playReverse')" , this.speed);		
	}
	
	//start slideshow playing
	function slideshowplayOne(){	
			
		//validate before attempting to play			
		if (! this.validation()){return false};
		//play one forward
		this.transition("play");		
	}
	
	//start slideshow playing in reverse
	function slideshowreverseOne(){		
	
		//validate before attempting to play			
		if (! this.validation()){return false};		
		//play one back
		this.transition("playReverse");		
	}
	
	//play frame passed in by user
	function slideshowplayFrame(intFrame){		
		
		//validate before attempting to play			
		if (! this.validation()){return false};	
		//set image index
		this.int_index_image  = intFrame;
		//set transition index
		this.int_index_transition = intFrame;
		//play passed-in frame
		this.transition("frame");		
	}
	
	//stop slideshow playing
	function slideshowStop(){
	
		//clears interval if it exists
		this.interval_ref ? clearInterval(this.interval_ref) : '';
	}
	
	//end slideshow methods
	
	//this changes the image and applies the transition
	function slideshowTransition(dir){
		
		switch(dir){
		
		case "play": 
								
			//if not set to loop, stop show after it reaches end of show
			if (! this.loop){
				if (this.int_index_image >= this.images.length -1){
					//stop show
					this.stop();
					//exit function
					return;
					}
			}	
			
			//increment image index	
			this.int_index_image ++;
			//increment transition index
			this.int_index_transition ++;
					
			//if at end of images array, reset index
			if (this.int_index_image == this.images.length){				
				//reset image index
				this.int_index_image = 0;
			}
			//if at end of transitions array, reset index
			if (this.int_index_transition == this.transitions.length){
				//reset transition index
				this.int_index_transition = 0;
			}
						
			break;
			
		case "playReverse":
		
			//if not set to loop, stop show after it reaches end of show
			if (! this.loop){
				if (this.int_index_image <= 0){
					//stop show
					this.stop();
					//exit function
					return;
					}
			}
		
			//decrement image index	
			this.int_index_image --;
			//decrement tansition index	
			this.int_index_transition --;			
						
			//if beyond beginning (ie. playing backwards) of images array, reset counter
			if (this.int_index_image < 0){
				//reset image frame
				this.int_index_image = this.images.length -1;
			}
			//if at end of transitions array, reset index
			if (this.int_index_transition < 0){
				this.int_index_transition = this.transitions.length -1;
			}
			
			break;
			
		case "frame":	
				
			//check we have the image passed-in
			if (typeof this.images[this.int_index_image] == "undefined"){
				this.str_err += "Selected frame is out of range. ";
				this.validation();
				return false;
			}
			//check we have relevant transition
			if (typeof this.transitions[this.int_index_transition] == "undefined"){
				//if not set index to first transition
				 this.int_index_transition = 0;
			}			
			
			break;
		default: //otherwise
			
			break;
		}		
			
		//perform platform specific transitions/swaps
		
		if (this.supportsFilters){	
			document.getElementById(this.img_tag_id).style.filter = this.transitions[this.int_index_transition];
			document.getElementById(this.img_tag_id).filters.item(0).apply();
			document.getElementById(this.img_tag_id).src = this.image_path + this.images[this.int_index_image];
			document.getElementById(this.img_tag_id).filters.item(0).play();
		}else{			
			document.images[this.img_tag_id].src = this.image_path + this.images[this.int_index_image];
		}			
		return true;
	}
	
	//fucntion to create slideshow object
	function createSlideShow(name){
		eval("window."+name+" = new slideshow('"+name+"');");
		return eval(name);
	}
	
	//validate and check for errors
	function slideshowValidation(){		
		//check for no image array or image tag id errors
		this.images.length < 2 ? this.str_err += "No images array defined or too few images in array. " : '';
		this.img_tag_id == "" ? this.str_err += "No image ID tag defined. " : '';
		
		//set properties for object support
		if (document.getElementById){		
			//check that image ID exists			
			if (document.getElementById(this.img_tag_id)){			
				////check that filters are supported
				if (typeof document.getElementById(this.img_tag_id).style.filter != "undefined"){
					this.supportsFilters = true;
				}
			}else{			
			//image ID error
			this.str_err += "No or incorrect ID defined in image tag or no image tag defined. ";
			}
		//no filters, but check image name exists
		}else if (! document.images[this.img_tag_id]){
		//image name error
		this.str_err += "No or incorrect NAME defined in image tag or no image tag defined. ";
		}		
		
		//show error alert if any
		if (this.str_err != ""){
			alert("Slideshow Error: " + this.str_err);
			this.stop();
		return false;
		}else{
			return true;
		}		
	}