// © copyright AdSolutions
// Author: Olaf Buitelaar
// Contact: info@adsolutions.nl
//
//tools
function wbds_ObjectWaiter(){
	this.list = [];
	this.intid = -1;
	this.time = 35;
	this.timeout = 15000;
	this.notempty = false;
	this.owner = null;
}
pt = wbds_ObjectWaiter.prototype;
pt.check_loaded = function(){
	var l = this.list.length;
	if(this.notempty && l==0) return false;
	for(var i=0;i<l;i++){
		if(!this.list[i]){
			return false;
		}
	}
	return true;
}
pt._onLoad = function(){
	if(this.owner == null){
		this.onLoad();
	}else{
		this.onLoad.apply(this.owner);
	}
}
pt.onLoad = function(){};
pt._check = function(){
	var ce = arguments.callee;
	if(ce.owner.check_loaded()){
		ce.owner._onLoad();
		clearInterval(ce.owner.intid);
		ce.owner.intid = -1;
	}
}
pt.timeOut = function(){
	var ce = arguments.callee;
	clearInterval(ce.owner.intid);
	ce.owner.intid = -1;
}
pt.start = function(){
	if(this.intid>0){
		clearInterval(this.intid);
		this.intid = -1;
	}
	this._check.owner = this;
	this.intid = setInterval(this._check,this.time);
}
pt.clear = function(){
	if(this.intid>0){
		clearInterval(this.intid);
		this.intid = -1;
	}
	this.list = [];
}
pt.setDone = function(id){
	this.list[id] = true;
}
pt.addPoint = function(){
	var l = this.list.length;
	this.list[l] = false;
	return l;
}
function isS(v){
	if(typeof(v) != 'undefined' && v != null) return true;
	return false;
}

//constructor
function wbds_writeTags(url,protocol,script_id){
	//single ton; 1 instantie mogelijk;
	//waarde(window.__wbdsads) wordt ook gebruikt om in de iframes dit object terug te vinden.
	if(arguments[3] != null){
		this.globalvar = arguments[3];
	}else{
		this.globalvar = '__wbdsads';
	}
	if(!this.registGlobalVar(window)) return;
	
	wbds_BroadCaster.prototype.initialize(this);
	this.addListener(this);
	this.browser = new wbds_GetBrowser();
	this.d = document;
	this.w = window;
	
	this.waiter = new wbds_ObjectWaiter();
	this.waiter.owner = this;
	this.alwaysWait = false;
	this.loadedOnce = false;
	
	this.alwaysClearOnNewRequest = false;
	this.alwaysRequestOn = -1;
	this.requestdone = false;
	this.onlyRequestAfter = 0;
	this.saveBannerCnt = 0;	
	
	this.onload.owner = this;
	this.onload.contcall = false;//continue the call.
	this.onload.initool = false;//call old on load
		
	if (window.addEventListener){
		this.onload.initool = true;
  	window.addEventListener('DOMContentLoaded', this.onload, false);
	} else if (window.attachEvent){
		this.onload.initool = true;
  	window.attachEvent('onload', this.onload);
	}else{
		this.oldOnload = window.onload;
		window.onload = this.onload;
	}	
	
	this._onunload.closeooul = false;
	if(window.addEventListener){
  	window.addEventListener('unload', this._onunload, false);
  	this._onunload.closeooul = true;
	} else if (window.attachEvent){
  	window.attachEvent('onunload', this._onunload);
  	this._onunload.closeooul = true;
	}else if(window.onunload != this._onunload){
		this.oldOnUnload = window.onunload;
		window.onunload = this._onunload;
	}
	
	this.backupOnLoad.owner = this;
	this.backupOnLoad.calltimes = 0;
	this.backupOnLoad.tid = setTimeout(this.backupOnLoad,15000);
	
	
	
	this._onunload.owner = this;
	this._onunload.closeooul = false;//call old on load
	this.__imgerr.owner = this;
	this.__imgld.owner = this;
	//de locatie die het iframe zal krijgen met een normale write.
	this.scriptid = ((script_id == null || script_id == undefined)?'wbds_smarttags':script_id);
	var tmp_obj = this.d.getElementById(this.scriptid);
	if(isS(tmp_obj)){
		this.ifrmsrc = tmp_obj.src+'.html';
	}else{
		this.ifrmsrc = 'wbds_smarttags.js.html';
	}

	this.singelf = true; //is het een singel file, of hebben we een js en html file.
	this.checkonload = true; //check of we alle vars ontvangen hebben.
	this.__checkonload; //call back function
	this.bust_ifrm_docwrt = true;//wordt gebruikt in het ladende iframe, als deze geladen is en er komt nog een doc.write aan
	
	this.sURLS = []; //source urls
	this.domain = this.sURLS[0] = 'adserver.webads.nl';
	this.imaged = this.sURLS[1] = 'images.webads.nl';
	
	this.bURLS = []; //backup urls
	this.bip = this.bURLS[0] = '80.79.200.10';
	this.imageip = this.bURLS[1] = '80.79.200.11';
	
	//init
	if(!isS(protocol)) protocol = '';
	else if(protocol.toLowerCase() != 's') protocol = 's';
	if(protocol.length <= 1) this.protocol = 'http'+protocol+'://';
	else this.protocol = protocol;


	this.server_type  = 'bservers';
	this.server_selection = true; //true=auto gebruik bij meerdere ads de bservers, false=gebruik altijd server_type.
	this.ads_url = this.protocol+this.domain+'/'+this.server_type+'/'+url;
	this.base_target = url;
	this.checkurl = this.protocol+this.domain+'/xserver/SITE=SITEMONITOR/AAMSZ=1x1/NOLOG';
	this.bcheckurl = this.protocol+this.imageip+'/ads/default.gif';

	//this.checkurl = this.protocol+this.domain+
	this.pageid = -1;
	this.setPageId();
	
	//error status, properties.
	//if(this.readCookie('wbds_server_checked') == null || !this.readCookie('wbds_server_checked')){
	//	this.checkforadserverisonline = true;
	//}else{
		this.checkforadserverisonline = false;
	//}
	this.status = 2;
	this.statusimg = null;
	
	
	//Object Arrays
	this._qWrite = [];//wachtlijst voor banners na de onload
	this.banners = [];//banners
	this.scripts = [];//owrite banners
	
	this.knowBannerTypes = [['VIDEO_LAST',null],['VIDEO','wbds_VideoBanner']];
	this.knowBannerDefaultType = 'wbds_IframeBanner';
	//server check, als we in de body worden aangeroepen
	/*if(this.d.getElementsByTagName("body")[0] instanceof Object){
		this.checkServer();
	}*/
}
var pt = wbds_writeTags.prototype;
//util functions
pt.registGlobalVar = function(obj){
	if(obj[this.globalvar] != this){
		obj[this.globalvar] = this;
		return true;
	}
	return false;
}
pt.getRnd = function(){
	var tmp = (Math.round(Math.random()*10000000000)).toString(),length,i;
	length = tmp.length;
	//doubleclick counting
	if(length<10){
		length = 10-length;
		for(i=0;i<=length;i++){
			tmp = tmp+i;
		}
	}
	return tmp;
}
pt.setPageId = function(id){
	if(id==null)this.pageid = this.getRnd();
	else this.pageid =id;
}
pt.pl_placement = function(id){
	this.d.write("<div id=\"wbds_plnt"+id+"\"></div>");
	return "wbds_plnt"+id;
}
pt.setIfrmSrc = function(src){
	var tmp_obj = this.d.getElementById(this.scriptid);
	if(tmp_obj != null && tmp_obj.src != src)
		this.singelf = false;
	else
		this.singelf = true;
	this.ifrmsrc = src;
}
pt.getAAMBCont = function(id){
	return  eval('window.AAMB'+id);
}
pt.handleOnLoad = function(){
	clearTimeout(this.backupOnLoad.tid);
	if(window.removeEventListener){
		window.removeEventListener('DOMContentLoaded', this.onload, false);
	}else if(window.detachEvent){
		window.detachEvent('onload', this.onload);
	}else{
		this.onload.initool = true;
		window.onload = this.oldOnload;
	}
}
pt.setKnownBannerType = function(size,_class){
	for(var i=0;i<this.knowBannerTypes.length;i++){
		if(this.knowBannerTypes[i][0] == size.toUpperCase()){
			this.knowBannerTypes[i][1] = _class;
			return true;
		}
	}
	return false;
}
pt.getKnownBannerTypes = function(size){
	for(var i=0;i<this.knowBannerTypes.length;i++){
		if(this.knowBannerTypes[i][0] == size.toUpperCase()){
			return this.knowBannerTypes[i][1];
		}
	}
	return this.knowBannerDefaultType;
}
pt.getSize = function(size){
	var _sz = size.toUpperCase(),sz;
	if(_sz.lastIndexOf("X") > 0  &&  _sz.indexOf("X") == _sz.lastIndexOf("X")){
		sz = _sz.split('X');
		for(var i in sz) sz[i] = parseInt(sz[i]);
		if(isNaN(sz[0]) || isNaN(sz[1])) sz = [-1,-1];
	}else{
		sz = [-1,-1];
	}
	return sz;
}
pt.reload = function(force){
	if(!force || typeof(force)=='undefined' || force == null){				
		if(!this.requestdone && this.loadedOnce) return;
		
		var d = new Date();
		if(this.onlyRequestAfter>0 && d.getTime()-this.lastRequest< this.onlyRequestAfter &&  d.getTime()-this.lastRequest < 3000000){
			this.tellTimeCap();
			return;
		}
	}
	this.setPageId();
	if(this.banners.length>0){
		var bsrvcnt = 0;
		for(var i=0;i<this.banners.length;i++){
			if(this.banners[i]){
				switch(this.banners[i].callinf.calltype){
					case 'bservers':
						eval('window.AAMB'+this.banners[i].bcallid+' = null;');
						bsrvcnt++;
					break;
					default:
						this.banners[i].reload();
					break;
				}	
			}
		}
		if(bsrvcnt>0){
			wbds_Include(this.getBserURL());			
		}
		if(this.__checkonload.intid>0){
			clearInterval(this.__checkonload.intid);
		}
		this._checkonload();	
	}else if(!this.loadedOnce){
		this.onload();
	}else{
		this.proc_qWrites();
	}
	if(!this.isListener(this)){
		this.addListener(this);
	}	
}
pt.tellTimeCap = function(){
	for(var i=0;i<this.banners.length;i++){
		if(this.banners[i]){
			if(typeof(this.banners[i].tellTimeCap) ==  'function'){
				this.banners[i].tellTimeCap();
			}
		}
	}
};
pt.clear = function(){
	for(var i=0;i<this.banners.length;i++){
		if(this.banners[i]){
			eval('window.AAMB'+this.banners[i].bcallid+' = null;');
			eval('AAMB'+this.banners[i].bcallid+' = null;');
			if(typeof(this.banners[i].clear) == 'function')
				this.banners[i].clear();	
		}
	}
	this.banners = [];
}
pt._clear = function(){
	for(var i=0;i<this._qWrite.length;i++){
		if(!isS(this._qWrite[i][5]) || !isS(this._qWrite[i][5]._dontclear) || !this._qWrite[i][5]._dontclear){
			this._qWrite.splice(i,1);
			i--;
		}
	}
	this.saveBannerCnt = this._qWrite.length;
	this.clear();
}


