var selfWindowNameLC = self.window.name.toLowerCase();
var isMainWindow = selfWindowNameLC == "shiwebtop" || selfWindowNameLC == "weka_webng" || selfWindowNameLC == "weka_rdbviewer";

var documentcssPath    = (isMainWindow ? "" : "../.") + "./weka/css/documentcss/";
var jsPathStd          = (isMainWindow ? "" : "../.") + "./standard/js/";
var documentjsPathWeka = (isMainWindow ? "" : "../.") + "./weka/js/documentjs/";
if(typeof g_InlineDocUri == "undefined")
	var g_InlineDocUri = self.location.search;

var sSearch = isMainWindow ? g_InlineDocUri : self.location.search;
var sCSS = "";
var g_doAfterLoadShiFunction;

if(typeof addJSRef == "function") {
	doOnLoadWmShi();
} else {
	g_doAfterLoadShiFunction = doOnLoadWmShi;
	document.write("<SCRIPT LANGUAGE=\"JavaScript\" SRC=\""
			+ jsPathStd
			+ "shiFunction.js\" TYPE=\"text/javascript\" CHARSET=\"ISO-8859-1\"></SCRIPT>");
}

function doOnLoadWmShi() {

	if(sSearch === undefined) {
		return;
	}

	addJSRef(jsPathStd + "shiFunction.js");
	addJSRef(documentjsPathWeka + "wmshiSpanningLinks.js");
	addJSRef(jsPathStd + "jipajax.js");
	
	if(g_InlineDocUri == undefined || g_InlineDocUri == null)
		g_InlineDocUri = "";
	
	if ((sSearch.toLowerCase().indexOf("hhvar=") > -1)
			|| (sSearch.toLowerCase().indexOf("q=") > -1)) {
	
		addCSSRef(documentcssPath + "hilit.css");
		addJSRef(jsPathStd + "documentjs/hilitviajs.js");
	}
	
//	var sCSSPrefix = "WEKA";
	var sCSSPrefix = "WEKA2";
//	if(isMainWindow)
//		sCSSPrefix = "WEKA2";
	
	if (sSearch.toLowerCase().indexOf("css=small") > -1) {	
		sCSS = "_small";
		addCSSRef(documentcssPath + sCSSPrefix + sCSS + ".css");
	} else if (sSearch.toLowerCase().indexOf("css=big") > -1) {
		sCSS = "_big";
		addCSSRef(documentcssPath + sCSSPrefix + sCSS + ".css");
	}
}

function scrollToTop() {
	var win = getWebAppTop();
	win.scrollTo(0,0);
	var mainContent = win.document.getElementById("appFrameMainContent");
	if(mainContent != null) {
		mainContent.scrollTop = 0;
	}
}

/**
 * Blendet progress bar ein oder aus, dabei wird content Bereich 
 * ausgeblendet und umgekehrt.
 * Der Progressbar muss aber in der Seite auf display:block gesetzt sein und content Bereich
 * display:none
 * @param on
 * @return
 */
function makeProgress(on) {
	var documentw = getWebAppTop().document.getElementById("contentAreaWithMarginal");
	var progress = getWebAppTop().document.getElementById("progressBarId");
	if (on) {
		if (documentw != null)
			documentw.style.display = "none";
		if (progress != null)
			progress.style.display = "";
	} else {
		if (documentw != null)
			documentw.style.display = "";
		if (progress != null)
			progress.style.display = "none";
	}

	
	try {
		var iframe = getWebAppTop().document.getElementById("documentwindow");
		var anchor = getAnchor(iframe.contentWindow);
		if(anchor != null) {
			iframe.contentWindow.setTimeout("scrollToAnchor('" + anchor + "', null);", 1);
		}
	} catch(e) {
	}
}

