
/*----------------------------------------------------------------------------
SWAPCONTENT.JS

Copyright by
plenum stoll & fischbach Communication GmbH,
Herrenberg, Germany

swapcontent.js 		contains functionality to swap content,
					counting of divs to swap and
					building swap navi happens automatically
----------------------------------------------------------------------------*/


// Defining initial swap content
var activeSwapElement = 1;
var swapContentDivCounter;
var swapContentImgCounter;
var o_swapContentNavi;
var onloadTrigger = true;


//	swapContent()
//
//	arguments:
//	- swapTo (required):	direction or index of element to be swapped to
//
//	what:	swaps divs by direction or by index
//	how:	by showing and hiding divs with display 'block' and 'none'
function swapContent(swapTo)
{
		// get activeSwapElement, that was active before swapping
		activeSwapElementBefore = activeSwapElement;

		// counter handling
		if (swapTo == '+') 
		{
			if (activeSwapElementBefore < swapContentDivCounter)
				++activeSwapElement;
		}
		else if (swapTo == '-')
		{
			if (activeSwapElementBefore > 1)
			 	--activeSwapElement;
		}
		else if (swapTo)
		{
			activeSwapElement = swapTo;
		}

		if (o_swapContentNavi.id == 'swapContentNavi')
		{
			// clear highlighting of navi item
			var lastNaviItem = '';
			if (activeSwapElementBefore == swapContentDivCounter) lastNaviItem = ' last';
			o_swapTextLink = document.getElementById('swapTextLink' + activeSwapElementBefore);
			o_swapTextLink.className = 'swapTextLink' + lastNaviItem;
	
			// highlight navi item
			var lastNaviItem = '';
			if (activeSwapElement == swapContentDivCounter) lastNaviItem = ' last';
			o_swapTextLink = document.getElementById('swapTextLink' + activeSwapElement);
			o_swapTextLink.className = 'swapTextLinkActive' + lastNaviItem;
		}
		else if (o_swapContentNavi.id == 'swapContentNaviOnlyImages')
		{
			// clear highlighting of navi item
			if (activeSwapElementBefore != activeSwapElement)
			{
			
				var o_img = document.getElementById('swapContentImgNavi' + activeSwapElementBefore);
				o_img.src = o_img.src.replace(/_over/,'');
				// set all initial properties to mouseOverImages
				initMouseOverImages();
			}

			// highlight navi item by removing mouseover events (overImage will be shown)
			var o_img = document.getElementById('swapContentImgNavi' + activeSwapElement);
			// set over image (only needed at initial onload event)
			o_img.onmouseout = function() {};
			o_img.onmouseover = function() {};
			if (onloadTrigger == true)
			{
				o_img.src = o_img.src.replace(/(\.(gif|jpg))/, '_over' + '$1');
				onloadTrigger = false;
			}
		}

		// hide element before swapping
		elementObj = document.getElementById('swapContent' + activeSwapElementBefore);
		elementObj.style.display = 'none';
		elementObj.style.visibility = 'hidden';
				
		// show element to swap to
		elementObj = document.getElementById('swapContent' + activeSwapElement);
		elementObj.style.display = 'block';
		elementObj.style.visibility = 'visible';
}


//	initSwapContentNavi()
//
//	no arguments
//
//	what:	initializes swapContent navigation full automatic
//	how:	by looking, how much divs are to swap, hiding all divs
//			and building the swapContentNavi
function initSwapContentNavi()
{
	if (document.getElementById && document.getElementsByTagName)
	{
		var o_swapContentContainer = document.getElementById('swapContentContainer');

		// Find out kind of navigation
		o_swapContentNavi = document.getElementById('swapContentNavi');
		if (!o_swapContentNavi)
		{
			o_swapContentNavi = document.getElementById('swapContentNaviOnlyImages');

			// get amount of divs to swap by img in swapContentNaviOnlyImages div
			var a_swapContentImg = o_swapContentNavi.getElementsByTagName('img');
			swapContentDivCounter = a_swapContentImg.length - 1;
		}
		else
		{
			// get amount of divs to swap by divs in swapContentContainer div
			var a_swapContentDiv = o_swapContentContainer.getElementsByTagName('div');
			swapContentDivCounter = a_swapContentDiv.length - 1;
		}

		// hide all swapContents
		for (var i=1; i<=swapContentDivCounter; i++)
		{
			document.getElementById('swapContent' + i).style.display = 'none';
			document.getElementById('swapContent' + i).style.visibility = 'hidden';
		}

		if (o_swapContentNavi.id == 'swapContentNavi')
		{
			// build swapContentNavi with back-, next-button and number navigation
			var lastNaviItem = '';
			var swapContentNaviHtmlCode = '<a href="#" onclick="swapContent(\'-\');return false;"><img class="mouseOverImage" src="' + backButtonSrc + '" alt="' + backButtonText + '" /></a>';
			for (var i=1; i<=swapContentDivCounter; i++)
			{
				if (i == swapContentDivCounter) lastNaviItem = ' last';
				var activeNaviItem = activeSwapElement;
				swapContentNaviHtmlCode += '<a href="#" onclick="swapContent(' + i + ');return false;" class="swapTextLink' + lastNaviItem + '" id="swapTextLink' + i + '">' + i + '</a>';
			}
			swapContentNaviHtmlCode += '<a href="#" onclick="swapContent(\'+\');return false;"><img class="mouseOverImage" src="' + nextButtonSrc + '" alt="' + nextButtonText + '" /></a>';

			o_swapContentNavi.innerHTML = swapContentNaviHtmlCode;
		}
		else if (o_swapContentNavi.id == 'swapContentNaviOnlyImages')
		{
			// get amount of imgs to swap
			a_swapContentImg = o_swapContentNavi.getElementsByTagName('img');
			swapContentImgCounter = a_swapContentImg.length;
			var swapContentNaviHtmlCode = '';

			for (var i=0; i<swapContentImgCounter; i++)
			{
				swapContentNaviHtmlCode += '<a href="#" onclick="swapContent(' + (i+1) + ');return false;"><img id="swapContentImgNavi' + (i+1) + '" class="mouseOverImage" src="' + a_swapContentImg[i].src + '" alt="' + a_swapContentImg[i].alt + '" /></a>';
			}

			o_swapContentNavi.innerHTML = swapContentNaviHtmlCode;
		}
	}
}

// Add function to window.onload event
AddToOnloadHandler(initSwapContentNavi);
AddToOnloadHandler(swapContent);

