var movideoAds = (function() {
	
	function showMrec()
	{
		var mrec = document.getElementById('mrec-wrapper');
		var dirWrapper = document.getElementById('directory-wrapper');
		var dirPanel = document.getElementById('directory-panel');
		mrec.style.marginTop = ((360 - 250) / 2) + "px";
		dirPanel.style.marginTop = "-1000px";
		dirPanel.style.display = 'block';
		
		// Update name of clip here if required
		var message = document.getElementById('mrec-message');
		message.innerHTML = 'Advertisement: Your clip is next';
	};
	
	function hideMrec()
	{
		var mrec = document.getElementById('mrec-wrapper');
		var dirPanel = document.getElementById('directory-panel');
		mrec.style.marginTop = "-1000px";
		dirPanel.style.marginTop = 0;
		dirPanel.style.display = 'inline-block';
	}
	
	// find a Companion Ad with matching width and height
	// returns Companion Ad if matches
	// returns null if no matches
	function findCompanionAd(companionAds, width, height) 
	{
		for (var i in companionAds) 
		{
			var companionAd = companionAds[i];
			if (companionAd.width == width && companionAd.height == height) 
			{
				return companionAd;
			}
		}
		return null;
	}
	
	var defaultResourceHandlers = {
		// for html resources, replace the innerHTML with the resource. 
		// NOTE does not support <script> elements in the HTML resource.
		// 
		'html': function(companionAd, element) 
		{
			element.innerHTML = companionAd.resource;
		},
		
		// for iframe resources, simply change the IFrameElement.src to point to the new URL.
		// 
		'iframe': function(companionAd, element) 
		{
			element.innerHTML = '<iframe src="' + companionAd.resource + '" style="width:' + companionAd.width + 'px,height:' + companionAd.height + 'px" />' 
		},
		
		// for static resources, create HTML to wrap the resource, and inject to DOM. 
		// 
		'static': function(companionAd, element) 
		{
			// handle images resource by creating an <a> link with an <img />
			// matches creativeTypes such as: image/jpeg, image/gif
			// 
			if (companionAd.creativeType.indexOf('image') == 0) {
				var html 
					= '<a href="' + companionAd.clickThroughUrl + '" target="_blank">' 
					+ '<img border="0" src="' + companionAd.resource 
					+ '" width="' + companionAd.width 
					+ '" height="' + companionAd.height
					+ '" />'
					+ '</a>';
				
				element.innerHTML = html;
			}
			// handles SWFs by embedding in the ad container
			//
			else if (companionAd.creativeType == 'application/x-shockwave-flash') 
			{
				swfobject.embedSWF(companionAd.resource + '?clickTAG=' + companionAd.clickThroughUrl, element.id, companionAd.width, companionAd.height, '9.0.0');
			}
		}
	};
	
	return {
		adSlots: [{ id: 'leave-behind-wrapper', width: 468, height: 60, onChanged: showMrec }, 
    	          { id: 'mrec', width: 300, height: 250 }],
    	          
    	adResourceHandlers: defaultResourceHandlers,
		
		advertChangedCallback: function (advertData)
		{
			for (var i in movideoAds.adSlots)
			{
				var adSlot = movideoAds.adSlots[i];
				if (adSlot)
				{
					var adElement = document.getElementById(adSlot.id);
					if (adElement)
					{
						var companionAd = findCompanionAd(advertData.companionAds, adSlot.width, adSlot.height);
						if (companionAd)
						{
							var resourceHandler = movideoAds.adResourceHandlers[companionAd.resourceType];
							if (resourceHandler) 
							{
								resourceHandler(companionAd, adElement);
							}                                                
						}
					}
					
					if (adSlot.onChanged != null)
					{
						adSlot.onChanged();
					}
				}
			}
		},
		
		controlsEnabledCallback: function(showing)
		{
			if (showing)
			{	
				hideMrec();
			}
			else
			{
				showMrec();
			}
		}
	};
	
})();

