// -----------------------------------------------------------------------------
//	Module:		Photo Album
//	Copyright: 	ROSE Development
//	Author:		Silvester Kok
//	Version:	0.2
// -----------------------------------------------------------------------------

var intervalID = 0;
var preLoadImage = new Image();
var photoView = 0;
var	loadedImage = new Image();
var imageWidth = 0;
var imageHeight = 0;
var	currentImageUrl = "";
var currentImageID = 0;

var imageAlbum; // Array holding the current album images

var fullView = false;
var interfaceVisible = false;

var uploadWindow;

// -----------------------------------------------------------------------------
// Generate a random adition to ajax requests to overcome caching
// -----------------------------------------------------------------------------
function randomNumber()
{
	fTime = new Date();
	fMonth = (fTime.getMonth()+1).toString(16);
	fDay = (fTime.getDay()+1).toString(16);
	fYear = (fTime.getYear()+1).toString(16);
	fHours = (fTime.getHours()+1).toString(16);
	fMinutes = (fTime.getMinutes()+1).toString(16);
	fSeconds = (fTime.getSeconds()+1).toString(16);
	fRandom =  (Math.round(100*Math.random())).toString(16);
	return fMonth + fDay + fYear + fHours + fMinutes + fSeconds + fRandom;
}

// -----------------------------------------------------------------------------
// Fetch all photos in the given category
// -----------------------------------------------------------------------------
function foto_FetchFotoList(inCategory,firstLoad)
{
	foto_ShowLoading("fotolist");
	fAjaxHandler = new CAjax();
	fObject = document.getElementById("fotoview");
	if ((fObject) && (!firstLoad)) {
		fObject.innerHTML = "";
		
	}
	fObject = document.getElementById("fotolist");
	if (fObject) {
		fAjaxHandler.GetData('fotos.php?cat=' + inCategory + "&time=" + randomNumber(),fObject);
	}
	
	fListAjaxHandler = new CAjax();
	
	fList = document.getElementById("albumlist");
	if (fList) {
		fListAjaxHandler.GetData('fotos.php?catlist=' + inCategory + "&time=" + randomNumber(),fList);
	}
	interfaceVisible=false;
	initinterface();
	foto_GetSlideShowList(inCategory);
}

// -----------------------------------------------------------------------------
// Display a given photo (No Preload handler)
// -----------------------------------------------------------------------------
function foto_DisplayPhoto(inID)
{
	
	//fObject.style.overflow = 'visible';
	foto_ShowLoading("fotoview");
	fAjaxHandler = new CAjax();
	fObject = document.getElementById("fotoview");
	if (fObject) {
		fAjaxHandler.GetData('fotos.php?id=' + inID,fObject);
	}
}

// -----------------------------------------------------------------------------
// Preload image
// -----------------------------------------------------------------------------
function foto_PreloadImageData(source,object)
{
	fURL = "";
	if (intervalID) {
		window.clearInterval(intervalID);
		intervalID = 0;
	}
	if (window.XMLHttpRequest) {
		mXMLObject = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		mXMLObject = new ActiveXObject("Microsoft.XMLHTTP");
	}
	mData = "";
	if (mXMLObject) {
		mXMLObject.open("GET",source);
		mXMLObject.onreadystatechange = function()
		{
			if (mXMLObject.readyState == 4 && mXMLObject.status == 200) {
				fURL = mXMLObject.responseText;
				preLoadImage = new Image();
				preLoadImage.src = fURL;
				photoView = fObject;
				if (intervalID == 0) {
					intervalID = window.setInterval(foto_DisplayIfLoaded, 1000);
					
				}
			}
		}
		mXMLObject.send(null);
	}
}

