//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 = [];		
	//path to images, to keep the images array above, shorter	
	this.image_path = "";
	//id of the image on the pgae
	this.img_tag_id = "";
	//array of links used by slideshow
	this.links = [];
	//path to links, to keep the links array above, shorter	
	this.link_path = "";
	//array of alt tags used by slideshow
	this.alt_tags = [];
	//possible random array
	this.arr_rand = [];
		
		
		
	//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 = 2000;
	//slideshow loops by default otherwise plays once
	this.loop = true;		
	
	//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.transition("playReverse");		
	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) : '';
}
	
//for randomising the output
function randomiseArray(arr){
	$arr_copy = new Array();
	for ($i in arr){
	    arr_copy[arr_copy.length] = arr[i];
	}	
	$arr_rand = new Array();	
	while (arr_copy.length > 0){		
		$int_random = Math.round(Math.random() * (arr_copy.length - 1));	
		arr_rand[arr_rand.length] = arr_copy[int_random];
		arr_copy[int_random] = arr_copy[arr_copy.length - 1];
		arr_copy.length = arr_copy.length - 1;
	}	
	return arr_rand;
}
	
//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;
	}//end switch	
	
	//perform platform specific transitions/swaps
			
	//do this once, if no random array passed in, create a non-random one (bit of a hack for randomness)
	if (! this.bln_rand){			
		if (this.arr_rand.length <= 0){			
			for ($i = 0; i < this.images.length; i ++){
				this.arr_rand[i] = this.arr_rand.length;
			}
		}			
		this.bln_rand = true;
	}
		
	if (this.supportsFilters){					
		//select transition
		document.getElementById(this.img_tag_id).style.filter = this.transitions[this.int_index_transition];
		//apply transition
		document.getElementById(this.img_tag_id).filters.item(0).apply();
		//swap image
		document.getElementById(this.img_tag_id).src = this.image_path + this.images[this.arr_rand[this.int_index_image]];			
		//play filter
		document.getElementById(this.img_tag_id).filters.item(0).play();				
	}else{	
		//swap image	
		document.images[this.img_tag_id].src = this.image_path + this.images[this.arr_rand[this.int_index_image]];			
	}	
	
	//call image change function
	if (this.onImageChange){
		this.onImageChange();
	}
	
	//do alt and title tag	
	if (this.alt_tags.length > 0){		
		document.getElementById(this.img_tag_id).alt = this.alt_tags[this.arr_rand[this.int_index_image]];
		document.getElementById(this.img_tag_id).title = this.alt_tags[this.arr_rand[this.int_index_image]];
	}		

	//apply link
	if (this.links.length > 0){		
		document.getElementById('slide_href').href = this.link_path + this.links[this.arr_rand[this.int_index_image]];	
	}		
				
}//end slideshowTransition
	
					
	
//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;
	}		
}