var navModImages;
var leftPan=0;
var rightPan=0;
var oldSrc="";
// simple image preload function
function preloadImages(imgArray) {
	if(imgArray) {
		if(imgArray.length) {
			for (var i=0;i<imgArray.length;i++) {
				var nullImg = new Image();
				nullImg.src = imgArray[i];
			}
		}
	}
}

// match height of columns with specific class to the heighest of them
function matchColumns(className,panCtr)
{
	// set sleep time for different browser, default is half a second
	var userAgent = navigator.userAgent.toLowerCase();
	var sleepTime = 0;
	if (userAgent.indexOf("safari")!=-1) sleepTime=1650;
	if (className=='hdrcols') sleepTime+=500;
	window.setTimeout(function()
	{
		var allDivs,matchDivs,maxHeight,divHeight,newHeight,tmpObj,panHeight,panObj;
	
		// get all search elements in the document and init match div array
		allDivs=document.getElementsByTagName('div'); 
		matchDivs=[];
			
		// initialize maximum height value
		maxHeight=0;
		minHeight=0;
	
		// initialize for panorama
		panHeight = 0;
		panObj = getObject(panCtr);
		if(panObj)
		  panHeight = getElementHeight(panObj);
		// iterate over all search elements in the document
		for(var i=0;i<allDivs.length;i++)
		{
			// clean height stile before set height style.
			if (allDivs[i].className=="colArea") {
				allDivs[i].style.height=""; 
			}
			
			if (allDivs[i].className=="panSurCtrLeft")
				leftPan=1;
			if (allDivs[i].className=="isRightNav")
				rightPan=1;
			
			// make collection with <div> elements
			if(allDivs[i].className.search(className) >= 0)
			{
				tmpObj=allDivs[i];
				matchDivs[matchDivs.length]=tmpObj; 
	
				// determine height for <div> element
				divHeight = getElementHeight(tmpObj);
	
				// the panorama never regards the first column,
				// add panorama height if not first column
				if(i>0)
				  divHeight = divHeight+panHeight;
	
				// calculate maximum height 
				maxHeight=Math.max(maxHeight,divHeight); 
			} 
	
		}
	
		// assign maximum height value to all of container <div> elements
		for(var i=0;i<matchDivs.length;i++)
		{	
			if (leftPan!=1) {
				newHeight = (i>0) ? maxHeight-panHeight : maxHeight; 
			} else if ((i>0) && (rightPan == 1)) { // check right vertical nav bafore "if (i<2)". Column 2 or 3 have maxHeight without cutting.
				newHeight = maxHeight;
			} else if ((i<3) && (rightPan == 1)) { 
				newHeight = maxHeight-panHeight;
			} else if (i<2) { // if panorama is on left with 3 columns cut columns 1 and 2
				newHeight = maxHeight-panHeight;  
			} else if (i>1) {
				newHeight = maxHeight;
			}
			matchDivs[i].style.height=newHeight + "px"; 
		}
	}, sleepTime);
}

