var histItems							= new Array();
var lastClass 						= "toggleExpanded";
var lastView							=	"table";
var lastSearch						=	"search";
var lastUrl								= "url";
var lastSearchStart				=	"searchStart";
var lastModelInfo					= "modelinfo";
var lastScrollUp					= "scrollup";
var lastScrollDown				= "scrolldown";
var zoomSlider 						= new Slider("Slider1");
var searchFuncsLoaded			=	false;
var bModelLoaded					= false;
var bSwitch								= false;
var actHighLightedObject	= "";
var firstVisibleView			= 1;

with (zoomSlider)
{
	leftValue 			= 0;
	rightValue			= zoomArray.length - 1;
	defaultValue		=	getFlag(iZoom);
	imgPath					=	"../misc/images/header/";
	buttonImg				= "button.png";
	buttonHiliteImg	=	"button.png";
	buttonHoverImg	=	"button.png";		
	offsetX					= 35;
	offsetY					= 16;
	maxSlide				=	100;
	buttonWidth			=	5;
	buttonHeight		=	9;
	onmouseup				= "try{this.setValue(this.getValue(0));doZoom(this.getValue(0));}catch(e){}"
	onmouseover			= "quickTip('" + top.strTipZoomForGraphic + "');"
}
zoomSlider.writeSlider();

window.onload = function()
{
	if (zoomArray.length > 1)
	{
		placeSliders();
		createImageMapForSlider();
	}
	setTitle();
	writeViews();
	toggleHeader();
	changeView(activeView);

	var currElement = document.getElementById("toggleExpanded");
	currElement.onclick 		= function(){toggleHeader();quickTip(top.strTipMaximizeHeader)};
	currElement.onmouseover	= function(){changeImage(true, this, "toggle");quickTip(top.strTipMinimizeHeader)};
	currElement.onmouseout	= function(){changeImage(false, this, "toggle");quickTip('')};

	var currElement = document.getElementById("toggleCollapsed");
	currElement.onclick 		= function(){toggleHeader();quickTip(top.strTipMinimizeHeader)};
	currElement.onmouseover	= function(){changeImage(true, this, "toggle");quickTip(top.strTipMaximizeHeader)};
	currElement.onmouseout	= function(){changeImage(false, this, "toggle");quickTip('')};
	
	var currElement = document.getElementById("table");
	currElement.onclick			= function(){switchModel();quickTip(top.strTipGraphicView)};
	currElement.onmouseover = function(){changeImage(true, this, "view");quickTip(top.strTipTableView)};
	currElement.onmouseout 	= function(){changeImage(false, this, "view");quickTip('')};
	
	var currElement = document.getElementById("graphic");
	currElement.onclick			= function(){switchModel();quickTip(top.strTipTableView)};
	currElement.onmouseover = function(){changeImage(true, this, "view");quickTip(top.strTipGraphicView)};
	currElement.onmouseout 	= function(){changeImage(false, this, "view");quickTip('')}	;

	var currElement = document.getElementById("url")
	currElement.onclick			= function(){addLink()}
	currElement.onmouseover	= function(){changeImage(true, this, "url");quickTip(top.strTipUrl)}
	currElement.onmouseout	= function(){changeImage(false, this, "url");quickTip('')}
	
	var currElement = document.getElementById("mail");
	currElement.onmouseover	= function(){quickTip(top.strTipResponsiblePerson)};
	currElement.onmouseout	= function(){quickTip('')};
	
	var currElement = document.getElementById("zoomSlider")
	currElement.onmouseover	= function(){quickTip(top.strTipZoomForGraphic)}
	currElement.onmouseout 	= function(){quickTip('')}
	
	var currElement = document.getElementById("search")
	currElement.onclick			= function(){openSearch()}
	currElement.onmouseover	= function(){changeImage(true, this, "search");quickTip(top.strTipSearch)}
	currElement.onmouseout	= function(){changeImage(false, this, "search");quickTip('')}
	
	var currElement = document.getElementById("modelinfo")
	currElement.onclick			= function(){openModelInfo()}
	currElement.onmouseover	= function(){changeImage(true, this, "modelinfo");quickTip(top.strTipModelInformation);showModelInfo(true)}
	currElement.onmouseout	= function(){changeImage(false, this, "modelinfo");quickTip('');showModelInfo(false)}
	
	var currElement = document.getElementById("scrollUp")
	currElement.onclick			= function(){scrollViews("up")}
	currElement.onmouseover	= function(){changeImage(true, this, "scrollup");quickTip(top.strTipScrollUp)}
	currElement.onmouseout	= function(){changeImage(false, this, "scrollup");quickTip('')}
	
	var currElement = document.getElementById("scrollDown")
	currElement.onclick			= function(){scrollViews("down")}
	currElement.onmouseover	= function(){changeImage(true, this, "scrolldown");quickTip(top.strTipScrollDown)}
	currElement.onmouseout	= function(){changeImage(false, this, "scrolldown");quickTip('')}	
}

