(function($){
	$(function() {
		ass_reposition();
		// Keep things in line by calling the reposition function twice a second
		window.setInterval(ass_reposition, 500);
	});
})(jQuery);

function ass_reposition()
{
	jQuery('.ass_ad_hidden').each(function(i) {
		var cover = jQuery('#'+this.id.replace('-hidden', ''));


		if (cover.length > 0)
		{
			// Move the element overtop of this one
			offset = cover.offset();
			jQuery(this).offset({ top: Math.round(offset.top), left: Math.round(offset.left) });
		}
	});
}

function ass_ad(ad_id, ad_width, ad_height)
{
	document.write('<div class="ass_ad" id="ass'+ad_id+'" style="width:'+ad_width+'px; height:'+ad_height+'px; line-height:' + ad_height + 'px;">&nbsp;</div>');
}

function ass_find_x(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
	{
		curleft += obj.x;
	}
	return curleft;
}

function ass_find_y(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
	{
		curtop += obj.y;
	}
	return curtop;
}

function ass_move_above(sourceObj, targetObj)
{
	var height = 0;
	if (sourceObj.offsetHeight)
	{
		height = sourceObj.offsetHeight;
	}
	else if (sourceObj.clip && sourceObj.clip.height)
	{
		height = sourceObj.clip.height;
	}
	else if (sourceObj.style && sourceObj.style.pixelHeight)
	{
		height = sourceObj.style.pixelHeight;
	}

	if (window.XMLHttpRequest)
	{
		if (document.all)
		{
			// IE7, do nothing
		}
		else
		{
			// Mozilla, etc
			targetObj.style.paddingTop = 0;
			targetObj.style.paddingBottom = 0;
		}
	}
	else
	{
		// IE6, older browsers, do nothing
	}
	if (height > 0)
	{
		targetObj.style.height = height;
	}
	sourceObj.style.left = ass_find_x(targetObj) + 'px';
	sourceObj.style.top = ass_find_y(targetObj) + 'px';
}

function ass_move_ads(priority, numads)
{
	for(i = 0; i < numads; i++)
	{
		var adName = 'ass' + priority + '-' + i;
		var offScreenAdName = adName + '-hidden';
		if (document.getElementById)
		{
			targetAdDivID = document.getElementById(adName);
			sourceAdDivID = document.getElementById(offScreenAdName);
		}
		else if (document.all)
		{
			targetAdDivID = document.all[adName];
			sourceAdDivID = document.all[offScreenAdName];
		}
		else if (document.layers)
		{
			targetAdDivID = document.layers[adName];
			sourceAdDivID = document.layers[offScreenAdName];
		}
		if ((targetAdDivID != null) && (sourceAdDivID != null))
		{
			ass_move_above(sourceAdDivID, targetAdDivID);
		}
	}
}

