/**
 * JavaScript for xiframe.php
 *
 * @package agdistis
 * @version $Id: xiframe.js,v 1.2 2008/01/23 18:01:33 amirabella Exp $
 */

var sv = domain_strip(document.domain);
sv = sv.match(/([a-zA-Z_-]+\.[a-zA-Z]{2,})$/);
sv = RegExp.$1;

if (location.href.match(/~/))
{
	document.domain = 'dev.' + sv;
}
else
{
	document.domain = sv;
}

/**
 * Refresh swapAd components when a link is followed
 */
function refreshAds()
{
	var iFames = top.document.getElementsByTagName('iframe');
	var x, i = iFames.length;
	var adRE = /swapAd\.php/;
	while ( i-- )
	{
		x = iFames[i];
		if (x.src.match(adRE))
		{
			x.src = x.src;
		}
	}
}

/**
 * Determine document height for resizing
 */
function get_doc_height(doc) 
{
	var docHt = 0;
	var sh;
	var oh;

	if (doc.height) 
	{
		docHt = doc.height;
	}
	else if (doc.body) 
	{
		if (doc.body.scrollHeight)
		{
			docHt = sh = doc.body.scrollHeight;
		}
		if (doc.body.offsetHeight)
		{
			docHt = oh = doc.body.offsetHeight;
		}
		if (sh && oh)
		{
			docHt = Math.max(sh, oh);
		}
	}
	return docHt;
}

/**
 * Resize iframe to correct height to avoid scrollbars
 */
function set_iframe_height(iframeName) 
{
	var iframeWin = window.frames[iframeName];
	var iframeEl = document.getElementById ? document.getElementById(iframeName) : document.all ? document.all[iframeName]: null;

	if (iframeEl && iframeWin) 
	{
		iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous
		var docHt = get_doc_height(iframeWin.document);

		// need to add to height to be sure it will all show
		if (docHt) 
		{
			iframeEl.style.height = docHt + 30 + "px";
		}
	}
	refreshAds();
}

/**
 * Load URL in iframe
 */
function load_iframe(iframeName, url)
{
	if (window.frames[iframeName]) 
	{
		window.frames[iframeName].location = url;
		return false;
  	}
	return true;
}

/**
 * clean domain for setting document.domain
 */
function domain_strip(url)
{
	var _url = url.match(/^(http:\/\/)?([0-9a-zA-Z\.]+)/);
	return RegExp.$2;
}