function onDocLoad() 
{
	// Get Top Window, else bailout
	var topWindow = null;
	try {topWindow = getWebAppTop();} 
	catch (e) {topWindow = null;}
	if (topWindow == null) 
		return;

	// Handle printing
	if (sSearch.toLowerCase().indexOf("printyourself=1") > -1) 
	{
		if(getWebAppTop && doZoom) {
			var mainWin = getWebAppTop();
			if(mainWin != null && mainWin.g_zoomedImages) {
				for(var i = 0; i < document.images.length; i++) {
					if(mainWin.g_zoomedImages.isZoomed(document.images[i].src))
						doZoom(document.images[i]);
				}
			}
		}
		
		self.focus();
		self.print();
		makeProgress(false);
		return;
	}

	// Eventually Hilit and remember pos of first hit
	var jumpTo = null;
	
	// new hilit method called, because in rdbviewer, the old function
	//crashed the browser if the #words to hilit was > 5
	if (typeof (restrictedjshilitondocload) == "function" && typeof topWindow.isJavaScriptHighligh != 'undefined' && topWindow.isJavaScriptHighligh) 
		jumpTo = restrictedjshilitondocload(false);
	// Disable progress screen after hiliting (When it is even on then...)
	makeProgress(false);
	
	// The following part defines special behaviour for the RDBV:
	// If an anchor is available and the anchor is a paragraph, don't
	// scroll to the first highlighted word, scroll to the given 
	// paragraph anchor instead
	var anchorIndex = document.URL.indexOf("#");
	if (anchorIndex > -1) 
	{
		var anchor = document.URL.substr(anchorIndex + 1);
		if (anchor.indexOf("_PARA_") > -1) 
		{
			// Get node, set ID and remember it
			var nodes = document.getElementsByName(anchor);
			if (nodes.length > 0) 
			{
				setTimeout("scrollToPos(document.getElementsByName('"+anchor+"')[0]);", 1000);
				return;
			}
		}
	}

	// Scroll to hilit position
	if (jumpTo != null && jumpTo != undefined && jumpTo != 0 && jumpTo != "")
	    setTimeout("scrollToPos('"+jumpTo+"');", 1000);
}

function isnumber(s)
{
	if(typeof(s) == "number")return true;
	if(typeof(s) != "string")return false;
	var i=0;
	for(i =0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if((c < '0') || (c > '9'))return false;
	}
	return true;
}

function scrollToPos(id)
{
	if (isnumber(id))
	{
		// Scroll to absolute position
		window.scrollTo(0, id);
		return;
	}

	// Scroll to ID?
	if (typeof(id) == "string")
		id = document.getElementById(id);	

	// Scroll to element!
	if(id)
		id.scrollIntoView(true);
		
	scrollToTop();
}

function doLink(isbn, alias, colanchor, htmlanchor) 
{
	// There's legacy productions still needing this
	masterLink(isbn, alias, '', htmlanchor, colanchor);	
}

function doFileNameAnchorLink(filename, anchor) 
{
	// There's legacy productions still needing this
	masterLink('', '', filename, anchor, '');
}

function doSHIWebLink(docReqFP) 
{
	var hostURL = generateAbsoluteURL();
	var absURL = hostURL + docReqFP;
	masterLink('', '', absURL, '', '');
}

function masterLink(isbn, alias, filename, htmlanchor, colanchor)
{
	if(isbn == "" &&
		alias == "" &&
		filename == "" &&
		colanchor == ""	)
	{
		scrollToPos(document.getElementsByName(htmlanchor)[0]);
		return;
	}
	
	var oTopWindow = getWebAppTop();
	if(oTopWindow == null)
		return;

	oTopWindow.document.SHIparamForm.isbn.value 		= isbn;
    oTopWindow.document.SHIparamForm.alias.value 		= alias;
	
	if (oTopWindow.document.SHIparamForm.docId != null) 
	{
		// RDBViewer
	    oTopWindow.document.SHIparamForm.docId.value 	= filename;
	    oTopWindow.document.SHIparamForm.anchor.value	= htmlanchor;
	}
	else
	{
		// WebNG
	    oTopWindow.document.SHIparamForm.fn.value 					= filename;
	    oTopWindow.document.SHIparamForm.htmlanchor.value			= htmlanchor;
	    oTopWindow.document.SHIparamForm.colanchor.value 			= colanchor;
		oTopWindow.document.SHIparamForm.invalidatesession.value 	= "false";
	}

    oTopWindow.document.SHIparamForm.submit();
}

function DoLink(softparams) {

	var oTopWindow = getWebAppTop();
	oTopWindow.document.getElementById('softFormId:softCommandLinkInput').value = softparams;
	var link = oTopWindow.document.getElementById('softFormId:softCommandLink');
	link.click();
}

function ipdolink(sLink)
{
	// Get application top
	var oTop = getWebAppTop();
	if(!oTop) return false;

	// Get communication IFrame 
	var oIFrame = oTop.document.getElementById('servercomiframe');
	if(!oIFrame) return false;
	
	// Build URL to JSP
	var sUrl = getpath(oTop.location.href) + "/wekacd/jsp/" + sLink;
    oIFrame.src = sUrl;

    // Cancel navigation
    return false;
}

