// 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(imgList,imgPath) {
	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/AJAX.propImg.php",
			data: 'imgPath='+imgPath2,
			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") {
		$('mainImg').cancelLoop=true;
		clearTimeout(t);
		inAnimation.cancel();
		outAnimation.cancel();
	} else if ($('mainImg').playing) {
		$('mainImg').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.
		$('mainImg').slideNum++;
		$('mainImg').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); $('mainImg').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);
	$('mainImg').slideNum=0;
	if(firstImage==imgList[$('mainImg').slideNum]) {
		$('mainImg').slideNum++;
	} 
		
	  //the timer function for the fade
	   function fadeTimer(time) {
			t=setTimeout(function() {fadeInOut();},time);
		}
	   function fadeInOut() {
			if($('mainImg').cancelLoop) {//resets the divs to default if a new section is choosen
				resetImages('');
				$('mainImg').cancelLoop=false;
			}
			//loops if all the images have been played
			if($('mainImg').slideNum==imgList.length) {
				$('mainImg').slideNum=0;
			}
			i=$('mainImg').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);
}
function loadFades() {
	var mainImg = $('mainImg');
	var mainImagePaths=mainImg.firstChild;
	mainImg.innerHTML='<div id="in" class="ssLrg">'+mainImg.innerHTML+'</div><div id="out" class="ssLrg" style="display:none;"></div>';
		var thisPath=mainImagePaths.src.substring(0,mainImagePaths.src.lastIndexOf("\/")-4); 
	var thisImg=mainImagePaths.src.substring(mainImagePaths.src.lastIndexOf("\/")+1);
	//tmbs list
	var thumbs = $('tmbs');
	if(thumbs) {
		var tmbDivs = thumbs.childNodes;
		//storage for saved images
		var tmbImagesOnly=new Array(thisImg);
		for (var i in tmbDivs) {
			if(tmbDivs[i].nodeName=='DIV') {
				var tmbA=tmbDivs[i].childNodes;
				for (var j in tmbA) {
					if(tmbA[j].nodeName=='A') {
						var tmbImg=tmbA[j].childNodes;
						for (var k in tmbImg) {
							if(tmbImg[k].nodeName=='IMG') {
								var imgClass=tmbA[j].className;
								var imageName=tmbImg[k].src.substring(tmbImg[k].src.lastIndexOf("\/")+1);
								if(imageName!="DVD-AMENITIES-Map.jpg") {
									
									tmbImagesOnly.push(imageName);
								}
							}
						}
					} else if(tmbA[j].nodeName=='IMG') {
						var imgClass=tmbA[j].className;
						var imageName=tmbA[j].src.substring(tmbA[j].src.lastIndexOf("\/")+1);
						if(imageName!="DVD-AMENITIES-Map.jpg") {
							if(imgClass!="exclude") {
								
								tmbImagesOnly.push(imageName);
							}
						}
					}
				}
			}
		}
		fade(tmbImagesOnly,thisPath+"/lrg/")
		//alert(tmbImagesOnly);
	}
}
addLoadEvent(loadFades);

/* checks the link just clicked to see if it's the current page... if so, change the ID=self */
function linkCurrentPage() { 
	if (!document.getElementsByTagName) return;  
		 //oDiv=document.getElementById('divName') //if I want to limit it to a specific div
		 //checks all the links
		 var links = document.getElementsByTagName("a"); 
		 //gets the pages location
		 var thispage = document.location.href; 
		 //checks to see if the page has a .php/.htm extension, if so adjusted comparisson
		 var postFix=thispage.slice(-4);
		 if(postFix!='.htm' && postFix!='.php') {//has to return something to compare, so compares directory
			 thispage=thispage.split('/');
			 thispage=thispage.slice(-2);
			 thispage=thispage.join("/");
		 }  else {//compares page name if exists
			thispage=thispage.split('/');
			thispage=thispage.slice(-1);
			thispage=thispage.join("/");
		 }
		 for (var i=0; i<links.length; i++) {//loops all the links
			 var link = links[i];
			 //breaks apart the links (don't show relative path info)
			 thisHREF = link.getAttribute("href");
			 	 
			 thisHREF=thisHREF.split('/');
			 thisHREF=thisHREF.slice(-2);
			 thisHREF=thisHREF.join("/");
		 
			 if (thisHREF == thispage) {//if the page and link are the same, add a self class
				link.className = "self"; 
				//return; 
			 }
	 	}  
} 
addLoadEvent(linkCurrentPage);
