var IE = /*@cc_on!@*/false;
var d=document, imgs = new Array(), links = new Array(), links2 = new Array(),links3 = new Array();
var timer,debugMode = false;
 


/*
 *
 */
var ImageRotator = Class.extend({   
     
     //did = divId 
    construct: function(HTMLElementId,JSNameIEOnly) { 
	 
		this.version 	= "2010.01.11";
		this.rSettingsArray			= new Array(10,25,50,75,100,0,0,0,0,0,0,5000,0,0,0,0,0);

		this.Name 				= JSNameIEOnly;  /* JSObject Name  */	 
		this.Id 				= HTMLElementId;  /* HTMLElement Id */
		
		this.Title 				= "Emergency ID Product Range";
		this.speed 				= 50;
		this.Interval 			= 2300; /*private please!! */

		this.Captions 			= false; //true;

		this.FirstRun			= true;
		this.paused				= false;
		this.current 			= 0;
		
		this.init();
		
		this.Motion 			= "Forward"; /* Backward */
		this.sInterval 			= this.rSettingsArray[9];
				
		$(this.Id).className	= "ImageRotatorClass";
	
		
	},
	
	init:function() 	{ if(!d.getElementById || !d.createElement)return; },
     	
	Id:function() 		{ return (this.Id); },    

	SetFast:function() 	{ this.speed = this.rSettingsArray[0]; },   
	
	SetNormal:function() 	{ this.speed = this.rSettingsArray[1]; },  

	SetMedium:function() { this.speed = this.rSettingsArray[2]; }, 	
	 
	SetSlow:function() 	{ this.speed = this.rSettingsArray[3]; },  
	
	SetSlowest:function() 	{ this.speed = this.rSettingsArray[4]; },  

	SetInterval:function(interval) {
	
		this.sInterval = interval;
		this.Interval 	= interval;
	},
	
	StartFrom:function(i) {
		this.current = i;
	
	},

	AddImage:function(filename,title,link,linkText) {

		var img=document.createElement("img")//create img element
	    img.src=filename; //src of img attribute
		
	    $(this.Id).appendChild(img);//append to body
		
		if(link) {
			links[links.length] = link; 
			links2[links2.length] = linkText; 
			links3[links3.length] = title; 
		}
		else  {
			links[links.length] = ""; 
			links2[links2.length] = ""; 
			links3[links3.length] = ""; 
		}		
	},
	
	AddImagesByFolder:function(path,count,title) {
		
		if(title==null) title = "";
		
		for(i=1;i<(count+1);i++) this.AddImage(path + "img" + i + ".jpg",title,"","");

	},
	
	start:function() {
 

		// this.speed 
		
		imgs = d.getElementById(this.Id).getElementsByTagName("img");
		for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
		
		imgs[0].style.display = "block";
		imgs[0].xOpacity = .99;
		
		document.getElementById(this.Id).className += " ImageRotatorClass";
		
		var fade = this.Name + ".Fade(" + this.Interval + ")" ;
		
		timer = setTimeout(fade,this.Interval);
		
		this.overlayStyle(this.Captions);
		
		$("IRTitle").innerHTML = this.Title;
	
		this.FirstRun = false;
	},
	
	Fade:function(interval,nextInterval,prev) {

	 
		cOpacity = imgs[this.current].xOpacity;
		
		if(prev==true) {
			nIndex = imgs[(this.current-1)]?this.current-1:imgs.length -1;
			 
			if(debugMode) status = "prev";
			this.Motion = "Backward";
				 
			 
		}
		else {
			nIndex = imgs[this.current+1]?this.current+1:0;status = "next";
			this.Motion = "Forward";
		}
		

		if(debugMode) {
			$("debug").innerHTML = this.current;
			$("debugnotes").innerHTML = status;
		}
		
		nOpacity = imgs[nIndex].xOpacity;
		
		//fade values
		cOpacity-=.01; nOpacity+=.01;
		
		imgs[nIndex].style.display = "block";
		imgs[this.current].xOpacity = cOpacity;
		imgs[nIndex].xOpacity = nOpacity;
		
		this.setOpacity(imgs[this.current]); 
		this.setOpacity(imgs[nIndex]);
		
		if(nextInterval==null) nextInterval = interval;
		
		var fade = this.Name + ".Fade(" + nextInterval + ","+nextInterval+"," +prev+ ")" ;
		
		if(cOpacity<=0) {
			imgs[this.current].style.display = "none";
			this.current = nIndex;
			
			timer = setTimeout(fade,interval);
		
		} else {
			timer = setTimeout(fade,this.speed); //slow 80 fast 10
		}
		
		$("alink").href = links[nIndex];
		$("alink").innerHTML = links2[nIndex];
		$("IRITitle").innerHTML = links3[nIndex];
		

	},
	
	
	overlayStyle:function(overlay) {

		var martop = 15;
		if(IE) martop = 0;
		
		if(overlay) {
			$("Overlay").style.marginTop = "210px";
			$("Overlay").style.height = "100px";
			$("OverlayText").style.marginTop = martop + "px";
			$("OverlayText").style.paddingTop = "5px";
			$("OverlayText").style.width =  $("Overlay").style.width;
			$("OverlayText").style.textAlign = "center";
			
			
			$("OverlayText").style.fontFamily = "verdana,arial";
		}
		else $("Overlay").style.display = "none";

	},
	
	
	setOpacity:function(obj) {


		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		
		
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";

	},

	next:function(interval) { this.stopAll(); this.Fade(100,interval); },
	prev:function(interval) { this.stopAll(); this.Fade(1000,interval,true); },
	
	/**
	 * pause/Resume
	 */
	pause:function(interval){ 
	
		if(this.paused) {
		
			//must resume
			if(this.Motion == "Forward")  
				this.next(this.sInterval);
			else 
				this.prev(this.sInterval);
				
			this.paused = false;
		}
		else {
		
			this.stopAll(); 
			this.paused = true;
		}

	
	},
	
	stopAll:function() 		{ if(timer) clearTimeout(timer); }
   
});





function $(){ 

	if(document.all) // If it's an older version
    {
		return document.all[arguments[0]];
        //return eval("document.all['" + arguments[0] + "']");
 
    }
    
    
    return document.getElementById(arguments[0]);
    
	
}










 