// apply the height of a master element to other with
// the same class, but only if needed.
// other elements can be on the same level or childs
function matchColumnsByMaster(columnId,className,panCtr)
{
	var masterHeight,allDivs,tmpColumn,oldHeight;
	var masterColumn = getObject(columnId);

	if(masterColumn)
	{
		// if there is none of the searched classNames
		// on the same level (as attribute on the object)
		// we should get the object with that className
		// as master column object
		if(masterColumn.className.search(className) < 0)
		  masterColumn = findChildClass(masterColumn,className);

		if(masterColumn)
		{
			// initialize new height value
			oldHeight=masterColumn.style.height;
			masterColumn.style.height = "";
			masterHeight=getElementHeight(masterColumn)+"px";
		
			// iterate over all search elements in the document
			if(masterHeight != oldHeight)
			{
				allDivs=document.getElementsByTagName('div'); 
				for(var i=0;i<allDivs.length;i++)
				{
					if(allDivs[i].className.search(className) >= 0)
					{
						allDivs[i].style.height=masterHeight;
					}
				}
			}
			// altough we didnt change anything to the other columns,
			// we should reapply the earlier removed height of the
			// master column
			else
			{
				masterColumn.style.height = masterHeight;
			}
		}
	}

}
// this function is just a helper of matchColumnsByMaster
//
//   hint: this method is limited to elements with just a single! child block element
//
function findChildClass(object,className)
{
	var child;
	if(object)
	{
		// here we jump over empty spaces because they
		// are not a block element
		for(var i=0;i<object.childNodes.length;i++)
		{
			if(object.childNodes[i].nodeType == '1')
			{
			  // take current (first present) child
			  child = object.childNodes[i];
			  // and stop jumping
			  i=object.childNodes.length;
			}
		}
		if(child != object)
		{
			// if the current child has className
			// stop and return object/child through
			// the recursive call, else step deeper
			// into recursive call and search next
			// child
			if(child.className.search(className) < 0)
			{
			  if(child.hasChildNodes())
			    child = findChildClass(child,className);
			  else
			    child = null;
			}
		}
		else
		{
		  child = null;
		}
	}
	return child;
}

// matches the columns by master width
function matchCopyColumn(masterElement,slaveElement)
{
	// read height of master element and copy to slave element
	var masterObj = getObject(masterElement);
	var slaveObj = getObject(slaveElement);
	if(masterObj && slaveObj)
	{
		window.setTimeout(function()
		{
			setElementHeight(slaveObj,getElementHeight(masterObj));
		}, 99);
	}
}

// reset cols heights by css class name
function resetColHeights(colClass)
{
	// reset height of all columns that participate at gallery view
	if(colClass)
	{
		// query a list of all possible elements
	    colDivs=document.getElementsByTagName('div');
	
		// iterate over all possible elements
		for(var i=0;i<colDivs.length;i++)
		{ 
			// make collection with <div> elements with class attribute 'colClass'
			if(colDivs[i].className.search(colClass) >= 0)
				{
					// reset height of found div element
				colDivs[i].style.height = "";
			}
		}
	}
}

// change style of elements when mouseover or mouseout
function changeStyle(mouse,objects)
{
	var classString;
	if(mouse == 0 || mouse == 1)
	{
		// change for each element in object list
		for(var i=0;i<objects.length;i++)
		{
			// if object is not null
			if(document.getElementById(objects[i]))
			{
				// fetch classnames of object
				classString = document.getElementById(objects[i]).className;

				// switch mouseover classes
				switch(mouse)
				{
					case 0:
					  classString = classString.replace(/\bmouseOver\b/, 'unselected');
					  break;
					case 1:
					  classString = classString.replace(/\bunselected\b/, 'mouseOver');
					  break;
				}
				// replace new with old string
				document.getElementById(objects[i]).className = classString;
			}
		}
	}
}

// changes the style of a button on mouse over
function changeStyleModBtn(mouse,object)
{
	if ((object.parentNode.className=="unselected") || (object.parentNode.className=="mouseOver") || (object.parentNode.className=="selected"))
	{
		var classString=object.parentNode.className;
		var idString=object.parentNode.id;
		var idString=idString.split("_")[1];
		activeButton=idString;
		switch(mouse)
		{
			case 0:
				if (activeButton && var2Row!="OK") {
					if (document.getElementById('sub'+idString)) {
					  if (document.getElementById('sub'+idString).firstChild==null) {
					 	 classString = classString.replace(/\bmouseOver\b/, 'unselected');
					  }
					 }
				} else {
					classString = classString.replace(/\bmouseOver\b/, 'unselected');
					var2Row="";
				}
			  break;
			case 1:
			  classString = classString.replace(/\bunselected\b/, 'mouseOver');
			  break;
		}
		object.parentNode.className=classString;
	}
	else
	{
		changeStyleModBtn(mouse,object.parentNode);
	}
}