// -----------------------------------------------------------------------------
// Preload handler routine (window Interval)->foto_PreloadImageData
// -----------------------------------------------------------------------------
function foto_DisplayIfLoaded()
{
	if ((preLoadImage.complete) && (preLoadImage.width)) {
		o_wd = preLoadImage.width ;
		o_ht = preLoadImage.height ;
		
		fheight = 300;
		fwidth = 522;
		
		w = Math.round(o_wd * fheight / o_ht);
		h = Math.round(o_ht * fwidth / o_wd);
		if((fheight-h)<(fwidth-w)) {
			fwidth = w;
		} else {
			fheight = h;
		}
		

	
		photoView.innerHTML = "<img id='photo' src='" + fURL + "' style='width:" + fwidth + "px;height:" + fheight + "px;margin:0 auto;text-align:center;' />";
		imageWidth = fwidth;
		imageHeight = fheight;
		// This might cause problems in proportional displaying the image after 
		updateZoom(1);
		currentImageUrl = fURL; // hold the image url in a global
		if (intervalID) {
			
			window.clearInterval(intervalID);
			intervalID = 0;
		}
		interfaceVisible=true;
		initinterface();
		preLoadImage = new Image(100,100);
		preLoadImage.src='';
		// -----------------------------------------------------
		// Bug fix for bad browser behavior
		// -----------------------------------------------------
		fObject = document.getElementById("photo");
		fObject.width = fwidth;
		fObject.height = fheight;
		
	}
}

// -----------------------------------------------------------------------------
// Display a given photo with preload handler
// -----------------------------------------------------------------------------
function foto_DisplayPhoto2(inID)
{
	foto_ShowLoading("fotoview");
	fObject = document.getElementById("fotoview");
	if (fObject) {

		foto_PreloadImageData('fotos.php?id=' + inID + "&action=url" + "&time=" + randomNumber(),fObject);
	}
	currentImageID = inID;
	initinterface();
	
}

// -----------------------------------------------------------------------------
// Show loading spinning wheel in given div
// -----------------------------------------------------------------------------
function foto_ShowLoading(inObject)
{
	fObject = document.getElementById(inObject);
	if(fObject) {
		htmlCode = "<div id='loading' style='width:64px;height:64px;background-color:#333;margin:100px auto;'>";
		htmlCode = htmlCode + "<img src='images/spinner32_invert.gif' style='float:left;clear:right;' /><br/>";
		htmlCode = htmlCode + "<p style='float:left;background-color:#333;color:#fff;font: bold 11px/16px Verdana,Arial,serif;clear:left;'>Loading...</p>";
		htmlCode = htmlCode + "</div>";
		fObject.innerHTML = htmlCode;
	}
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Photo Upload
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

function js_Window(inHeight,inWidth,inID) {
	
	// ----------------------------------------------------------------------------------
	// Make the window visible
	// ----------------------------------------------------------------------------------
	this.showWindow = function()
	{
		this.centerWindow();
		fWindow = document.getElementById(this.ID);
		this.makeTransparent(90);
		fWindow.style.zIndex='20000';	
		fWindow.style.display='block';	
	}
	// ----------------------------------------------------------------------------------
	// Hide the window
	// ----------------------------------------------------------------------------------
	this.hideWindow = function()
	{
		fWindow = document.getElementById(this.ID);
		fWindow.style.display='none';
	}
	// ----------------------------------------------------------------------------------
	// Center the window
	// ----------------------------------------------------------------------------------
	this.centerWindow = function()
	{
		var fHeaderHeight = 160;
		var fScreenWidth = 0, fScreenHeight = 0;
		  if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			fScreenWidth = window.innerWidth;
			fScreenHeight = window.innerHeight;
		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			fScreenWidth = document.documentElement.clientWidth;
			fScreenHeight = document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			fScreenWidth = document.body.clientWidth;
			fScreenHeight = document.body.clientHeight;
		  }
		fWindow = document.getElementById(this.ID);
		fTop = fHeaderHeight + (fScreenHeight - this.height)/5;
		if (fTop < 160) fTop = 160;
		fLeft = (fScreenWidth - this.width)/2;
		if (fLeft < 0) fLeft = 0;
		fWindow.style.top = fTop + "px";
		fWindow.style.left = fLeft + "px";
	}
	
	this.makeTransparent = function (opacity){
		fWindow = document.getElementById(this.ID);
		if (navigator.appName.indexOf("Netscape")!=-1
			&&parseInt(navigator.appVersion)>=5){	
				fWindow.style.MozOpacity=opacity/100
				fWindow.style.Opacity=opacity/100;	
		} else if (navigator.appName.indexOf("Microsoft")!= -1 
			&&parseInt(navigator.appVersion)>=4) {
				fWindow.style.filter="alpha(opacity=" + opacity + ")";
		} else {
			fWindow.style.Opacity=opacity/100;				
		}
	
	}
	
	// ----------------------------------------------------------------------------------
	// Strip the "px" from a css size parameter
	// ----------------------------------------------------------------------------------
	function stripPx(value) { 
		if (value == "") return 0; 
		return parseFloat(value.substring(0, value.length - 2)); 
	}
	this.width = inHeight;
	this.height = inWidth;
	this.ID = inID;
}
// -----------------------------------------------------------------------------
// Check the type of the file being uploaded
// -----------------------------------------------------------------------------
function foto_UploadCheck()
{
	fUploadFile = document.getElementById("uploadfile");
	ext = fUploadFile.value;
	ext = ext.substring(ext.length-3,ext.length);
	ext = ext.toLowerCase();
	if (ext != 'jpg') {
		// TODO: Better alert !.....
		alert('You selected a .'+ext+
			  ' file; please select a .jpg file instead!');
		fUploadForm = document.getElementById("image_upload_form");
		
		// TODO: Seems not to work in safari.....
		fUploadForm.reset();
		return false;
	}
}

