// JavaScript Document

var debug= false; // true;

var currentSlide=0;
var autoShowMore= false;
var galleryName;
var curSlide= new _slide();
var gallery= new _gallery();

//-------------------------------------------------
function _gallery()
{
	// this object will get dymanic properties
}
_gallery.prototype.toString= showProps;

//-------------------------------------------------
function _slide()
{
	// properties are created by the parseXML member function
}
_slide.prototype.toString= showProps;

_slide.prototype.empty= function()
{
	this.id= null;
	this.title= null;
	this.img= null;
	this.media= null;
	this.url= null;
	this.text= null;
	this.moreInfo= null;
}
_slide.prototype.parseXML= function(xml)
{
	this.id= xml.getAttribute('id');
	this.title= xml.getAttribute('title');
	this.bg= xml.getAttribute('bg');
	$imgFile= xml.getAttribute('img');
	this.img= 'topics/'+galleryName+'/'+$imgFile;	// add topic name
	$mediaFile= xml.getAttribute('media');
	if($mediaFile)
		this.media= 'topics/'+galleryName+'/'+$mediaFile;	// add topic name
	this.url= xml.getAttribute('url');
	this.cycle= xml.getAttribute('cycle');
	if(xml.hasChildNodes())
		this.text= xml.firstChild.nodeValue;
	else
		this.text= "";
	nodeList= xml.getElementsByTagName('moreInfo');
	if(nodeList && (nodeList.length > 0))
	{
		this.moreInfo= nodeList.item(0).firstChild.nodeValue;
	}
	else
		this.moreInfo= "";
}
//-------------------------------------------------
function showProps()
{
	str= "<br />";
	for(prop in this)
	{
		if(typeof(this[prop]) != 'function')
			str += (prop+':'+this[prop]+"<br />\r\n");
	}
	return str;
}
function dumpObj(obj)
{
	str= "<br />";
	for(prop in obj)
		str += prop+':'+obj[prop]+"<br />\r\n";
	return str;
}

