/**
 * guide_ads.js
 **/

function ad(objectName) {
	this.objectName = objectName;
	this.rand_num = Math.floor(Math.random()*10000000000);
	this.tile_count = 1;
	
	this.getSect = function (section_in) {
		// Translates Guide sections into adserver sections
		var t = new Array();
		t['adtest'] = 'adtest'; // for adcode testing purpose only
		t['contextpane'] = 'context';
		t['contextpane/music'] = 'context_music';
		t['contextpane/entertainment'] = 'context_entertainment';
		t['contextpane/movies'] = 'context_entertainment';
		t['contextpane/erotic'] = 'context_adult';
		t['contextpane/news'] = 'context_news';
		t['contextpane/sports'] = 'context_sports';
		t['contextpane/radiopass'] = 'context_radiopass';
		t['contextpane/radiotv'] = 'context_radiotv';
		t['music'] = 'music';
		t['entertainment'] = 'entertainment';
		t['movies'] = 'entertainment';
		t['erotic/erotic_2'] = 'adult';
		t['erotic'] = 'other';
		t['lifestyle'] = 'other';
		t['games'] = 'games';
		t['news'] = 'news';
		t['radiotv'] = 'tuner';
		t['sports'] = 'sports';
		t['home'] = 'home';
		if (page.getVar('edition_code')=='jp') {
			t['contextpane/gravureidol'] = 'context_adult';
			t['gravureidol'] = 'adult';
			t['contextpane/movies'] = 'context_other';
			t['movies'] = 'other';
		}
		var section_out = (t[section_in]) ? t[section_in] : null;
		if (!section_out) {
			section_out = 'other';
			for (i in t) {
				if (section_in.indexOf(i) != -1) {
					var section_out = t[i];
					break;
				}
			}
		}
		return section_out;
	}
	
	this.getAdCode = function (format) {
		var p = (typeof format == 'object') ? format : {format:format};
		var format = p.format;
		var mode = p.mode || null;
		var sect = this.getSect(page.getVar('page_path'));
		var ec = page.getVar('edition_code');
		if (ec == 'eu') ec = 'uk'; // for advertisements UK and EU are the same...
		if (typeof p.win != 'object') p.win = self;
		if (p.win.isAdvertisementIFrame != true) {
			switch (p.format) {
				
				case 'minisky' :
					p.iframe = true; p.width = 125; p.height = 125; break;
					
				case 'fullbanner' :
					p.iframe = true; p.width = 468; p.height =  60; break;
					
				case 'button' :
					p.iframe = true; p.width = 120; p.height =  60; break;
					
				case 'skyscraper' :
					p.iframe = true; p.width = 120; p.height = 600; break;
					
				case 'rectangle' :
					p.iframe = true; p.width = 300; p.height = 250; break;
					
				case 'square' :
					p.iframe = true; p.width = 250; p.height = 250; break;
					
				case 'leaderboard' :
					p.iframe = true; p.width = 728; p.height =  90;
					break;
			}
			if (p.iframe == true) {
				return '<iframe src="'+page.getVar('basepath')+'inc/guide_ads/get.html?format='+format+'&amp;bid='+(p.bid||this.rand_num)+'" width="'+p.width+'" height="'+p.height+'" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" name="adFrame_'+format+'"></iframe>';
			}
		}
		if (page.getVar('previewmode') && mode!='test') {
			return false;
			return '<div style="font-family:sans-serif;font-size:11px;padding:5px;color:#000;">'+
				'Ad format: <b>'+format+'</b>, Section: <b>'+sect+'</b>, Edition: <b>'+ec+'</b>'+
				(p.win.isAdvertisementIFrame == true ? ', <a href="'+page.getVar('basepath')+'inc/guide_ads/get.html?format='+format+'&mode=test">Test</a>':'')+
				'<br><span style="color: silver;">'+this.getAdCode({format:format,win:p.win,mode:'test'}).toString().replace('<','&lt;')+'<br>&nbsp;</span>'+
				'</div>';
		}
		else if (sect && this.userShouldGetAds()) {
			switch (format) {
				case 'minisky' :
					return '<script language="JavaScript" type="text/javascript" src="http://ad.'+ec+'.doubleclick.net/adj/real.'+ec+'/guide/minisky_125x125/'+sect+'/;sect='+sect+';sz=125x125;tile='+(this.tile_count++)+';ord='+this.rand_num+'?"><\/script>';
				break;
				case 'fullbanner' :
					return '<script language="JavaScript" type="text/javascript" src="http://ad.'+ec+'.doubleclick.net/adj/real.'+ec+'/guide/fullbanner_468x60/'+sect+'/;dcopt=ist;sect='+sect+';sz=468x60;tile='+(this.tile_count++)+';ord='+this.rand_num+'?"><\/script>';
					break;
				case 'button' :
					return '<script language="JavaScript" type="text/javascript" src="http://ad.'+ec+'.doubleclick.net/adj/real.'+ec+'/guide/button_120x60/'+sect+'/;sect='+sect+';sz=120x60;tile='+(this.tile_count++)+';ord='+this.rand_num+'?"><\/script>';
					break;
				case 'skyscraper' :
					switch (page.getVar('edition_code')) {
						case 'mx':
							if (window.skyscraper) {
							window.skyscraper.google_ad_client = "pub-0805021295990372";
							window.skyscraper.google_ad_width = "120";
							window.skyscraper.google_ad_height = "600";
							window.skyscraper.google_ad_format = "120x600_as";
							window.skyscraper.google_ad_channel = "8493135112";
							window.skyscraper.google_ad_type = "text";
							window.skyscraper.google_color_border = "1E60AA";
							window.skyscraper.google_color_bg = "6A95C6";
							window.skyscraper.google_color_link = "FFFFFF";
							window.skyscraper.google_color_url  = "31373D";
							window.skyscraper.google_color_text = "CCE2FB";
							window.skyscraper.google_page_url = window.location.href;
							return '<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>';
							}
						break;
						default:
							return '<script language="JavaScript" type="text/javascript" src="http://ad.'+ec+'.doubleclick.net/adj/real.'+ec+'/guide/skyscraper_120x600/'+sect+'/;sect='+sect+';sz=120x600;tile='+(this.tile_count++)+';ord='+this.rand_num+'?"><\/script>';
					}
					break;
				case 'rectangle' :
					return '<script language="JavaScript" type="text/javascript" src="http://ad.'+ec+'.doubleclick.net/adj/real.'+ec+'/guide/rectangle_300x250/'+sect+'/;sect='+sect+';sz=300x250;tile='+(this.tile_count++)+';ord='+this.rand_num+'?"><\/script>';
					break;
				case 'square' :
					return '<script language="JavaScript" type="text/javascript" src="http://ad.'+ec+'.doubleclick.net/adj/real.'+ec+'/guide/square_250x250/'+sect+'/;sect='+sect+';sz=250x250;tile='+(this.tile_count++)+';ord='+this.rand_num+'?"><\/script>';
					break;
				case 'leaderboard' :
					return '<script language="JavaScript" type="text/javascript" src="http://ad.'+ec+'.doubleclick.net/adj/real.'+ec+'/guide/leaderboard_728x90/'+sect+'/;dcopt=ist;sect='+sect+';sz=728x90;tile='+(this.tile_count++)+';ord='+this.rand_num+'?"><\/script>';
					break;
				case 'stream' :
					var plyrObj = window.external;

					if (zeitgeist.isBrowserRealPlayer() && !zeitgeist.isUserLoggedIn()) {
						switch (page.getVar('edition_code')) {
							case 'uk':
								this.plyr_obj = parent.window.external;
								bandwidth = this.plyr_obj.PlayerProperty('BANDWIDTH');
								stream_url = 'http://ad.'+ec+'.doubleclick.net/adx/real.'+ec+'/guide/stream_780x180/'+sect+'/;bw='+bandwidth+';ctgry='+sect+';sz=780x180;ord='+this.rand_num+'.smi';
								this.playStreamAd(stream_url);
								break;
							default :
								this.plyr_obj = parent.window.external;
					
for (i in this.plyr_obj){
alert (i + ' --> '+this.plyr_obj[i]);
}

								bandwidth = this.plyr_obj.PlayerProperty('BANDWIDTH');
								stream_url = 'http://ad.'+ec+'.doubleclick.net/adx/real.'+ec+'/guide/stream_240x180/'+sect+'/;bw='+bandwidth+';ctgry='+sect+';sz=240x180;ord='+this.rand_num+'.smi';
								//this.playStreamAd(stream_url);
								break;
						}
					}
					return;
					break;
				default:
					return 'Unknown ad format: "'+format+'"';
					break;
			}
		}
		else {
			switch (format) {
				case 'skyscraper' :
					return '<style type="text/css">body{background-color:#b9cde5}</style>';
				default:
					return '';
				break;
			}
		}
	}
	
	this.playStreamAd = function (stream_url) {
		if (document.readyState == 'complete' ) {
			if( !this.plyr_obj.GetPlayerState() ) {
				this.plyr_obj.PlayClip(stream_url, '', '', 0, 0, '', '', false);
			}
		}
		else {
			setTimeout(this.objectName+'.playStreamAd("'+stream_url+'")', 1000);
		}
	}
	
	this.userShouldGetAds = function () {
		// when the user is viewing the player guide and has any 
		// subscription, he should get no ads
		if (page.getCurrentBasePath() == "/guide-player/") {
			var snValue = zeitgeist.getSNValue();
			if (snValue) return false;
		}
		// otherwise show ads
		return true;
	}
}
var advert = new ad('advert');
var my_ad = advert; // legacy

if (advert.userShouldGetAds())
	document.write('<style type="text/css"> .userShouldNotGetAds { display: none; }  .userShouldGetAds { display: block; } </style>');
else 
	document.write('<style type="text/css"> .userShouldGetAds { display: none; }  .userShouldNotGetAds { display: block; } </style>');