// -----------------------------------------------------------------------------
// Insert the upload window
// -----------------------------------------------------------------------------

function foto_insertUploadWindow(inWhere)
{
	fWindow = document.getElementById(inWhere);
	fHTML = "";
	fHTML = fHTML + "<div class='header'></div>";//<img src='images/foto_upload_window_header.gif' />";
	fHTML = fHTML + "<form method='post' enctype='multipart/form-data' accept='image/gif,image/jpeg' id='image_upload_form' action=''>"; // action was fotos.php
	fHTML = fHTML + "<div>";
	fHTML = fHTML + "<label>Titel:</label><textarea name='form_title' class='title'></textarea><br />";
	fHTML = fHTML + "<label>Omschrijving:</label><textarea name='form_description'></textarea><br />";
	//fHTML = fHTML + "<label>Datum:</label><textarea name='form_day' class='day'>DD</textarea>";
	//fHTML = fHTML + "<textarea name='form_month' class='month'>MM</textarea>";
	//fHTML = fHTML + "<textarea name='form_year' class='year'>YYYY</textarea><br />";
	fHTML = fHTML + "<label>JPG file:</label><input type='file' id='uploadfile' title='Selecteer een JPEG of PNG file...' name='form_image_file' accept='image/gif,image/jpeg' class='file' onchange='foto_UploadCheck()' /><br />";
	fHTML = fHTML + "<div class='buttonframe'>";
	fHTML = fHTML + "<img src='images/foto_upload.gif' id='upload_barber' style='display:none;' />";
	fHTML = fHTML + "<input type='hidden' value='" + randomNumber() + "' name='form_key' />";
	fHTML = fHTML + "<div id='form_submit' class='submit' onmousedown='foto_UploadImage()'></div>";
	fHTML = fHTML + "<div id='form_cancel' class='cancel' onmousedown='foto_HideUploadWindow()'></div><br />";
	fHTML = fHTML + "</div>";
	fHTML = fHTML + "</div>";
	fHTML = fHTML + "</form>";
	// Imterface cover
	fWindow.innerHTML = fHTML;
}

// -----------------------------------------------------------------------------
// initialise the upload window
// -----------------------------------------------------------------------------
function foto_InitialiseUploadWindow()
{
	uploadWindow = new js_Window(350,270,"window");
	
}


function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

// -----------------------------------------------------------------------------
// Show the upload window
// -----------------------------------------------------------------------------
function foto_ShowUploadWindow()
{
	// Show and position the interface cover
	fCover = document.getElementById("cover");
	fMenu = document.getElementById("menu");
	fMenuPos = findPos(fMenu);
	fFooter = document.getElementById("footer");
	fFooterPos = findPos(fFooter);
	fCover.style.display ='block';
	fCover.style.top  = fMenuPos[1] + "px";
	fCover.style.left = fMenuPos[0] + "px";
	fCover.style.height = (fFooterPos[1] - 160 )+ "px";
	
	uploadWindow.showWindow();
	foto_insertUploadWindow("window");
	window.onresize = function () {uploadWindow.centerWindow();}
}