function generateAbsoluteURL() {
	var _iframeURL = g_sCurrentSrc;
	var _splitUrl = _iframeURL.split("?");
	var _lastSign = _splitUrl[0].lastIndexOf('/');
	var _baseURL = _splitUrl[0].substring(0, _lastSign) + "/";

	var _relFileName = getSHIParameterValueFromURL("xsltfilename", _iframeURL);
	if (_relFileName == null || _relFileName == "")
		_relFileName = getSHIParameterValueFromURL("relFileName", _iframeURL);

	if (_relFileName == null || _relFileName == "") {
		// See if there is a licensed download src
		var sDownload = getSHIParameterValueFromURL("dwnld_src", _iframeURL);
		if (sDownload != null && sDownload != "nolicense")
			_relFileName = sDownload;
	}

	if (_relFileName == null || _relFileName == "")
		_relFileName = getSHIParameterValueFromURL("img_src", _iframeURL);
	if (_relFileName != null) {
		_relFileName = unescapeUrlEncodedString(_relFileName);
		_lastSign = _baseURL.lastIndexOf('/');
		_baseURL = _baseURL.substring(0, _lastSign);
		_lastSign = _baseURL.lastIndexOf('/');
		_baseURL = _baseURL.substring(0, _lastSign);
		_lastSign = _baseURL.lastIndexOf('/');
		_baseURL = _baseURL.substring(0, _lastSign);

		_baseURL = _baseURL + "/" + _relFileName;

		_lastSign = _baseURL.lastIndexOf('/');
		_baseURL = _baseURL.substring(0, _lastSign) + "/";
		return _baseURL;
	}

	var _absFileName = getSHIParameterValueFromURL("absFilePath", _iframeURL);
	if (_absFileName != null) {
		_absFileName = unescapeUrlEncodedString(_absFileName);
		_baseURL = _baseURL + "binary.load?binaryPath=" + _absFileName;
		_lastSign = _baseURL.lastIndexOf('/');
		_baseURL = _baseURL.substring(0, _lastSign) + "/";
		return _baseURL;
	}

	return _baseURL;
}

function getSHIParameterValueFromURL(searchParameter, currentSHIURL) {
	var _splitUrl = currentSHIURL.split("?");
	var _lastSign = _splitUrl[0].lastIndexOf('/');
	var _fileName = _splitUrl[0].substring(_lastSign, _splitUrl[0].length);
	var _fileType = _fileName.split(".");
	if (_fileType[1] != null
			&& ((_fileType[1].toLowerCase() == "jsp") || (_fileType[1]
					.toLowerCase() == "jsf"))) {

		var _filePathPosition = _splitUrl[1].lastIndexOf(searchParameter);
		if (_filePathPosition > -1) {

			var _ParameterString = _splitUrl[1].substring(_filePathPosition,
					_splitUrl[1].length);
			var _SubParameterString = _ParameterString.substring(
					searchParameter.length + 1, _ParameterString.length);
			if (_SubParameterString == null)
				return null;
			var _endOfParameterValue = _SubParameterString.indexOf("&");
			if (_endOfParameterValue < 0)
				_endOfParameterValue = _SubParameterString.length;
			var _currentParameterValue = _SubParameterString.substring(0,
					_endOfParameterValue);
			return _currentParameterValue;
		}
	}
	return null;
}

function smartConnect(xpath) {
	if (xpath == null || xpath == "")
		return false;

	var topWin = getWebAppTop();
	if (topWin == null)
		return false;

	var url = getpath(topWin.location.href)
			+ "/standard/jsp/shiajaxcommunicator.jsp?class=com.weka.sc.SmartConnectLinkHandler"
	url += "&xpath=" + escape(xpath);
	url += "&tmpstmp=" + (new Date()).getMilliseconds()
	callUrl(url, onAnswerComplete);

	return false;

	function onAnswerComplete(answer) {

		if (answer == null || topWin == null)
			return;

		var iPosStart = answer.indexOf("<url>");
		if (iPosStart < 0)
			return;

		var iPosEnd = answer.indexOf("</url>", iPosStart);
		if (iPosEnd < 0)
			return;

		var url = answer.substring(iPosStart + 5, iPosEnd);
		if (url == "" || url == null)
			return;

		topWin.location.href = url;
	}
}

function ZoomedImages() {
	
	var aZoomedImages = new Array();
	
	function ZoomInfo(sPath, bIsZoomed) {
		this.sImgPath     = sPath;
		this.bIsImgZoomed = bIsZoomed;
	}
	
	function getZoomInfo(sPath) {
		for(var i = 0; i < aZoomedImages.length; i++) {
			if(aZoomedImages[i].sImgPath.toLowerCase() == sPath.toLowerCase()) {
				return aZoomedImages[i];
			}
		}
		return null;
	}
	
	this.setZoomInfo = function(sPath, bIsZoomed) {
		var zoomInfo = getZoomInfo(sPath);
		if(zoomInfo == null) {
			aZoomedImages.push(new ZoomInfo(sPath, bIsZoomed));
		} else {
			zoomInfo.bIsImgZoomed = bIsZoomed;
		}
	}

	this.isZoomed = function(sPath) {
		var zoomInfo = getZoomInfo(sPath);
		if(zoomInfo != null)
			return zoomInfo.bIsImgZoomed;
		else
			return false; 
	}
}