//iframe functions
pt.resolvIfrm = function(obj){
	var length = this.banners.length,i;
	for(i=0;i<length;i++){
		if(typeof(this.banners[i]) == 'object' && typeof(this.banners[i].iframe) == "object" && this.banners[i].iframe != null){
			if(this.getIfrmWin(this.banners[i].iframe) == obj) return this.banners[i];
		}
	}
}
pt.getIfrmDoc = function(elm){
	if(this.browser.is_ie) return elm.contentWindow.document;
	else return elm.contentDocument;
}
pt.getIfrmWin = function(elm){
	try{
		if(typeof(elm.contentWindow) != 'undefined' && elm.contentWindow == null) return null;
		else return elm.contentWindow;
	}catch(e){
		return null;
	}
}
pt.getBannerFromId = function(id){
	return this.banners[id];
}
pt.getQBannerFromId = function(id){
	return this._qWrite[id];
}
pt.findBanner = function(size,position,euri,type){
	var ret = this.findBanners(size,position,euri,type,1);
	if(ret.length==0) return false;
	else return ret[0];
}
pt.findBanners = function(size,position,euri,type,limit){
	var req,hit,ret=[];
	for(var i=0;i<this.banners.length;i++){		
		req = 0;
		hit = 0;
		if(isS(size)){
			req++;
			if(size == this.banners[i].callinf.size) hit++;
		}
		if(isS(position)){
			req++;
			if(position == this.banners[i].callinf.position) hit++;
		}
		if(isS(euri)){
			req++;
			if(euri == this.banners[i].callinf.euri) hit++;
		}
		if(isS(type)){
			req++;
			if(type == this.banners[i].callinf.type) hit++;
		}		
		if(req == hit)
			ret[ret.length] = this.banners[i];
		if(isS(limit) && limit < ret.length) return ret;
	}
	return ret;	
}

