var globalPosition	= new Array();
var globalWidth		= new Array();
var globalHeight	= new Array();
var globalAd		= new Array();
var globalZone		= new Array();
var mpuHeight		= 300;

function initInlineGallery(id, ad, zid)
{
	globalPosition[id]	= 0;
	globalZone[id]		= zid;
	globalAd[id]		= ad;
	globalWidth[id] 	= parseInt($("inline-gallery-"+id).style.width);
	globalHeight[id]	= parseInt($("inline-gallery-"+id).style.height);
		
	var images = $("image-list-"+id).getElementsByTagName("div");
		
	for(i = 0; i < images.length; i++)
	{
		images[i].style.width = globalWidth[id] + "px";
		$("inline-mpu-"+id).style.width = globalWidth[id] + "px";
		
		if(i == 0)
		{
			var heightTest = new Image();
			
			heightTest.onload = function()
			{
				globalHeight[id] = heightTest.height;
				if(heightTest.width > heightTest.height)
				{
					globalHeight[id] = (heightTest.height * (parseInt($("image-list-"+id).getElementsByTagName("div")[0].style.width) / heightTest.width));
				}
				else
				{
					globalHeight[id] = $("image-list-"+id).getElementsByTagName("div")[0].getElementsByTagName("img")[0].offsetHeight;
				}
				
				if(globalHeight[id] < mpuHeight)
				{
					globalHeight[id] = mpuHeight;
				}
								
				$("image-list-"+id).style.height = globalHeight[id] + "px";
				$("inline-mpu-"+id).style.height = globalHeight[id] + "px";
								
				var diff = globalHeight[id] - $("image-list-"+id).getElementsByTagName("div")[0].getElementsByTagName("img")[0].offsetHeight;	
				if(diff > 0)
				{
					$("image-list-"+id).getElementsByTagName("div")[0].style.margin = (diff/2) + "px 0px 0px 0px";
				}
			};
			
			heightTest.src = $("image-list-"+id).getElementsByTagName("div")[0].getElementsByTagName("img")[0].src;
			
		}
	}
}

function shove(id, position)
{
	var images		= $("image-list-" + id).getElementsByTagName("img");
	var imageCount	= images.length;
				
	// which direction are we going?
	if(position == "right")
	{
		globalPosition[id]++;
	}
	else
	{
		globalPosition[id]--;
	}
	
	// at the start and going back?
	if(globalPosition[id] < 0)
	{
		globalPosition[id] = imageCount;
	}
	
	// at the end and going forward?
	if(globalPosition[id] > imageCount)
	{
		globalPosition[id] = 0;
	}
	
	// fix the image's top offset	
	if(images[globalPosition[id]])
	{	
		var diff = globalHeight[id] - images[globalPosition[id]].height;
				
		if(diff > 0)
		{
			if(globalPosition[id] > 0)
			{
				images[globalPosition[id]].style.margin = (diff/2) + "px 0px 0px 0px";
			}
		}
	}

	// are we in the ad slot? if so, embed an iframe advert	
	if(globalPosition[id] == globalAd[id])
	{
		$("image-list-"+id).getElementsByTagName("div")[globalAd[id]].getElementsByTagName("p")[0].style.marginTop = (((globalHeight[id] - mpuHeight) / 2) + 6) + "px";
		$("image-list-"+id).getElementsByTagName("div")[globalAd[id]].getElementsByTagName("iframe")[0].src = "http://ox.eurogamer.net/oa/delivery/afr.php?zoneid=" + globalZone[id];
	}
		
	// how far should we move the image list?
    offs = parseInt(document.getElementById("image-list-" + id).style.left);
    var final = ((globalWidth[id] - (globalWidth[id]*2)) * globalPosition[id]) - offs;

	// actually move it
	new Effect.MoveBy(
		$("image-list-" + id),
		0,
		final,
		{
			duration: 0.3,
			queue:
			{
				position: "end",
				scope: "slider",
				limit: 1
			},
			afterFinish: function()
			{
				// update the image number on screen
				$("image-number-" + id).innerHTML = globalPosition[id]+1 + "/" + (imageCount+1);
			}
		}
	);
}