window.onresize = function()
{
	writeHistory();
}

function setTitle()
{
	document.getElementById("mainTitleExpanded").innerHTML = mainTitle;
	document.getElementById("mainTitleCollapsed").innerHTML = mainTitle;
	document.getElementById("subTitleExpanded").innerHTML = subTitle;
	document.getElementById("subTitleCollapsed").innerHTML = subTitle;
	document.getElementById("externalLink").href = externalLink;
}

function switchModel()
{
	bSwitch = true;
	if (lastView == "graphic")
	{	
		var name = parent.model.location.href.substring(parent.model.location.href.lastIndexOf("/") + 1, parent.model.location.href.lastIndexOf("t.html"));	
		parent.model.location.href = "../" + name + "/" + name + ".html";
	}
	else
	{
		var name = parent.model.location.href.substring(parent.model.location.href.lastIndexOf("/") + 1, parent.model.location.href.lastIndexOf(".html"));	
		parent.model.location.href = "../" + name + "/" + name + "t.html";		
	}
}

function getFile(searchString)
{
	for (var i = 0; i < iconItem.length; i++)
	{
		if (iconItem[i].elemName == searchString)
		{
			return iconItem[i].iconFile
		}
	}
	return ""
}

function changeImage(activate, elem, type)
{
	if (type == "toggle")
	{
		var lastElemClass = lastClass;
	}
	else if (type == "view")
	{
		var lastElemClass = lastView;
	}
	else if (type == "url")
	{
		var lastElemClass = lastUrl;
	}	
	else if (type == "search")
	{
		var lastElemClass = lastSearch;
	}
	else if (type == "searchStart")
	{
		var lastElemClass = lastSearchStart;
	}
	else if (type == "modelinfo")
	{
		var lastElemClass = lastModelInfo;
	}
	else if (type == "scrollup")
	{
		var lastElemClass = lastScrollUp;
	}
	else if (type == "scrolldown")
	{
		var lastElemClass = lastScrollDown;
	}	
	else
	{
		return;
	}
	
	if (activate)
	{
		elem.className = lastElemClass + "Active";
	}
	else
	{
		elem.className = lastElemClass;
	}
}

function toggleHeader()
{
  if (!parent || !parent.header)
  {
    window.setTimeout("toggleHeader()",100); 
  }
  else
  {
    elemGlobal				= parent.document.getElementById('global');
    elemExpanded 			= document.getElementById('expanded');
    elemCollapsed 		= document.getElementById('collapsed');
    elemNavbar 				= document.getElementById('navbar');
    elemNavbar2				= document.getElementById('navbar2');

    if (!elemGlobal || !elemExpanded || !elemCollapsed || !elemNavbar || !elemNavbar2)
    {
      window.setTimeout("toggleHeader()",100);     
    }
    else
    {
			if (bCollapsed)
			{
				elemCollapsed.style.display = 'none';
				elemExpanded.style.display = 'block';
				elemGlobal.rows = "170, *";
				elemNavbar.style.top = 102;
				elemNavbar2.style.display = "block";
				bCollapsed = false;
				lastClass = "toggleExpanded";
			}
			else
			{
				elemCollapsed.style.display = 'block';
				elemExpanded.style.display = 'none';
				elemGlobal.rows = "61, *";
				elemNavbar.style.top = 25;
				elemNavbar2.style.display = "none";
				bCollapsed = true;
				lastClass = "toggleCollapsed";
			}

      if (zoomArray.length > 1)
      {
      	actualizeSlider();
      }    
    }
  }
}

function quickTip(tip)
{
	if (!parent.header)
	{
		window.setTimeout("quickTip('" + tip + "');", 100);
	}
	else
	{
		tipElem = parent.header.document.getElementById('quicktip');
		if (!tipElem)
		{
			window.setTimeout("quickTip('" + tip + "');", 100);
		}
		else
		{
			if (tip != "")
			{
				tip = '<nobr><img src="../misc/images/header/info.gif"><span class="tiptext">' + tip + '</nobr>';
			}		
			tipElem.innerHTML = tip;
		}
	}
}