// change image and caption in gallery module
function changeImage(number,data,imageId,captionId,tooltipId,colClass,panoramaCtr,masterCol,slaveCol)
{
	// default
	var imgElement = null;
	var cptElement = null;
	var ttElement = null;

	// reset height of all columns that participate at gallery view
	if(colClass)
	{
		resetColHeights(colClass);
	}

	// operate if input vars are valid
	if(data.length > 0 && number >= 0 && imageId && captionId)
	{
		// replace image
		if(imgElement = document.getElementById(imageId))
		{
			// set attributes with input var values
			imgElement.setAttribute("width", data[number].width,0);
			imgElement.setAttribute("height", data[number].height,0);
			imgElement.setAttribute("alt", data[number].alt,0);
			imgElement.setAttribute("src", data[number].url,0);
		}

		// replace caption
		if(cptElement = document.getElementById(captionId))
		{
			// fill node value with input var value
			if (cptElement.firstChild) {
			  cptElement.firstChild.nodeValue = data[number].caption;
			}
		}

		// replace tooltip
		if(ttElement = document.getElementById(tooltipId))
		{
			// fill node value with input var value
			ttElement.innerHTML = data[number].tooltip;
		}
	}

	// reset height of all columns that participate at gallery view
	if(colClass)
	  matchColumns(colClass,panoramaCtr);
	if(masterCol && slaveCol)
	  matchCopyColumn(masterCol,slaveCol);
}

// changes the image of navigation module item
function resetNavImage()
{
	var i,x,a=navModImages;
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc&&x.oWidth&&x.oHeight;i++)
	{ x.src=x.oSrc; x.width=x.oWidth; x.height=x.oHeight; }
}
function swapNavImage(url,width,height,node)
{
	var i,j=0,x;
	navModImages=new Array;

	if ((x=findChildImage(node))!=null)
	{
		navModImages[j++]=x;
		if(!x.oSrc)
		{
		  x.oSrc=x.src;
		  x.oWidth=x.width;
		  x.oHeight=x.height;
		}
		x.src=url;
		x.width=width;
		x.height=height;
	}
}
function findChildImage(object)
{
	var child;
	if(object)
	{
		for(var i=0;i<object.childNodes.length;i++)
		{
			if(object.childNodes[i].nodeType == '1')
			{
			  child = object.childNodes[i];
			  i=object.childNodes.length;
			}  
		}
		if(child != object)
		{
			if(child.nodeName != "IMG")
			{
			  if(child.hasChildNodes())
			    child = findChildImage(child);
			  else
			    child = null;
			}
		}
		else
		{
		  child = null;
		}
	}
	return child;
}