// -----------------------------------------------------------------------------
// Show the upload window
// -----------------------------------------------------------------------------
function foto_HideUploadWindow()
{
	fCover = document.getElementById("cover");
	fCover.style.display ='none';
	uploadWindow.hideWindow();
	window.onresize = function () {uploadWindow.centerWindow();}
}

// -----------------------------------------------------------------------------
// Upload an image
// -----------------------------------------------------------------------------
function foto_UploadImage()
{
	uploadForm = document.getElementById("image_upload_form");
	if (uploadForm) {
		// Hide submit/cancel buttons and show upload barber
		fSubmit = document.getElementById("form_submit");
		fSubmit.style.display = 'none';
		fCancel = document.getElementById("form_cancel");
		fCancel.style.display = 'none';
		fBarber = document.getElementById("upload_barber");
		fBarber.style.display = 'block';
		uploadForm.submit();
	}
	

	// Show progress bar here
	// pass current location in the header so we can restore the state
	//foto_HideUploadWindow();
}

// -----------------------------------------------------------------------------
// Get a named flash object
// -----------------------------------------------------------------------------
function foto_getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}


// -----------------------------------------------------------------------------
// Get a named flash object (Does not work for slideshow pro)
// -----------------------------------------------------------------------------
function foto_StopFlashMovie()
{
	flashMovie = foto_getFlashMovieObject("header");
	flashMovie.StopPlay();
}


// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Photo Download
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