function getFlag(zoom)
{
	if (!zoomArray)
	{
		return 0;
	}
	else
	{
		for (var currFlag = 0; currFlag < zoomArray.length; currFlag++)
		{
			if (zoomArray[currFlag] == zoom)
			{
				return currFlag;
			}
		}
		return 0;
	}
}

function createImageMapForSlider()
{
	imgMapElem = document.getElementById('sliderMap');
	imgPaddingLeft = 30;
	imgPadding = 10;
	
	if (!imgMapElem || (document.Slider1RailImg.complete == false))
	{
		window.setTimeout("createImageMapForSlider()", 100);
	}
	else
	{
		imgHeight = document.Slider1RailImg.height;
		imgWidth  = document.Slider1RailImg.width;
		try
		{
			factor = (imgWidth - (imgPaddingLeft + imgPadding)) / zoomArray.length;
		}
		catch (e)
		{
			factor = imgWidth;
		}
		var mapValue = "";
		var yTop = imgPadding;
		var yBottom = imgHeight - imgPadding;
		var xLeft = imgPaddingLeft;
		var xRight = imgPaddingLeft;
		var counter = 0;
		
		while (xRight < (imgWidth - imgPadding))
		{
			xRight = xRight + factor;
			mapValue = mapValue + "<area shape='rect' coords='" + xLeft + ", " + yTop + ", " + xRight + ", " + yBottom + "' href='javascript:zoomSlider.setValue(" + counter + ");doZoom(" + counter + ");'>\n";
			xLeft = xRight;
			counter = counter + 1;
		}
		imgMapElem.innerHTML = mapValue;
	}
}

function placeSliders()
{
	document.getElementById("zoomSliderForm").style.display = "block";
	for (var i=0; i<window.sliders.length; i++)
	{
  	window.sliders[i].placeSlider();
  }
}

function actualizeSlider()
{
  if (!parent.header)
  {
    window.setTimeout("actualizeSlider();", 100);    
  }
  else
  {
    if (!parent.header.zoomSlider)
    {
      window.setTimeout("actualizeSlider();", 100);        
    }
    else
    {
     	placeSliders()
     	zoomSlider.setValue(getFlag(iZoom));
    }
  }
}

function writeViews()
{
	var viewElemExp = document.getElementById('viewExpanded');
	var viewElemCol = document.getElementById('viewCollapsed');
	
	if (!viewElemExp || !viewElemCol)
	{
		window.setTimeout("writeViews()", 100);
	}
	else
	{	
		var numberOfViews = viewObject.getNumberOfViews();
		var allViewIds = viewObject.getIdsOfAllViews();
		var ergExp = "";
		var ergCol = "";

		for (var i = 0; i < numberOfViews; i++)
		{
			var currViewId = allViewIds[i];
			with (viewObject)
			{
				var currViewName	= getNameOfView(currViewId);
				var currBackgrd 	= getBackgroundOfView(currViewId);
				var currActView 	= getActiveImage(currViewId);
				var currInactView = getInactiveImage(currViewId);
			}

			var styleParam = (i >= maxVisibleViews) ? "display:none; " : "";
			
			ergExp = ergExp + "<div id='" + currViewId + "' class='viewElem' style='" + styleParam + "background-image:url(./images/header/" + currInactView + ");' onclick='changeView(\"" + currViewId + "\")' onmouseout='if(this.id != activeView){this.style.backgroundImage = \"url(./images/header/" + currInactView + ")\"};' onmouseover='this.style.backgroundImage = \"url(./images/header/" + currActView + ")\";'>" + currViewName + "</div>";
			ergCol = ergCol + "<div id='" + currViewId + "Col' class='viewElem' onclick='changeView(\"" + currViewId + "\")' onmouseout='this.style.backgroundImage = \"url(./images/header/" + currInactView + ")\";' onmouseover='this.style.backgroundImage = \"url(./images/header/" + currActView + ")\";' style='background-image:url(./images/header/" + currInactView + ");'>" + currViewName + "</div>";
		}
		
		viewElemExp.innerHTML = viewElemExp.innerHTML + ergExp;
		viewElemCol.innerHTML = ergCol;
		
		if (numberOfViews > maxVisibleViews)
		{
			document.getElementById("scrollDown").style.visibility = "visible";
		}
		showActiveView();
	}
}

