

/*********************************IMAGEHANDLER CLASS DEFINITION********************************************************************/

function ImageHandler(maxImage)
{
	this.pictures = new Array();
	this.imgPositions = new Array();
	this.position = 0;
	this.loaded = 0;
	this.totalImg = 0;
	this.direction = 1;
	this.timer = 3000;
	this.slideStatus = 0;
	this.applyfilter = 0;
	//records the position of the last image
	this.duration = 1000;
	this.finalImg = 0;
	
	if(typeof(ImageHandler_prototype_called) == 'undefined')
	{
		ImageHandler_prototype_called = true;
		//declare loadImages method
		ImageHandler.prototype.loadImages = loadImages;
		//declare changePicture method
		ImageHandler.prototype.changePicture = changePicture;
		//declare swapImage method
		ImageHandler.prototype.swapImage = swapImage;
		//declare removeContainers method
		ImageHandler.prototype.removeContainers = removeContainers;
		//declare cycleImages method
		ImageHandler.prototype.cycleImages = cycleImages;
		//declare slideShow method
		ImageHandler.prototype.slideShow = slideShow;
		//declare pauseSlideShow method
		ImageHandler.prototype.pauseSlideShow = pauseSlideShow;
		//declare startSlideShow method
		ImageHandler.prototype.startSlideShow = startSlideShow;
		//declare opacity method
		ImageHandler.prototype.opacity = opacity;
		//declare changeOpac method
		ImageHandler.prototype.changeOpac = changeOpac;
	}
}

function loadImages(maxImages)
{
	/*Function to load image src into the pictures array
	returns the number of images*/
	var pic;
	var x;
	var images
	if(typeof(maxImages)=="undefined")
	{
		maxImages = 10;	
	}
	if(document.getElementById("PICTURE1"))
	{
		this.pictures[0] = document.getElementById("PICTURE1").src;
		this.imgPositions[0] = 1;
	}
	else
	{
		return 0;	
	}
	var y=1;
	for(x=2; x<maxImages+1; x++)
	{
		
		pic = "PICTURE" + x;
		if(document.getElementById(pic))
		{
			
			this.pictures[x-1]= document.getElementById(pic).src;
			this.imgPositions[x-1] = x;
			y++;
			this.finalImg = x-1;
		}
		else
		{
			this.imgPositions[x-1] = 0;
		}
	}
	this.totalImg = this.pictures.length;
	
	this.loaded = 1;
	return this.pictures.length;
}

function changePicture(direction)
{
	//swaps the main picture with the next inline
	//+1 is forwards -1 is backwards
	if(this.loaded == 1)
	{
		this.position = this.position + direction;
		var length = this.pictures.length-1
		if(this.position == 0)
		{
			this.position = length;
		}
		else if(this.position>length)
		{
			this.position = 1;
		}
		this.swapImage(this.position);
	}
}

function swapImage(image)
{
	//swap main image with image number passed in
	if(this.loaded == 1)
	{
		var tempSrc;
		var newPic;
		var tempPos;
		this.slideStatus = 0;
		tempPos = this.imgPositions[0];
		this.imgPositions[0] = this.imgPositions[image];
		this.imgPositions[image] = tempPos;
		newPic =  image + 1;
		
		//apply a filter??
		if(this.applyfilter ==1)
		{
			//if IE use blendTrans filter
			if(document.all)
			{
				document.getElementById("PICTURE1").style.filter="blendTrans(duration=2)";
				document.getElementById("PICTURE1").filters.blendTrans.Apply();
				document.getElementById("PICTURE" + newPic).style.filter="blendTrans(duration=2)";
				document.getElementById("PICTURE" + newPic).filters.blendTrans.Apply();
				document.getElementById("PICTURE1").src = this.pictures[this.imgPositions[0]-1];
				document.getElementById("PICTURE" + newPic).src = this.pictures[this.imgPositions[image]-1];
				document.getElementById("PICTURE1").filters.blendTrans.Play();
				document.getElementById("PICTURE" + newPic).filters.blendTrans.Play();
			}
			else
			{
				this.opacity("PICTURE1", 100, 0);
				this.opacity("PICTURE" + newPic, 100, 0);
				document.getElementById("PICTURE" + newPic).src = this.pictures[this.imgPositions[image]-1];
				document.getElementById("PICTURE1").src = this.pictures[this.imgPositions[0]-1];
				this.opacity("PICTURE1", 0, 100);
				this.opacity("PICTURE" + newPic, 0, 100);
			}
			
		}
		else
		{
			document.getElementById("PICTURE1").src = this.pictures[this.imgPositions[0]-1];
			document.getElementById("PICTURE" + newPic).src = this.pictures[this.imgPositions[image]-1];
		}
		this.applyfilter = 0;
		this.position = image;
	}
}

