// JavaScript Document


/*********************************RENTALPERIODHANDLER CLASS DEFINITION***************************************/
//object to handle price change for a rentalperiod change
function rentalPeriodHandler()
{
	this.letPrice = new Array;
	this.salesPrice = new Array;
	this.additionalText = " ";
	this.additionalLetText = " ";
	
	if(typeof(rentalPeriodHandler_prototype_called) == 'undefined')
	{
		rentalPeriodHandler_prototype_called = true;
		//declare selectChange method
		rentalPeriodHandler.prototype.selectChanged = selectChanged;
		//declare radioChanged method
		rentalPeriodHandler.prototype.radioChanged = radioChanged;
		//declare writeSelect method
		rentalPeriodHandler.prototype.writeSelect = writeSelect;
		//declare createPriceText method
		rentalPeriodHandler.prototype.createPriceText = createPriceText;
	}
}
function selectChanged()
{
	//function to handle rentalperiod select box onchange event
	//alters the price range select boxes
	if(document.getElementById("rentalPeriod").selectedIndex==0)
	{
		this.writeSelect(this.salesPrice, this.additionalText)	
	}
	else if(document.getElementById("rentalPeriod").selectedIndex==1)
	{
		this.writeSelect(this.letPrice, this.additionalLetText)
	}
}

function radioChanged(elementID)
{
	//function to handle rentalperiod radio buttons onclink event
	//alters the price range select boxes
	//requires the element id name
	if(document.getElementById(elementID).value==0)
	{
		this.writeSelect(this.salesPrice, this.additionalText)	
	}
	else
	{
		this.writeSelect(this.letPrice, this.additionalLetText)
	}
}

function writeSelect(priceArray, additionalText)
{
	//function to write the values to the minPrice and maxPrice select boxes
	document.getElementById("maxPrice").options.length = priceArray.length+2;
	document.getElementById("minPrice").options.length = priceArray.length+1;
	var priceText;
	
	for(x=0; x<priceArray.length; x++)
	{
		priceText = this.createPriceText(priceArray[x], additionalText);
		document.getElementById("minPrice").options[x+1].value = priceArray[x];
		document.getElementById("minPrice").options[x+1].text = priceText;
		document.getElementById("maxPrice").options[x+1].value = priceArray[x];
		document.getElementById("maxPrice").options[x+1].text = priceText;
	}
	priceText = this.createPriceText( priceArray[priceArray.length-1], additionalText);
	document.getElementById("maxPrice").options[document.getElementById("maxPrice").length-1].text = priceText + "+";
	document.getElementById("minPrice").selectedIndex = 0;
	document.getElementById("maxPrice").selectedIndex = 0;
}	

function createPriceText(priceText, additionalText)
{
	var start;
	var end;
	for(i=3; i<priceText.length; i=i+4)
	{
		start = priceText.slice(priceText.length-i,priceText.length);
		end = priceText.slice(0,priceText.length-i);
		priceText = end + "," + start;
	}
	priceText = "£" + priceText + " " + additionalText;
	return priceText;
}


/*********************************END RENTALPERIODHANDLER CLASS DEFINITION***************************************/


/*********************************IMAGEHANDLER CLASS DEFINITION********************************************************************/

function ImageHandler(maxImage)
{
	this.pictures = new Array();
	this.imgPositions = new Array();
	this.position = 1;
	this.loaded = 0;
	this.totalImg = 0;
	this.direction = 1;
	this.timer = 4000;
	this.slideStatus = 0;
	this.slideType = 1;
	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)
	{
		if(this.pictures.length>1)
		{
			do
			{
				var length = this.pictures.length-1
				if(this.position == 0)
				{
					this.position = length;
				}
				else if(this.position>length)
				{
					this.position = 1;
				}
			}
			while(this.imgPositions[this.position]==0)
			
			this.swapImage(this.position);
			this.position = this.position + direction;
		}
	}
}

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", this.pictures[this.imgPositions[0]-1], 100, 0);

				this.opacity("PICTURE" + newPic, this.pictures[this.imgPositions[image]-1],  100, 0);
			}
			
		}
		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;
			if(typeof(this.slideType)=="undefined" || this.slideType==1)
			{
				this.cycleImages(imageHandler.direction);
			}
			else if(this.slideType == 2)
			{
				this.changePicture(imageHandler.direction);
 				this.slideStatus = 1;
			}
			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 changeImage(id, image)
{
	document.getElementById(id).src = image;	
}

function opacity(id, image, opacStart, opacEnd) { 
    //speed for each frame 
    var speed = Math.round(this.duration / 99); 
    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++; 
        } 
		setTimeout("changeImage('"+id+"','" + image + "')", timer * speed);
		
        for(i = opacEnd; i <= opacStart; 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******************************************/






