wbds_counttags=0;
wbds_serverup=true;
wbds_firstdone=false;
wbds_waitserver=4000;
wbds_pageid=-1;

function wbds_testserver() {
 if (wbds_waitserver>0 && !wbds_firstdone) {
 wbds_waitserver-=200;
 setTimeout('wbds_testserver()',200);
 } else if (!wbds_firstdone) {
 wbds_serverup=false;
 if (document.all) {
 var ua=navigator.userAgent;
 if (parseFloat(ua.slice(ua.indexOf('MSIE')+5))>=5) document.scripts['wbds_tag0'].src='';
 }
 }
}


function wbds_writeTag(wbds_size, wbds_position) {
 var rnd=Math.round(Math.random()*100000000);
 if (wbds_pageid<0) wbds_pageid=Math.round(Math.random()*100000000);
 if (wbds_serverup) {
	var parent = document.body;
 if (wbds_position == "RIGHT") {
	  if (document.all) {
  document.write('<div style="position:absolute;top:87px;left:805px;width:auto;clear:both;">')	
	  } else{ 
  document.write('<div style="position:absolute;top:87px;left:10px;width:auto;clear:both;">')	
	  }
  
 }
 document.write('<scr'+'ipt language="javascript" id="wbds_tag'+wbds_counttags+'" src="');
 document.write(wbds_target+'/ADSIZE='+wbds_size+'/AAMSZ='+wbds_size+'/POSITION='+wbds_position+'/PAGEID='+wbds_pageid+'/ACC_RANDOM='+rnd+'"></scr'+'ipt>');
 if (wbds_position == "RIGHT") {
  document.write("</div>");	
 }
 if (wbds_counttags++==0) setTimeout('wbds_testserver()',200);
 }
}

function findTranslation (id) {
	if (typeof(translationsById) == undefined) {
		return "UPCLIVE_OTHER";
	}
	
	for (var idx = 0; idx < translationsById.length; idx++) {
		for (var idx2 = 0; idx2 < translationsById[idx].id.length; idx2++) {
			if (id == translationsById[idx].id[idx2]) {
				return translationsById[idx].translation;
			}
		}
	}
	return "UPCLIVE_OTHER";
}

function getBannerCode () {
	var domain = document.domain;
	var t_channel = null;
	var t_subchannel = null;
	if ( arguments.length == 1 ) {
		var locale = null;
		var channel = null;
		var subchannel = null;
		var position = arguments[0];
	} else {
		var locale = arguments[0];
		var channel = arguments[1];
		var subchannel = arguments[2];
		var position = arguments[3];
	}

	if ( locale == null ) {
		locale = domain.substring(domain.lastIndexOf(".")+1, domain.length);
		if (locale == 'be') {
      		lang = Cookie.Read('lang');
      		if (lang != '') {
        		locale = 'be_' + lang;
      		} else {
				locale = 'be_nld';	
			}
    	}
	}	
	
	if ( channel == null ) {
		var channel_re = new RegExp("^\/([a-zA-Z0-9_]*)\/?([a-zA-Z0-9_]*)\/?.*", "gi");
		var pathname_values = channel_re.exec(document.location.pathname);
		if ( pathname_values != null ) {
			if ( pathname_values.length > 1 && pathname_values[1] != '' ) {
				channel = pathname_values[1];
				subchannel = pathname_values[2];
			} else {
				channel = "Start";
				subchannel = null;
			}
		}
	}
	
	if ( typeof translations[locale] != 'undefined' ) {
		if ( channel != null && translations[locale][channel] != null ) {
			t_channel = translations[locale][channel]["_name_"] || "";
  		if ( subchannel != null && translations[locale][channel][subchannel] != null  ) {
	   		t_subchannel = translations[locale][channel][subchannel] || "";
	 	  }
	 	}
	}
	
	wbds_target = 'http://adserver.webads.nl/jserver/SITE=UPCLIVE/AREA=' + (t_subchannel || t_channel || "UPCLIVE_OTHER");
		
	if ( typeof position != 'undefined' ) {
		switch ( position ) {
			case 'leaderboard':
			case 'fullbanner':
				size = '468X60';
				position = 'TOP';
				break;
			case 'largerectangle':
				break;
			case 'skyscraper':
			case 'skyscraper_chello':
				size = '120X600';
				position = 'RIGHT';				
			  	break;
			case 'mediumrectangle':			
			case 'mediumrectangle_plus':
				size = '336X280';
				position = 'MIDDLE';
				break;
		}
		if (document.location.search.indexOf("bannerDebug=1") > -1) {
			alert('wbds_writeTag(' + size + ',' + position + ')');
		}
		if (size != null) {
			wbds_writeTag(size, position);
		}
	} else {
			wbds_writeTag('468X60','TOP');
			wbds_firstdone = true;
//			wbds_writeTag('120X600', 'RIGHT');		
	}	
}

function getBannerCodeById (channelId, position) {	
	var domain = document.domain;
	var t_channel = null;
	var t_subchannel = null;
//	var inChello = CookieRead("in_chello_range") ? 1 : 0;
	var size = null;
	
	wbds_target = 'http://adserver.webads.nl/jserver/SITE=UPCLIVE/AREA=' + findTranslation(channelId);
		
	if ( typeof position != 'undefined' ) {
			switch ( position ) {
			case 'leaderboard':
			case 'fullbanner':
				size = '468X60';
				position = 'TOP';
				break;
			case 'skyscraper':
			case 'skyscraper_chello':
				size = '120X600';
				position = 'RIGHT';
				break;
			case 'mediumrectangle':
			case 'mediumrectangle_plus':
				break;
		}
		if (document.location.search.indexOf("bannerDebug=1") > -1) {
			alert('wbds_writeTag(' + size + ',' + position + ')');
		}
		if (size != null) {
			wbds_writeTag(size, position);
		}
	}
}
