﻿
function AdRefresh() {
	$(function() {
		if (typeof wAds == 'undefined') return;
		for (var i = 0; i < wAds.length; i++) {
			var framesrc = Edentity.ResolveUrl('~/AdHandler.aspx') + '?AdOnly=1&ContentReferenceName=' + wAds[i].ContentReferenceName + '&id=' + (new Date()).getTime();
			var iframe = $('<iframe width="0" height="0" frameborder="0"></iframe>')
				.attr('id', 'adRefreshIframe').css('display', 'none').data('ad', wAds[i])
				.attr('src', framesrc)
				.bind('load', function() {
				
					var ad = $(this).data('ad');
					var adSpot = $('#' + ad.ContainerID);
					var adContentOld = adSpot.find(':not(script)').clone(false).get(0);
					var adContentNew = $(this).contents().find('.AdModule :not(script)');
					
					try {
						if (adContentNew != null && adContentNew.length > 0) {
							adSpot.empty().append(adContentNew.clone(false).get(0));
						}
					} catch (err) {
						adSpot.empty().append(adContentOld);
					}
					
					$(this).remove();
				});
				
			$('#' + wAds[i].ContainerID).parent().append(iframe);
		}
	});
}
