
	if (typeof(G2) != "object")
		G2 = {};

	G2.ad = new function() { 	

	   	this.ini = function() 
	   	{		   	
	   		G2.ad.discover_banner();	
	   	
	 		if (G2.ad.config.companion_default_insert)
	 			G2.ad.change_banner();			 			
	
	 		G2.ad.log_message('[STOP] ini()');
	 		
	   	};	
	
		this.config = {
			prefix: 'e360_companion_',
			companion_default_insert: false,
			sizes: { 
				'300x300':  { 'width': 300, 'height': 300 },
				'300x250':  { 'width': 300, 'height': 250 },
				'300x60':   { 'width': 300, 'height': 60 },
				'728x90':   { 'width': 728, 'height': 90 }, 
				'120x600':  { 'width': 120, 'height': 600 },
				'160x600':  { 'width': 160, 'height': 600 },
				'468x60':   { 'width': 468, 'height': 60 },
				'420x103':  { 'width': 420, 'height': 103 },
				'430x104':  { 'width': 430, 'height': 104 }
			},
			log: {
				id: 'e360_log',
				enabled: false
			},
			timer: {
				'set': false,
				'length': 30000,
				'rotate': true,
				'impression': false,
				'tracking': false
			}
		}	
		
		this.data = {
			container: [],
			banner: [],
			log: '',
			timer: 0,
			run: { 'change_banner': 0, 'discover_banner': 0 }
		}
		
		this.discover_banner = function()
		{				
			for (var size in this.config.sizes)
			{
				var element = document.getElementById(this.config.prefix + size);
				
				if (element != null) 
					this.data.container[size] = { 'element': element, 'rotation': 0, 'tracking': { 'element': 0 } };		 	
			}
			this.data.run.discover_banner++;
			
			this.log_message('[STOP] discover_banner()');
		};	
		
	  	this.display_companion = function(type, content)
	  	{	  
	  		this.log_message('[START] display_companion()');	
			
			this.timer_stop();
			
			switch(type)
			{
				case 'acudeo':
					G2.ad.display_companion_acudeo(content);
				  	break;    
				case 'single':
					G2.ad.display_companion_single(content);
				  	break;  
				default:
					return;
			}
			
			this.log_message('[STOP] display_companion()');
		};	
		
	  	this.display_companion_single = function(size, url) 
	  	{
			if (this.isset(this.data.container[size]))
			{
				this.data.container[size]['element'].innerHTML = this.helper_iframe(url, this.config.sizes[size]['width'], this.config.sizes[size]['height']);
				
				return TRUE;
			}
			else
			{
				return FALSE;
			}
			
			this.log_message('[STOP] display_companion_single()');
		};	
		
	  	this.display_companion_acudeo = function(json_banners) 
	  	{
	  		var banners = eval(json_banners);
	   		for (var i in banners)
	  		{
	  			if (this.isset(this.data.container[banners[i].size]))
	  				this.data.container[banners[i].size]['element'].innerHTML = ' ';
	  		}
	  	
			for (var size in this.data.container)
			{	
				tmDisplayBanner(banners, this.config.prefix + size, this.config.sizes[size]['width'], this.config.sizes[size]['height'], size);	
			}
				
			this.log_message('[STOP] display_companion_acudeo()');
		};		
		
		this.change_banner = function() 
		{			
			this.log_message('[START] change_banner()');
		
			this.timer_stop();
		
			for (var size in this.data.container)
			{		
				if (this.isset(this.data.container[size]) && this.isset(this.data.banner[size])) 
				{
			 		this.data.container[size]['element'].innerHTML = this.helper_iframe(this.data.banner[size][this.data.container[size]['rotation']]['image'], this.config.sizes[size]['width'], this.config.sizes[size]['height']);
					 				
			 		this.banner_rotation(size);
			 	}
			}
			this.data.run.change_banner++;
			
			if (this.config.timer.set)
				this.data.timer = setTimeout(auto_rotation, this.config.timer.length);		
			
			this.log_message('[STOP] change_banner()');
		};
		
		this.auto_rotation = function() 
		{			
			this.log_message('[START] auto_rotation()');
		
			if (this.config.timer.rotate)
			{
				this.change_banner();
			}
			else
			{
				for (var size in this.data.container)
				{		
					if (this.isset(this.data.container[size]) && this.isset(this.data.banner[size]))
					{
						if (this.config.timer.impression)
							this.timer_impression(size);
						else if (this.config.timer.tracking)
				 			this.tracking_insert(size);
				 	}
				}
			
				this.data.timer = setTimeout(auto_rotation, this.config.timer.length);
			}
			
			this.log_message('[STOP] auto_rotation()');
		};
		
		this.timer_impression = function(size)
		{
			this.log_message('[START] timer_impression()');
			
			var rotation = this.banner_rotation_last(this.data.container[size]['rotation']);
		
			this.data.container[size]['element'].innerHTML = this.helper_iframe(this.data.banner[size][rotation]['image'], this.config.sizes[size]['width'], this.config.sizes[size]['height']);
		
			this.log_message('[STOP] timer_impression()');
		}
		
		this.tracking_insert = function(size)
		{		
			this.log_message('[START] tracking_insert()');
		
			if (this.data.container[size]['tracking']['element'] == 0)
			{
				var tracking_element = document.createElement('div');
				tracking_element.style.display = 'none';
			
				this.data.container[size]['tracking']['element'] = document.body.appendChild(tracking_element);	
			}
			
			var rotation = this.banner_rotation_last(this.data.container[size]['rotation']);
			
			this.data.container[size]['tracking']['element'].innerHTML = this.helper_iframe(this.data.banner[size][rotation]['image'], 1, 1);
			
			this.log_message('[STOP] tracking_insert()');
		}
		
		this.timer_stop = function()
		{
			clearTimeout(this.data.timer);
		}
		
		this.banner_rotation_last = function(rotation)
		{
			if (rotation > 0)
				return rotation-1;
			else
				return 0;
		}
		
		this.banner_reset = function()
		{
			this.timer_stop();
		
			for (var size in this.data.container)
			{
				this.data.container[size]['element'].innerHTML = ' ';
				this.data.container[size]['rotation'] = 0;
			}
			
			this.log_message('[STOP] banner_reset()');
		};
		
		this.load_banner = function(json_banner)
		{				
			this.log_message('[START] load_banner()');
			
			if (this.data.run.change_banner > 0)
			{		
				this.banner_reset();
				this.data.banner = new Array();			
			}
			else
			{
				// discover_banner should have already run, another chance
				if (this.data.run.discover_banner == 0)
					this.discover_banner();
			}

			var banner = eval('('+json_banner+')');
			
			for (var i in banner)
	  		{	
	  			// BEGIN: find new key
				var key = 0;
			
				if (this.isset(this.data.banner[banner[i].size]))
					key = this.data.banner[banner[i].size].length;
				else 
					this.data.banner[banner[i].size] = [];
				// END: find new key
				
				// Set banner configuration for later use
				this.data.banner[banner[i].size][key] = { 
					image:			banner[i].imageUrl
				};	  			 		
	  		}
			
			this.change_banner();
			
			this.log_message('[STOP] load_banner()');
		};
		
		this.banner_rotation = function(size)
		{						
			if (this.data.banner[size].length == this.data.container[size]['rotation'] + 1)
				this.data.container[size]['rotation'] = 0;
			else
				this.data.container[size]['rotation']++;
				
			this.log_message('[STOP] banner_rotation() [' + size + '] rotation: ' + this.data.container[size]['rotation']);
		};    	 
		
		this.isset = function(node)
		{
			if (typeof(node) != "undefined")
				return true;
			else
				return false; 
		};

		this.helper_iframe = function(url, width, height)
		{
			return "<iframe src='" + url + "' marginwidth=0 border=0 frameborder=0 width=" + width + " height=" + height + " scrolling=no></iframe>";
		};

		this.log_message = function(message)
		{
			if (this.config.log.enabled)
			{
				var time = new Date();
				var current_time = time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();
			
				this.data.log = current_time + ' ' + message + '<br />' + this.data.log;
				
				if (typeof(console) != "undefined")
					console.log(current_time + ' ' + message);
				
				var container = document.getElementById(this.config.log.id);
			
				if (this.isset(container))
					container.innerHTML = this.data.log;
			}
		};

		this.add_load_event = function(func) 
		{
			var oldonload = window.onload;
			if (typeof window.onload != 'function') {
				window.onload = func;
			} else {
				window.onload = function() {
					if (oldonload) {
						oldonload();
				    }
					func();
				}
			}
		};
		this.add_load_event(this.ini);

	}();
		
	function auto_rotation()
	{
		G2.ad.auto_rotation();
	
	}		
	
	/* BEGIN: acudeo */
	
	function tmDisplayBanner(banners, divName, width, height, defaultSize) {
		if (!defaultSize) defaultSize = "300x250";
		var size = width + 'x' + height;
		var adDiv = document.getElementById(divName);
		
		// Loop through all the banners
		var newBanners = new Array(); 
		if (banners.constructor.toString().indexOf("Array") == -1) {
			for (var i in banners) {
				if (banners[i].type) {
					var newBanner = null;
					for (var j=0; j<newBanners.length; j++) {
						if (newBanners[j].size == banners[i].size) {
							newBanner = newBanners[j];
							break;
						}
					}
					if (newBanner == null) {
						newBanner = new Object();
						newBanner.size = banners[i].size;
						newBanner.width = banners[i].width;
						newBanner.height = banners[i].height;
						newBanners[newBanners.length] = newBanner;
					}
					if (banners[i].type == "image") {
						newBanner.imageUrl = banners[i].imageUrl ? banners[i].imageUrl : banners[i].url;
						newBanner.clickUrl = banners[i].clickUrl;
					} else if (banners[i].type == "iframe") {
						newBanner.htmlUrl = banners[i].htmlUrl ? banners[i].htmlUrl : banners[i].url;
					} else if (banners[i].type == "swf") {
						newBanner.swfUrl = banners[i].swfUrl ? banners[i].swfUrl : banners[i].url;
					}
				} else {
					tmDisplaySingleBanner(banners[i], adDiv, width, height, size, defaultSize);
				}
			}
			if (newBanners.length > 0) {
				for (var i=0; i<newBanners.length; i++) {
					tmDisplaySingleBanner(newBanners[i], adDiv, width, height, size, defaultSize);
				}
			}
		} else {
			for (var i=0; i<banners.length; i++) {
				if (banners[i].type) {
					var newBanner = null;
					for (var j=0; j<newBanners.length; j++) {
						if (newBanners[j].size == banners[i].size) {
							newBanner = newBanners[j];
							break;
						}
					}
					if (newBanner == null) {
						newBanner = new Object();
						newBanner.size = banners[i].size;
						newBanner.width = banners[i].width;
						newBanner.height = banners[i].height;
						newBanners[newBanners.length] = newBanner;
					}
					if (banners[i].type == "image") {
						newBanner.imageUrl = banners[i].imageUrl ? banners[i].imageUrl : banners[i].url;
						newBanner.clickUrl = banners[i].clickUrl;
					} else if (banners[i].type == "iframe") {
						newBanner.htmlUrl = banners[i].htmlUrl ? banners[i].htmlUrl : banners[i].url;
					} else if (banners[i].type == "swf") {
						newBanner.swfUrl = banners[i].swfUrl ? banners[i].swfUrl : banners[i].url;
					}
				} else {
					tmDisplaySingleBanner(banners[i], adDiv, width, height, size, defaultSize);
				}
			}
			if (newBanners.length > 0) {
				for (var i=0; i<newBanners.length; i++) {
					tmDisplaySingleBanner(newBanners[i], adDiv, width, height, size, defaultSize);
				}
			}
		}
	}
	
	function tmDisplaySingleBanner(banner, adDiv, width, height, size, defaultSize) {
		// Find the correct sized one
		var bannerSize = banner.size ? banner.size : banner._size;
		if ((bannerSize == null) || (bannerSize == "undefinedxundefined") || (bannerSize == "NaNxNaN") || (bannerSize == "_default"))
			bannerSize = defaultSize;
		if (bannerSize == size) {
			// Remove old ads
			var old_iframe = document.getElementById("acudeo_iframe");
			if (old_iframe) {
				adDiv.removeChild(old_iframe);
			}
			var old_span = document.getElementById("acudeo_span");
			if (old_span) {
				adDiv.removeChild(old_span);
			}
			var old_a = document.getElementById("acudeo_a");
			if (old_a) {
				adDiv.removeChild(old_a);
			}
			
			// If there is a iframe, use it
			var htmlUrl = banner.htmlUrl ? banner.htmlUrl : banner._htmlUrl;
			var swfUrl = banner.swfUrl ? banner.swfUrl : banner._swfUrl;
			var imageUrl = banner.imageUrl ? banner.imageUrl : banner._imageUrl;
			var clickUrl = banner.clickUrl ? banner.clickUrl : banner._clickUrl;
			if (htmlUrl) {
				// Create the iframe
				var iframe = document.createElement("iframe");
				iframe.setAttribute("id", "acudeo_iframe");
				iframe.style.width = width + "px";
				iframe.style.height = height + "px";
				iframe.style.border = "0";
				iframe.scrolling = "no";
				iframe.marginWidth = "0";
				iframe.marginHeight = "0";        
				iframe.frameBorder = "no";
				adDiv.appendChild(iframe);
				
				// IE has a weird caching issue, so we have to append the 
				// iframe, then set the src
				var new_iframe = document.getElementById("acudeo_iframe");
				new_iframe.src = htmlUrl;
				new_iframe.frameborder = "no";
				
			// Otherwise if there is a SWF URL, use that
			} else if (swfUrl) {
				// Get the object HTML
				var objHtml = '<object';
				objHtml += ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + width + '" height="' + height+ '" id="acudeo_swf">';
				objHtml += '<param name="movie" value="' + swfUrl + '" />';
				objHtml += '<param name="quality" value="high" />';
				objHtml += '<param name="wmode" value="transparent" />';
				objHtml += '<param name="allowfullscreen" value="true" />';
				objHtml += '<param name="allowscriptaccess" value="always" />';
				objHtml += '<!--[if !IE]>-->';
				objHtml += '<object type="application/x-shockwave-flash" data="' + swfUrl + '" width="' + width + '" height="' + height + '" id="acudeo_swf">';
				objHtml += '<param name="quality" value="high" />';
				objHtml += '<param name="wmode" value="transparent" />';
				objHtml += '<param name="allowfullscreen" value="true" />';
				objHtml += '<param name="allowscriptaccess" value="always" />';
				objHtml += '<!--<![endif]-->';
				objHtml += '<a href="http://www.adobe.com/go/getflashplayer">';
				objHtml += '<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />';
				objHtml += '</a>';
				objHtml += '<!--[if !IE]>-->';
				objHtml += '</object>';
				objHtml += '<!--<![endif]-->';
				objHtml += '</object>';
		
				// Create a span to hold the
				var span = document.createElement("span");
				span.setAttribute("id", "acudeo_span");
				
				// Append the span and set the HTML
				if (navigator.appName.indexOf("Microsoft") != -1) {
					adDiv.appendChild(span);
					var span = document.getElementById("acudeo_span");
					span.outerHTML = objHtml;
				} else {
					span.innerHTML = objHtml;
					adDiv.appendChild(span);
				}
				
			// Otherwise if there is an image, use that
			} else if (imageUrl) {
				// Create the anchor tag
				var a = document.createElement("a");
				a.setAttribute("href", clickUrl);
				a.setAttribute("target", "_blank");
				a.setAttribute("id", "acudeo_a");
				
				// Create the image tag
				var img = document.createElement("img");
				img.setAttribute("src", imageUrl);
				img.setAttribute("border", 0);
				img.setAttribute("width", width);
				img.setAttribute("height", height);
				a.appendChild(img);
				
				// Add the tag
				adDiv.appendChild(a);
			}
		}
	}
	
	/* END: acudeo */
	