//-------------------------------------------------
// pass the gallery name without the xml extension
function getGallery(name)
{
	galleryName= name;	// save a global copy
	req= "ubaSrv.php?g="+galleryName;
	loadXMLDoc(req, null, onLoadedGallery);
}
//-------------------------------------------------
function onLoadedGallery(data,req)
{
//trace('<pre>'+data+'</pre>');
	//xmlDoc= req.responseXML.documentElement;	// getXMLDoc();
	// get a new DOM doc object
	xmlDoc= getXMLDoc();
	
	if(req.responseXML)
	{
//trace("typeof xmlDoc.loadXML: "+typeof xmlDoc.loadXML);
		if(typeof xmlDoc.loadXML != 'undefined')
			xmlDoc.loadXML(data);
		else
		{
			var oParser= new DOMParser();
			xmlDoc= oParser.parseFromString(data,"text/xml");
		}
	}
//trace("xmlDoc:"+xmlDoc.xml);
	xmlRoot= xmlDoc.documentElement;
	gallery.title= xmlRoot.getAttribute('title');
	gallery.bg= xmlRoot.getAttribute('bg');
	gallery.author= xmlRoot.getAttribute('author');
	gallery.audioMode= xmlRoot.getAttribute('audio');
	gallery.cycle= xmlRoot.getAttribute('cycle');
	nodeList= xmlRoot.getElementsByTagName('slide');
	gallery.slideCount= nodeList.length;
	trace(gallery.toString());
	
//	el= document.getElementById('gallery');
//	el.innerHTML= "Gallery: "+gallery.title;
//	el= document.getElementById('author');
//	el.innerHTML= "Author: "+gallery.author;
	el= document.getElementById('slideCount');
	if(el)
		el.innerHTML= "Slides: "+gallery.slideCount;

	document.getElementById('cbAudioAuto').checked= (gallery.audioMode == "auto");

	// show the opening slide with its overlay
	//autoShowMore= true;
	slide(null,1);
}
//-------------------------------------------------
function showMore()
{
	el= document.getElementById('moreInfoDiv');
	if(el.style.display == 'none')
	{
		el.innerHTML= curSlide.moreInfo;
		el.style.display= 'block';
	}
	else
		el.style.display= 'none';
}
//-------------------------------------------------
function slide(e,dir)
{
	if(e && e.shiftKey) dir *= 10;
	
	newSlide= currentSlide + dir;
	if(newSlide > gallery.slideCount) 
		newSlide= gallery.slideCount;
	else if(newSlide < 1) 
		newSlide= 1;
		
	showSlide(newSlide);
}
//-------------------------------------------------
function enablePrevNext()
{
	prevDisable= (currentSlide < 2)?true:false;
	el= document.getElementById('prevBtn');
	el.disabled= prevDisable;
	if(prevDisable)
		el.style.top= 0;
	nextDisable= (currentSlide == (gallery.slideCount))?true:false;
	el= document.getElementById('nextBtn');
	el.disabled= nextDisable;
	if(nextDisable)
		el.style.top= 0;
}
//-------------------------------------------------
function showSlide(newSlide)
{
	currentSlide= newSlide;
	
	// make sure the 'more text' is hidden
	document.getElementById('moreInfoDiv').style.display= 'none';
	
	// if there is audio playing then stop it
	if(curSound)
	{
		soundManager.destroySound(curSound.sID);
		curSound= null;
trace("<br /> curSound destroyed ");
	}

	el= document.getElementById('slideCount');
	el.innerHTML= "Slide: "+currentSlide+" of "+gallery.slideCount;

	enablePrevNext();
	reqSlide(currentSlide);
}
//-------------------------------------------------
function reqSlide(slideID)
{
	// empty the current slide info
	curSlide.empty();
	
	// request the XML record for the specified slide
	req= "ubaSrv.php?g="+galleryName+"&slide="+slideID;
trace(req);
	loadXMLDoc(req, null, onLoadedSlide);
}
//-------------------------------------------------
function onLoadedSlide(data,req)
{
trace('<pre>'+data+'</pre>');
	//xmlDoc= req.responseXML.documentElement;	// getXMLDoc();
	// get a new DOM doc object
	xmlDoc= getXMLDoc();

	if(data.indexOf("Error") != 0)
	{
		if(req.responseXML)
		{
//trace("typeof xmlDoc.loadXML: "+typeof xmlDoc.loadXML);
			if(typeof xmlDoc.loadXML != 'undefined')
			{
//trace("<br />using xmlDoc.loadXML ");
				xmlDoc.loadXML(data);
			}
			else
			{
//trace("<br />using DOMParser.parseFromString ");
				var oParser= new DOMParser();
				xmlDoc= oParser.parseFromString(data,"text/xml");
			}
			xmlRoot= xmlDoc.documentElement;
			// get all the new current slide info
			curSlide.parseXML(xmlRoot);
			trace("<br />Slide props:"+curSlide.toString());
		}
		else
		{
			trace("No XML response");
		}
	}	

	var rtSlide= false;			// RunTime slide flag

	// if there is no new slide then show the error message
	if(curSlide.id == null)
		document.getElementById('RunTimeSlide').innerHTML=data;
	else
	{
		if(curSlide.text && (curSlide.text.length > 0))
		{
			rtSlide= true;
			el= document.getElementById('RunTimeSlide');
			el.innerHTML=curSlide.text;
			if(curSlide.img != null)
			{
				el.style.backgroundImage= "url("+curSlide.img+")";
			}
			else
				el.style.backgroundColor= curSlide.bg;
		}
		else
		{
			el= document.getElementById('slideImg');
			if(curSlide.img.indexOf('/') >= 0)
				el.src= curSlide.img;
			else
				el.src= 'slides/'+curSlide.img;
		}
	}	
	// show either the slide image div or the slide text div
	el= document.getElementById('slideImgDiv');
	el.style.display= rtSlide? 'none':'inline';
	
	el= document.getElementById('RunTimeSlide');
	el.style.display= rtSlide? 'inline':'none';
	
	// enable the 'More Info' button if there is an image and text
	miDisabled= (!curSlide.moreInfo || (curSlide.moreInfo.length == 0));
	el= document.getElementById('btnMoreDiv');
	if(el) el.style.display= miDisabled?"none":"block";
	
	// 'autoShowMore' is set if the moreInfo is to be shown without first clicking
	if(autoShowMore)
	{
		if(!miDisabled)
			showMore();
		autoShowMore= false;
	}

	// if there is audio for this slide then enable the audio section	
	var audioDisable= (curSlide.media == null)? true:false;
	var autoAudio= document.getElementById('cbAudioAuto').checked;
trace("<br />  audioDisable="+audioDisable+", autoAudio="+autoAudio+"<br />");

	if(!audioDisable && soundManager.supported())
	{
		curSound= soundManager.createSound({id:'soundID', url:curSlide.media, autoPlay:autoAudio, onfinish:soundFinished});
trace("  curSound created ("+curSlide.media+")<br />");
	}
	//document.getElementById('cbAudioAuto').disabled= audioDisable;
	document.getElementById('audioStop').disabled= audioDisable;
	document.getElementById('audioPlay').disabled= audioDisable;
}
//-------------------------------------------------
function audio(mode)
{
	if(!curSound)
	{
		var autoAudio= document.getElementById('cbAudioAuto').checked;
		curSound= soundManager.createSound({id:'soundID', url:curSlide.media, autoPlay:autoAudio, onfinish:soundFinished});
	}
	if(mode == 'play')
	{
		if(curSound.paused)
			curSound.resume();
		else
			curSound.play();
	}
	else
	{
		curSound.pause();
	}
}
//-------------------------------------------------
function trace(str)
{
	if(!debug) return;
	if(!document.traceWin || document.traceWin.closed)
		document.traceWin= open("","traceWindow","width=700,height=500,resizable,scrollbars");
	document.traceWin.document.writeln(str);
}
function traceClear()
{
	if(document.traceWin)
	{
		document.traceWin.document.open();
		document.traceWin.document.writeln('Trace Window:');
	}
}