function foto_downloadImage()
{
	source = "?dlid=" + currentImageID;
	document.location.href= source;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Foto Zoom
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

var dragging = false; 
var top; 
var left; 
var dragStartTop; 
var dragStartLeft; 

function showFotoToolbar(inShowHide)
{
	if (inShowHide) {
		var toolbar = document.getElementById("fotolistheader");
		toolbar.style.backgroundimage = '';
		var slider = document.getElementById("foto_sliderbg");
		slider.style.display='none';
		
		
	} else {
		var toolbar = document.getElementById("fotolistheader");
		toolbar.style.backgroundimage = '../images/foto_toolbar_bg.gif';
		var slider = document.getElementById("foto_sliderbg");
		slider.style.display='block';
	}
}

// -----------------------------------------------------------------------------
// When the photo zoom knob is in it's resting place, auto zoom when
// switching between small/full view
// -----------------------------------------------------------------------------
function foto_FitToView()
{
	var knobDiv = document.getElementById('foto_knob');
	
	fOldKnobPos = stripPx(knobDiv.style.left);
	if (fOldKnobPos <= 10)
	{
		var fotoView = document.getElementById("fotoview");
		var fPhoto = document.getElementById("photo");
		fWidth = stripPx(fotoView.style.width);
		fHeight = stripPx(fotoView.style.height);
		
		o_wd = stripPx(fPhoto.style.width);
		o_ht = stripPx(fPhoto.style.height);
		
		height = fHeight;
		width = fWidth;
		
		w = Math.round(o_wd * height / o_ht);
		h = Math.round(o_ht * width / o_wd);
		if((height-h)<(width-w)) {
			width = w;
		} else {
			height = h;
		}
		fPhoto.style.width = width +"px";
		fPhoto.style.height = height +"px";
		
		// Adjust the location of the knob slider
		
		//fKnobPos = (Math.round(width-o_wd)*100);
		//alert("width:"+width+", oldwidth: "+o_wd);
		//fOldKnobPos = fOldKnobPos + fKnobPos;
		//knobDiv.style.left = fOldKnobPos + "px";
	}
}

// -----------------------------------------------------------------------------
// Switch to full photo view
// -----------------------------------------------------------------------------
function switchToFullFotoView()
{
	if (!fullView) {
		var leftColumn = document.getElementById("leftcol");
		leftColumn.style.display = 'none';
		var fotoList = document.getElementById("fotolist");
		fotoList.style.display = 'none';
		var fotoListHeader = document.getElementById("fotolistheader");
		fotoListHeader.style.width = '780px';
		var fotoZoomSlider = document.getElementById("foto_sliderbg");
		fotoZoomSlider.style.marginLeft = '100px';
		var fotoView = document.getElementById("fotoview");
		fotoView.style.width = '780px';
		fotoView.style.height = '574px';
		var playButtons = document.getElementById("slideshow_buttonset");
		playButtons.style.marginLeft = '150px';
		foto_FitToView();
		fullView = true;
	} else {
		var leftColumn = document.getElementById("leftcol");
		leftColumn.style.display = 'block';
		var fotoList = document.getElementById("fotolist");
		fotoList.style.display = 'block';
		var fotoListHeader = document.getElementById("fotolistheader");
		fotoListHeader.style.width = '522px';
		var fotoZoomSlider = document.getElementById("foto_sliderbg");
		fotoZoomSlider.style.marginLeft = '0px';
		var fotoView = document.getElementById("fotoview");
		fotoView.style.width = '522px';
		fotoView.style.height = '300px';
		var playButtons = document.getElementById("slideshow_buttonset");
		playButtons.style.marginLeft = '0px';
		foto_FitToView();
		fullView = false;
	}
}

// -----------------------------------------------------------------------------
// Initialise the photo toolbar
// -----------------------------------------------------------------------------
function initinterface()
{
	if (interfaceVisible) {
		var buttonPlay = document.getElementById("slideshow_play");
		buttonPlay.style.display = 'block';
		var buttonPause = document.getElementById("slideshow_pause");
		buttonPause.style.display = 'block';
		var buttonStop = document.getElementById("slideshow_stop");
		buttonStop.style.display = 'block';
		var zoomSlider = document.getElementById("foto_sliderbg");
		zoomSlider.style.display = 'block';
		var fotoDownload = document.getElementById("foto_download");
		fotoDownload.style.display = 'block';
		//var fotoECard = document.getElementById("foto_ecard");
		//fotoECard.style.display = 'block';
		var fotoFullView = document.getElementById("foto_fullview");
		fotoFullView.style.display = 'block';
		var knobDiv = document.getElementById('foto_knob');
		knobDiv.onmousedown = startMove; 
		knobDiv.onmousemove = processMove; 
		knobDiv.onmouseup = stopMove; 
		knobDiv.onmouseout = stopMove;
		knobDiv.onmouseover = setPointerCursor
		// necessary to enable dragging on IE 
		knobDiv.ondragstart = function() { return false; } 
		knobDiv.style.top = '-2px';
		knobDiv.style.left = '6px';
	} else {
		var buttonPlay = document.getElementById("slideshow_play");
		buttonPlay.style.display = 'none';
		var buttonPause = document.getElementById("slideshow_pause");
		buttonPause.style.display = 'none';
		var buttonStop = document.getElementById("slideshow_stop");
		buttonStop.style.display = 'none';
		var zoomSlider = document.getElementById("foto_sliderbg");
		zoomSlider.style.display = 'none';
		var fotoDownload = document.getElementById("foto_download");
		fotoDownload.style.display = 'none';
		//var fotoECard = document.getElementById("foto_ecard");
		//fotoECard.style.display = 'none';
		var fotoFullView = document.getElementById("foto_fullview");
		fotoFullView.style.display = 'none';
		var fotoView = document.getElementById("fotoview");
		fotoView.style.width = '522px';
		fotoView.style.height = '300px';
	}
	// init_imageprotect(); // !!!! conflict with existing code
	foto_InitialiseUploadWindow();
	foto_HandlePlaybackInterface();
}

// -----------------------------------------------------------------------------
// Set a pointer cursor on mouseover
// -----------------------------------------------------------------------------
function setPointerCursor(event)
{
	if (!event) event = window.event; 
	var knobDiv = document.getElementById("foto_knob");
	if (knobDiv.style.cursor)
		knobDiv.style.cursor = "pointer"; 
}

// -----------------------------------------------------------------------------
// Start move
// -----------------------------------------------------------------------------
function startMove(event) { 
	// necessary for IE 
	
	if (!event) event = window.event; 
	dragStartLeft = event.clientX; 
	dragStartTop = event.clientY; 
	var knobDiv = document.getElementById("foto_knob");
	if (knobDiv.style.cursor)
		knobDiv.style.cursor = "pointer"; 
	top = stripPx(knobDiv.style.top); 
	left = stripPx(knobDiv.style.left); 
	dragging = true; 
	return false; 
}

// -----------------------------------------------------------------------------
// process the move
// -----------------------------------------------------------------------------
function processMove(event) { 
	if (!event) event = window.event; // for IE 
	var knobDiv = document.getElementById("foto_knob");
	if (knobDiv.style.cursor)
		knobDiv.style.cursor = "pointer"; 
	if (dragging) {
		newleft = parseFloat(left) + (event.clientX - dragStartLeft);
		if (newleft < 6)
			newleft = 6;
		if (newleft > 65)
			newleft = 65;
		knobDiv.style.left = newleft + "px";
		updateZoom(newleft-5);
	}
	
}

// -----------------------------------------------------------------------------
// stop the move
// -----------------------------------------------------------------------------
function stopMove() { 
	var knobDiv = document.getElementById("foto_knob"); 
	
	if (knobDiv.style.cursor)
		knobDiv.style.cursor = ""; 
	dragging = false; 
}

// -----------------------------------------------------------------------------
// strip px from the measurement
// -----------------------------------------------------------------------------
function stripPx(value) { 
	if (value == "") return 0; 
	return parseFloat(value.substring(0, value.length - 2)); 
}

// -----------------------------------------------------------------------------
// Change the zoom level
// -----------------------------------------------------------------------------
function updateZoom(inValue)
{
	var viewer = document.getElementById("fotoview");
	if (inValue == 1) {
		viewer.style.overflow='visible';
	} else {
		viewer.style.overflow='auto';
	}
	var image = document.getElementById("photo");
	if (image) {
		image.style.width = (imageWidth * (100+((inValue-1) * 3.5))/100) + "px";
		image.style.height = (imageHeight * (100+((inValue-1) * 3.5))/100) + "px";
	}

}

// -----------------------------------------------------------------------------
// Get slideshow elements as xml document
// -----------------------------------------------------------------------------
var fImageXML = "";
var fSlideImageArray = new Array();
var fSlideIDArray = new Array();
var fCurrentSlide = 0;
var fSlideTimer = 0;
var fPreloadedImage = false;
var fSlideIsPreloading = false;
var fSlideShowState ="";

// Parse the XML document containing the slides
// -----------------------------------------------------------------------------
function foto_ParseAlbumXML(xmldoc)
{
	var nodeset = xmldoc.getElementsByTagName('path');
	var idset = xmldoc.getElementsByTagName('id');
	var setlength = nodeset.length;
	if (setlength) {
		fSlideImageArray = new Array();
		fSlideIDArray = new Array();
		for (var i = 0 ; i < setlength ; i++) {
			aNode = nodeset[i];
			aChild = aNode.firstChild;
			fSlideImageArray.push(aChild.nodeValue);
			aNode = idset[i];
			aChild = aNode.firstChild;
			fSlideIDArray.push(aChild.nodeValue);
		}
	}
}

// Fetch all slides in current album as XML document
// -----------------------------------------------------------------------------
function foto_GetSlideShowList(inCategory)
{
	foto_StopSlideShow();
	if (window.XMLHttpRequest) {
		mXMLObject = new XMLHttpRequest();
		if (mXMLObject.overrideMimeType) {
            mXMLObject.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
            mXMLObject = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
            try {
				mXMLObject = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
		}
	}
	fImageXML = false;
	if (mXMLObject) {
		source = 'fotos.php?xml=' + inCategory + "&time=" + randomNumber();
		mXMLObject.open("GET",source,true);
		mXMLObject.onreadystatechange = function()
		{
			if (mXMLObject.readyState == 4 && mXMLObject.status == 200) {
				xmldoc = mXMLObject.responseXML;
				foto_ParseAlbumXML(xmldoc);
				//foto_PlaySlideShow();
				//alert(fImageXML.getElementsByTagName('images').item(0));
			}
		}
		mXMLObject.send(null);
	}	
}

// Play slideshow
// -----------------------------------------------------------------------------
function foto_PlaySlideShow()
{
	if (fSlideShowState=='pause') {
		fSlideIsPreloading = false;
		fSlideTimer = window.setInterval(foto_DisplayNextSlide, 5000);
		fSlideRunSpinner = document.getElementById("slideshow_spinner");
		fSlideRunSpinner.innerHTML = "<img src='images/Waiting_spinner_slideshow.gif' />";
		fSlideShowState = "play";
		foto_HandlePlaybackInterface();
	} else {
		if (fSlideTimer) {
			window.clearInterval(fSlideTimer);
			fSlideTimer = 0;
		}
		if (intervalID) {
			window.clearInterval(fSlideTimer);
			intervalID = 0;
		}
		fCurrentSlide = 0;
		fSlideIsPreloading = false;
		fSlideTimer = window.setInterval(foto_DisplayNextSlide, 5000);
		fSlideRunSpinner = document.getElementById("slideshow_spinner");
		fSlideRunSpinner.innerHTML = "<img src='images/Waiting_spinner_slideshow.gif' />";
		fSlideShowState = "play";
		foto_HandlePlaybackInterface();
	}
}

// Pause slideshow
// -----------------------------------------------------------------------------
function foto_PauseSlideShow()
{
	if (fSlideShowState == "pause") {
		foto_PlaySlideShow();
	} else if (fSlideShowState == "play") {
		if (fSlideTimer) {
			window.clearInterval(fSlideTimer);
			fSlideTimer = 0;
		}
		if (intervalID) {
			window.clearInterval(fSlideTimer);
			intervalID = 0;
		}
		fSlideRunSpinner = document.getElementById("slideshow_spinner");
		fSlideRunSpinner.innerHTML = "";
		fSlideShowState = "pause";
	}
}

// Stop slideshow
// -----------------------------------------------------------------------------
function foto_StopSlideShow()
{
	if (fSlideTimer) {
		window.clearInterval(fSlideTimer);
		fSlideTimer = 0;
		fCurrentSlide = 0;
	}
	if (intervalID) {
		window.clearInterval(fSlideTimer);
		intervalID = 0;
	}
	fSlideIsPreloading = false;
	fSlideRunSpinner = document.getElementById("slideshow_spinner");
	fSlideRunSpinner.innerHTML = "";
	fSlideShowState = "stop";
	foto_HandlePlaybackInterface();
}


// Display slides
// -----------------------------------------------------------------------------
function foto_DisplayNextSlide()
{
	// Introduce a timeout for bad pictures to skip them 
	if (!fSlideIsPreloading) {
		fCurrentSlide = fCurrentSlide + 1;
		if (fCurrentSlide > (fSlideImageArray.length-1)) {
			fCurrentSlide = 0;
		}
		fObject = document.getElementById("fotoview");
		fPreloadedImage = new Image();
		fPreloadedImage.src = unescape(fSlideImageArray[fCurrentSlide]);
		intervalID = window.setInterval(foto_DisplaySlideIfLoaded, 1000);
		fSlideIsPreloading = true;
	 }
}

// Display the slide if the image is preloaded
// -----------------------------------------------------------------------------
function foto_DisplaySlideIfLoaded()
{
	if ((fPreloadedImage.complete) && (fPreloadedImage.width)) {
		if (intervalID) {
			
			window.clearInterval(intervalID);
			intervalID = 0;
		}
		fSlideIsPreloading = false;
		o_wd = fPreloadedImage.width ;
		o_ht = fPreloadedImage.height ;
		
		if (!fullView) { // image is not in zoom mode
			height = 300; 
			width = 522;
		} else {
			height = 574;
			width = 780;
		}
		
		w = Math.round(o_wd * height / o_ht);
		h = Math.round(o_ht * width / o_wd);
		if((height-h)<(width-w)) {
			width = w;
		} else {
			height = h;
		}
		
		imageWidth = width;
		imageHeight = height;
		
		var knobDiv = document.getElementById("foto_knob");
		var zoomLevel = stripPx(knobDiv.style.left)-5;
		
		var viewer = document.getElementById("fotoview");
		if (zoomLevel == 1) {
			viewer.style.overflow='visible';
		} else {
			viewer.style.overflow='auto';
		}
		
		
		photoView.innerHTML = "<img id='photo' src='" + unescape(fSlideImageArray[fCurrentSlide]) + "' style='width:" + width + "px;height:" + height + "px;margin:0 auto;text-align:center;' />";
		currentImageUrl = unescape(fSlideImageArray[fCurrentSlide]); // hold the image url in a global
		currentImageID = fSlideIDArray[fCurrentSlide];
		
		var image = document.getElementById("photo");
		if (image) {
			image.style.width = (width * (100+((zoomLevel-1) * 3.5))/100) + "px";
			image.style.height = (height * (100+((zoomLevel-1) * 3.5))/100) + "px";
		}
		
		// Reset preloading var's
		fPreloadedImage=0;
		
	}
}

function foto_HandlePlaybackInterface()
{
	fPlayElement = document.getElementById("slideshow_play");
	fPauseElement = document.getElementById("slideshow_pause");
	fStopElement = document.getElementById("slideshow_stop");
	switch (fSlideShowState)
	{
		case 'stop':
		{
			//fStopElement.onmousedown = function(){return false;}
			//fPlayElement.onmousedown = foto_PlaySlideShow;
			//fPauseElement.onmousedown = foto_PauseSlideShow;
			break;
		}
		case 'pause':
		{
			//fStopElement.onmousedown = function(){return false;}
			//fPlayElement.onmousedown = foto_PlaySlideShow;
			//fPauseElement.onmousedown = foto_PauseSlideShow;
			break;
		}
		case 'play':
		{
			//fStopElement.onmousedown = function(){return false;}
			//fPlayElement.onmousedown = foto_PlaySlideShow;
			//fPauseElement.onmousedown = foto_PauseSlideShow;
			break;
		}
		default:
		{
			//fStopElement.onmousedown = function(){return false;}
			//fPlayElement.onmousedown = foto_PlaySlideShow;
			//fPauseElement.onmousedown = foto_PauseSlideShow;
			break;
		}
	}
}

/*
// -----------------------------------------------------------------------------
// Slideshow pre init
// -----------------------------------------------------------------------------
function foto_SlideShowPreInit()
{
	if ((document.getElementById) && (album =document.getElementById(albumId))) {
		album.style.visibility = "hidden";
		if (typeof preInitTimer != 'undefined') clearTimeout(preInitTimer);
	} else {
		preInitTimer = setTimeout("foto_SlideShowPreInit()",2);
	}
}

// -----------------------------------------------------------------------------
// Slide show fader
// -----------------------------------------------------------------------------
function foto_Fader(imageNumber,opacity)
{
	var obj=albumImages[imageNumber];
	if (obj.style) {
		if(obj.style.MozOpacity!=null) {
			obj.style.MozOpacity = (opacity/100) - .001;
		} else if (obj.style.opacity!=null) {
			obj.style.opacity = (opacity/100) - .001;
		} else if (obj.style.filter!=null) {
			obj.style.filter="alpha(opacity="+opacity+")";
		}
	}
}


function foto_CrossFade(opacity) {
	if (opacity < 100) {
		// current image not faded up fully yet...so increase its opacity 
		fader(currentImage,opacity);
		// fader(previousImage,100-opacity); 
		opacity += 10;
		window.setTimeout("crossfade("+opacity+")", 30);
	} else {
		// make the previous image - which is now covered by the current one fully - transparent 
		fader(previousImage,0);
		// current image is now previous image, as we advance in the list of images 
		previousImage=currentImage;
		currentImage+=1;
		if (currentImage>=galleryImages.length) {
			// start over from first image if we cycled through all images in the list 
			currentImage=0;
		}
		// make sure the current image is on top of the previous one 
		galleryImages[previousImage].style.zIndex = 0;
		galleryImages[currentImage].style.zIndex = 100;
		// and start the crossfade after a second's pause 
		opacity=0;
		window.setTimeout("crossfade("+opacity+")", 1000);
	}
	
}


// -----------------------------------------------------------------------------
// Initialize photo fader
// -----------------------------------------------------------------------------
function foto_FadeInit()
{
	if (document.getElementById) {
		foto_SlideShowPreInit();
		galleryImages = new Array;
		var node = gallery.firstChild;
		while (node) {
			if (node.nodeType == 1) {
				galleryImages.push(node);
			}
			node = node.nextSibeling;
		}
		for(i=0;i<galleryImages.length;i++) {
			
	}
}

*/