var xPos = 0, yPos = 0, goPos = 0; loadDelay = false;
var showTabRowDelay, tooltipFlag, xPosOffset, yPosOffset;

if(top.frames.length > 0 && self.name!="fileMngPreview" && self.name!="specProjFrame"){top.location.href=self.location;}

// ---------------------------------- Events ----------------------------------

document.onmousedown = getPos;
document.onmousemove = setShowDivPos;

function checkScrollPos(){
	if(window.name.indexOf('pos')!=-1){
		window.scrollBy(0,parseInt(window.name.slice(3)));
		window.name='cont';
	}
}

function getPos(e){
	if(document.all){
		xPos = (document.compatMode=='CSS1Compat') ? window.event.clientX + document.documentElement.scrollLeft : window.event.clientX + document.body.scrollLeft;
		yPos = (document.compatMode=='CSS1Compat') ? window.event.clientY + document.documentElement.scrollTop : window.event.clientY + document.body.scrollTop;
	}else{	/* pageXY = Fensterrelativ / screenXY = Bildschirmabsolut */
		xPos = e.pageX;
		yPos = e.pageY;
	}
	document.body.scrollTop ? goPos = document.body.scrollTop : goPos = window.pageYOffset;
	if(goPos==null) goPos=0;
}

function clPos(){window.name="pos" + goPos;}

function setShowDivPos(e){
if(tooltipFlag){
	getPos(e);
	var sD = document.getElementById("tooltipDiv");
	sD.style.left = (xPos + xPosOffset) + "px";
	sD.style.top = (yPos + yPosOffset) + "px";
}}

// --------------------------------- Standard ---------------------------------

function topInfo(cont,target,w,h)
{var topInf = window.open(cont,target,'width='+w+',height='+h+',dependent=yes,resizable=yes,scrollbars=yes,menubar=yes,status=yes'); topInf.focus(); return false;}

function hideTooltip(){
	clearTimeout(showTabRowDelay);
	document.getElementById("tooltipDiv").style.display = "none";
	tooltipFlag = false;
}

function uncoverTooltip(){
	document.getElementById("tooltipDiv").style.display = "block";
}

// ----------------------------------- Tooltip --------------------------------

var cTTObj, cTitle

function showTT(obj,x){
	if(x!="") var ttNode = document.getElementById(x).firstChild;
	var ttTxt; 
	ttNode ? ttTxt = ttNode.nodeValue : ttTxt = obj.title;
	
	cTTObj = obj; cTitle = obj.title; obj.title = ""; 
	var sD = document.getElementById("tooltipDiv");
	var sDf = sD.firstChild;
	while (sDf!=null){
		sD.removeChild(sDf); 
		sDf = sD.firstChild;}
	if(ttTxt!=null && ttTxt!=""){
		var newEntry = document.createTextNode(ttTxt);
		sD.appendChild(newEntry);
		tooltipFlag = true; xPosOffset = 15; yPosOffset = 20;
		showTabRowDelay = setTimeout("uncoverTooltip()",500);}
}

function hideTT(){
	if(cTTObj!=null) {cTTObj.title = cTitle; cTTObj = null; cTitle = "";}
	hideTooltip(); 
}

