//Javascript Document
var vportId;
var maxPos=-1;//Position of the last picture which has been loaded.
var preloaded=false;
var memory=[];

/**
 * Loads a window and displays a library of images
 */
function showDiapos(libIdx, sectionName)
{
	var window_idx='';
	var viewportId='';
	
	window_idx=libIdx + 1;
	viewportId="Win" + window_idx  + "Contents";
	loadWindow("Layer1", window_idx);
	
	loadDiaporamaSection(viewportId, sectionName, libIdx);	
}

function loadDiaporamaSection(viewportId, sectionName, libIdx)
{
	if(xmlhttp==null)
	{
		InitXmlHttp();
	}
	if(xmlhttp==null)
	{
		return false;
	}
	
	vportId=viewportId;	
	
	currentLibIdx=libIdx;
	
	xmlhttp.open('post', '../section/get.php');
	xmlhttp.onreadystatechange = getDiaporamaSectionData;
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('name=' + sectionName + '&format=xml');
}

function getDiaporamaSectionData()
{
	if(xmlhttp==null)return;
	
	var body;
	var thumbs;
	var viewports;
	var arr;
	var i;
	var j;
	var k;
	var buffer;
	var diapo;
	var viewport;
	var elm;
	var numofthumbsperrow;
	
	buffer="";
	
	libs[currentLibIdx]=new Array();

	if(xmlhttp.readyState == 4)
	{
		thumbs=new Array();
		if(xmlhttp.responseXML==null) return;
		
		thumbs=xmlhttp.responseXML.getElementsByTagName("thumb");
				
		for(i=0;i<thumbs.length;i++)
		{
			diapo=new Diapo();
			diapo.desc='&nbsp;';
			for(j=0;j<thumbs[i].childNodes.length;j++)
			{
				switch(thumbs[i].childNodes[j].tagName)
				{
					case 'sml':
						//buffer+=thumbs[i].childNodes[j].firstChild.nodeValue + "\n";			
						diapo.smlPic=thumbs[i].childNodes[j].firstChild.nodeValue;
						break;
					case 'lrg':
						//buffer+=thumbs[i].childNodes[j].firstChild.nodeValue + "\n";			
						diapo.lrgPic=thumbs[i].childNodes[j].firstChild.nodeValue;
						break;
					case 'desc':						
						diapo.desc=getNodeContentXml(thumbs[i].childNodes[j]);
						break;
						
				}	
			}
			libs[currentLibIdx][i]=diapo;
		}
		
		diapoBufferLength=0;
		
		arr=new Array();
		
		if(xmlhttp.responseXML==null) return;
		
		arr=xmlhttp.responseXML.getElementsByTagName("numofthumbsperrow");
		
		if(arr[0])
		{
			diapoBufferLength=arr[0].childNodes[0].nodeValue;
		}
		
		//alert(diapoBufferLength);
		
		viewports=new Array();
		viewports=xmlhttp.responseXML.getElementsByTagName("viewport");
		
		arr=new Array();
		
		for(i=0;i<viewports.length;i++)
		{
			viewport=new Viewport();
			for(j=0;j<viewports[i].childNodes.length;j++)
			{
				switch(viewports[i].childNodes[j].tagName)
				{
					case 'type':
						//buffer+=viewports[i].childNodes[j].firstChild.nodeValue + "\n";			
						viewport.type=viewports[i].childNodes[j].firstChild.nodeValue;
						break;
					case 'x':
						//buffer+=viewports[i].childNodes[j].firstChild.nodeValue + "\n";			
						viewport.x=new Number(viewports[i].childNodes[j].firstChild.nodeValue);
						break;
					case 'y':
						//buffer+=viewports[i].childNodes[j].firstChild.nodeValue + "\n";			
						viewport.y=new Number(viewports[i].childNodes[j].firstChild.nodeValue);
						break;
				}	
			}
			arr[i]=viewport;
		}
		
		loadDiaporama(vportId, currentLibIdx);
		
		elm=document.getElementById("Viewport" + currentLibIdx);
		elm.style.width=arr[0].x+10;
		elm.style.height=arr[0].y+10;
		elm.style.margin='auto';
				
		loadImageViewer(("Viewport" + currentLibIdx), ("LrgDisp" + currentLibIdx));
	}
}

