
/**
 *	DyframeResize - V1.0
 *	-------------------------------------------------------------------------------
 *	(c) 2006 - Infosupport
 *	Author: Kevin Overmars
  *
 *	Whatever:hover is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation; either
 *	version 2.1 of the License, or (at your option) any later version.
 *
 *	DyframeResize is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; See the GNU	Lesser General Public License
 *  for more details.
 *
 *
 *	howto: - change remoteserver variabel and let it point to the server
 *           that is serving the iframe.
 *         - add <script type="text/javascript" src="dyframe.js"></script> tag 
 *           in the head of you html page.
 *         - call registerheight on the pageload of your htmlpage to tell 
 *           the remote server that your page is loaded and that you want
 *           the iframe sizze changed.
 *         - make sure the remote server had implemented the registerheight alias 
 *           on the PPS server.
 *	--------------------------------------------------------------------------------
 */
 
var iframeElement_;

function init_iframe(iframeName) {
		iframeElement_ = window.document.getElementById(iframeName);
		resize_iframe();
}

//doesn't work with third party url's (permmision denied error)
// incase of this error the iframe is resized to body height

function iecompattest(wobj){
		if (wobj) return(wobj.document.compatMode && wobj.document.compatMode!="BackCompat")? wobj.document.documentElement : wobj.document.body;
		return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
    }

function collectHeight(){
    return document.all? iecompattest().scrollHeight : iecompattest().offsetHeight;
    }
    
function collectWidth(){
    return document.all? iecompattest().scrollWidth : iecompattest().offsetWidth;
    }

function registerIframeHeight(iframe_src,iheight) {
	//register cookie with height of iframe
	//make path relative
  set_cookie(getIframeCookiename(iframe_src),iheight);
}

function resize_iframe() {
		var iframeElement = iframeElement_;
		if (iframeElement) {
			var iframeWindow;
			if (iframeElement.id != "" && iframeElement.name == ""){ iframeElement.name = iframeElement.id;}
			iframeWindow = window.frames[iframeElement.name];
			//determine frame height on contents
			
			try {
				  var resizeHeight = iframeWindow.document.all ? iecompattest(iframeWindow).scrollHeight : iecompattest(iframeWindow).offsetHeight;
				  iframeElement.style.height = resizeHeight + 20 + 'px';
				} //try 
			catch(e) {
				//try to check whether it is a domain security error and the iframe content was rendered using popup.xml thus a cookie was set with the height.
				 
				var iframe_name = getIframeCookiename(iframeElement.src);
				if (get_cookie(iframe_name)) {
					iframeElement.style.height = get_cookie(iframe_name);
					delete_cookie(iframe_name);
				}
				else 
					{
					if(document.documentElement.clientHeight>document.body.clientHeight) {
							bodyheight = document.documentElement.clientHeight;
						} else {
							bodyheight = document.body.clientHeight;
						}
					if (document.all) {	
						iframeElement.height = bodyheight - getPosition(iframeElement).top;
					} else if (window.innerHeight) {
							iframeElement.height = window.innerHeight - getPosition(iframeElement).top;
					} else {
						iframeElement.height = bodyheight- getPosition(iframeElement).top;
					}
				}
			} //catch
		}
		k = setTimeout('resize_iframe()', 200);
	}
	
	
function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );
  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function getIframeCookiename(iframeSrc) {
	//debugger;
	var iframe_name = 'iframe_'
	if (iframeSrc.substring(0,4) == 'http') {
		//absolute path
		if (iframeSrc.split('/')[3]) 
			//in case of http://www.domain.com/test.html?fjsdfkjsdlfksdjfl
			iframe_name += iframeSrc.split('/')[3].split('?')[0];
		 else 
			//in case of http://www.domain.com?fjsdfkjsdlfksdjfl
			iframe_name += iframeSrc.split('/')[2].split('?')[0];
	} else {
		//relative path
		if (iframeSrc.split('/')[1]) 
			//in case of /test.html?fjsdfkjsdlfksdjfl
			iframe_name += iframeSrc.split('/')[1].split('?')[0];
		else
			//in case of test.html?fjsdfkjsdlfksdjfl
			iframe_name += iframeSrc.split('?')[0];
	}
	return(iframe_name);
}

function getPosition(objReference) {
	var arrPosition = new Array();
	
	if (objReference != null) {
  	arrPosition.left = 0;
  	arrPosition.right = objReference.offsetWidth;
  	arrPosition.top = 0;
  	arrPosition.bottom = objReference.offsetHeight;
  	while (objReference != null && objReference.tagName != 'BODY' && (objReference.style.position == '' || objReference.style.position == 'relative')) {
    	arrPosition.left += objReference.offsetLeft;
    	arrPosition.top += objReference.offsetTop;
	  	objReference = objReference.offsetParent;
	  } 
	  arrPosition.right += arrPosition.left;
	  arrPosition.bottom += arrPosition.top;
  }
 	return arrPosition;
}

