/*
	$Revision: 1.8 $Date: 2012/01/04 11:39:26 $
	SevenOne Media Ad Integration for www.sport1.de
*/

window.myAd = {
	revision: '$Revision: 1.8 $Date: 2012/01/04 11:39:26 $',
	soi_site_script:  'sport1.js',

	container_prefix: 'ad-',

	delivered:  {},
	finished:   {},
	
	loadScript: function(which) {
			if (!window.SOI_WERBUNG) return;
			
			var src = '';
			switch (which) {
				case 'global':
					src = '/globalV6.js';
					break;
				case 'site':
					if (this.soi_site_script) src = '/Sites/' + this.soi_site_script;
					break;
			}
			if (!src) return;
			var server = 'http://ad.71i.de/global_js';

			document.write('<script src="' + server + src + '" type="text/javascript"><\/script>');
		},
	insertAd: function(ad_id) {
			if (!window.SOI_WERBUNG) return;
			if (!window.SoiAd) return;
			if (this.delivered[ad_id]) return;

			var go = false;
			switch (ad_id) {
				case 'popup1':
					go = window.SOI_PU1;
					// adjust possible misconfiguration
					if (window.SOI_BB && !document.getElementById(this.container_prefix + 'fullbanner2-billboard'))
						window.SOI_BB = false;
					break;
				case 'fullbanner2':
					go = window.SOI_FB2;
					break;
				case 'skyscraper1':
					go = window.SOI_SC1;
					break;
				case 'rectangle1':
					go = window.SOI_RT1;
					break;
				case 'ateaser': // contentbox
					go = window.SOI_TSA;
					break;
				case 'promo1':
					go = window.SOI_PB1;
					break;
				case 'promo2':
					go = window.SOI_PB2;
					break;
				case 'promo3':
					go = window.SOI_PB3;
					break;
				case 'performance1':
					go = window.SOI_PF1;
					break;
			}
			if (!go) return;
			// misconfiguration: target container is missing
			if (!document.getElementById(this.container_prefix + ad_id))
				return;
			
			this.delivered[ad_id] = true;
			SoiAd.write(ad_id);
		},
	finishAd: function(ad_id, mode) {
			if (this.finished[ad_id]) return;
			this.finished[ad_id] = true;
			switch (mode) {
				case 'move':
					this.moveAd(ad_id);
					break;
			}

			this.adjustLayoutForAd(ad_id, mode);
		},
	moveAd: function(ad_id) {
			if (!window.SOI_WERBUNG) return;
			if (!window.SoiAd) return;
			
			// sport1 special
			if (ad_id.match(/^(promo[1-3])$/) && window.SoiAd && window.SoiAd.exists(ad_id))
				this.renderAd(ad_id);
			
			SoiAd.moveAd(ad_id, this.container_prefix + ad_id + '-postponed', this.container_prefix + ad_id);
		},
	adjustLayoutForAd: function(ad_id, mode) {
			var container = document.getElementById(this.container_prefix + ad_id);
			if (!container) return;

			var exists = window.SoiAd ? SoiAd.exists(ad_id) : false;

			if (exists) {
				if (ad_id != 'fullbanner2' || !SoiAd.isBillboard(ad_id)) SoiAd.removeStyleAttribute(container);

				if (SoiAd.reserveSowefoSpace(ad_id)) {
					container.style.backgroundColor = 'transparent';
					container.style.backgroundImage = 'none';
					container.style.borderColor     = 'transparent';
				}
				else if (SoiAd.isBlockpixel(ad_id)) {
					container.style.display = 'none';
				}
			}

			if (ad_id == 'popup1') {
				if (exists) {
					// Nothing to be done
				}
			}
			else {
				var width  = 0;
				var height = 0;

				if (exists) {
					width  = SoiAd.getWidth(ad_id);
					height = SoiAd.getHeight(ad_id);
				}

				if (ad_id == 'fullbanner2') {
					if (exists) {
						var is_powerbanner = SoiAd.isPowerbanner(ad_id);
						var is_pushdown    = SoiAd.isPushdown(ad_id);
						var is_wallpaper   = SoiAd.isWallpaper(ad_id);
						var is_fireplace   = SoiAd.isFireplace(ad_id);
						var is_billboard   = SoiAd.isBillboard(ad_id);
						
						container.style.width = width + 'px';

						if (is_pushdown || is_powerbanner) {
							SoiAd.setAutoHeight(ad_id, this.container_prefix + ad_id);
						}
						else if (is_wallpaper || is_fireplace) {
							container.style.height = Math.max(height, 90) + 'px';
						}
						else if (is_billboard) {
							container.style.backgroundImage = 'none';
							container.style.display = 'none';
							var special_id = this.container_prefix + ad_id + '-billboard';
							var special_container = document.getElementById(special_id);
							if (special_container) {
								SoiAd.removeStyleAttribute(special_container);
								var max_width = parseInt(SoiQuery.getCurrentStyle(special_id + '-outer', 'width')) || 810;
								var delta = Math.max(parseInt((width - max_width) / 2), 0);
								if (delta) {
									// Wider than content - centrify via negative margin-left
									special_container.style.marginLeft = (-1 * delta) + 'px';
									SoiUtils.addStyleElement('#ad-skyscraper1-outer {margin-left:' + delta + 'px;}');
								}
								else {
									// Not wider than content - centrify via auto margin
									special_container.style.width = width + 'px';
								}
							}
						}
					}
					else {
						// No collapse if not delivered
						// if (!window.SOI_VP || window.SOI_AUTOPLAY == 'off') container.style.display = 'none';
					}
				}
				else if (ad_id == 'rectangle1') {
					if (exists) {
						var is_halfpage = SoiAd.isHalfpage(ad_id);
						if (is_halfpage) {
							SoiAd.setAutoHeight(ad_id, this.container_prefix + ad_id);
						}
						if (width && width > 300) {
							// powerrectangle case
							container.style.width = width + 'px';
						}
					}
				}
				else if (ad_id == 'skyscraper1') {
					if (exists) {
						if (width && width > 160) {
							container.style.width = width + 'px';
						}
					}
				}
				else if (ad_id == 'ateaser') {
					if (exists) {
					}
				}
				else if (ad_id.match(/^promo[1-3]$/)) {
					if (exists) {
						container.style.width = width + 'px';
						if (width && width > 290)
							container.style.marginLeft = parseInt((290 - width) / 2) + 'px';
					}
				}
				else if (ad_id == 'performance1') {
					if (exists) {
						container.style.width = width + 'px';
					}
				}
			}
		},
	renderAd: function(ad_id) {
			if (!ad_id.match(/^(?:promo[1-3])$/)) return;
			if (!window.soi_json_ad || typeof window.soi_json_ad != 'object') return;

			var data = {};
			for (var k in window.soi_json_ad) {
				data[k] = window.soi_json_ad[k];
			}
			// reset global variable
			window.soi_json_ad = undefined;
			
			var width  = SoiAd.getWidth(ad_id);

			var container = document.getElementById(this.container_prefix + ad_id + '-postponed');
			if (container) {
				var title    = data.title || '';
				var text     = data.text || '';
				var image    = data.image;
				var alt      = data.alt || '';
				var linktext = data.linktext || '';
				var url      = data.url;
				
				if (url && (title || text || image || linktext)) {
					var s = ''
						+ '<div style="position:relative;padding:0px 8px 8px;overflow:hidden;">'
							+ '<div style="position: absolute;">'
								+ '<a class="link image" href="' + url + '" target="_blank">'
									+ '<img src="' + image + '" alt="' + alt + '" title="' + alt + '" style="width: 90px; height: 75px; border-style: none;" \/>'
								+ '<\/a>'
							+ '<\/div>'
							+ '<div style="margin-left:100px;padding-bottom:10px;">'
								+ '<h3 class="font_30" style="margin-bottom:2px;""><a href="' + url + '" target="_blank" class="font_31">' + title + '<\/a><\/h3>'
								+ '<div class="text font_0" style="margin-bottom:3px;">' + text + '<\/div>'
								+ '<a style="color:#ef6f0a;font-size:11px;font-weight:bold;" href="' + url + '" target="_blank">' + linktext + '&nbsp;&raquo;<\/a>'
							+ '<\/div>'
						+ '<\/div>';
					container.innerHTML = s;
				}

				var head = data.head;
				if (head) {
					var bar = document.createElement('div');
					bar.className = 'reiter font_24_active';
					bar.style.marginLeft = parseInt((width - 290) / 2) + 'px';
					bar.innerHTML = head + ' <div class="advert">Anzeige<\/div>';
					container.insertBefore(bar, container.firstChild);
				}
			}
		},
	clearAd: function(ad_id) {
			var container = document.getElementById(this.container_prefix + ad_id);
			if (!container) return;
			container.innerHTML = '';
			container.style.display = 'none';
		},
	clearAds: function() {
			for (var k in this.delivered) {
				this.clearAd(this.delivered[k]);
			}
		},
	// var url = myAd.getVideoAdRequest('preroll1b', {category: 'bundesliga'});
	getVideoAdRequest: function(ad_id, data) {
			if (!window.SOI_WERBUNG) return '';
			return window.soi_VideoAdRequestSport1 ?
				  soi_VideoAdRequestSport1(ad_id, data)
				: '';
		}
};