var phase=0;

function showNextDiapo(loop)
{
	var diapoPos=0;
	var diap;

	if(!loop)
	{
		loop=false;
	}
	
	/*if(currentDiapoIdx<libs[currentLibIdx].length-1)
	{
		preloadPic(currentLibIdx, currentDiapoIdx+1);
	}*/
		
	if(currentDiapoPos<diapoBufferLength-1)
	{
		//currentDiapoIdx++;
		compressSwitchExpand("Viewport" + currentLibIdx, "x", currentDiapoPos+1)
		return;
	}
	
	if(currentDiapoIdx<libs[currentLibIdx].length-1)
	{
		currentDiapoIdx++;
		compressSwitchExpand("Viewport" + currentLibIdx, "x", currentDiapoPos)
		return;
	}
	
	if(loop==true)
	{
		currentDiapoIdx=0;
		currentDiapoPos=0;
		compressSwitchExpand("Viewport" + currentLibIdx, "x", currentDiapoPos)
	}
}

function showPrevDiapo(loop)
{
	var diapoPos=0;
	
	if(!loop)
	{
		loop=false;
	}
	
	/*
	if(currentDiapoIdx>0)
	{
		preloadPic(currentLibIdx, currentDiapoIdx-1);
	}*/
	
	if(currentDiapoPos>0)
	{
		compressSwitchExpand("Viewport" + currentLibIdx, "x", currentDiapoPos-1)
		return;
	}
	
	if(currentDiapoIdx>0)
	{
		currentDiapoIdx--;
		compressSwitchExpand("Viewport" + currentLibIdx, "x", currentDiapoPos)
		return;
	}
	
	if(loop==true)
	{
		diapoPos=currentDiapoPos;
		currentDiapoIdx=libs[currentLibIdx].length-1;
		currentDiapoPos=diapoBufferLength-1;
		compressSwitchExpand("Viewport" + currentLibIdx, "x", currentDiapoPos)
	}
}

function compressSwitchExpand(id, axis, switchToDiapoIdx)
{
	var img;
	iwElm=document.getElementById(id);
	iElm=iwElm.getElementsByTagName('img')[0];
		
	if(!axis || axis=='auto')
	{
		axis='x';
		if(iElm.height>iElm.width)
		{
			axis='y';
		}
	}
	
	clearInterval(iwTimer);
	
	iwElm.style.overflow='hidden';
	
	expandedWidth=iElm.width;
	expandedHeight=iElm.height;
	speed=25;
	
	img=new Image();
	
	//Will start the animation after the target image is loaded.
	img.onload=function(){iwTimer=setInterval("compressByOneSwitchExpand('" + axis +  "'," + switchToDiapoIdx + ")",33);};	
	img.src='../files/' + libs[currentLibIdx][currentDiapoIdx-currentDiapoPos+switchToDiapoIdx].lrgPic;
	//alert(img.onload);
	//iwTimer=setInterval("compressByOneSwitchExpand('" + axis +  "'," + switchToDiapoIdx + ")",33);
}

function compressByOneSwitchExpand(axis, switchToDiapoPos)
{
	switch(phase)
	{
		case 0://Compression
			compressWinByOne(axis, false);
			if(timesCompressedY==100 || timesCompressedX==100)
			{
				phase++;			
			}
			break;
		case 1://Image switching
			_showDiapoByPos(switchToDiapoPos);
			refreshThumbs();
			expandedWidth=iElm.width;
			expandedHeight=iElm.height;
			phase++;
			break;
		case 2://Expansion
			expandWinByOne(axis, false);
			if(timesCompressedX==0 && timesCompressedY==0)
			{
				phase=0;
				clearInterval(iwTimer);
			}
			break;
	}
}

/**
 * Overloads function of the same name in diaporama.js
 */
function showDiapoByPos(switchToDiapoPos)
{
	//preloadPic(currentLibIdx, currentDiapoIdx + switchToDiapoPos);
	compressSwitchExpand("Viewport" + currentLibIdx, "x", switchToDiapoPos);
}