function showTabRow(xy){ 
	clearTimeout(showTabRowDelay);
	var sD = document.getElementById("tooltipDiv");
	var sDf = sD.firstChild;
	var xNode = xy.firstChild;
	while (sDf!=null){
		sD.removeChild(sDf);
		sDf = sD.firstChild;}
	var counter = 0; 
	while (xNode!=null){
		if(xNode.nodeType==1 && xNode.childNodes[0].nodeValue!=null){
			counter+=1; 
			var xNV = xNode.childNodes[0].nodeValue
			var newEntry = document.createTextNode(counter + ": " + xNV);
			var newBr = document.createElement("br");
			// ggf. Bild einfuegen
			xNV = xNV.replace(/\s/,"");
			if(xNV.search(/.jpg$/) != -1 || xNV.search(/.jpeg$/) != -1){
				xNV = xNV.replace(/^images\//,"");
				var newImg = document.createElement("img");
			var newImgAlt = document.createAttribute("class");
			newImgAlt.nodeValue = "showTabRowImgPreview";
			newImg.setAttributeNode(newImgAlt);
			var newImgSrc = document.createAttribute("src");
			newImgSrc.nodeValue = "images/minSize/" + xNV;
			newImg.setAttributeNode(newImgSrc);
			sD.appendChild(newImg);
			}
			sD.appendChild(newEntry);
			sD.appendChild(newBr); 
		}
		xNode = xNode.nextSibling; }
	/* var x = 0, p = 40;
		if(document.all){p = p + document.body.scrollTop;}
		else if(document.getElementById){p = p + window.pageYOffset;}
		if(p<80) p = 80; 
		sD.style.top = p + "px";
		sD.style.right = "20px"; */
	tooltipFlag = true; xPosOffset = 50; yPosOffset = -60;
	showTabRowDelay = setTimeout("uncoverTooltip()",500);
}

function hideTabRowShowDiv(){
	hideTooltip();
}


// ------------------------------- Such-Formular ------------------------------

function cFocus(x,txt){if(x.value==txt)x.value = "";}

function cBlur(x,txt){if(x.value=="")x.value = txt;}

// ----------------------------- Kontakt-Formulare ----------------------------

function checkContactInp(){
	var df = document.contactForm
	if(df.contactTel.value == "" && df.contactMail.value == "")
	{var sX = confirm("|%|pcItem|%|lang:cformConfirm|%|"); if(sX==false) return false;} 
	return true;
}

// ----------------------------- Preview-Overlay ------------------------------

var overlayFlag = 0, cOpacity = 1, fadeAction, hideDelay

/* Einblenden */
function showOverlay(){
	var ovl = document.getElementById("imgPreviewImgOverlay").style;
	clearTimeout(hideDelay); 
	if(overlayFlag!=1 && overlayFlag!=2){
		clearTimeout(fadeAction);
		overlayFlag = 1;
		// ovl.visibility = 'visible';
		overlayFade(cOpacity, 71, 6, 1);
	}
}

/* Ausblenden */
function hideOverlay(){hideDelay = setTimeout('hideOverlayFadeOut()', 100);}

function hideOverlayFadeOut(){
	if(overlayFlag==1){
	var ovl = document.getElementById("imgPreviewImgOverlay").style;
	clearTimeout(fadeAction);
	overlayFlag = 0;
	overlayFade(cOpacity, 1, -6, 2);
	}
}

/* Reset Overlay */
function resetOverlay(){
	clearTimeout(fadeAction);
	clearTimeout(hideDelay); 
	overlayFlag = 0
	cOpacity = 1
}

// Fading
// Achtung! Bug bei opacity = 100 in einigen Browsern. Deshalb Maximal-Wert = 99.999
function overlayFade(cStart, cEnd, cStep, cSpeed){
	var ovl = document.getElementById("imgPreviewImgOverlay").style;
	var speedX = 0, hlp = 1; 
	if(cStep<0){ speedX = 3; hlp=-1};
	if((hlp * cStart) < (hlp * cEnd)){
		if(cStart < 1) cStart = 1;
		ovl.opacity = cStart/100;
		// ovl.MozOpacity = cStart/100; //
		// ovl.KHTMLOpacity = cStart/100; //
		ovl.filter = 'alpha(opacity:' + cStart + ')';
		cOpacity = cStart
		var fadeAction = setTimeout('overlayFade(' + (cStart + cStep) + ',' + cEnd + ',' + cStep + ',' + (cSpeed + speedX) + ')', cSpeed);
	}
	/* if(cOpacity <= 1 + Math.abs(cStep))
		{cOpacity=1; ovl.visibility = 'hidden';}
	else
		{ovl.visibility = 'visible';} */
	if(cOpacity <= 1 + Math.abs(cStep)){cOpacity=1; ovl.opacity = '0'; ovl.filter = 'alpha(opacity:0)';}
}

// ------------------------------- Inhalte ersetzen ---------------------------

/* Preview */
function getNewPreview(cHref, cImgHref, cTargetID){
	document.getElementById("imgPreviewLoadWaiting").style.display = "block"; 
	try{ con = new XMLHttpRequest();													// Firefox
			}catch(eX){ try{ con = new ActiveXObject("Msxml2.XMLHTTP");			// IE 1
				}catch(eY){ try{ con = new ActiveXObject("Microsoft.XMLHTTP");	// IE 2
				}catch(eZ){ con = false; }}}
	if(!con){window.location.href = cHref; return false;}
	// Bild laden
	con.onreadystatechange = function(){ loadPreviewHtml(cHref, cImgHref, cTargetID); }
	var cB = document.getElementsByTagName("base")[0].getAttribute("href");
	con.open("GET", cB + cImgHref, true);
	con.send(null);
}

function loadPreviewHtml(cHref, cImgHref, cTargetID){
if(con.readyState == 4){ 
	if(con.status == 200){
		try{ con = new XMLHttpRequest();													// Firefox
				}catch(eX){ try{ con = new ActiveXObject("Msxml2.XMLHTTP");			// IE 1
					}catch(eY){ try{ con = new ActiveXObject("Microsoft.XMLHTTP");	// IE 2
					}catch(eZ){ con = false; }}}
		if(!con){window.location.href = cHref; return false;}
		con.onreadystatechange = function(){ writeResponseAsNewPreview(cHref,cImgHref,cTargetID); }
		var qHlp = (window.location.search != "") ? "&" : "?";
		con.open("GET", cHref + qHlp + "extractData=1", true);
		con.send(null);
	}else{
		window.location.href = cHref;
	}
}}

/* Serverdaten auswerten */
function writeResponseAsNewPreview(cHref,cImgHref,cTargetID){
if(con.readyState == 4){ 
	if(con.status == 200){
		var cData = con.responseText;
		if(cData!="") {
			document.getElementById(cTargetID).innerHTML = cData;
			resetOverlay();
			if(autoCTime > 0) setAutoControl(autoCTime);
			// Ladeanzeige
			document.getElementById("imgPreviewLoadWaiting").style.display = "none"; 
			if(typeof newPreviewReadyEvent == "function") newPreviewReadyEvent();
			// ggf. Analytics-Tracker aufrufen
			if(typeof urchinTracker == "function") urchinTracker(cHref.replace(trackerBaseUrl,""));
		}else{
			window.location.href = cHref;
		}
	}else{
		window.location.href = cHref;
   }
}}

/* liefert XMLHttpReq-Objekt */
function getHiddenConn(){
	try{ con = new XMLHttpRequest();													// Firefox
		}catch(eX){ try{ con = new ActiveXObject("Msxml2.XMLHTTP");			// IE 1
			}catch(eY){ try{ con = new ActiveXObject("Microsoft.XMLHTTP");	// IE 2
				}catch(eZ){ con = false; }}}
	return con;
}


// -------------------------------- AutoControl -------------------------------

var autoCTime, nPage;

function setAutoControl(ct){
var df = document.autoControl.time;
window.clearTimeout(nPage);
if(ct!="" && ct>0){
	autoCTime = ct;
	df.options[ct/2].selected = true; 
	nPage = window.setTimeout("reloadPage(document.autoControl.time.value);", ct * 1000);
}else{
	autoCTime = 0;
	if(df.value!="" && df.value!="0"){
		autoCTime = df.selectedIndex * 2;
		reloadPage(df.value);
	}
}}

function reloadPage(target){
var cB = document.getElementsByTagName("base")[0].getAttribute("href");
var cF = document.autoControl;
window.clearTimeout(nPage);

if(overlayFlag != 0) nPage = window.setTimeout("reloadPage(document.autoControl.time.value);", autoCTime * 1000);

if(target != "" && overlayFlag == 0) getNewPreview(cB + target, cF.nextImg.value, cF.targetElementID.value);
}


// --------------------------------- Tab-Steuerung ----------------------------
// Konventionen zur ID-Verwendung: 
// Tab-Navigation: idBasis_tabnav_tabkennung || Tabs: idBasis_tab_tabkennung

var tabRestoreExpires = 0;

// Tab-Initialisierung: vList und hList sind kommaseparierte tabkennungen;
// expDays = Tage, bis Cookies verfallen
function initTab(base,vList,hList,expDays){
	tabRestoreExpires = expDays;
	var vc = getCookie(base + "_vList"); if(expDays > 0 && vc!="") vList = vc;
	var hc = getCookie(base + "_hList"); if(expDays > 0 && hc!="") hList = hc;
	vList = vList.replace(/_/,","); hList = hList.replace(/_/,",");
	tabVisible(base,vList); tabHidden(base,hList);
}

function tabVisible(base,vList){
	if(tabRestoreExpires>0) setCookie(base + "_vList",vList.replace(/,/,"_"),tabRestoreExpires);
	var v = vList.split(","); for(var i = 0; i < v.length; ++i){
	document.getElementById(base + "_tab_" + v[i]).style.display = "block";
	document.getElementById(base + "_tabnav_" + v[i]).className = base + "_tabnav_on";
}}

function tabHidden(base,hList){
	if(tabRestoreExpires>0) setCookie(base + "_hList",hList.replace(/,/,"_"),tabRestoreExpires);
	var h = hList.split(","); for(var i = 0; i < h.length; ++i){
	document.getElementById(base + "_tab_" + h[i]).style.display = "none";
	document.getElementById(base + "_tabnav_" + h[i]).className = base + "_tabnav_off";
}}

// ------------------------------- Kleine Helferlein --------------------------

// Cookies holen
function getCookie(x){
	var k = document.cookie.split(";");
	for (var i = 0; i < k.length; ++i){
		k[i] = k[i].replace(/\s/,"");
		if(x==k[i].substr(0,k[i].search('='))) return k[i].substr(k[i].search('=')+1,k[i].length);
	}
	return "";
}

function setCookie(xName,xValue,expDays){
	var d = new Date(); d = new Date(d.getTime() + (expDays * 24 * 60 * 60 * 1000));
	document.cookie = xName + "=" + xValue + "; expires=" + d.toGMTString(); 
}

function killCookie(xName){
	document.cookie = xName + "=; ; expires=Thu, 01-Jan-70 00:00:01 GMT;";
}

