// get the offset from top to the iframe
function getElementOffsetTopBody(elemObj) {
	var offset=0;
	while (elemObj != document.body) {
		offset += elemObj.offsetTop;
		elemObj = elemObj.offsetParent;
	}
	return offset;
}

// get the inner height of the iframe; Only possible if iframe content comes from Portal Server (Domain relaxing must be on !)
function getHeight(wndRef) {
	var result = 0;
	if (EPCM.getUAType() == EPCM.MSIE) {
		var loBody = wndRef.document.body;
		result = loBody.scrollHeight +
				 loBody.offsetHeight -
				 loBody.clientHeight;
	} else if ((EPCM.getUAType() == EPCM.MOZILLA) && (EPCM.getUAVersion() >= 5)) {
		result = wndRef.document.height + 32;
	}
	return result;
}

// adopt iframe height 
var footer = 0;
function adjustIsolatedContentFrame() {
	var bodyHeight;
	var minIViewHeight = 100;
	var reqHeight = minIViewHeight;
	var isolatedContentFrame = document.getElementById("isolatedContent");
	var isolatedContentFrameWindow = window.frames["isolatedContent"];
	
	if(typeof isolatedContentFrame == "undefined" || typeof isolatedContentFrameWindow == "undefined")
		return;
		
	try {
	     // try to set the iframe's height according to the content's height
	     reqHeight = getHeight(isolatedContentFrameWindow);
	}
	catch(e){
		// access denied !
		// calculate document height
		if (EPCM.getUAType() == EPCM.MSIE) {
			bodyHeight = document.body.clientHeight;
		} else if ((EPCM.getUAType() == EPCM.MOZILLA) && (EPCM.getUAVersion() >= 5)) {
			bodyHeight = window.innerHeight;
		}
		
		reqHeight = bodyHeight - getElementOffsetTopBody(isolatedContentFrame);
	}
	
	if (EPCM.getUAType() == EPCM.MSIE) {
		isolatedContentFrame.style.pixelHeight = reqHeight +footer;
	} else if ((EPCM.getUAType() == EPCM.MOZILLA) && (EPCM.getUAVersion() >= 5)) {
		isolatedContentFrame.height = reqHeight - 8 +footer;

	}
}

//EPCM.subscribeEvent("urn:com.sapportals.portal:browser", "resize", adjustIsolatedContentFrame);
//EPCM.subscribeEvent("urn:com.sapportals.portal:browser", "load", adjustIsolatedContentFrame);

// Will be called from the service iView, if a link is clicked in the dropdown box
function launchService(selection){
	var selected = selection.selectedIndex;
	if(selection.options[selected].value != ""){
	  var optionValue = selection.options[selected].value;
		if((optionValue.indexOf("_")) == 0){
      // open in new window
      window.open(optionValue.substring(1, optionValue.length), '', 'width=800,height=600,resizable=yes,menubar=yes,toolbar=yes,status=yes,scrollbars=yes,location=yes');
    }
    else{
      // open in same window
      location.href = optionValue;
    }
	}
}

function setIFrameHeight() {
	if (document.getElementById("isolatedContent")) {
		var height1=document.getElementById("height1").offsetHeight;
		var height2=document.getElementById("height2").offsetHeight;
		var height3=0;
		if (document.getElementById("height3"))
			height3=document.getElementById("height3").offsetHeight;
		var headerHeight = height1+height2+height3;
		var documentHeight = document.body.offsetHeight;
		var frameHeight = documentHeight-headerHeight-20;
	   
		document.getElementById("isolatedContent").height=frameHeight;
	}
}

function toggleNav() {
	if (document.getElementById('navcell').style.display != 'none') {
		document.getElementById('navcell').style.display = 'none';
		var icon = document.getElementById('toggleicon');
		var newsrc = icon.src.replace(/arrow_left/, "arrow_right");
		icon.src = newsrc;
		icon.title = "Navigation einblenden";
		icon.alt = "Navigation einblenden";
	}
	else {
		document.getElementById('navcell').style.display = '';
		var icon = document.getElementById('toggleicon');
		var newsrc = icon.src.replace(/arrow_right/, "arrow_left");
		icon.src = newsrc;
		icon.title = "Navigation ausblenden";
		icon.alt = "Navigation ausblenden";
	}
}

