﻿function $(d){
	return document.getElementById(d);
}
function ToggleRotator() {
	if(Rotator.IsRunning()){
		Rotator.Stop();
	}else{
		Rotator.Start();
	}
} // ToggleRotator
function SetToggleRotatorText() {
	var element=$('RotatorToggle');
	if(Rotator.IsRunning()){
		element.innerHTML="Stop";
	}else{
		element.innerHTML="Start";
	}
} // SetToggleRotatorText

/* Image rotator / slideshow
---------------------------------------------------------------- */
var Rotator = function(){
	var imageArray=new Array();
	var imageElement=null,titleElement=null,textElement=null;
	var arrayIndex=-1,arrayLength=0;
	var timer=null;
	var changeTimeout=5000;
	var onRunningChangedCallBack=null;
	return {
		Add:function(src,title,text){
			imageArray[imageArray.length]={Src:src,Title:title,Text:text};
			arrayLength=imageArray.length;
		},
		Initialize:function(imageElementId,titleElementId,textElementId){
			imageElement=$(imageElementId);
			titleElement=$(titleElementId);
			textElement=$(textElementId);
			if(imageElement){				
				for(var i=0; i<arrayLength; i+=1){
					imageArray[i].ImageLoaded=false;
					imageArray[i].Image=new Image();
					imageArray[i].Image.onload=new function(){Rotator.setImageLoaded(i);}
					imageArray[i].Image.src=imageArray[i].Src;
				}
				Rotator.Start();
			}
		},
		setImageLoaded:function(index){
			imageArray[index].ImageLoaded=true;
		},
		getNextLoadedImageIndex:function(index){
		    if(typeof(index)=='undefined')index=arrayIndex;			
			if(++index>=arrayLength)index=0;
			
			if(arrayLength==0){
				index=-1;
			}else if(arrayLength==1){
				if(!imageArray[index].ImageLoaded)index=-1;
			}else{
				var lastIndex=index>0?index-1:arrayLength-1;
				while(index!=lastIndex){
					if(imageArray[index].ImageLoaded)break;
					if(++index>=arrayLength)index=0;
				}
				if(index==lastIndex)index=-1;
		    }
			return index;
		},
		getPrevLoadedImageIndex:function(index){
		    if(typeof(index)=='undefined')index=arrayIndex;			
			if(--index<0)index=arrayLength-1;
			
			if(arrayLength==0){
				index=-1;
			}else if(arrayLength==1){
				if(!imageArray[index].ImageLoaded)index=-1;
			}else{
				var lastIndex=index<=0?arrayLength-1:index-1;
				while(index!=lastIndex){
					if(imageArray[index].ImageLoaded)break;
					if(--index<0)index=arrayLength-1;
				}
				if(index==lastIndex)index=-1;
		    }
			return index;
		},
		SetTitle:function(index){
			if(titleElement){
				var title=imageArray[index].Title;
				if(typeof(title)!='string')title='';
				titleElement.innerHTML=title;
			}
		},
		SetText:function(index){
			if(textElement){
				var text=imageArray[index].Text;
				if(typeof(text)!='string')text='';
				textElement.innerHTML=text;
			}
		},
		Show:function(index){
			if(typeof(index)=='undefined')index=arrayIndex;
			else arrayIndex=index;
			if(index>=0 && index<arrayLength){
				Rotator.SetTitle(index);
				Rotator.SetText(index);
				imageElement.src=imageArray[index].Src;
				imageElement.alt=imageArray[index].Title;
		    }
		},
		Prev:function(callStart){
			if(typeof(callStart)=='undefined')callStart=false;
			if(arrayLength>0){
			    arrayIndex=Rotator.getPrevLoadedImageIndex();
			    if(arrayIndex>-1)Rotator.Show();
			    if(callStart)Rotator.Start(false);
				else Rotator.Stop();
			}
		},
		Next:function(callStart){
			if(typeof(callStart)=='undefined')callStart=false;
			if(arrayLength>0){
			    arrayIndex=Rotator.getNextLoadedImageIndex();
			    if(arrayIndex>-1)Rotator.Show();
			    if(callStart)Rotator.Start(false);
				else Rotator.Stop();
			}
		},
		Start:function(callEvent){
			if(typeof(callEvent)=='undefined')callEvent=true;

			if(arrayLength>0){
			    Rotator.Stop(false);
				
				if(arrayIndex==-1){
					Rotator.Next(false);
					timer=window.setTimeout("Rotator.Start(false)", 100);
				}else{
					if(callEvent)Rotator.Next(true);
					else timer=window.setTimeout("Rotator.Next(true)", changeTimeout);
				}
			}
			
			if(callEvent && typeof(onRunningChangedCallBack)=='function')onRunningChangedCallBack();
		},
		Stop:function(callEvent){
			if(typeof(callEvent)=='undefined')callEvent=true;

			if(timer){
				window.clearTimeout(timer);
				timer=null;
			}

			if(callEvent && typeof(onRunningChangedCallBack)=='function')onRunningChangedCallBack();
		},
		OnRunningChangedCallBack:function(func){
			onRunningChangedCallBack=func;
		},
		IsRunning:function(){
			return (timer!=null);
		}
	};
}();
var rotatorLoad=window.onload;
window.onload=function(e){if(typeof(rotatorLoad)=='function')rotatorLoad(e);Rotator.Initialize('RotatorImage', 'RotatorTitle', 'RotatorText');}
Rotator.OnRunningChangedCallBack(SetToggleRotatorText);
