// JavaScript Document
// multiple onloads
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      func();
	  if (oldonload) {
        oldonload();
      }
    }
  }
}

//fade function
function fade(imgPath) {
	var mainImg = $('bigImg');
	mainImagePaths=mainImg.firstChild;
	mainImg.innerHTML='<div id="in" class="ssLrg">'+mainImg.innerHTML+'</div><div id="out" class="ssLrg" style="display:none;"></div>';
	//if($('residences')) {//uses a defined list for residences pages, 
	//otherwise is uses the same name as the thumbnail (passed from loadFades() function)
		//old code for PHP
		//var imgPath2=imgPath.split("/");
		//imgPath2=imgPath2.slice(-5,-1);
		//imgPath2='../'+imgPath2.join("/");
		var imgLocList = jQuery.ajax({
			type: "POST",
			url: "../scripts/ss/AJAX.propImg.php",
			data: 'imgPath='+imgPath,
			async: false
		}).responseText;
		var imgList=new Array();
		imgList=imgLocList.split(',');
		//imgPath=imgPath2;
		/*
		//get XML file name based on the image directory (same name as the image directory)
		imgPath2=imgPath.split("/");
		imgPath2=imgPath2.slice(-3,-2);
		imgPath2=imgPath2.join("/");
		//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",imgPath2+".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);
		}*/
	//}
	
	//animation settings
	duration=1;
	fps=30;
	nextFade=3000;
	//clears any existing itmeouts
	if(typeof t!="undefined") {
		$('bigImg').cancelLoop=true;
		clearTimeout(t);
		inAnimation.cancel();
		outAnimation.cancel();
	} else if ($('bigImg').playing) {
		$('bigImg').cancelLoop=true;
		inAnimation.cancel();
		outAnimation.cancel();
	}
	//resets images
	function resetImages(outImage) {
		var oValue=0;
		var iValue=10;
		$('in').style.display="block";
		$('out').style.display="none";
		$('out').style.opacity = oValue/10;
		$('out').style.filter = 'alpha(opacity=' + oValue*10 + ')';
		$('in').style.opacity = iValue/10;
		$('in').style.filter = 'alpha(opacity=' + iValue*10 + ')';
		//clears both images
		$('in').innerHTML='<img id="loader" src="../images/bw_loader.gif"/ >';
		$('out').innerHTML=outImage;
	}
	function startAnimation() {
		//advances the current clip
		//sets playing to true... don't start a new one if playing.
		$('bigImg').slideNum++;
		$('bigImg').playing=true;
		//starts the fade effect
		inAnimation = new Effect.toggle('in', 'Appear', {duration:duration, fps:fps,  afterFinish: function (obj) {if (imgList.length>1) {fadeTimer(nextFade); $('bigImg').playing=false;}}});
		outAnimation = new Effect.toggle('out', 'Appear', {duration:duration, fps:fps});
	}
	//adds images... starts slideshow
	//sets vars
	//doesn't slideshow first image on top of itself
	var firstImage=$('in').firstChild.src.substring($('in').firstChild.src.lastIndexOf("\/")+1);
	$('bigImg').slideNum=0;
	if(firstImage==imgList[$('bigImg').slideNum]) {
		$('bigImg').slideNum++;
	} 
		
	  //the timer function for the fade
	   function fadeTimer(time) {
			t=setTimeout(function() {fadeInOut();},time);
		}
	   function fadeInOut() {
			if($('bigImg').cancelLoop) {//resets the divs to default if a new section is choosen
				resetImages('');
				$('bigImg').cancelLoop=false;
			}
			//loops if all the images have been played
			if($('bigImg').slideNum==imgList.length) {
				$('bigImg').slideNum=0;
			}
			i=$('bigImg').slideNum;
			//adds the image
			if($('in').style.display=="none"){//if the in div is hidden, load images to it
				var imgPreload = new Image();
				imgPreload.onload = function() {
					$('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() {
					$('out').innerHTML='<img src="'+imgPreload.src+'"/>';
					startAnimation();
				}
				imgPreload.src = imgPath+imgList[i];
			}
	   } 
	   //1000 = 1 second
	   //load instantly on new section rather than waiting.
	   fadeTimer(3000);
}
addLoadEvent(function() {
	fade('../img/home_slidshow/');
});