function startSlideShow()
{
 	this.slideStatus = 1;
	this.slideShow()
}

function slideShow()
{
		if(imageHandler.slideStatus==1)
		{
			this.applyfilter = 1;
			this.cycleImages(imageHandler.direction);
			window.setTimeout('imageHandler.slideShow()', imageHandler.timer);
		}
}

function pauseSlideShow()
{
 	this.slideStatus = 0;
}

function cycleImages(direction)
{
	//cycles forwards or backwards through the images
	//direction > 0 = forwards
	//direction < 0 = backwards
	//alters the positions for the images stored in imgPositions, then applies the new positions to the screen
	var temp
	//lastImg records the last valid image to be swapped (ignores missed images)
	var lastImg = 0
	
	
	//cycles forwards
	if(direction >0)
	{
		temp = this.imgPositions[0];
		for(x=0; x<this.imgPositions.length-1; x++)
		{
			//if next image is missing ignore
			 if(this.imgPositions[x+1] != 0)
			 {
				//swap image position with last valid image position
				this.imgPositions[lastImg] = this.imgPositions[x + 1];
				//record this positions as last valid position
				lastImg = x+1;
			 }
		}
		this.imgPositions[lastImg] = temp;
	}
	//cycles backwards
	else
	{
		temp = this.imgPositions[this.finalImg];
		lastImg = this.finalImg;
		for(x=this.imgPositions.length-1; x>0; x--)
		{
			 if(this.imgPositions[x-1] != 0)
			 {
				this.imgPositions[lastImg] = this.imgPositions[x-1];
				lastImg = x-1
			 }
		}
		this.imgPositions[0] = temp;
	}
	
	var newPic;
	
	//apply img positions to screen images.
	for(x=0;x<this.imgPositions.length;x++)
	{
		newPic = x+1;
		if(this.imgPositions[x]!=0)
		{
			//apply filter??
			if(this.applyfilter == 1)
			{
				//if IE use blendTrans filter
				if(document.all)
				{
					document.getElementById("PICTURE" + newPic).style.filter="blendTrans(duration=2)";
					document.getElementById("PICTURE" + newPic).filters.blendTrans.Apply();
					document.getElementById("PICTURE" + newPic).src = this.pictures[this.imgPositions[x]-1];
					document.getElementById("PICTURE" + newPic).filters.blendTrans.Play();
				}
				//else manipulate opacity style
				else
				{
					this.opacity("PICTURE" + newPic, 100, 0);
					document.getElementById("PICTURE" + newPic).src = this.pictures[this.imgPositions[x]-1];
					this.opacity("PICTURE" + newPic, 0, 100);
					
				}
			}
			else
			{
				document.getElementById("PICTURE" + newPic).src = this.pictures[this.imgPositions[x]-1];	
			}
		}
	}
		this.applyfilter = 0;
}

function removeContainers(container)
{
	//hides empty image containing elements
	//pass in the element generic name and the total number of property images
	
	if(document.getElementById(container + "1"))
	{
		this.imgPostions[0] = 1;
	}
	else
	{
		return 0;	
	}
	for(x=2; x<10; x++)
	{
		if(x>this.totalImg)
		{
			document.getElementById(container + x).style.display = "none";	
		}
		else
		{
			this.imgPostions[x-1] = x;
		}
		alert(pictures[imgPosition[x]]);
	}
	
}

function opacity(id, opacStart, opacEnd) { 
    //speed for each frame 
    var speed = Math.round(this.duration / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("imageHandler.changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("imageHandler.changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

/************************************END IMAGEHANDLER DEFINITION******************************************/







