function reload_ad($el, opts)
{
	if (!$el.length) { return; }
	if (!opts) { opts = {}; }
	$el = $($el.get(0));
	var ad_zoneid = $el.data('ad_zoneid');
	if (!ad_zoneid)
	{
		var script = $($el.children('script').get(0)).html();
		var m;
		m = script.match(/GA_googleFillSlot\("([^"]+)"\)/);
		if (!m) { return; }
		ad_zoneid = RegExp.$1;
		$el.data('ad_zoneid', ad_zoneid);
	}
	
	var iframe_id = 'ad' + Math.floor(Math.random()*99999999999);
	var $iframe = $('<iframe id="'+iframe_id+'" scrolling="no" frameborder="0" style="position: absolute; top: -9999px; left: -9999px; width: '+(opts.width || 0)+'px; height: +(opts.height || 0)+px; margin: 0; padding: 0; border: none;" src="/adframe-dfp.php?zoneid='+escape(ad_zoneid)+'&amp;id='+escape(iframe_id)+'"></iframe>');
	$iframe.bind('load', function() {
		var iframe = $iframe.get(0);
		var iframe_doc = iframe.document;
		if (iframe.contentDocument)
		{
			iframe_doc = iframe.contentDocument; // NS6
		}
		else if (iframe.contentWindow)
		{
			iframe_doc = iframe.contentWindow.document; // IE5.5 and IE6
		}
		var iframe_ad =  iframe_doc.getElementById('ad');
		if (!iframe_ad) { return; } // may be some error 
		var resize_timout = 100; // 200, 400, 800, 1600, 3200, 6400, 12800, 25600
		function resize_iframe()
		{
			var width = Math.max(iframe_ad.clientWidth || 0, iframe_ad.offsetWidth || 0, iframe_ad.scrollWidth || 0, iframe_ad.innerWidth || 0);
			var height = Math.max(iframe_ad.clientHeight || 0, iframe_ad.offsetHeight || 0, iframe_ad.scrollHeight || 0, iframe_ad.innerHeight || 0);
			if (height <= 30) // FF 2.0 Workaround
			{
				try
				{
					iframe_ad.style.display = '-moz-inline-block';
					height = Math.max(height, iframe_ad.clientHeight || 0, iframe_ad.offsetHeight || 0, iframe_ad.scrollHeight || 0, iframe_ad.innerHeight || 0);
					iframe_ad.style.display = 'block';
				}
				catch (e)
				{
					// nothing to do
				}
			}
			$el.children().not('#'+iframe_id).remove();
			$iframe.css({
				width: width,
				height: height,
				position: 'static',
				visibility: 'visible'
			});
			if (resize_timout < 2000)
			{
				window.setTimeout(resize_iframe, resize_timout);
				resize_timout = resize_timout * 2;
			}
		}
		resize_iframe();
		// Fix position:relative hack
		var divs = iframe_doc.getElementsByTagName('div');
		var class_test = new RegExp('\\bhtml-ad\\b');
		for (var i = 0; i < divs.length; i++)
		{
			var el = divs[i];
			if (class_test.test(el.className) && el.style.position == 'relative')
			{
				$iframe.css({
					position: 'relative',
					top: '0px',
					left: '0px'
				});
				if (el.style.top && el.style.top.substr(0, 1) == '-')
				{
					$iframe.css({
						top: el.style.top,
					});
					iframe_ad.style.paddingTop = el.style.top.substr(1);
				}
				if (el.style.left && el.style.left.substr(0, 1) == '-')
				{
					$iframe.css({
						left: el.style.left,
					});
					iframe_ad.style.paddingLeft = el.style.left.substr(1);
				}
				/*
				$iframe.css({
					position: 'relative',
					top: el.style.top && el.style.top || 'auto',
					left: el.style.left && el.style.left || 'auto'
				});
				el.style.top = 'auto';
				el.style.left = 'auto';
				break;
				*/
			}
		}
	});
	//$el.empty();
	$el.append($iframe);
}

var _block_ad_reloading = true;
window.setTimeout(function() { _block_ad_reloading = false; }, 22000);

function reload_ads()
{
	if (_block_ad_reloading) { return; }
	_block_ad_reloading = true;
	window.setTimeout(function() { _block_ad_reloading = false; }, 22000);
	if (typeof console != 'undefined') { console.log('reloading ads'); }
	if (!$('body').hasClass('wallpaper'))
	{
		reload_ad($('.banner'));
		reload_ad($('.skyscraper'));
	}
	/*
	reload_ad($('#block-block-33 center')); // Klappt nicht, da mehrfachanzeige geblockt wird
	reload_ad($('#block-block-34 center'));
	*/
	reload_ad($('.cad'), {width: 578});
	reload_ad($('#wizard-ad'), {width: 578});
}



