/*
Author/Owner: 	Daniel Peel.
Contact: 		mintpants@tiscali.co.uk.
Copyright: 		This script may not be reproduced or used in any way without the consent of the owner.
Details: 		Class to manipulate opacity.
Created:		22.02.08
*/

//*****************************************************************
//					      Animate Opacity
//*****************************************************************


function opac(target)	{
	
	if(typeof(target) != 'object')
	{
		this.target = document.getElementById(target);
	}
	else
	{
		this.target = target;
	}
}

opac.prototype.callBack = function()	{
	//alert("FINSIHSED");//callback function must be defined before opacity method is called
}
opac.prototype.inc = function(newValue)	{
	
	this.target.style.opacity = newValue;
	
    this.target.style.KhtmlOpacity = (opacity / 100);
    
	this.target.style.filter = "alpha(opacity=" + opacity + ")";
	
}

opac.prototype.dec = function(newValue)	{
	
	this.target.style.opacity = newValue;
	
}


opac.prototype.changeOpac = function(opacStart, opacEnd, totalTime) {
    
	var self = this;
	
	incOpac = function()	{
   		
		if(self.cycle == self.cycles)
		{	
			clearInterval(self.intervalId);
			self.callBack();			
			return;
		}
			
		self.newOpacity += self.opacity;
		
		self.target.style.opacity = (self.newOpacity / 100);
    	
    	self.target.style.KhtmlOpacity = (self.newOpacity / 100);
    	
		self.target.style.filter = "alpha(opacity=" + self.newOpacity + ")";
		
		self.cycle++;
	}
	
	decOpac = function()	{
   		
		if(self.cycle == self.cycles)
		{	
			clearInterval(self.intervalId);
			self.callBack();
			return;
		}
			
		self.newOpacity -= self.opacity;
		
		self.target.style.opacity = (self.newOpacity / 100);
    	
    	self.target.style.KhtmlOpacity = (self.newOpacity / 100);
    	
		self.target.style.filter = "alpha(opacity=" + self.newOpacity + ")";
		
		self.cycle++;
	}
	
	//calcualte the number of cycles we need
	this.cycles = totalTime/100;	
	
	this.cycle = 0;
	
	this.newOpacity = opacStart;
	
	this.target.style.visibility = 'visible';
	
	
	
	//determine the direction for the blending
    if(opacStart < opacEnd) 
	{		
		//calculate the opacity change value
		this.opacity = (opacEnd - opacStart)/this.cycles; 		
		this.intervalId = setInterval(incOpac,  10);
		
    } 
	else if(opacStart > opacEnd) 
	{
		
        //calculate the opacity change value
		this.opacity = (opacStart - opacEnd)/this.cycles;
		this.intervalId = setInterval(decOpac, 10);
		
    }
	
	
}

