/*
_|      _|  _|            _|                            _|  _|              _|      _|            
_|_|  _|_|      _|_|_|        _|_|_|  _|_|      _|_|_|  _|        _|_|_|  _|_|_|_|      _|    _|  
_|  _|  _|  _|  _|    _|  _|  _|    _|    _|  _|    _|  _|  _|  _|_|        _|      _|    _|_|    
_|      _|  _|  _|    _|  _|  _|    _|    _|  _|    _|  _|  _|      _|_|    _|      _|  _|    _|  
_|      _|  _|  _|    _|  _|  _|    _|    _|    _|_|_|  _|  _|  _|_|_|        _|_|  _|  _|    _|

  Website: Minimalistix.be
  Version: 1.0
  Programming: Hulpia Frederik - info at powerplant dot be - http://www.powerplant.be
  
  Filename: photoloader.js
  Info: Ajax photo loader
*/

// Function photoPreload
function photoPreload()
{
	// Set ajax handler
	photoPreLoader = new flexAjax();
	photoPreLoader.init();
			
	// Set onreadystatechange
	photoPreLoader.onreadystatechange(photoLoaded);
			
	// Send AJAX request
	photoPreLoader.send("../includes/ajaxLoadPhoto.php", "");
}

// Function ajax response photoloaded
function photoLoaded()
{
	// Get Ajax Handler
	var ajax = photoPreLoader.AjaxHandler;

	// Check if ready and get response
	if (ajax.readyState == 4 && ajax.status == 200)
	{
		// Get ajax data
		var result 	= ajax.responseXML.documentElement;
		var imgs	= result.getElementsByTagName('image'); 		
		var other	= result.getElementsByTagName('preloadimages');
		
		// Create photos
		for(var c=0 ; c < imgs.length ; c++)
		{
			// Preload photos
			photos[c] = new Image();
			photos[c].src = imgs[c].firstChild.nodeValue;
		}
		
		// Create preload images
		for(var c=0; c < other.length ; c++)
		{
			// Preload images
			preloadedImgs[c] = new Image();
			preloadedImgs[c].src = other[c].firstChild.nodeValue;
		}
	}	
}

// Function setphoto
function setPhoto(photo)
{
	// Get size of the chosen photo
	var w = photos[photo].width;
	var h = photos[photo].height;
	
	// Get objects
	var newImg = flexGlobal.getObject('photoPlacer');
	var photoCount	= flexGlobal.getObject('photoCount');
	
	if(newImg)
	{
		// Set new size
		newImg.width = w; newImg.height = h;
	
		// Set new source
		newImg.src = photos[photo].src;
	
		// Change curPhoto
		curPhoto = photo;

		// Set the photo counter
		var countCorrection = photo + 1;
		photoCount.innerHTML = countCorrection + " of " + photos.length + "<br /><br />";
	}
	else
	{
		return false;
	}
}

// Function doNext
function doNext()
{
	// Check for end
	if(curPhoto > photos.length -2)
	{
		// Reset current photo
		curPhoto = 0;
		
		// Set new image
		setPhoto(curPhoto);
	}
	else
	{
		// Add 1 to curPhoto
		curPhoto++;
		
		// Set new image
		setPhoto(curPhoto);
	}
}

// Function doPrev
function doPrev()
{
	// Check for start
	if(curPhoto > 0)
	{
		// Substract 1 from curPhoto
		curPhoto--;
		
		// Set new image
		setPhoto(curPhoto);
	}
	else
	{
		// Set current photo
		curPhoto = photos.length -1;
		
		// Set new image
		setPhoto(curPhoto);		
	}
}

// Function switchauto
function switchAuto()
{
	// Get objects
	var auto = flexGlobal.getObject('auto_photo');
	
	// Check current state
	if(auto.value == "Pause")
	{
		// Stop the timer
		clearInterval(countID);
		
		// Set new Value
		auto.value = "Restart";
	}
	else
	{
		// Restart the timer
	 	countID = setInterval("doNext();", interval);		
		
		// Set new value
		auto.value = "Pause";
		
		// Show next photo
		doNext();
	}
}