// JavaScript Document
function getImages(path,fileName) {
	//alert(path+fileName+".xml");
	//XML request
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	//xml file is located in the same directory as the residences.
	//xml file has the same name as the media directory
	xmlhttp.open("GET",path+fileName+".xml",false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML; 
	x=xmlDoc.getElementsByTagName("image");
	
	var imgList=new Array();
	for (var i=0;i<x.length;i++){ 
		imgList.push(x[i].childNodes[0].nodeValue);
	}
	return imgList;
}
//fade function
function fade(target,imgPath,imgList,initalDelay) {
	//counter for multiple animations
	var pic1Count=0;
	var pic2Count=0;
	//animation settings
	duration=1;
	fps=30;
	nextFade=3000;
	//clears any existing itmeouts
	if(typeof $(target).t!="undefined") {
		$(target).cancelLoop=true;
		clearTimeout($(target).t);
		$(target).inAnimation.cancel();
		$(target).outAnimation.cancel();
	} else if ($(target).playing) {
		$(target).cancelLoop=true;
		$(target).inAnimation.cancel();
		$(target).outAnimation.cancel();
	}
	//resets images
	function resetImages(outImage) {
		var oValue=0;
		var iValue=10;
		$(target+'_in').style.display="block";
		$(target+'_out').style.display="none";
		$(target+'_out').style.opacity = oValue/10;
		$(target+'_out').style.filter = 'alpha(opacity=' + oValue*10 + ')';
		$(target+'_in').style.opacity = iValue/10;
		$(target+'_in').style.filter = 'alpha(opacity=' + iValue*10 + ')';
		//clears both images
		$(target+'_in').innerHTML='<img id="loader" src="../images/bw_loader.gif"/ >';
		$(target+'_out').innerHTML=outImage;
	}
	function startAnimation() {
		//advances the current clip
		//sets playing to true... don't start a new one if playing.
		$(target).slideNum++;
		$(target).playing=true;
		//starts the fade effect
		$(target).inAnimation = new Effect.toggle(target+'_in', 'Appear', {duration:duration, fps:fps, afterFinish: function (obj) {if (imgList.length>1) {fadeTimer(nextFade); $(target).playing=false; }}});
		$(target).outAnimation = new Effect.toggle(target+'_out', 'Appear', {duration:duration, fps:fps});
	}
	//adds images... starts slideshow
	//sets vars
	//doesn't slideshow first image on top of itself
	var firstImage=$(target+'_in').firstChild.src.substring($(target+'_in').firstChild.src.lastIndexOf("\/")+1);
	$(target).slideNum=0;
	if(firstImage==imgList[$(target).slideNum]) {
		$(target).slideNum++;
	} 
		
	//the timer function for the fade
	function fadeTimer(time) {
		$(target).t=setTimeout(function() {fadeInOut();},time);
	}
	function fadeInOut() {
		if($(target).cancelLoop) {//resets the divs to default if a new section is choosen
			resetImages('');
			$(target).cancelLoop=false;
		}
		//loops if all the images have been played
		if($(target).slideNum==imgList.length) {
			$(target).slideNum=0;
		}
		i=$(target).slideNum;
		//adds the image
		if($(target+'_in').style.display=="none"){//if the in div is hidden, load images to it
			var imgPreload = new Image();
			imgPreload.onload = function() {
				$(target+'_in').innerHTML='<img src="'+imgPreload.src+'"/>';
				startAnimation();
			}
			imgPreload.src = imgPath+imgList[i];
		} else {//else the out div is hidden, then load images to it
			var imgPreload = new Image();
			imgPreload.onload = function() {
				$(target+'_out').innerHTML='<img src="'+imgPreload.src+'"/>';
				startAnimation();
			}
			imgPreload.src = imgPath+imgList[i];
		}
	} 
	//1000 = 1 second
	//load instantly on new section rather than waiting.
	fadeTimer(initalDelay);
}
function setPicAnimation(divName) {
	var mainImgName = divName;
	var mainImg = $(mainImgName);
	var mainImagePaths=mainImg.firstChild;
	//adds the in/out animation divs
	mainImg.innerHTML='<div id="'+mainImgName+'_in" class="ssLrg">'+mainImg.innerHTML+'</div><div id="'+mainImgName+'_out" class="ssLrg" style="display:none;"></div>';
	//defines the list, XML name must match "divName"
	var imgList=getImages("contactImages/",divName);
	fade(mainImgName,"contactImages/",imgList,1500);	
}

addLoadEvent(function() {
	//start the animation with "peace"
	setPicAnimation('flash');
});