function changeView(vid)
{
	var viewElem = document.getElementById(vid);
	
	if (!viewElem || !parent || !parent.menu)
	{
		window.setTimeout("changeView('" + vid + "');", 100); 	
	}
	else
	{
		var strOldView = activeView;		
		var numberOfViews = viewObject.getNumberOfViews();
		var allViewIds = viewObject.getIdsOfAllViews();
		var posChangedView = 0;

		for (var i = 0; i < numberOfViews; i++)
		{
			var currViewId = allViewIds[i];
			with (viewObject)
			{
				var currViewName 	= getNameOfView(currViewId);
				var currBackgrd 	= getBackgroundOfView(currViewId);
				var currActView 	= getActiveImage(currViewId);
				var currInactView	= getInactiveImage(currViewId);
			}
			
			document.getElementById(currViewId + 'Col').style.display = 'none';
			
			if (currViewId == vid)
			{
				parent.menu.location.href = "../menu/menu_" + currViewId + ".html";
				document.getElementById('mainExpanded').style.backgroundImage = "url(./images/header/" + currBackgrd + ")";
				document.getElementById('mainCollapsed').style.backgroundImage = "url(./images/header/" + currBackgrd + ")";
				document.getElementById(currViewId).style.backgroundImage = "url(./images/header/" + currActView + ")";
				activeView = vid;	
				posChangedView = i;
			}
			else if (currViewId == strOldView)
			{
				document.getElementById(currViewId).style.backgroundImage = "url(./images/header/" + currInactView + ")";
			}
		}
		
		if (posChangedView == (numberOfViews-1))
		{
			document.getElementById(allViewIds[0] + "Col").style.display = "block";
		}
		else
		{
			document.getElementById(allViewIds[posChangedView + 1] + "Col").style.display = "block";
		}		
	}
}

function scrollViews(direction)
{
	var numberOfViews = viewObject.getNumberOfViews();
	var topLimit 		= Math.max((direction == "down") ? (firstVisibleView + 1) : (firstVisibleView - 1), 1);
	var bottomLimit	=	Math.min(topLimit + (maxVisibleViews - 1), numberOfViews);
	var allViewIds = viewObject.getIdsOfAllViews();	
	for (var i = 1; i <= numberOfViews; i++)
	{
		document.getElementById(allViewIds[i-1]).style.display = ((i >= topLimit) && (i <= bottomLimit)) ? 'block' : 'none';	
	}
	firstVisibleView = topLimit;	
	document.getElementById("scrollDown").style.visibility 	= (bottomLimit == numberOfViews) ? "hidden" : "visible";
	document.getElementById("scrollUp").style.visibility 		= (topLimit == 1) ? "hidden" : "visible";	
}

function showActiveView()
{
	var numberOfViews = viewObject.getNumberOfViews();
	var allViewIds = viewObject.getIdsOfAllViews();	
	var activeViewPos = 1;
	for (var i = 0; i < numberOfViews; i++)
	{
		if (allViewIds[i] == activeView)
		{
			var activeViewPos = (i+1);
			break;
		}
	}

	var topLimit 		= Math.min(activeViewPos, (numberOfViews - (maxVisibleViews - 1)));
	var bottomLimit	=	Math.min(topLimit + (maxVisibleViews - 1), numberOfViews);
	for (var i = 1; i <= numberOfViews; i++)
	{
		document.getElementById(allViewIds[i-1]).style.display = ((i >= topLimit) && (i <= bottomLimit)) ? 'block' : 'none';	
	}
	firstVisibleView = topLimit;
	document.getElementById("scrollDown").style.visibility 	= (bottomLimit == numberOfViews) ? "hidden" : "visible";
	document.getElementById("scrollUp").style.visibility 		= (topLimit < 1) ? "hidden" : "visible";		
}

function doZoom(sliderFlag)
{
	if (!parent || !parent.model)
  {
    window.setTimeout("doZoom(" + sliderFlag + ");",100); 
  }
  else
  {
		zoom = zoomArray[sliderFlag];
		iZoom = zoom;
		if(parent.model.document.getElementById('modelimage'))
		{
			imageSrc = eval("parent.model.modelimage" + zoom + ".src");
			if (imageSrc == "")
			{
				window.setTimeout("doZoom(" + sliderFlag + ");", 100);
			}
			else
			{
				parent.model.document.getElementById('modelimage').firstChild.src = imageSrc;
				parent.model.actualizeImageArea();
				parent.model.actualizeAS();
				parent.model.highLightObject();
			}
		}
	}
}

