/*
*************************************************************************
*                                                                       *
* Copyright 2008 Steffen Erlecke                                        *
*                                                                       *
* This program is free software: you can redistribute it and/or modify  *
* it under the terms of the GNU General Public License as published by  *
* the Free Software Foundation, either version 3 of the License, or     *
* (at your option) any later version.                                   *
*                                                                       *
* This program is distributed in the hope that it will be useful,       *
* but WITHOUT ANY WARRANTY; without even the implied warranty of        *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
* GNU General Public License for more details.                          *
*                                                                       *
* You should have received a copy of the GNU General Public License     *
* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
*                                                                       *
* see http://www.e-i-s-solutions.com                                    *
*************************************************************************
*/
var activeButton;
var FilePath;
var WindowParameter = new Array ();

function getURLParameter ()
{
	var QueryString = window.location.href.split ('?');
	var URLParameter = new Array ();
	
	if (QueryString[1])
	{
		var URLParameters = QueryString[1].split ('&');
	
		for (var i = 0; i < URLParameters.length; i++)
		{
			URLParameter.push (URLParameters[i].split ('='));
		}
	}
	
	return URLParameter;
}

function init ()
{
	var URLParameter = getURLParameter ();
	var jumpTarget;
	
	if (FilePath.substr (FilePath.length - 1, 1) != '/')
		FilePath += '/';

	for (var i = 0; i < URLParameter.length; i++)
	{
		if (URLParameter[i][0] == 'jump' && URLParameter[i][1])
		{
			jumpTarget = URLParameter[i][1].split ('_');
		}
	}

	var element = document.getElementsByName('Button');

	for (var i = 0; i < element.length; i++)
	{
		if (!jumpTarget && i == 0)
		{
			jumpTarget = element[i].id.split ('_');
			jumpTarget.shift ();												// we don't need to know that it is a button
		}
			
		element[i].onclick = function (event) {clickButton (event)};
		element[i].onmouseover = function (event) {rolloverButton (event)};
		element[i].onmouseout = function (event) {normalButton (event)};
		element[i].style.cursor = 'pointer';
	}

	element = document.getElementsByTagName('MAP');

	for (var i = 0; i < element.length; i++)
	{
		for (var j = 0; j < element[i].childNodes.length; j++)
		{
			var MapHRef = new String (element[i].childNodes[j].href);
			if (MapHRef.substr (MapHRef.length - 1, 1) == "#")
				element[i].childNodes[j].onclick = clickLink;
		}
	}

	TargetFileNameExtension = jumpTarget.pop ();
	TargetID = jumpTarget.join ('_');
	
	if (document.getElementById ('content').tagName == 'IMG')
	{
		document.getElementById ('content').src = FilePath + 'content_' + TargetID + '.' + TargetFileNameExtension;

		if (document.getElementsByTagName('MAP')[TargetID])
			document.getElementById ('content').useMap = '#' + TargetID;
		else
			document.getElementById ('content').useMap = '#';
			
		if (document.getElementsByTagName('DIV')[TargetID])
			document.getElementsByTagName('DIV')[TargetID].style.display = 'block';
	}

	var jumpButton = document.getElementById ('bt_' + TargetID + '_' + TargetFileNameExtension);
	if (jumpButton.tagName == 'IMG')
	{
		jumpButton.src = FilePath + 'bt_' + TargetID + '_hl' + '.' + TargetFileNameExtension;
		activeButton = getTargetData (jumpButton);
	}
}