// compute next image object id if navigate back or forward
function computeNavStep(step,current,data,btnBack,btnForward,folioIds)
{
	// default
	var id = current;
	var btnBackElement = null;
	var btnForwardElement = null;
	var all = 0;
	var maxFolios = 20;
	var hideCount = 0; var hideFolioTill = 0; var hideFolioFrom = 0;
	var folioElements = new Array();

	// fill variable with counter of all images
	if(data) all = data.length-1;

	// fill hideCount
	if (data) hideCount = all - maxFolios;

	// compute
	if(step == '-') id -= 1;
	if(step == '+') id += 1;

	// decide to reset and hide back forward buttons
	if(id <= 0)
	{
		// set id to min
		id = 0;

		// hide or unhide nav button if needed
		if(btnBackElement = document.getElementById(btnBack))
		  btnBackElement.style.display = 'none';
	}
	if(id >= all)
	{
		// set id to max
		id = all;

		// hide or unhide nav button if needed
		if(btnForward)
		  if(btnForwardElement = document.getElementById(btnForward))
		    btnForwardElement.style.display = 'none';
	}
	
	// unhide back forward buttons when needed
	if(id < all)
	{
		if(btnForward)
		  if(btnForwardElement = document.getElementById(btnForward))
		    btnForwardElement.style.display = 'inline';
	}
	if(id > 0)
	{	
		if(btnBack)
		  if(btnBackElement = document.getElementById(btnBack))
		    btnBackElement.style.display = 'inline';
	}

	// hide single folios if needed
	var maxIntFolios = maxFolios-1;
	if(folioIds && data)
	{
		if(all > maxIntFolios)
		{
			// if current position is at the end of the list
			if(id > (hideCount + Math.round(maxIntFolios/2)))
			{
				// hide last shorter and show first shorter
				if(folioElements[0] = document.getElementById(folioIds+'[0]'))
				  folioElements[0].style.display = 'inline';
				if(folioElements[all+2] = document.getElementById(folioIds+'['+(all+2)+']'))
				  folioElements[all+2].style.display = 'none';
				
				// hide folios
				for(var i=0;i<=hideCount;i++)
				{
					if(folioElements[i+1] = document.getElementById(folioIds+'['+ (i+1) +']'))
					  folioElements[i+1].style.display = 'none';
				}
				
				// display the other folios
				for(var i=hideCount+1;i<=all;i++)
				{
					if(folioElements[i+1] = document.getElementById(folioIds+'['+ (i+1) +']'))
					  folioElements[i+1].style.display = 'inline';
				}
			}
			// if current position is at the beginning of the list
			else if(id <= Math.round(maxIntFolios/2))
			{
				// hide first shorter and show last shorter
				if(folioElements[0] = document.getElementById(folioIds+'[0]'))
				  folioElements[0].style.display = 'none';
				if(folioElements[all+2] = document.getElementById(folioIds+'['+(all+2)+']'))
				  folioElements[all+2].style.display = 'inline';
				
				// hide folios
				for(var i=maxFolios;i<=all;i++)
				{
					if(folioElements[i+1] = document.getElementById(folioIds+'['+ (i+1) +']'))
					  folioElements[i+1].style.display = 'none';
				}
				
				// display the other folios
				for(var i=0;i<maxFolios;i++)
				{
					if(folioElements[i+1] = document.getElementById(folioIds+'['+ (i+1) +']'))
					  folioElements[i+1].style.display = 'inline';
				}
			}
			// else current position is somewhere between
			else
			{
				// compute
				var hideFolioTill = id-Math.round(maxFolios/2);
				var hideFolioFrom = id+Math.round(maxFolios/2);
	
				// show first and last shorter
				if(folioElements[0] = document.getElementById(folioIds+'[0]'))
				  folioElements[0].style.display = 'inline';
				if(folioElements[all+2] = document.getElementById(folioIds+'['+(all+2)+']'))
				  folioElements[all+2].style.display = 'inline';
	
				// hide folios
				for(var i=0;i<=hideFolioTill;i++)
				{
					if(folioElements[i+1] = document.getElementById(folioIds+'['+ (i+1) +']'))
					  folioElements[i+1].style.display = 'none';
				}
				for(var i=hideFolioFrom;i<=all;i++)
				{
					if(folioElements[i+1] = document.getElementById(folioIds+'['+ (i+1) +']'))
					  folioElements[i+1].style.display = 'none';
				}
				
				// display the other folios
				for(var i=hideFolioTill+1;i<hideFolioFrom;i++)
				{
					if(folioElements[i+1] = document.getElementById(folioIds+'['+ (i+1) +']'))
					  folioElements[i+1].style.display = 'inline';
				}
			}
		}
		else
		{
			// hide first and last shorter
			if(folioElements[0] = document.getElementById(folioIds+'[0]'))
			  folioElements[0].style.display = 'none';
			if(folioElements[all+2] = document.getElementById(folioIds+'['+(all+2)+']'))
			  folioElements[all+2].style.display = 'none';
		}

		// change class of the button of the current image id to apply interaction effects
		if(folioElements[id+1] = document.getElementById(folioIds+'['+(id+1)+']'))
		  folioElements[id+1].className = folioElements[id+1].className + ' selected';

		// change class of all other buttons to reset interaction effects to css defaults
		var classString;
		for(var i=0; i<data.length; i++)
		{
			if(i != id)
			{
				if(folioElements[i+1] = document.getElementById(folioIds+'['+(i+1)+']'))
				  folioElements[i+1].className = folioElements[i+1].className.replace(/\bselected\b/, '');
			}
		}
	}

	// return computed id
	return id;
}