//html repair functions
pt.prep_shtml = function(shtml){
	return shtml.replace(/^((\<font\>)?(<\/font\>)?\"\)\;)/g,"");
}
pt.rewr_shtml = function(shtml){
	for(i=0;i<this.sURLS.length;i++){
		shtml = shtml.replace(new RegExp(this.sURLS[i],'g'),this.bURLS[i]);
	}
	return shtml;
}
pt.prepb_shtml = function(shtml){
	var indx = shtml.indexOf('<');
	shtml = shtml.replace(/\/\/\/\//g,';').replace(/\/\/\s*flash\s*detection/,'').replace(/\\r/g, "\\\\r").replace(/\\r/g, "\\\\r").replace(/\\n/g,"\\\\n");
	if(indx < 0 || indx > 20){
		shtml = shtml.replace(/\'/g,"\\\'");
		return "document.write('<scr"+"ipt\\>"+shtml+"</scr"+"ipt\\>');";
	}else
		return shtml;
}
pt.isDefault = function(str){
	str = str.toLowerCase();
	if(str.indexOf('fffffffcfffffffcfffffffc')>0 || str.indexOf('fffffffefffffffefffffffe')>0 || str == null)
		return true;
	return false;
}
pt.isClick = function(click){
	if(isS(click) && click.length>0 && click != '""' && click != "''" && click != "" && click != "null")
		return true;	
	return false;
}
pt.doIMPCNT = function(url){	
	if(isS(url)) if(url.length<=0) return;	
	var img = this.d.createElement("img");
	var body = this.d.getElementsByTagName("body")[0];
	if(typeof(body) == "object"){
		body.appendChild(img);
	}else{
		this.d.appendChild(img);
	}

	img.src = url;
	img.style.display = "none";
	img.style.visibility = "hidden";
}
pt.getIMPCNT = function(url){
	var https = [];
	var _indx = -1;
	var indx = url.indexOf("/IMPCNT/");
	var i = url.indexOf("http://",_indx+1)
	while(i>0 && i<=url.length){
		https.push(i);
		i = url.indexOf("http://",_indx+1);
		_indx = i;
	}
	var div=url.length,__indx;
	var reg1=new RegExp('("|\'|;|\r\n|\n)');
	for(i=0;i<https.length;i++){
		if((indx-https[i])<0) break;
		if((indx-https[i])<div){
			div = (indx-https[i]);
			__indx = https[i];
		}
	}
	var url2 = url.substring(__indx,url.length);
	return url.substring(__indx,url2.search(reg1)+__indx);
}

//error functions
pt.checkServer = function(){
	if(!this.checkforadserverisonline) {
		return false;
	};		
	if(this.statusimg != null) return false;
	var chk_img = this.d.createElement("IMG");
	chk_img.style.display = 'none';
	chk_img.style.visibility = 'hidden';
	chk_img.src = this.checkurl;
	chk_img.onload = this.__imgld;
	chk_img.onerror = this.__imgerr;
	if(arguments.length > 0){
		this.statusimg = this.d.getElementById(arguments[0]).appendChild(chk_img);
	}else{
		this.statusimg = this.d.getElementsByTagName("body")[0].appendChild(chk_img);
	}
	//voor onload functie
	return true;
}
pt.__imgld = function(){
	if(this.parentNode instanceof Object)
		this.parentNode.removeChild(this);
		
	var ce = arguments.callee;
	ce.owner.createCookie('wbds_server_checked',true,30);
	if(ce.owner.onload.contcall) ce.owner.onload();
}
pt.__imgerr = function(){
	var own = arguments.callee.owner;
	if(own.status == 2){
		own.status = 1;
		this.src = own.bcheckurl;
	}else if(own.status == 1){
		own.status = 0;
		own.createCookie('wbds_server_checked',false,1);
	}else{
		return;
	}
	own.handle_error();
}

pt.handle_error = function(){
	if(this.status == 1){
		this.domain = this.bip;
		//wbds_Include(this.getBserURL());
		if(this.__checkonload.intid<0){
			this._checkonload();
		}
	}else if(this.status == 0){
		this.broadcastMessage("onFatal");
	}
}
//write function
pt.qWrite = function(size,position,euri,id,type,properties){
	if(this.browser.is_other  && (isS(properties) && (properties._skipbrowsercheck != null && !properties._skipbrowsercheck))){
		this.oWrite(size,position,euri);
	}else{
		var length = this._qWrite.length;
		if(this.requestdone && this.alwaysClearOnNewRequest && length>0){			
			this.requestdone = false;			
			//alert('clear '+this.requestdone);
			this._clear();
		}
		length = this._qWrite.length;
		if(!isS(id) && id != false){
			id = this.pl_placement(size+"_"+length);
		}
		//alert('qwrite '+this._qWrite.length+' '+this.alwaysRequestOn);
		this._qWrite[length] = [size,position,euri,id,type,properties];
		if(this.alwaysRequestOn>0 && this.alwaysRequestOn == length-this.saveBannerCnt){
			this.reload(true);
		}
	}
	return length;
}
pt.oWrite = function(size,position,euri){
	if(euri == null||euri == undefined) euri = '';
	var ot = this.server_type;
	this.server_type = 'jserver';
	this.d.write('<scr'+'ipt language="javascript" id="wbds_scrpt_'+this.ban_cnt+'" src="'+this.getAdSURL({size:size,position:position,euri:euri})+'"></scr'+'ipt>');
	this.scripts[this.scripts.length] = this.d.getElementById("wbds_scrpt_"+this.ban_cnt);
	this.server_type = ot;
	this.ban_cnt++;
}
//write handle function.
pt.proc_qWrites = function(){
	var bservercount = 0,i,temp,bcall = '';

	for(i=0;i< this._qWrite.length;i++){
		temp = this._qWrite[i];
		if(!isS(temp[4]))
			this._qWrite[i][4] = this.server_type;

		this.write(temp[0],temp[1],temp[2],temp[3],temp[4],bservercount,temp[5]);
		switch(this._qWrite[i][4]){
			case "bservers":
				bcall += 'AAMB'+bservercount+''+this.getTargetURI(temp[0],temp[1],temp[2],temp[4])+'/';
				bservercount++;
			break;
			default:				
				//
			break;
		}
	}
	this._checkonload();
	if(bservercount>0){
		wbds_Include(this.getBaseURL('bservers')+bcall);	
	}
}
pt.write = function(size,position,euri,id,type,bcount,properties){
	if(!isS(euri)) euri = '';
	if(!isS(id)) return false;
	
	
	var bannerclass = this.getKnownBannerTypes(size);
	var _bannercls = eval(bannerclass);
	if(bannerclass != null && (typeof(_bannercls) == 'function' || typeof(_bannercls) == 'object')){
		var banner = new _bannercls(this,size,position,euri,id,type,bcount,this.banners.length,properties);
		this.banners[this.banners.length] = banner;
	
		if(this.status > 0){
			var loaded = true;
			switch(type){
				case "bservers":
					var bvalue = this.getAAMBCont(bcount);
					if(!isS(bvalue)){
						loaded = false;
					}
				break;
			}		
			if(loaded){
				//alert('initialize banner calles from write');
				banner.initialize();
			}
		}	
	}else{
		switch(type){
			case "bservers":
				this.banners[this.banners.length] = {callinf:{size:size,position:position,euri:euri,calltype:type},bcallid:bcount,bannerid:this.banners.length};	
			break;
		}
	}
}


//Cookies
pt.createCookie = function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

pt.readCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

pt.eraseCookie = function(name) {
	this.createCookie(name,"",-1);
}


//url functions
//get AdServer URl.
//[arg1 Object adinf].
pt.getAdSURL = function(callinf){
	var base = this.getBaseURL(callinf.calltype);
	var target = this.getTargetURI(callinf.size,callinf.position,callinf.euri,callinf.calltype);
	return base+target;
}
pt.getAdSRnd = function(){
	return '/PAGEID='+this.pageid+'/ACC_RANDOM='+this.getRnd();
}
pt.getBaseURL = function(stype){
	if(isS(stype)){
		stype = this.server_type;
	}
	var base = this.protocol+this.domain+'/'+stype;
	switch(stype){
		case 'bservers':
			base += '/AAMALL'+this.getAdSRnd();
		break;		
	}
	base += this.getBaseTarget(this.base_target);
	return base;
}
pt.getBaseTarget = function(target){
	if(target.indexOf("/") !=0 ) target = '/'+target;
	if(target.lastIndexOf("/")< target.length) target = target+'/';
	return target;
}
pt.getTargetURI = function(size,position,euri,stype){
	if(!isS(euri)) euri = '';
	else if(euri.indexOf('/') != 0 ) euri = '/'+euri;
	target = '/AAMSZ='+size+((isS(position) && position != '')?'/POSITION='+ position:'')+euri;//removed +'/ADSIZE='+size
	if(stype != 'bservers')
		target += this.getAdSRnd();
	if(target.lastIndexOf("/") == target.length-1) target = target.substr(0,target.length-1);
	return target;
}
pt.getBserURL = function(){
	var i,length=this.banners.length,bservercount = 0,ban,bcall ='';
	for(i=0;i<length;i++){
		if(this.banners[i].callinf.calltype == 'bservers'){
			ban = this.banners[i];
			bcall += 'AAMB'+ban.bcallid+''+this.getTargetURI(ban.callinf.size,ban.callinf.position,ban.callinf.euri,ban.callinf.calltype)+'/';
			bservercount++;
		}
	}
	return this.getBaseURL('bservers')+bcall;
}


//onload
pt.onload = function(){
	var ce = arguments.callee,t = ce.owner;
	//check if ool(old onload) is function and called
	clearTimeout(t.backupOnLoad.tid);
	if((typeof(t.oldOnload) == "function" && t.oldOnload != null) && !ce.initool){
		t.oldOnload();
		ce.initool = true;
	}
	if(t.alwaysWait){
		t.waiter.notempty = true;
	}
	if(!t.waiter.check_loaded()){
		t.waiter.onLoad = ce;
		if(t.waiter.intid<0){
			t.waiter.start();
		}
		return true;
	}

	if(t.server_selection == true && length>1) t.server_type = "bservers";
	if(t.checkServer()) {
		ce.contcall = true;
		//backup
		ce.timeid = window.setTimeout(ce,15000);
		return true;
	}	
	window.clearTimeout(ce.timeid);
	t.waiter.clear();
	t.loadedOnce = true;
	t.proc_qWrites();
}
pt.backupOnLoad = function(){
	var ce = arguments.callee,t = ce.owner;
	if(!t.requestdone && ce.calltimes==0){
		t.onload();
		ce.calltimes++;
	}
}
pt._onunload = function(){
	var ce = arguments.callee,t = ce.owner,i;
	if((t.oldOnUnload != null && typeof(t.oldOnUnload) == "function") && !ce.closeooul){
		t.oldOnUnload();
		ce.closeooul = true;
	}		
	t.broadcastMessage('onunload');
	t.clear();
	t.w[t.globalvar] = null;	
}
pt.onCheckLoaded = function(){
	var i;
	var d = new Date();
	this.lastRequest = d.getTime();
	for(i=0;i<this.banners.length;i++){
		if(typeof(this.banners[i].initialize) == "function"){
			this.banners[i].initialize();
		}
	}
}
pt._checkonload = function(){
	this.requestdone = true;
	this.__checkonload.owner = this;
	this.__checkonload.intid = setInterval(this.__checkonload,60);
	this.__checkonload();
}
pt.__checkonload = function(){
	var count = 0,i,ce=arguments.callee;
	var bservercnt = 0;
	for(i=0;i<ce.owner.banners.length;i++){
		switch(ce.owner.banners[i].callinf.calltype){
			case 'bservers':
				bservercnt++;
			break;
			default:
				continue;
			break;
		}
		adstr = eval('window.AAMB'+ce.owner.banners[i].bcallid);
		if(adstr != null && adstr != '' && typeof(adstr) != 'undefined'){
			count++;
		}
	}
	if(count == bservercnt){
		ce.owner.broadcastMessage("onCheckLoaded");
		window.clearInterval(ce.intid);
		ce.intid = -1;
	}
}





//banner classes
function wbds_IframeBanner(owner,size,position,euri,id,type,bcallid,bannerid,properties){
	this.d = document;
	this.owner = owner;
	this.callinf = {size:size,position:position,euri:euri,calltype:type};
	var t = typeof(id);
	if(t == 'object' || t == 'function'){
		this.element = id;
		/*	try{
			//this.elementid = this.element.getAttribute('id');
		}catch(e){
		}*/
	}else{ 
		this.elementid = id;
		this.element = this.d.getElementById(id);
	}
	this.bcallid = bcallid;
	this.bannerid = bannerid;
	this.checkBannerExecuted.owner = this;
	this.checkBannerExecuted.checkCnt = 0;
	this.loaded = false;
	
	var i;
	for(i in properties){
		this[i] = properties[i];
	}
	
	this.iframe = null;
	this.initFrame();

	this.loopcnt = 0;
	this.maxloop = 36;	
}
pt = wbds_IframeBanner.prototype;
pt.initFrame = function(){
	try{
		this.iframe = this.d.createElement('IFRAME');
		this.iframe.setAttribute("id", "wbds_ban"+this.bannerid);

		var tmp = this.owner.getSize(this.callinf.size),w = tmp[0],h = tmp[1];
		this.iframe.setAttribute("border", 0);
		this.iframe.setAttribute("frameBorder", 0);
		this.iframe.setAttribute("allowTransparency", true);
		this.iframe.allowTransparency = true;
		this.iframe.setAttribute("scrolling", "no");
		this.iframe.setAttribute("marginHeight", 0);
		this.iframe.setAttribute("marginWidth", 0);
		this.iframe.style.borderStyle = "none";
		this.iframe.style.overflow = "hidden";
		this.iframe.style.marginLeft = "0px";
		this.iframe.style.marginTop = "0px";
		this.iframe.style.backgroundColor="transparent";

		if((w>0 && h>0)){
			this.iframe.setAttribute("width",w+0.1);
			this.iframe.setAttribute("height",h+0.1);
		}
	}catch(e){}
}
pt.initialize = function(){
	if(this.iframe == null || typeof(this.iframe) != 'object') this.initFrame();
	try{	
		if(this.d.getElementById("wbds_ban"+this.bannerid) == null)
			this.iframe = this.element.appendChild(this.iframe);

		switch(this.callinf.calltype){
			case 'hserver':
				this.iframe.src = this.owner.getAdSURL(this.callinf);
			break;
			default:
				if(this.callinf.calltype == 'bservers'){
					var strup = this.owner.getAAMBCont(this.bcallid).toUpperCase();
					if(this.owner.isDefault(strup)){
						this.clear();
						return false;
					}
				}
				this.loaded = false;
				this.checkBannerExecuted.checkCnt = 0;
				this.iframe.src = this.owner.ifrmsrc;
				//window.setTimeout(this.checkBannerExecuted,25);			
			break;
		}	
	}catch(e){
	}
}
pt.tellTimeCap = function(){};
pt.checkBannerExecuted = function(){
	var ce = arguments.callee;
	if(ce.checkCnt>10 && !ce.owner.loaded){
		ce.checkCnt = 0;
		var idoc = ce.owner.owner.getIfrmDoc(ce.owner.iframe);
		idoc.location.reload();
	}else if(!ce.owner.loaded){
		ce.checkCnt++;
	}else{
		ce.checkCnt = 0;
		return;
	}
	window.setTimeout(ce,25);
}
pt.clear = function(){
	try{
		if(typeof(this.iframe) != 'undefined' && this.iframe != null && typeof(this.iframe.parentNode) != 'undefined' && this.iframe.parentNode != null)
			this.iframe.parentNode.removeChild(this.iframe);
		}catch(e)	{}
	this.iframe = null;		
}
pt.getIfrmHTML = function(_window){
	html = '<html><head><scr'+'ipt language="javascript">';
	html += '</scr'+'ipt></head><body style="background-color:transparent;">';
	
	return html;
}
pt.reload = function(){
	this.clear();
}
pt.ifrmOnLoadFunc = function(){
	var images = this.document.getElementsByTagName('A'),href;
	for(i=0;i< images.length;i++){
		href = images[i].href.toLowerCase();
		if(href.indexOf('fffffffcfffffffcfffffffc')>0 || href.indexOf('fffffffefffffffefffffffe')>0){
			this.banner.clear();
			return false;
		}
	}
	
	var b=this.document.getElementsByTagName("body")[0];
	//alert(this.banner.bcallid+'  '+parseInt(b.scrollHeight-this.banner.iframe.height)+'  '+parseInt(b.scrollWidth-this.banner.iframe.width));
	if(isS(this.banner.iframe)){
		if(b.scrollWidth > 0 && b.scrollHeight > 0 && ((parseInt(b.scrollHeight-this.banner.iframe.height) < -8 || parseInt(b.scrollHeight-this.banner.iframe.height) > 8) && (parseInt(b.scrollWidth-this.banner.iframe.width) < -8 || parseInt(b.scrollWidth-this.banner.iframe.width) > 8))){
			this.banner.iframe.height = b.scrollHeight;
			this.banner.iframe.width = b.scrollWidth;
		}
	}
}
pt.getIMPCNT = function(){
	return this.owner.getIMPCNT(this.owner.getAAMBCont(this.bcallid));
}

//video banner
function wbds_VideoBanner(owner,size,position,euri,id,type,bcallid,bannerid,properties){
	if(owner == null || typeof(owner) == 'undefined') return;
	this.d = document;
	this.owner = owner;
	this.callinf = {size:size,position:position,euri:euri,calltype:type};
	
	this.bcallid = bcallid;
	this.bannerid = bannerid;
	this.defaultWMVClassId = '6BF52A52-394A-11d3-B153-00C04F79FAA6';
	
	this.waitForObject.owner = this;
	
	this.twin_spot = null;
	this.playTwinAfter = 1;
	
	//this.duration = 60000;
	this.duration = 15500;
	
	if(!isS(this.swfplayer))
		this.swfplayer = null;
	this.player = null;
	if(this.playertype == null || typeof(this.playertype) == 'undefined')
		this.playertype = '';
	if(this.fplayertype == null || typeof(this.fplayertype) == 'undefined')
		this.fplayertype = '';
	this.flashVarsReceivable = true;
	this.playnexturl = '';
	this.playbaseasxurl = '';
	this.playbaseasxvar = '';
	this.asxcountable = false;
	this.hasfixedasx = false;
	this.swfcountable = true;
	this.videosize = {w:320,h:240};
	this.videoparams = null;
	this.videoattributes = null;
	this.videodata = null;
	this.videoid = null;
	this.placePlayerDiv = true;
	this.skiphtmlurlget = false;
	
	if(!isS(this.directInitPlayer))
		this.directInitPlayer = false;	
	this.adsize = {w:336,h:280};
	
	this.iframe = null;
	this.clips = null;
	if(!isS(this.clipTypes))
		this.clipTypes = ['VIDEO','VIDEO_LAST'];
	this.clipIndex = -1;
	this.clickarea = null;
	
	this.watchVideoObj = false;
	
	//workaround props
	this.initialized = false;
	this._default = false;
	
	if(this.reportEmpty == null || typeof(this.reportEmpty) == 'undefined')
		this.reportEmpty = false;
	
	if(!isS(this.videoparams)) this.videoparams = {};
	if(!isS(this.videoattributes)) this.videoattributes = {};
	if(!isS(this.videovars)) this.videovars = {};
	if(!isS(this.videoprops)) this.videoprops = {};
	
	var i;
	for(i in properties){
		this[i] = properties[i];
	}	
	
	var t = typeof(id);
	if(t == 'object' || t == 'function'){
		this.element = id;
		if(isS(this.element.id) && this.element.id != '' && this.element.id.length > 0){
			this.elementid = this.element.id;
		}else{
			this.element.id = this.callinf.size+'__'+this.owner.getRnd();
			this.elementid = this.element.id;
		}
		try{
			//this.elementid = this.element.getAttribute('id');
		}catch(e){
		}
	}else{ 
		this.elementid = id;
		this.element = this.d.getElementById(id);
	}
	if(!isS(this.HTMLPlayerId)){
		this.HTMLPlayerId = this.elementid;
	}else{
		this.element.parentNode.removeChild(this.element);	
		this.elementid = this.HTMLPlayerId;
		this.element = this.d.getElementById(this.HTMLPlayerId);
	}
	
	if(this.placePlayerDiv){
		this.playerdiv = this.getPlayerDiv();
		this.playerdiv.id = 'wbds_player_div_'+bannerid;	
	}else{
		this.playerdiv = this.element;		
	}
	
	/*if(this.player == null || this.fplayertype == 'msmedia'){
		if(typeof(HTMLWMediaPlayer) != 'function'){
			//wbds_Include(this.owner.protocol+this.owner.imaged+'/js/Class/HTMLWMediaPlayer.js');
		}
	}
	if(this.fplayertype == 'flash'){
			if(typeof(HTMLFlashVideoPlayer) != 'function'){
				//wbds_Include(this.owner.protocol+this.owner.imaged+'/js/Class/HTMLFlashVideoPlayer.js');
			}
	}*/
	wbds_BroadCaster.prototype.initialize(this);
	
	
	if(!isS(this.varname))
		this.varname = '__wbds_banner'+this.elementid;
	this.setVarName(this.varname);
	
	this.addListener(this);
	
	if(this.directInitPlayer){
		this.initPlayer();
	}
}
pt = wbds_VideoBanner.prototype;
pt.setVarName = function(varname){
	this.varname = varname;
	eval(this.varname + ' = this');
	window[this.varname] = this;
}
pt.initialize = function(){
	if(this._setDuration.timid > 0){
		clearTimeout(this._setDuration.timid);
		this._setDuration.timid = -1;
	}
	switch(this.callinf.calltype){
		case 'bservers':
			if(this.owner.isDefault(this.owner.getAAMBCont(this.bcallid))){
				if(isS(this.player)){					
					if(this.reportEmpty || this.playertype == 'flash'){
						this.player.setCnt('DEFAULT;');
					}					
				}
				this.onFinishAd();
				this._default = true;
				return;
			}
			this._default = false;
			if(isS(this.player)){
				this.player.reset();
			}
			this.clearRemoveableListeners();
			this.handleValue(this.owner.getAAMBCont(this.bcallid));			
		break;
		default:
			this.setIframe();
		break;
	}
	this.initialized = true;	
}
pt.tellTimeCap = function(){
	if(this.player != null){
		this.player.setSrc('');
		this.player.setCnt('');
		this.player.setClick('');
	}	
};
pt.getPlayerDiv = function(){
	return this.element.appendChild(this.d.createElement('DIV'));
}
pt.getClipType = function(type){
	for(var i=0;i<this.clipTypes.length;i++){
			if(this.clipTypes[i] == type){
			return i;
		}
	}
	return -1;
}
pt.handleValue = function(aamb){	
	var data,i;
	this.videodata = null;
	var videodata = this.handleAll(aamb);
	this.videodata = videodata;
	this.element.width = this.videosize.w;
	this.element.height = this.videosize.h;	
	this.initClips();		
	if(this.clips.length>0){
		this.clipIndex = 0;
		this.playertype = this.initPlayerType(this.clips[this.clipIndex]);		
		this.initPlayer();
		this.initClip();
	}
}
pt.addAsxClip = function(id,data){
	var url = this.playbaseasxvar+id +"="+data.wmvurl;
	if(this.asxcountable){
		for(var i=0;i<data.counts.length;i++){
			url += "&counts" +id+"[]=" + escape(data.counts[i]);
		}		
	}	
	return url;
}
pt.getAsxURL = function(baseurl,sindex){
	var url,uri = '',skip=0;
	var tmp_indx = baseurl.lastIndexOf('?');
	if(tmp_indx < 0) baseurl += '?';//|| tmp_indx < this.playbaseasxurl.length
	else baseurl += '&';		
	
	if(arguments.length == 1 || !isS(sindex))
		sindex = this.clipIndex;
	for(i=sindex;i<this.clips.length;i++){
		if(this.clips[i].player == 'script') {
			skip++;
			continue;
		}
		uri += ((i-(skip+sindex)>0)?'&':'')+this.addAsxClip(i+1-skip,this.clips[i]);
	}
						
	return baseurl+uri;
}
pt.setFlashData = function(){
	
}
pt.setIframe = function(bannerid){
	var banner;
	if(arguments.length == 1) banner = this.owner.banners[bannerid];
	else banner = this;
	this.iframeobj = new wbds_IframeBanner(this.owner,banner.callinf.size,banner.callinf.position,banner.callinf.euri,this.elementid,banner.callinf.calltype,banner.bcallid,banner.bannerid);
	this.getIfrmHTML = this.iframeobj.getIfrmHTML;
	banner.ifrmOnLoadFunc = this.ifrmOnLoadFunc = this.iframeobj.ifrmOnLoadFunc;
	banner.iframe = this.iframe = this.iframeobj.iframe;	
	this.iframe.width = this.adsize.w;
	this.iframe.height = this.adsize.h;
	this.iframeobj.initialize();
	this.iframe = this.iframeobj.iframe;
}
pt.waitForObject = function(obj){
	if(typeof(eval(obj)) == "object"){
		var ce = arguments.callee;
		ce.owner.ObjectLoaded();
		window.clearTimeout(ce.intit);
	}
}
pt.initPlayer = function(){	
	this.clearIframe();
	switch(this.playertype){
		case 'iframe':						
			if(isS(this.player)){
				this.player.hide();
			}
			this.setIframe();
		break;
		case 'flash':
			this.initFLP();
		break;
		case 'html':
			if(!isS(this.HTMLPlayerId)){					
				this.playertype = '';
				arguments.callee.call(this);
				return;
			}else{								
				this.initHTML();
			}
		break;
		case 'msmedia':
		default:
			this.playertype = 'msmedia';
			this.initWMP();
		break;
	}	
}
pt.initClip = function(){
	var tmppt_ = this.playertype;
	if(this.fplayertype == 'html' && !isS(this.player)){
		var tmppt = this.playertype;
		this.playertype = '';
		this.initHTML();		
		this.player.hide();
		tmppt_ = this.playertype;
		this.playertype = tmppt;
	}	
	
	var clip = this.clips[this.clipIndex],setduration = true,setsoftdur = false;
	var twinclip = this.getNextClip({cnted:false,type:1},0);
	var url,list1 = {
						_removeableListner:true,
						owner:this,						
						onEndAd:function(){
								if(this.owner.player.hasClick){	
									this.owner.player.removeClick();																	
								}
								this.owner.player.unRegisterEvent('playStateChange(ns)');
								this.owner.player.playCount = 0;
								this.owner.removeListener(this);
								if(!this.owner.hasfixedasx || twinclip.player == 'script'){
									this.skiphtmlurlget = false;
									this.owner.broadcastMessage('onFinishAd');
								}else{
									this.owner.player.registerEvent('CurrentItemChange','if(arg.sourceURL == "'+twinclip.wmvurl+'"){this.owner.owner.broadcastMessage("onActivateTwin");;this.owner.unRegisterEvent("CurrentItemChange");}');
								}
							}
						};
						
	var setduration = true;
	switch(this.playertype){
		case 'iframe':
			if(this.playbaseasxvar != '' && this.playbaseasxurl != '' && this.playnexturl == ''){					
				this.hasfixedasx = true;
			}
		break;
		case 'flash':
			url = clip.flvurl;
			if(twinclip){
				if(twinclip.player == 'script'){
					this.player.setTwinCnts('DEFAULT;');
				}else{
					this.player.setTwin(twinclip.flvurl);
					if(isS(twinclip.counts) && twinclip.counts instanceof Array) this.player.setTwinCnts(twinclip.counts.join('|^|'));
					if(this.owner.isClick(twinclip.click))
						this.player.setTwinClick(twinclip.click);
				}
			}
			if(!this.swfcountable && clip.counts != null && clip.counts.length>0){
				this.doCounts(clip);
			}else{
				if(clip.counts != null && clip.counts.length>0){
					this.player.setCnt(clip.counts.join('|^|'));
				}
			}
			setduration = false;
		break;
		case 'msmedia':
		default:
				if(this.player.hidden)
					this.player.unhide();
				if(this.playbaseasxvar != '' && this.playbaseasxurl != '' && this.playnexturl == ''){					
					this.hasfixedasx = true;
					url = this.getAsxURL(this.playbaseasxurl);				
					setduration = false;
					setsoftdur = true;						
				}else{
					url = clip.wmvurl;
				}
				if(!this.asxcountable && clip.counts != null && clip.counts.length>0){
					this.doCounts(clip);
				}
				this.skiphtmlurlget = true;
					
				if(this.owner.browser.hasActiveX){
					this.player.registerEvent('playStateChange(ns)','if(this.owner.isAd && ns == 8){this.owner.isAd = false;this.owner.owner.broadcastMessage(\'onEndAd\');};');
					this.addListener(list1);
				}
		break;
	}
	
	if(this.playertype != 'iframe'){
		this.player.isAd = true;
		this.player.setSrc(url);
		if(this.owner.isClick(clip.click))
			this.player.setClick(clip.click);			
	}
	
	if(this.watchVideoObj && tmppt_ == 'msmedia'){
		if(!this.player.isListener(this))
			this.player.addListener(this);
		this.player.watchSrc();			
	}
	
	
	if(this.playertype == 'flash' && !this.flashVarsReceivable)
		this.setFLP();
	
	if(this._setDuration.timid > 0){
		clearTimeout(this._setDuration.timid);
		this._setDuration.timid = -1;
	}
	try{
		var duration = this.duration;
		if(isS(clip.duration) && clip.duration > 0)
			duration = clip.duration;
		if(!this.owner.browser.hasActiveX && this.playertype == 'msmedia')
			duration += 500;		
		if(setduration && (!this.owner.browser.hasActiveX || this.playertype == 'iframe'))
			this.setDuration(duration);
		else if(setsoftdur && !this.owner.browser.hasActiveX)
			this.setDuration(duration,true);
	}catch(e){
		if(setduration)
			this.setDuration(duration);
		else if(setsoftdur)
			this.setDuration(duration,true);
	}
}
pt.initHTML = function(){	
	var cont = this.d.getElementById(this.HTMLPlayerId);
	if(cont != null){
		var a = cont.getElementsByTagName('OBJECT'),b = cont.getElementsByTagName('EMBED');
		var args,i;
		if(a.length>0 && (this.owner.browser.hasActiveX || b.length==0)){			
			if(typeof(a[0].classid) != 'undefined'){
				switch(a[0].classid.substr(6).toUpperCase()){
					case '6BF52A52-394A-11D3-B153-00C04F79FAA6':
					case '22D6F312-B0F6-11D0-94AB-0080C74C7E95':
						this.playertype = 'msmedia';
						this.getAndSetHTMLWMVObj(a[0]);
						args = {
							blockCInner:true,
							cInnerElement:cont,
							element:a[0],
							owner:this
						};							
					break;
					case 'D27CDB6E-AE6D-11CF-96B8-444553540000':
						this.playertype = 'flash';
						this.getAndSetHTMLFlashObj(a[0]);
						args = {
							blockCInner:true,
							cInnerElement:cont,
							element:a[0],
							owner:this
						};
					break;
				}
			}
			if(this.playertype == '' || this.playertype.toLowerCase() == 'html'){
				switch(a[0].type){
					case 'video/x-ms-wmv':
					case 'video/x-msvideo':
					case 'video/x-ms-asf':
					case 'video/x-ms-asf':
					case 'application/x-mplayer2':																	
						this.playertype = 'msmedia';     				
      			this.getAndSetHTMLWMVObj(a[0]);			
      			a[0].parentNode.removeChild(a[0]);
					break;
				}
			}						
		}else if(b.length>0){
				switch(b[0].type.toLowerCase()){
					case 'video/x-ms-wmv':
					case 'video/x-msvideo':
					case 'video/x-ms-asf':
					case 'video/x-ms-asf':
					case 'application/x-mplayer2':
							this.playertype = 'msmedia';
							this.getAndSetHTMLWMVObj(b[0]);
							b[0].parentNode.removeChild(b[0]);
							if(a.length>0){
								a[0].parentNode.removeChild(a[0]);
							}
						break;
						case 'application/x-shockwave-flash':							
							this.playertype = 'flash';
							this.getAndSetHTMLFlashObj(b[0]);
								args = {
									blockCInner:true,
									cInnerElement:cont,
									element:b[0],
									owner:this
								};
						break;
					}
		}else{
			//should never happen!
			this.playertype = '';
			this.initPlayer();
		}
		
		switch(this.playertype){
			case 'msmedia':						
				if(this.hasfixedasx){
					if(isS(args)) args._lastSrc = this.playbaseasxurl;
					this.videoattributes.url = this.playbaseasxurl;
				}else	if(isS(args))
					args._lastSrc = this.playnexturl;
				this.initWMP(args);				
			break;
			case 'flash':
					this.initFLP(args);
				break;		
		}
	}else{		
		this.playertype = '';
		this.initPlayer();
	}
}
pt.getAndSetBaseHTMLObj = function(obj){
	if(isS(obj.id) && obj.id != "")
  	this.videoid = obj.id;
	this.videosize = {w:obj.width,h:obj.height};	
}
pt.getAndSetHTMLFlashObj = function(obj){
	this.getAndSetBaseHTMLObj(obj);
	if(isS(obj.movie)){
		this.swfplayer = obj.movie;
	}else{
		this.swfplayer = obj.src;
	}
			
}
pt.getAndSetHTMLWMVObj = function(obj){
	this.getAndSetBaseHTMLObj(obj);
	params = obj.getElementsByTagName('param');	
  
  for(i=0;i<obj.attributes.length;i++){
		if(obj.attributes[i].nodeValue != null && obj.attributes[i].nodeValue != ''){
			this.videoattributes[obj.attributes[i].nodeName.toLowerCase()] = obj.attributes[i].nodeValue;
		}
	}
						
	if(this.owner.browser.hasActiveX){
		this.videoattributes.classid = 'CLSID:'+this.defaultWMVClassId;
		this.videoattributes.type = "application/x-oleobject";
	}
	
	if(!this.skiphtmlurlget){
		if(this.hasfixedasx){	
			if(isS(obj.URL) && obj.URL.length>0){
	      this.playbaseasxurl = obj.URL;
  	  }else if(isS(obj.FileName) && obj.FileName.length>0){
    		this.playbaseasxurl = obj.FileName;
    	}else if(isS(obj.data) && obj.data.length>0){
    		this.playbaseasxurl = obj.data;
    	}else if(isS(obj.src) && obj.src.length>0){
    		this.playbaseasxurl = obj.src;
    	}
		}else{ 
  		if(isS(obj.URL) && obj.URL.length>0){
    		this.playnexturl = obj.URL;
    	}else if(isS(obj.FileName) && obj.FileName.length>0){
	    	this.playnexturl = obj.FileName;
  	  }else if(isS(obj.data) && obj.data.length>0){
    		this.playnexturl = obj.data;
    	}else if(isS(obj.src) && obj.src.length>0){
    		this.playnexturl = obj.src;
    	}
  	} 
  }
  	
  	
  						
	for(i=0;i<params.length;i++){
		switch(params[i].name.toLowerCase()){
			case 'src':
				this.videoparams['url'] = params[i].value;
			default:
				this.videoparams[params[i].name] = params[i].value;
			break;
		}
	}		
}
pt.initWMP = function(args){
	if(this.player != null) return;
	var i;
	if(!isS(args))	
		args = {d:this.d,owner:this,params:this.videoparams,attributes:this.videoattributes};
	else{			
		if(!isS(args.d)) args.d = this.d;
		if(!isS(args.owner)) args.owner = this;
		if(isS(args.params)){
			for(i in this.videoparams){				
				if(!isS(args.params[i])){
					args.params[i] = this.videoparams[i];
				}
			}
		}else{
			args.params = this.videoparams;	
		}		
		if(isS(args.attributes)){
			for(i in this.videoattributes){
				if(!isS(args.attributes[i])){
					args.attributes[i] = this.videoattributes[i];
				}
			}
		}else{
			args.attributes = this.videoattributes;
		}
	}
	
	if(isS(this.videoid) && !isS(args.id)){
		args.id = this.videoid;
	}

	this.player = new HTMLWMediaPlayer(args);	
	if(isS(this.player.element)){
		this.element.owner = this.player;
	}
	if(this.videosize != null){
		this.player.width = this.videosize.w;
		this.player.height = this.videosize.h;
	}	
	
	this.player.owner = this;
	if(this.player.browser.hasActiveX){
		this.player.cInner(this.playerdiv);
	}else{
		this.player.cInnerElement = this.playerdiv;
		this.playerdiv.id = this.player.id;
	}
	this.owner.addListener(this.player);
}
pt.initFLP = function(args){
	if(this.player != null) return;
	if(!isS(args))
		args = {swf:this.swfplayer,params:this.videoparams,width:this.videosize.w,height:this.videosize.h};	
	else{
		if(!isS(args.d)) args.d = this.d;
		if(!isS(args.owner)) args.owner = this;
		for(i in this.videoparams){
			if(!isS(args.params[i])){
				args.params[i] = this.videoparams[i];
			}
		}
		for(i in this.videoattributes){
			if(!isS(args.attributes[i])){
				args.attributes[i] = this.videoattributes[i];
			}
		}
	}
	this.player = new HTMLFlashVideoPlayer(args);
	this.player.owner = this;
	if(this.flashVarsReceivable){
		this.setFLP();
		if(!this.owner.browser.is_ie){
			var func = function(_this){_this.player.setSaveVar('wbds_banner_id',_this.bannerid);_this.player.setSaveVar('wbds_banner_var',_this.varname);}
			setTimeout(func,20,this);
		}else{		
			this.player.setSaveVar('wbds_banner_id',this.bannerid);
			this.player.setSaveVar('wbds_banner_var',this.varname);
		}
	}
	this.owner.addListener(this.player);
}
pt.setFLP = function(){
	this.player.cInner(this.playerdiv);
}
pt.initClips = function(){
	this.clips = [];
	this.clips[0] = this.videodata;	
	this.clips[0].type = this.getClipType(this.callinf.size);
	this.clips[0].cnted = false;
	this.clips[0].bannerid = this.bannerid;
	var i;
	if(this.twin_spot!=null && (typeof(this.twin_spot) == 'array' || typeof(this.twin_spot) == 'object')){
		var aamb2,twin_indexs,j=0;
		for(i=0;i< this.twin_spot.length;i++){							
			aamb2 = this.owner.getAAMBCont(this.twin_spot[i]);
			if(this.owner.isDefault(aamb2)) continue;
			this.clips[j+1] = this.handleAll(aamb2);
			this.clips[j+1].type = this.getClipType(this.owner.getQBannerFromId(this.twin_spot[i])[0]);			 	
			this.clips[j+1].cnted = false;
			this.clips[j+1].bannerid = this.twin_spot[i];
			j++;
		}			
	}
}
pt.initPlayerType = function(clip){
	var ptype = "";
	switch(clip.player){	
		case "script":
			ptype = 'iframe';
		break;
		default:
			switch(this.fplayertype){
				case 'html':
					ptype = 'html';
				break;
				case 'flash':
					ptype = 'flash';
				break;
				case 'msmedia':
				default:
					ptype = 'msmedia';
				break;
			}			
		break;	
	}
	return ptype;
}
pt.getClip = function(id){
	if(isS(this.clips[id]))
		return this.clips[id];
	return false;
}
pt.getNextClip = function(search,index){
	var req,mat,clip;
	if(arguments.length == 1 || !isS(index))
		index = this.clipIndex+1;
	if(isS(this.clips)){
		for(var i=index;i<this.clips.length;i++){
			if(arguments.length == 0)
				return this.getClip(i);		
			clip = this.getClip(i);
			if(!clip) continue;
			req = 0;
			mat = 0;
			for(var j in search){
				if(clip[j] == search[j]){
					mat++;
				}
				req++;
			}
			if(req == mat) return clip;	
		}
	}
	return false;	
}
pt.requestPlay = function(url){
	this.skiphtmlurlget = true;	
	if(!this.hasfixedasx)
		this.playnexturl = url;
	else
		this.playbaseasxurl = url;
	setTimeout('window.'+this.owner.globalvar+'.reload()',5);
}
pt.setDuration = function(time,soft,evt){
	if(soft == null) soft = false;	
	if(time == null || typeof(time) == 'undefined'){
		time = this.duration;
	}	
	this._setDuration.owner = this;
	if(arguments.length <= 2 || !isS(evt)){
		if(soft){
			evt = 'onEndAd';
		}else{
			evt = 'onFinishAd';
		}
	}	
	if(this._setDuration.timid > 0){
		clearTimeout(this._setDuration.timid);
		this._setDuration.timid = -1;
	}
	if(time < 0) return;
	this._setDuration.evt = evt;
	this._setDuration.timid = setTimeout(this._setDuration,time);
}
pt._setDuration = function(){
	var ce = arguments.callee,t = ce.owner,evt = ce.evt;
	t.broadcastMessage(evt);
	ce.timid = -1;
}
pt.onFinishAd = function(){
	//this.broadcastMessage('onEndAd');
	this.clearIframe();
	var prevpltype = this.playertype;
	if(this.playertype == '' || this.playertype == 'iframe'){
		this.playertype = this.fplayertype;
	}
	
	if(!isS(this.player))
		this.initPlayer();
	if(this.player.hidden){
		this.player.unhide();
	}


	if(this.playertype == 'msmedia' || this.playertype==''){
		var clip = this.getNextClip({cnted:false,type:1});
		if(!this.hasfixedasx || !clip){
			if(this.playnexturl != ''){
				
				this.player.setSrc(this.playnexturl);
			}else if(this.playbaseasxurl != ''){
				this.player.setSrc(this.playbaseasxurl);
			}
		}else{
			if(clip.player != 'script'){
				this.player.setSrc(this.getAsxURL(this.playbaseasxurl,this.clipIndex+1));
				if(this.owner.browser.hasActiveX){
					this.player.registerEvent('CurrentItemChange','if(arg.sourceURL == "'+clip.wmvurl+'"){this.owner.owner.broadcastMessage("onActivateTwin");this.owner.unRegisterEvent("CurrentItemChange");}');			
				}
			}else{
				if(prevpltype == 'iframe'){
					if(this.playnexturl != ''){
						this.player.setSrc(this.playnexturl);
					}else if(this.playbaseasxurl != ''){
						this.player.setSrc(this.playbaseasxurl);
					}
				}
				this.player.registerEvent('playStateChange(ns2)','if((ns2 == 8 || ((typeof(this.owner._lastStatus) != "undefined" && this.owner._lastStatus != 3 && this.owner._lastStatus != 0) && ns2 == 10)) && !this.owner.isAd) {this.owner.playCount++;if(this.owner.playCount == this.owner.owner.playTwinAfter){this.owner.unRegisterEvent("playStateChange(ns2)");this.owner.owner.broadcastMessage("onActivateTwin")}};this.owner._lastStatus=ns2;');
			}
		}
	}
	if(this.playertype == 'flash'){
		if(!this.flashVarsReceivable)
			this.setFLP();
		else{			
			if(prevpltype == 'iframe'){
				this.player.setCnt('DEFAULT;');
				var clip = this.getNextClip({cnted:false,type:1});
				if(clip.player != 'script'){
					if(clip){
						this.player.setTwin(clip.flvurl);
						if(isS(clip.counts) && clip.counts instanceof Array) this.player.setTwinCnts(clip.counts.join('|^|'));
						if(this.owner.isClick(clip.click)) this.player.setTwinClick(clip.click);
					}else{
						this.player.setTwinCnts('DEFAULT;');
					}
				}else{
					this.player.setTwinCnts('DEFAULT;');
				}
				this.player.triggerEvent('onFinishAd');
			}
			if(this.owner.isDefault(this.owner.getAAMBCont(this.bcallid))){
				this.player.setCnt('DEFAULT;');
			}
		}
	}
	
	
	if(this.playertype == 'msmedia' && this.owner.browser.hasActiveX && !this.hasfixedasx && this.player.attributes.src != ''){
		this.player.registerEvent('playStateChange(ns2)','if((ns2 == 8 || ((typeof(this.owner._lastStatus) != "undefined" && this.owner._lastStatus != 3 && this.owner._lastStatus != 0) && ns2 == 10)) && !this.owner.isAd) {this.owner.playCount++;if(this.owner.playCount == this.owner.owner.playTwinAfter){this.owner.unRegisterEvent("playStateChange(ns2)");this.owner.owner.broadcastMessage("onActivateTwin")}};this.owner._lastStatus=ns2;');
	}


	
	if(this.playnexturl != null && this.playnexturl!=''){
		this.initialized = true;
	}
	
	if(this._setDuration.timid>0){
		clearTimeout(this._setDuration.timid);
		this._setDuration.timid = -1;
	}
	
}
pt.onActivateTwin = function(){
	var clip = this.getNextClip({cnted:false,type:1});
	if(!clip) return;	

	switch(clip.player){
		case 'script':
			if(isS(this.player)) this.player.hide();
				this.setIframe(clip.bannerid);
		break;
		default:
			switch(this.playertype){
				case '':
				case 'msmedia':
					this.skiphtmlurlget = true;
					this.initPlayer();
					if(!this.hasfixedasx){						
						this.player.setSrc(clip.wmvurl);
					}

					if(!this.asxcountable && clip.counts != null && clip.counts.length>0){
						this.doCounts(clip);
					}

					this.player.setClick(clip.click);
				break;
			}
		break;
	}
}
pt.onSourceChange = function(player){
	this.requestPlay(player._lastSrc);	
}
pt.reload = function(){	
	this.clearIframe();
}
pt.clear = function(){
	this.clearIframe();
	if(this.player != null && (typeof(this.player) == 'object' || typeof(this.player) == 'function')){
		this.player.clear();
		delete this.player;
	}
}
pt.clearIframe = function(){
	if(typeof(this.iframeobj) == 'object'){
		this.iframeobj.clear();
		if(this.iframeobj.bannerid != this.bannerid){
			var ban = this.owner.banners[this.iframeobj.bannerid];
			if(isS(ban))
				ban.iframe = null;
		}
		this.iframe = null;
	}
}
pt.adClickLayer = function(){
	//alert(this.player.width);
	/*this.clickarea = document.createElement('DIV')
	this.clickarea.style.position = 'absolute';
	this.clickarea.style.zIndex = 2;
	this.clickarea.style.border = '1px solid red';
	this.clickarea.style.backgroundColor = '#FFFFFF';
	this.clickarea.style.width = this.player.width+'px';
	this.clickarea.style.height = this.player.height+'px;';
		this.clickarea.onclick = function(){
		alert('test');
	}
	this.element.insertBefore(this.clickarea,this.playerid);*/
}
pt.getIMPCNT = function(){
	if((this.playertype == 'msmedia' || this.playertype == 'flash') && this.videodata != null){
		return this.videodata.vericount;
	}else{
		return this.owner.getIMPCNT(this.owner.getAAMBCont(this.bcallid));
	}
}
pt.doCounts = function(videodata){
	for(i=0;i<videodata.counts.length;i++){
		this.owner.doIMPCNT(videodata.counts[i]);
	}
}
pt.handleAll = function(aamb,indexs){
	var ret = {};
	if(arguments.length == 1){
		indexs = [aamb.indexOf('mms://'),aamb.indexOf('http://'),aamb.indexOf('https://'),aamb.indexOf('rtmp://'),aamb.indexOf('<'),aamb.indexOf('IMPCNT'),aamb.indexOf("<clips"),aamb.indexOf(".wmv"),aamb.indexOf(".flv")];
	}	
	if((indexs[4] < 19 && indexs[0] < 0 && indexs[3] < 0 && (aamb.indexOf("swf")>=0 ||  aamb.indexOf("<script")>=0 || aamb.indexOf("document.write")>=0)) && indexs[6] <0){
		ret.scriptsrc = aamb;
		ret.player = "script";
	}else if((indexs[0] >= 0 || indexs[1] >= 0 || indexs[2] >= 0 || indexs[3] >= 0 || indexs[6] >=0 || indexs[7] >=0) && (indexs[8] <0 || (indexs[8] >=0 && indexs[6]>=0)) ){
		ret = this.handleVideo(aamb,indexs);
		ret.player = "";		
	}
	return ret;
}
pt.handleVideo = function(str,indexs){
	var ret = {};
	if(str.indexOf("<clips")>= 0){
		var addom = new wbds_XMLBanner(this.owner);
		addom.initDom(str.replace(/\\\"/,'\"'));
		if(addom == null){
			
		}else{
			if(addom.dom.firstChild != null && addom.dom.firstChild.childNodes.length>0){
				var clip = null
				while(clip == null || clip.nodeName.toLowerCase() != 'clip'){
					clip = addom.dom.firstChild.childNodes[Math.floor(Math.random()*(addom.dom.firstChild.childNodes.length-1))];
				}
			}
			ret.vericount = clip.getAttribute('vericount');
			ret.counts = [ret.vericount];
			ret.flvurl = clip.getAttribute('flvsource');
			ret.wmvurl = clip.getAttribute('wmvsource');
			ret.click = clip.getAttribute('clickurl');
			
			if(clip.getAttribute('duration') != null && typeof(clip.getAttribute('duration')) != 'undefined')
				ret.duration = clip.getAttribute('duration');
			else
				ret.duration = null;
				
			for(var i=0;i<clip.attributes.length;i++){
				if(clip.attributes.item(i).nodeName.indexOf("count")>=0 && clip.attributes.item(i).nodeName.toLowerCase() != 'vericount'){
					ret.counts.push(clip.attributes.item(i).nodeValue);
				}
			}
		}		
	}else{
		var urls = str.split('<');
		if(indexs[5] >= 0){
			data = urls[0].split('|^|');
			url = data[0];
		}else{
			data = urls[0].split('|^|');
			url = data[0];
		}
		ext = wbds_getExtension(url);
		ret.counts = [this.getIMPCNT(str)];
		ret.wmvurl = url;
	}
	return ret;
}

function wbds_XMLBanner(owner,size,position,euri,id,type,bcallid,bannerid,properties){
	this.d = document;
	this.owner = owner;
	this.callinf = {size:size,position:position,euri:euri,calltype:type};
	var t = typeof(id);
	if(t == 'object' || t == 'function'){
		this.element = id;
		this.elementid = this.element.id;
	}else{ 
		this.elementid = id;
		this.element = this.d.getElementById(id);
	}

	this.bcallid = bcallid;
	this.bannerid = bannerid;	
	
	if(this.owner.browser.is_ie){
		var _msxmlver = ["MSXML4.DOMDocument","MSXML3.DOMDocument","MSXML2.DOMDocument","MSXML.DOMDocument","Microsoft.XmlDom"];
		var obj;
		for(var i=0;i<_msxmlver.length;i++){
			try{
				obj = new ActiveXObject(_msxmlver[i]);
				this.msxmlver = _msxmlver[i];
				delete obj;
				break;				
			}catch(err){
			}
		}
	}
	this.dom = null;
}
pt = wbds_XMLBanner.prototype;
pt.initialize = function(){
	switch(this.callinf.calltype){
		case 'bservers':
			if(this.owner.isDefault(this.owner.getAAMBCont(this.bcallid))){
				this._default = true;
				return;
			}
			this._default = false;
			this.handleValue(this.owner.getAAMBCont(this.bcallid));			
		break;
	}
}
pt.handleValue = function(aamb){
	this.initDom(aamb);
}
pt.initDom = function(str){
	if(this.dom == null){
		if(this.owner.browser.is_ie){
			this.dom = new ActiveXObject(this.msxmlver);
			this.dom.loadXML(str);
		}else if(document.implementation && document.implementation.createDocument){
			var parser = new DOMParser();
			this.dom = parser.parseFromString(str,'text/xml');
		}
	}
}
pt.clear = function(){
	this.dom = null;
}

//other classes
function wbds_GetBrowser(){
	var agt=navigator.userAgent.toLowerCase();
	this.is_ns = (agt.indexOf("; nav") != -1);
	this.is_opera = (agt.indexOf("opera") != -1);
	this.is_konq = (agt.indexOf("konqueror") != -1);
	this.is_ie = (!this.is_opera && !this.is_konq && agt.indexOf("msie") != -1)?true:false;
 	this.is_moz = (!this.is_ns && !this.is_opera && !this.is_knoq && !this.is_ie && typeof(document.getElementById) == "function")?true:false;
 	this.is_other = (!this.is_ns && !this.is_opera && !this.is_knoq && !this.is_ie && !this.is_moz)?true:false;
 	this.is_mac = (agt.indexOf("mac")!=-1);
 	this.is_win = ((agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1));
 	
 	var wmpObj;
 	try{
		if(window.ActiveXObject){
			wmpObj = new ActiveXObject("WMPlayer.OCX.7");
		}else if (window.GeckoActiveXObject){
			wmpObj = new GeckoActiveXObject("WMPlayer.OCX.7");
		}
	}catch(e){
		//
	}
	if(wmpObj){
		this.hasActiveX = true;
	}else{
		this.hasActiveX = false;
	}
};

function wbds_BroadCaster(){
}
var pt = wbds_BroadCaster.prototype;
pt.initialize = function(obj){
	obj._listeners = new Array();
	obj.addListener = wbds_BroadCaster.prototype.addListener;
	obj.removeListener = wbds_BroadCaster.prototype.removeListener;
	obj.broadcastMessage = wbds_BroadCaster.prototype.broadcastMessage;
	obj.isListener = wbds_BroadCaster.prototype.isListener;
	obj.clearRemoveableListeners = wbds_BroadCaster.prototype.clearRemoveable;
}
pt.addListener = function(obj){
	var found=false,length=this._listeners.length,i;
	for(i=0;i<length;i++){
		if(obj == this._listeners[i]){
			found = true;
			break;
		}
	}
	if(!found){
		this._listeners[length] = obj;
	}
}
pt.removeListener = function(obj){
	var length=this._listeners.length,i;
	for(i=0;i<length;i++){
		if(obj == this._listeners[i]){
			var obj2 = this._listeners[i+1];
			this._listeners.splice(i,1);
			break;
		}
	}
}
pt.isListener = function(obj){
	var length=this._listeners.length,i;
	for(i=0;i<length;i++){
		if(obj == this._listeners[i]){
			return true;
		}
	}
	return false;
}
pt.broadcastMessage = function(){
	var length=this._listeners.length,i;
	var args = [];
	for(i=0;i< arguments.length;i++){
		args[i] = arguments[i];
	}
	var func = args.shift();
	for(i=0;i<length;i++){
		//alert(i+") "+func+" "+this._listeners[i][func]);
		try{
			if(this._listeners[i] != null && typeof(this._listeners[i][func]) == 'function')
				this._listeners[i][func].apply(this._listeners[i],args);
				if(this._listeners.length < length ){
					 i-=length-this._listeners.length;
					 length=this._listeners.length
				}
		}catch(e){}
	}
}
pt.clearRemoveable = function(){
	var i;
	for(i=0;i<this._listeners.length;i++){
		if(isS(this._listeners[i]._removeableListner) && this._listeners[i]._removeableListner){			
			this._listeners.splice(i,1);
			i--;
		}
	}
}
//util function
function wbds_Include(file,meth){
	switch(meth){
		case 'direct':
			document.write('<scr'+'ipt src="'+file+'"></script>');
		break;
		default:
			var script = document.createElement("SCRIPT");			
			script.language = "JavaScript";
			script.type = "text/javascript";
			var elm = document.getElementsByTagName("head")[0];
			if(typeof(elm) != "object"){
					elm = document.getElementsByTagName("body")[0];
					if(typeof(elm) != "object"){
							elm = document.getElementsByTagName("html")[0];
					}
			}
			elm = elm.appendChild(script);
			try{
				elm.src = file;
			}catch(e){
				//alert(e);
			}
		break;
	}
}
function wbds_getExtension(file){
	var indx = file.lastIndexOf('.');
	if(indx<0 || file.length-indx < 5 || file.length-indx == 0){
		return '';
	}
	return file.substring(indx+1,file.length).toLowerCase();
}
//clear prototype
delete pt;