function getTargetData (Event)
{
	var Target;
	var TargetData = new Object ();

	if (Event && Event.id)															// maybe we got a target, not an event
		Target = Event;
	else
	{
		if (!Event)
			var Event = window.event;
	
		if (Event.target)															// official W3C standard
			Target = Event.target;
		else
			if (Event.srcElement)													// Micro$oft like
				Target = Event.srcElement;
		
		if (Target.nodeType == 3)													// Safari bug
			Target = Target.parentNode;
	}
	
	if (!Target)
	{
		return null;
	}

	TargetData['Target'] = Target;
	
	if (Target.tagName == 'IMG')
	{
		var SRCFilePath = Target.src.split ('/');										// src contains complete path information
		var FileName = SRCFilePath[SRCFilePath.length - 1].split ('.');					// file name HAS TO BE in format type_id[_sub-id]_state.extension
		TargetData['SRCFileNameExtension'] = FileName[1];
	
		var TargetSRC = FileName[0].split ('_');									// getting informations from SRC attribute
		TargetData['State'] = TargetSRC[TargetSRC.length - 1];
		TargetSRC.shift ();
		TargetSRC.pop ();
		TargetData['FileName'] = TargetSRC.join ('_');

		TargetData['FilePath'] = FilePath;
	}
	
	var TargetID = Target.id.split ('_');										// getting informations from ID attribute
	TargetData['Type'] = TargetID.shift ();										// we need to know the type
	TargetData['IDFileNameExtension'] = TargetID.pop ();						// and also the FileExtension given in the ID
	TargetData['MenuLevel'] = TargetID.length;
	TargetData['ID'] = TargetID.join ('_');										// we'd like to have a complete ID string
	
	TargetData['TagName'] = Target.tagName;
	return TargetData;
}

function clickButton (event)
{
	var TD = getTargetData (event);

	if (document.getElementById ('content').tagName == 'IMG')
	{
		document.getElementById ('content').src = TD['FilePath'] + 'content_' + TD['ID'] + '.' + TD['IDFileNameExtension'];

		if (document.getElementsByTagName('MAP')[TD['ID']])
			document.getElementById ('content').useMap = '#' + TD['ID'];
		else
			document.getElementById ('content').useMap = '#';
		
		if (document.getElementsByTagName('DIV')[TD['ID']])
			document.getElementsByTagName('DIV')[TD['ID']].style.display = 'block';
	}

	if (TD['TagName'] == 'IMG')
		TD['Target'].src = TD['FilePath'] + TD['Type'] + '_' + TD['FileName'] + '_hl' + '.' + TD['SRCFileNameExtension'];

	if (activeButton)
	{
		if (activeButton['TagName'] == 'IMG')
			activeButton['Target'].src = activeButton['FilePath'] + activeButton['Type'] + '_' + activeButton['FileName'] + '_nm' + '.' + activeButton['SRCFileNameExtension'];
			
		if (document.getElementsByTagName('DIV')[activeButton['ID']])
			document.getElementsByTagName('DIV')[activeButton['ID']].style.display = 'none';
		
	}
	
	activeButton = TD;
}

function rolloverButton (event)
{
	var TD = getTargetData (event);

	if (TD['State'] != 'hl')
		if (TD['TagName'] == 'IMG')
			TD['Target'].src = TD['FilePath'] +TD['Type'] + '_' +  TD['FileName'] + '_ro' + '.' + TD['SRCFileNameExtension'];
}

function normalButton (event)
{
	var TD = getTargetData (event);

	if (TD['State'] != 'hl')
		if (TD['TagName'] == 'IMG')
			TD['Target'].src = TD['FilePath'] + TD['Type'] + '_' + TD['FileName'] + '_nm' + '.' + TD['SRCFileNameExtension'];
}

function clickLink (event)
{
	var TD = getTargetData (event);

	switch (TD['Type'])
	{
		case 'il' :
			if (document.getElementById ('content').tagName == 'IMG')
			{
				document.getElementById ('content').src = FilePath + 'content_' + TD['ID'] + '.' + TD['IDFileNameExtension'];

				if (document.getElementsByTagName('MAP')[TargetID])
					document.getElementById ('content').useMap = '#' + TD['ID'];
				else
					document.getElementById ('content').useMap = '#';
			}
			
			break;
			
		case 'el' :
			var IDComponent = TD['ID'].split ('_');
			var PageID = IDComponent.shift ();

			var newWindow = window.open (PageID + '.html?jump=' + IDComponent.join ('_') + '_' + TD['IDFileNameExtension'], 'Detailansicht', WindowParameter[PageID]['params']);
			
			if (!newWindow)
			{
				newWindow = window.open ('', 'Detailansicht', WindowParameter[PageID]['params']);
				newWindow.location.href = PageID + '.html?jump=' + IDComponent.join ('_') + '_' + TD['IDFileNameExtension'];
			}

			newWindow.focus ();
			
			break;
			
		default :
			alert ('unsupported link type');
	}
}