function addLink()
{
	var searchstring = top.window.location.search;
	var modelversion = (bWithVersion && parent.model.currModelVersion != "") ? (" " + parent.model.currModelVersion) : "";
	top.location.href= top.window.location.href.replace(searchstring, "") + encodeURI("?model=" + parent.model.currModelName + modelversion + "?type=" + parent.model.currModelType);
}

function openModelInfo()
{
	if (parent.model.bModInfoWindow)
	{
		var modId = parent.model.currModelId;
		var windowLink = "../" + modId + "/" + modId + "m.html";
		var infoWindow = window.open(windowLink, "ModelInfoWindow", "scrollbars=yes, resizable=yes, width=400, height=600, dependent=yes, left=0, top=300");
		infoWindow.focus();
	}
}

function histItemElem(modelName, modelType, modelId)
{
	this.modelName 	= modelName;
	this.modelType 	= modelType;
	this.modelId 		= modelId;	
}

function writeHistory()
{
	var histValue = "";
	var htmlArrow = "<img class='icon' src='../misc/images/icons/small_hist_right.png' />";
	var maxStrLength = Math.floor(parent.model.document.body.offsetWidth / (8 * (histItems.length + 1)));
	
	for (var i = 0; i < histItems.length; i++)
	{
		var modelname = (histItems[i].modelName.length > maxStrLength) ? (histItems[i].modelName.substring(0, maxStrLength - 4) + "...") : histItems[i].modelName;
		histValue = histValue + htmlArrow + "<img class='icon' src='../misc/images/icons/" + getFile(histItems[i].modelType) + "' /><a class='histElem' href='../" + histItems[i].modelId + "/" + histItems[i].modelId + ".html' target='model' title='" + maskChars(histItems[i].modelName) + "'>" + modelname + "</a>";
	}
	document.getElementById('history').innerHTML = histValue;
}

function addHistory(modelName, modelType, modelId)
{
	var maxHistElems = 5;
	
	for (var i = 0; i < histItems.length; i++)
	{
		if (histItems[i].modelId == modelId)
		{
			histItems.splice(i, 1);
			break;
		}			
	}	
	histItems.push(new histItemElem(modelName, modelType, modelId));
	
	while (histItems.length > maxHistElems)
	{
		histItems.splice(0, 1);
	}
	writeHistory();
}

function showModelInfo(bShow)
{
	var currElem = document.getElementById('infoWindow');
	
	if (currElem)
	{
		currElem.style.display = (bShow) ? "block" : "none";
	}
}

function openSearch()
{
	if (typeof(searchWindow) == "undefined" || searchWindow.closed)
	{
		searchLink = "../misc/search.html";
		searchWindow = window.open(searchLink, "SearchWindow", "scrollbars=yes, resizable=yes, width=600, height=500, dependent=yes, left=0, top=300");
		searchWindow.focus();
	}
	else
	{
		searchWindow.focus();
	}

	if (!searchFuncsLoaded)
	{
		initSearch();
	}	
}

function initSearch()
{
	heads 					= document.getElementsByTagName("head");
	scriptElem			= document.createElement("script");
	scriptElem.type = "text/javascript";
	scriptElem.src 	= "../js/search_functions.js"
	heads[0].appendChild(scriptElem);
}

function changeSearch()
{
	elemSearch = document.getElementById('searchfield');
	if (elemSearch.style.display == 'block')
	{
		elemSearch.style.display = 'none';
	}
	else
	{
		elemSearch.style.display = 'block';
		
		if (!searchFuncsLoaded)
		{
			initSearch();
		}
	}
}

function searchFunctionsLoaded()
{
	searchFuncsLoaded = true;
}

function prepareSearch()
{
	if (!searchFuncsLoaded)
	{
		window.setTimeout("prepareSearch();",100); 	
	}
	else
	{
		if (searchWindow)
		{
			elemSearchError = searchWindow.document.getElementById('textError');
			elemSearchWait	= searchWindow.document.getElementById('searchWait');
			searchQuery 		= searchWindow.document.getElementById("searchtext").value.toLowerCase();
			if (searchQuery.length > 1)
			{
				elemSearchError.style.display = 'none';	
				elemSearchWait.style.display	= 'block';
				getSearchSettings();
			}
			else
			{
				elemSearchError.style.display = 'block';
				return;
			}
		}
	}
}

function maskChars(source)
{
  source = source.replace(/\</g, "&lt;");
  source = source.replace(/\>/g, "&gt;");  
  source = source.replace(/\'/g, "&#39;");
  source = source.replace(/\"/g, "&quot;");
  return source;
}