/******************************************
 *Extensions
 */
 /*
Function.prototype.bind = function (obj){
	var met = this;
	return function (){return met.apply(obj, arguments);};
};
*/
//Object.prototype.AdPlusInherits = function( parent ){
AdObject={};
AdObject.AdPlusInherits = function( mthis,parent ){
	if(typeof(parent)=="function"){
		if( arguments.length > 1 ){
			parent.apply( mthis, Array.prototype.slice.call( arguments, 1 ) );
		}else{
			parent.call( mthis);
		}
	}else{
		//alert(typeof(parent)+parent);
	}
};
Function.prototype.AdPlusInherits = function( parent ){
	this.prototype = new parent();
	this.prototype.constructor = this;
};


/******************************************
 *AdCustomEvent Class
 */
function AdCustomEvent(onlyOnceMode){
	this.adClassName="AdCustomEvent";
	this.onlyOnceMode=onlyOnceMode;
	this.onlyOnceFired=false;
	this.onlyOnceArguments=null;
	this.suscribers=[];
};
AdCustomEvent.prototype.addListener=function (eventHandler){
	this.suscribers[this.suscribers.length]=eventHandler;	
	if(this.onlyOnceMode&&this.onlyOnceFired){
		if(eventHandler){			
			eventHandler.apply(eventHandler,this.onlyOnceArguments);
		}		
	}
	
};
AdCustomEvent.prototype.removeListener=function (eventHandler){
	for(var i=0; i<this.suscribers.length;i++){
		if(this.suscribers[i]==eventHandler){
			this.suscribers[i]=false;
		}
	}
};
AdCustomEvent.prototype.fireEvent=function (){
	this.onlyOnceFired=true;
	this.onlyOnceArguments=arguments;
	for(var i=0; i<this.suscribers.length;i++){
		var eventHandler=this.suscribers[i]
		if(eventHandler){			
			eventHandler.apply(eventHandler,arguments);
		}
	}
};
AdCustomEvent.prototype.clear=function (){
	this.suscribers=[];
};
AdCustomEvent.prototype.detachEvent=AdCustomEvent.prototype.removeListener;
AdCustomEvent.prototype.removeEventListener=AdCustomEvent.prototype.removeListener;
AdCustomEvent.prototype.attachEvent=AdCustomEvent.prototype.addListener;
AdCustomEvent.prototype.addEventListener=AdCustomEvent.prototype.addListener;
AdCustomEvent.prototype.clearEvent=AdCustomEvent.prototype.clear;
AdCustomEvent.prototype.clearEventListener=AdCustomEvent.prototype.clear;

/******************************************
 *AdCustomEventManager Class
 */ 
function AdCustomEventManager(){	
	this.adClassName="AdCustomEventManager";
};
AdCustomEventManager.prototype.addListener=function (eventName,eventHandler){
	if(this[eventName]!=undefined)
		this[eventName].addListener(eventHandler);
	return this;
};
AdCustomEventManager.prototype.removeListener=function (eventName,eventHandler){
	if(this[eventName]!=undefined&&this[eventName] instanceof AdCustomEvent)
		this[eventName].removeListener(eventHandler);
	return this;
};
AdCustomEventManager.prototype.fireEvent=function (eventName){	
	if(this[eventName]!=undefined&&this[eventName] instanceof AdCustomEvent)
		this[eventName].fireEvent.apply(this[eventName],[this].concat(Array.prototype.slice.call(arguments,1)));
	return this;
};
AdCustomEventManager.prototype.clear=function (eventName){	
	if(this[eventName]!=undefined&&this[eventName] instanceof AdCustomEvent)
		this[eventName].clear();
	return this;
};
AdCustomEventManager.prototype.attachEvent=AdCustomEventManager.prototype.addListener;
AdCustomEventManager.prototype.addEventListener=AdCustomEventManager.prototype.addListener;
AdCustomEventManager.prototype.detachEvent=AdCustomEventManager.prototype.removeListener;
AdCustomEventManager.prototype.removeEventListener=AdCustomEventManager.prototype.removeListener;
AdCustomEventManager.prototype.clearEvent=AdCustomEventManager.prototype.clear;
AdCustomEventManager.prototype.clearEventListener=AdCustomEventManager.prototype.clear;


/******************************************
 *AdProperty Class
 */
function AdProperty(source,defValue,customSetter,customGetter){
	AdObject.AdPlusInherits(this,AdCustomEventManager);
	this.adClassName="AdProperty";
	this.source=source;
	this._value=defValue;	
	this._checkType=undefined;
	if(customSetter&&customGetter){
		this._customSet=customSetter;
		this._customGet=customGetter;
	}
	this.onPropertyChange=new AdCustomEvent(); 	
	this.onPropertySet=new AdCustomEvent(); 	
};
AdProperty.AdPlusInherits(AdCustomEventManager);
AdProperty.prototype.toString=function (){ 
	return this.get();
};	
AdProperty.prototype.readOnlyMode=function (){				
	this.set=function (newValue){
		throw "AdProperty.readOnlyMode :: you are trying to set a readOnly property ["+newValue+"]";
	};
	return this;
};
AdProperty.prototype.readWriteMode=function (){
	this.set=AdProperty.prototype.set;
	return this;
};
AdProperty.prototype.checkTypeEnabled=function (typeClass){
	this._checkType=typeClass;
	return this;
};
AdProperty.prototype.checkTypeDisabled=function (){
	this._checkType=undefined;
	return this;
};
AdProperty.prototype.set=function (newValue){
	if(this._checkType!=undefined&&newValue!=null){
		if(typeof(this._checkType)=="string"){
			if(typeof(newValue)!=this._checkType){
				throw "AdProperty.set :: you are trying to set a newValue of type ("+typeof(newValue)+") when a value of type ["+this._checkType+"] was expected";
			}
		}else{
			if(!(newValue instanceof this._checkType)){
				throw "AdProperty.set :: you are trying to set a newValue of type "+newValue+") when a value of type "+this._checkType+" was expected";
			}
		}
	}
	if(this._customSet!=undefined){	
		this._customSet.call(this.source?this.source:this,newValue);
	}else{
		var prevValue=this._value;
		this._value=newValue;
		if(newValue!=prevValue){
			this.onPropertyChange.fireEvent(this.source?this.source:this,prevValue,newValue);	
		}
		this.onPropertySet.fireEvent(this.source?this.source:this,prevValue,newValue);	
	}
	return this.source?this.source:this;
};
AdProperty.prototype.get=function (){
	if(this._customGet){	
			return this._customGet.call(this.source?this.source:this);
	}else{
		return this._value;
	}
};

/******************************************
 *AdTimer Class
 */
 function AdTimer(interval,timerMode,timerId){
 	AdObject.AdPlusInherits(this,AdCustomEventManager);
 	this.isEnabled=new AdProperty(this,false);
 	if(!timerMode){
 		timerMode="interval";
 	}
 	this.timerMode=new AdProperty(this,timerMode);
 	this.interval=new AdProperty(this,new Number(interval)).checkTypeEnabled(Number).addEventListener("onPropertyChange",function (sender,prevValue,newValue){
			if(sender.isEnabled.get()){
				sender.stop();
				sender.start();
			}
	});
	if(!timerId){
		timerId="adtimer_"+(new Date().getTime());
	}
	this._timerId=timerId;
	this._timerObjectId=undefined;
	if(window.AdTimers==undefined) window.AdTimers=[];
	window.AdTimers[this._timerId]=this;	
 	this.onTick=new AdCustomEvent();
 };
 AdTimer.AdPlusInherits(AdCustomEventManager);
 AdTimer.prototype.changeInterval=function (seconds){
 	this.interval.set(new Number(seconds));
 };
 AdTimer.prototype.start=function (){
 	if(!this.interval.get()){
 		throw "AdTimer.start :: interval required";
 	}
 	switch(this.timerMode.get()){
 		case "timeout":
 			this._timerObjectId=window.setTimeout("window.AdTimers['"+this._timerId+"']._tick('"+this._timerId+"');",this.interval.get()*1000); 			
 		break;
 		case "interval":
 		default:
 			this._timerObjectId=window.setInterval("window.AdTimers['"+this._timerId+"']._tick('"+this._timerId+"');",this.interval.get()*1000); 			
 		break;
 	}
 	this.isEnabled.set(true);
 };
 AdTimer.prototype.stop=function (){
 	switch(this.timerMode.get()){
 		case "timeout":
 			window.clearTimeout(this._timerObjectId);
 		break;
 		case "interval":
 		default:
 			window.clearInterval(this._timerObjectId);
 		break;
 	}
 	this.isEnabled.set(false);
 };
 AdTimer.prototype._tick=function (timerId){
 	window.AdTimers[timerId].onTick.fireEvent(window.AdTimers[timerId]);
 };
/******************************************
 *AdRequest Class
 */ 
function AdRequest(adArea,adKeywords,adPositionInvoke,adSite,adProvider){
        this.adClassName="AdRequest";
        if(adKeywords instanceof Array){
        	adKeywords=adKeywords.join(",");
       	}
        if(adArea instanceof AdRequest){ //clone from another
        	this.call(this,adArea.adArea,adArea.adKeywords,adArea.adPositionInvoke,adArea.adSite,adArea.adProvider);
      	}else{
        	this.adArea=new AdProperty(this,adArea?adArea:AdDefaults.adArea);
        	this.adPositionInvoke=new AdProperty(this,adPositionInvoke?adPositionInvoke:undefined);
        	this.adSite=new AdProperty(this,adSite?adSite:AdDefaults.adSite);
        	this.adKeywords=new AdProperty(this,adKeywords?adKeywords:AdDefaults.adKeywords);
        	this.adProvider=new AdProperty(this,adProvider?adProvider:AdDefaults.adProvider);
        }
};

/******************************************
 *AdPosition Class
 */ 
function AdPosition(adLayer,adPositionName,adPositionInvoke,adCloseMode,adExpiration,adRefreshTimeoutWait,adFirefoxFixRequired){
	AdObject.AdPlusInherits(this,AdCustomEventManager);	
	
	//eventos
	this.onRefreshBegin=new AdCustomEvent();
	this.onRefreshSend=new AdCustomEvent();
	this.onRefreshEnd=new AdCustomEvent();
	this.onRefreshTimeout=new AdCustomEvent();
	this.onLoaded=new AdCustomEvent(true);
	this.onExpired=new AdCustomEvent();
	this.onClosed=new AdCustomEvent();
	this.onEmpty=new AdCustomEvent();
	this.onHide=new AdCustomEvent();
	this.onShow=new AdCustomEvent();
	
	//propiedades 
	this.adLayer=new AdProperty(this,adLayer).readOnlyMode();
	this.adFirefoxFixRequired=new AdProperty(this,adFirefoxFixRequired).readOnlyMode();
	this.adRequest=new AdProperty(this,undefined).checkTypeEnabled(AdRequest);
	this.adRefreshTimeoutWait=new AdProperty(this,adRefreshTimeoutWait?adRefreshTimeoutWait:AdDefaults.adRefreshTimeoutWait);
	this.adExpiration=new AdProperty(this,adExpiration?adExpiration:AdDefaults.adExpiration);	
	this.adIsVisible=new AdProperty(this,false).addEventListener("onPropertyChange",function (sender,prevValue,newValue){
	//this.adIsVisible=new AdProperty(this,false).addEventListener("onPropertySet",function (sender,prevValue,newValue){
			AdDebug("AdPosition.adIsVisible="+newValue+","+sender.adPositionName.get());
			if(newValue){
				if(window.AdPosition_Show!=undefined){
					window.AdPosition_Show(sender._htmlDiv);
				}
				sender._htmlDiv.style.display="block";
				sender._htmlDiv.className="AdPosition";
				sender.onShow.fireEvent(this);
			}else{
				sender._htmlDiv.style.display="none";
				sender.onHide.fireEvent(this);
			}
	});	
	this.adIsVisible.addEventListener("onPropertySet",function (sender,prevValue,newValue){
			AdDebug("AdPosition.adIsVisible [set]="+newValue+","+sender.adPositionName.get());
			if(newValue){	
				sender._htmlDiv.style.display="block";
				sender._htmlDiv.className="AdPosition";			
			}else{
				sender._htmlDiv.style.display="none";			
			}
	});
	this.adIsEmpty=new AdProperty(this,undefined).addEventListener("onPropertySet",function (sender,prevValue,newValue){
			if(newValue){
				AdDebug("AdPositiion.adIsEmpty:"+sender.adPositionName.get());
				sender.adIsVisible.set(false);
				sender.onEmpty.fireEvent(this);
			}else{
				sender.adIsVisible.set(true);
			}
	});		
	this.adCloseMode=new AdProperty(this,adCloseMode);
	this.adPositionName=new AdProperty(this,adPositionName).readOnlyMode();
	this.adPositionInvoke=new AdProperty(this,adPositionInvoke).readOnlyMode();
	
	//privados
	
	this._htmlDivName=this.adLayer.get().adLayerName.get()+"_"+ this.adPositionName.get()+ "_AdPositionDiv";
	this._htmlIframeName=this.adLayer.get().adLayerName.get()+"_"+ this.adPositionName.get()+ "_AdPositionIFrame";
	this._htmlIframeSource=new AdProperty(this,undefined,
		function (){		
		},function (){
			var url="";
			if(this.adRequest.get()){
			      var adProvider="";
			      if(this.adRequest.get().adProvider.get()){
			      	adProvider="_"+this.adRequest.get().adProvider.get();
			      }			      
		              if(this.adFirefoxFixRequired.get()){		              	
		              	url="/adplus/adplus_positionprint"+adProvider+".html?AdManagerName=" + this.adLayer.get().adManager.get().adManagerName.get()+ "&AdPositionName=" + this.adPositionName.get() + "&AdPositionInvoke=" + (this.adRequest.get().adPositionInvoke.get()?this.adRequest.get().adPositionInvoke.get():this.adPositionInvoke.get()) + "&AdLayerName=" + this.adLayer.get().adLayerName.get() + "&AdFixRequired="+this.adFirefoxFixRequired.get()+"&AdArea=" + this.adRequest.get().adArea.get()+ "&AdSite=" + this.adRequest.get().adSite.get() + "&AdKeywords=" + this.adRequest.get().adKeywords.get();
		              }else{		              	
		              	url="/adplus/adplus_positionprint"+adProvider+".html?AdManagerName=" + this.adLayer.get().adManager.get().adManagerName.get()+ "&AdPositionName=" + this.adPositionName.get() + "&AdPositionInvoke=" + (this.adRequest.get().adPositionInvoke.get()?this.adRequest.get().adPositionInvoke.get():this.adPositionInvoke.get()) + "&AdLayerName=" + this.adLayer.get().adLayerName.get() + "&AdArea=" + this.adRequest.get().adArea.get()+ "&AdSite=" + this.adRequest.get().adSite.get() + "&AdKeywords=" + this.adRequest.get().adKeywords.get();
		              }
	                }else{            	                	
	                      url="/adplus/adplus_positionblank.html?AdManagerName=" + this.adLayer.get().adManager.get().adManagerName.get()+ "&AdPositionName=" + this.adPositionName.get() + "&AdPositionInvoke=" + this.adPositionInvoke.get() + "&AdLayerName=" + this.adLayer.get().adLayerName.get();
	                }
			url=url+"&AdRnd=" + (new Date().getTime());
			return url;
		}).readOnlyMode();
	this._hookHtmlElements();	
};
AdPosition.AdPlusInherits(AdCustomEventManager);
AdPosition.prototype._hookHtmlElements=function (){	
	if(document.getElementById(this._htmlDivName)!=undefined&&document.getElementById(this._htmlDivName).parentNode){
		document.getElementById(this._htmlDivName).parentNode.removeChild(document.getElementById(this._htmlDivName));
	}
	this._htmlLayerDiv=this.adLayer.get()._htmlDiv;
	this._htmlDiv=document.createElement("div");
	this._htmlDiv.className="AdPosition";
	this._htmlDiv.id=this._htmlDivName;
	this._htmlDiv.name=this._htmlDivName;	
	if(this.adIsVisible.get()){
		this._htmlDiv.style.display="block";
	}else{
		this._htmlDiv.style.display="none";
	}
	this._htmlLayerDiv.appendChild(this._htmlDiv);
	this._htmlIframe=document.createElement("iframe");
	this._htmlIframe.id=this._htmlIframeName;
	this._htmlIframe.name=this._htmlIframeName;	
	this._htmlIframe.setAttribute("src",this._htmlIframeSource.get());	
	this._htmlIframe.setAttribute("frameborder","0");
	this._htmlIframe.setAttribute("scrolling","no");
	this._htmlIframe.setAttribute("allowTransparency","true");
	this._htmlDiv.appendChild(this._htmlIframe);
	this.onLoaded.fireEvent(this);
};
AdPosition.prototype._iframeReplace=function (url){		
	if(window.AdPosition_Refresh){
		window.AdPosition_Refresh(this._htmlIframeName,url);
	}else{
		if(window.frames[this._htmlIframeName]){
			this._htmlIframe.setAttribute("src",url);
			window.frames[this._htmlIframeName].location.replace(url);
		}
	}
};
AdPosition.prototype.adBlank=function (){	
	this.adRequest.set(null);		
	this._iframeReplace(this._htmlIframeSource.get());
	return this;
};
AdPosition.prototype.adRefresh=function (adRequest){
	if(adRequest!=undefined){
		this.adRequest.set(adRequest);		
	}
	this._doRefresh();
	return this;
};
AdPosition.prototype._doRefresh=function (){	
	if(this.adRequest!=undefined){
		this.onRefreshBegin.fireEvent(this);
		if(this._doRefreshSend()){
			this._enabledTimeoutTimer();
			this.onRefreshSend.fireEvent(this);
		}
	}else{
		throw "AdPosition.doRefresh :: adRequest required";
	}	
	return true;
}; 
AdPosition.prototype._doRefreshSend=function (){
	//Oculto la posicion
	//this.adIsVisible.set(false);
	this._iframeReplace(this._htmlIframeSource.get());
	return true;
};
AdPosition.prototype.adRefreshCompleted=function (typeContent,innerContent){		
	if(typeContent){
		switch(typeContent.toLowerCase()){
			case "adempty":			
				this.adIsEmpty.set(true);
				break;
			case "adother":
			default: 
				//this._htmlDiv.className="AdPosition";
				this.adIsEmpty.set(false); 
				this.adIsVisible.set(true);
			break;
		}		
		this.onRefreshEnd.fireEvent(this);
		this._enabledExpirationTimer();
		this._disabledTimeoutTimer();
	}
	return true;
};
AdPosition.prototype.adClose=function (){
	switch(this.adCloseMode.get()){
		case "destroy":
			if(this._htmlDiv&&this._htmlLayerDiv){
				this._htmlLayerDiv.removeChild(this._htmlDiv);
			}
		break;
		case "changecss":
			this._htmlDiv.className="AdPosition PositionClosed";
		break;
		case "hide":
		default:
			AdDebug("AdPosition.adClose::"+this.adPositionName.get());
			this.adIsVisible.set(false);
		break;
	}
	this._disabledExpirationTimer();
	this.onClosed.fireEvent(this);
};
AdPosition.prototype._disabledExpirationTimer=function (){
	if(this._adTimerExpiration){
		this._adTimerExpiration.stop();
	}
};
AdPosition.prototype._disabledTimeoutTimer=function (){
	if(this._adTimerRefreshTimeoutWait){
		this._adTimerRefreshTimeoutWait.stop();
	}
};
AdPosition.prototype._enabledExpirationTimer=function (){
	var me=this;
	if(this.adExpiration.get()){
		AdDebug("enabledExpirationTimer ::"+this.adExpiration.get()+","+this.adPositionName.get());
		if(!this._adTimerExpiration){
			this._adTimerExpiration=new AdTimer(this.adExpiration.get(),"timeout").addEventListener("onTick",function (){
				//alert("Expiration Timer tick");
				me._adTimerExpiration.stop();
				me.adClose();
				me.onExpired.fireEvent(me);
			});
			this._adTimerExpiration.start();
		}else{
			if(this._adTimerExpiration.isEnabled.get()){
				this._adTimerExpiration.stop();	
			}		
			this._adTimerExpiration.changeInterval(this.adExpiration.get());
			this._adTimerExpiration.start();
		}
	}
};
AdPosition.prototype._enabledTimeoutTimer=function(){
	var me=this;
	if(this.adRefreshTimeoutWait.get()){
		if(!this._adTimerRefreshTimeoutWait){
			this._adTimerRefreshTimeoutWait=new AdTimer(this.adRefreshTimeoutWait.get(),"timeout").addEventListener("onTick",function (){
				//alert("RefreshTimeout Timer tick");
				me._adTimerRefreshTimeoutWait.stop();		
				me.adIsVisible.set(true);
				me.onRefreshTimeout.fireEvent(me);
			});
			this._adTimerRefreshTimeoutWait.start();
		}else{
			if(this._adTimerRefreshTimeoutWait.isEnabled.get()){
				this._adTimerRefreshTimeoutWait.stop();	
			}		
			this._adTimerRefreshTimeoutWait.changeInterval(this.adRefreshTimeoutWait.get());
			this._adTimerRefreshTimeoutWait.start();
		}	
	}
};
AdPosition.prototype.adExpirationSet=function (seconds){
	this._disabledExpirationTimer();
	AdDebug("adExpirationSet ::"+seconds+","+this.adPositionName.get());
	this.adExpiration.set(seconds);
	this._enabledExpirationTimer();
};
AdPosition.prototype.adExpirationDisabled=function (){
	this._disabledExpirationTimer();
};
AdPosition.prototype.adExpirationEnabled=function (){
	this._enabledExpirationTimer();
};
AdPosition.prototype.adRefreshTimeoutWaitSet=function (seconds){
	this._disabledTimeoutTimer();
	this.adRefreshTimeoutWait.set(seconds);
	this._enabledTimeoutTimer();
};
AdPosition.prototype.adRefreshTimeoutWaitDisabled=function (){
	this._disabledTimeoutTimer();
};
AdPosition.prototype.adRefreshTimeoutWaitEnabled=function (){
	this._enabledTimeoutTimer();
};
/*******************************
 *AdLayer Class
 */
function AdLayer(adManager,adLayerName,adCloseMode){
	AdObject.AdPlusInherits(this,AdCustomEventManager);	
	
	//Eventos
	this.onHide=new AdCustomEvent();
	this.onShow=new AdCustomEvent();	
	this.onClosed=new AdCustomEvent();
	this.onPositionAdded=new AdCustomEvent();
	
	//Eventos asociados
	this.onAllHide=new AdCustomEvent();
	this.onAllShow=new AdCustomEvent();	
	this.onAllClosed=new AdCustomEvent();	
	this.onAllRefreshBegin=new AdCustomEvent();
	this.onAllRefreshSend=new AdCustomEvent();
	this.onAllRefreshEnd=new AdCustomEvent();
	this.onAllRefreshTimeout=new AdCustomEvent();
	this.onAllLoaded=new AdCustomEvent(true);
	this.onAllExpired=new AdCustomEvent();	
	this.onAllEmpty=new AdCustomEvent();
	this.onAnyHide=new AdCustomEvent();
	this.onAnyShow=new AdCustomEvent();	
	this.onAnyClosed=new AdCustomEvent();	
	this.onAnyRefreshBegin=new AdCustomEvent();
	this.onAnyRefreshSend=new AdCustomEvent();
	this.onAnyRefreshEnd=new AdCustomEvent();
	this.onAnyRefreshTimeout=new AdCustomEvent();
	this.onAnyLoaded=new AdCustomEvent(true);
	this.onAnyExpired=new AdCustomEvent();	
	this.onAnyEmpty=new AdCustomEvent();
	
	//Propiedades
	this.adLayerName=new AdProperty(this,adLayerName).readOnlyMode();
	this.adManager=new AdProperty(this,adManager).readOnlyMode();	
	this.adCloseMode=new AdProperty(this,adCloseMode);
	this.adIsVisible=new AdProperty(this,false).addEventListener("onPropertyChange",function (sender,prevValue,newValue){
			if(newValue){
				sender._htmlDiv.style.display="block";
				sender.onShow.fireEvent(this);
			}else{
				sender._htmlDiv.style.display="none";
				sender.onHide.fireEvent(this);
			}
	});	
	
	this._htmlDivName=this.adLayerName.get()+"_AdLayerDiv";	
	this._htmlDiv=undefined;
	this._htmlManagerDiv=undefined;
	this._positions={};
	this._positionsEvents={};	
	this._hookHtmlElements();
};
AdLayer.AdPlusInherits(AdCustomEventManager);
AdLayer.prototype._hookHtmlElements=function (){	
	this._htmlManagerDiv=this.adManager.get()._htmlDiv;
	this._htmlDiv=document.createElement("div");		
	this._htmlDiv.className="AdLayer";
	this._htmlDiv.id=this._htmlDivName;
	this._htmlDiv.name=this._htmlDivName;	
	if(this.adIsVisible.get()){
		this._htmlDiv.style.display="block";
	}else{
		this._htmlDiv.style.display="none";
	}
	this._htmlManagerDiv.appendChild(this._htmlDiv);
};
AdLayer.prototype.addPosition=function (adPositionName,adPositionInvoke,adCloseMode,adExpiration,adRefreshTimeoutWait){
	var me=this;
	if(!this._positions[adPositionName]){
		this._positions[adPositionName]=new AdPosition(this,adPositionName,adPositionInvoke,adCloseMode,adExpiration,adRefreshTimeoutWait);
		this._positionsEvents[adPositionName]=[];
		this._positions[adPositionName].addEventListener("onHide",function (){me._positionAllChecker("onHide","onAllHide","onAnyHide",adPositionName);});
		this._positions[adPositionName].addEventListener("onShow",function (){me._positionAllChecker("onShow","onAllShow","onAnyShow",adPositionName);});
		this._positions[adPositionName].addEventListener("onClosed",function (){me._positionAllChecker("onClosed","onAllClosed","onAnyClosed",adPositionName);});
		this._positions[adPositionName].addEventListener("onRefreshBegin",function (){me._positionAllChecker("onRefreshBegin","onAllRefreshBegin","onAnyRefreshBegin",adPositionName);});
		this._positions[adPositionName].addEventListener("onRefreshSend",function (){me._positionAllChecker("onRefreshSend","onAllRefreshSend","onAnyRefreshSend",adPositionName);});
		this._positions[adPositionName].addEventListener("onRefreshEnd",function (){me._positionAllChecker("onRefreshEnd","onAllRefreshEnd","onAnyRefreshEnd",adPositionName);});
		this._positions[adPositionName].addEventListener("onRefreshTimeout",function (){me._positionAllChecker("onRefreshTimeout","onAllRefreshTimeout","onAnyRefreshTimeout",adPositionName);});
		this._positions[adPositionName].addEventListener("onExpired",function (){me._positionAllChecker("onExpired","onAllExpired","onAnyExpired",adPositionName);});
		this._positions[adPositionName].addEventListener("onEmpty",function (){me._positionAllChecker("onEmpty","onAllEmpty","onAnyEmpty",adPositionName);});		
		this.onPositionAdded.fireEvent(this,this._positions[adPositionName]);
	}else{
		throw "AdLayer.addPosition :: position "+adPositionName+" already Exists";
	}
	return this;
};
AdLayer.prototype._positionAllChecker=function (eventName,eventAllName,eventAnyName,adPositionName){
	this._positionsEvents[adPositionName][eventName]=true;
	this[eventAnyName].fireEvent(this,this._positions[adPositionName]);
	for(var i in this._positionsEvents){
		if(this._positions[i] instanceof AdPosition){
			if(!this._positionsEvents[i][eventName]){
				return;		
			}
		}
	}
	this[eventAllName].fireEvent(this);
};
AdLayer.prototype.getPosition=function (adPositionName){
	if(this._positions[adPositionName]){
		return this._positions[adPositionName];
	}
	return undefined;
};
AdLayer.prototype.adRefresh=function (adRequest){
	for(var i in this._positions){
		if(this._positions[i] instanceof AdPosition){
			this._positions[i].adRefresh(adRequest);
		}
	}
	return this;
};
AdLayer.prototype.adClose=function (adRequest){
	for(var i in this._positions){
		if(this._positions[i] instanceof AdPosition){
			this._positions[i].adClose(adRequest);
		}
	}
	switch(this.adCloseMode.get()){
		case "destroy":
			if(this._htmlDiv&&this._htmlManagerDiv){
				this._htmlManagerDiv.removeChild(this._htmlDiv);
			}
		break;
		case "changecss":
			this._htmlDiv.className="AdLayer LayerClosed";
		break;
		case "nothing": break;
		case "hide":
		default:
			this.adIsVisible.set(false);
		break;
	}
	this.onClosed.fireEvent(this);
	return this;
};

/*************************************************
 *AdManager Class
 */
function AdManager(adManagerName,container){	
	AdObject.AdPlusInherits(this,AdCustomEventManager);
	
	this.onLayerAdded=new AdCustomEvent();
	this.onPositionAdded=new AdCustomEvent();
	this.onAdRequiredPlay=new AdCustomEvent();
	this.onAdRequiredPause=new AdCustomEvent();
	this.onAdRequiredPlaylistDisabled=new AdCustomEvent();
	this.onAdRequiredPlaylistEnabled=new AdCustomEvent();
	this.onAdRequiredPlayVideoBanner=new AdCustomEvent();	
	this.onAdRequiredRadioPlay=new AdCustomEvent();
	this.onAdRequiredRadioPause=new AdCustomEvent();
	
	if(arguments.length==1){
		container=adManagerName;
		adManagerName="general";
	}
	if(window.AdManagers==undefined) window.AdManagers=[];
	window.AdManagers[adManagerName]=this;
	
	//Propiedades
	this.adManagerName=new AdProperty(this,adManagerName).readOnlyMode();
	
	//Privados
	this._layers=[];
	this._lastAdLayerName=undefined;
	if(typeof(container)=="string"){
		this._htmlDiv=document.getElementById(container);
	}else{
		this._htmlDiv=container;
	}
	if(!this._htmlDiv||(this._htmlDiv&&this._htmlDiv.tagName.toLowerCase()!="div")){
		throw "AdManager :: required a container DIV";
	}
};
AdManager.AdPlusInherits(AdCustomEventManager);
AdManager.prototype.addLayer=function (adLayerName,adCloseMode){
	if(!this._layers[adLayerName]){
		var me=this;
		this._layers[adLayerName]=new AdLayer(this,adLayerName,adCloseMode);
		this._lastAdLayerName=adLayerName;
		this._layers[adLayerName].addEventListener("onPositionAdded",function (sender,position){
			me.onPositionAdded.fireEvent(sender,position);
		});
		this.onLayerAdded.fireEvent(this,this._layers[adLayerName]);
	}else{
		throw "AdManager.addLayer :: layer  "+adLayerName+" already Exists";
	}	
	return this;
};
AdManager.prototype.addPosition=function (adPositionName,adPositionInvoke,adCloseMode,adExpiration,adRefreshTimeoutWait){
	if(this._layers[this._lastAdLayerName]){
		this._layers[this._lastAdLayerName].addPosition(adPositionName,adPositionInvoke,adCloseMode,adExpiration,adRefreshTimeoutWait);
	}else{
		throw "AdManager.addPosition :: lastLayerName doesnt exists";
	}
	return this;
};
AdManager.prototype.getLayer=function (adLayerName){
	if(this._layers[adLayerName]){
		return this._layers[adLayerName];
	}
	return undefined;
};
AdManager.prototype.getPosition=function (adLayerName,adPositionName){
	if(this._layers[adLayerName]){
		return this._layers[adLayerName].getPosition(adPositionName);
	}
	return undefined;	
};
AdManager.prototype.showAdLayer=function (adLayerName){
	if(this._layers[adLayerName]){
		this._layers[adLayerName].adIsVisible.set(true);
	}
	return this;	
};
AdManager.prototype.hideAdLayer=function (adLayerName){
	if(this._layers[adLayerName]){
		this._layers[adLayerName].adIsVisible.set(false);
	}
	return this;	
};
AdManager.prototype.adRefresh=function (adRequest,adLayerName,adPositionName){
	if(adLayerName){		
		if(this._layers[adLayerName]&&this._layers[adLayerName] instanceof AdLayer){
			if(adPositionName){
				if(this._layers[adLayerName].getPosition(adPositionName)){
					this._layers[adLayerName].getPosition(adPositionName).adRefresh(adRequest);				
				}
			}else{				
				this._layers[adLayerName].adRefresh(adRequest);				
			}
		}	
	}else{
		for(var i in this._layers){
			if(this._layers[i] instanceof AdLayer){
				this._layers[i].adRefresh(adRequest);
			}	
		}
	}
	return this;
};


/***********************************
 *AdGenericVideoBanner class
 */
function AdGenericVideoBanner(adPosition,url,timewaitforbutton,clickTag,pipImg,oWidth,oHeight){
	AdObject.AdPlusInherits(this,AdCustomEventManager);
	var _adPos=undefined;	
	var _notAdPosition=false;
	if(adPosition instanceof AdPosition){
		_adPos=adPosition;	
	}else if(adPosition instanceof Array){
		_adPos=this.getPosition(adPosition[0],adPosition[1]);
	}else if(typeof(adPosition)=="object"&&adPosition!=undefined&&adPosition.tagName){
		_adPos=this.getPosition(adPosition.adLayerName,adPosition.adPositionName);
	}else if(typeof(adPosition)=="string"){
		_notAdPosition=true;
		_adPos=document.getElementById(adPosition);
	}
	if(!_adPos){
		throw "AdGenericVideoBanner :: adPosition is Required";
	}
	if(_notAdPosition){
		this.adPosition=new AdProperty(this,undefined).readOnlyMode();
	}else{
		this.adPosition=new AdProperty(this,_adPos).readOnlyMode();
	}
	this.adUrl=new AdProperty(this,url).readOnlyMode();
	this.adTimeWaitForButton=new AdProperty(this,new Number(timewaitforbutton)).readOnlyMode();
	this.adClickTag=new AdProperty(this,clickTag).readOnlyMode();
	this.adPictureInPictureImage=new AdProperty(this,pipImg).readOnlyMode();
	this.adVideoPlayerWidth=new AdProperty(this,oWidth).readOnlyMode();
	this.adVideoPlayerHeight=new AdProperty(this,oHeight).readOnlyMode();
	this.adIsVisible=new AdProperty(this,undefined).addEventListener("onPropertyChange",function (sender,prevValue,newValue){
			sender.adPosition.get().adIsVisible.set(newValue);
			if(newValue){
				sender._htmlDiv.style.display="block";
				sender.onShow.fireEvent(this);
			}else{
				sender._htmlDiv.style.display="none";
				if(sender._htmlObject){
					sender._htmlObject.controls.stop();
				}
				sender.onHide.fireEvent(this);
			}
	});	
	this._htmlDiv=undefined;
	this._htmlObject=undefined;
	this._htmlEmbed=undefined;
	this._htmlDivObject=undefined;
	this._htmlDivPictureInPicture=undefined;
	this._htmlImgPictureInPicture=undefined;	
	this._htmlSpanPictureInPicture=undefined;
	if(this.adPosition.get()){
		this._htmlParentDiv=this.adPosition.get()._htmlDiv;
		if(this.adPosition.get().adGenericVideoBanner!=undefined){
			this._htmlDiv=this.adPosition.get().adGenericVideoBanner._htmlDiv;
			this._htmlDivObject=this.adPosition.get().adGenericVideoBanner._htmlDivObject;
			this._htmlDivPictureInPicture=this.adPosition.get().adGenericVideoBanner._htmlDivPictureInPicture;
			this._htmlImgPictureInPicture=this.adPosition.get().adGenericVideoBanner._htmlImgPictureInPicture;
			this._htmlSpanPictureInPicture=this.adPosition.get().adGenericVideoBanner._htmlSpanPictureInPicture;			
			this._htmlObject=this.adPosition.get().adGenericVideoBanner._htmlObject;			
			this._htmlEmbed=this.adPosition.get().adGenericVideoBanner._htmlEmbed;
			
			this._htmlObject.url=this.adUrl.get();
			this._htmlImgPictureInPicture.setAttribute("src",this.adPictureInPictureImage.get());			
			this._htmlSpanPictureInPicture.innerHTML="<span class='PlayIcon'>play</span>";
		}else{
			this.adPosition.get().adGenericVideoBanner=this;
		}
	}else if(_adPos!=undefined){
		this._htmlParentDiv=_adPos;
	}
	this.onHide=new AdCustomEvent();
	this.onShow=new AdCustomEvent();	
	this.onClosed=new AdCustomEvent();	
	this.onCloseEnabled=new AdCustomEvent();
	
	this._hookHtmlElements();
};
AdGenericVideoBanner.AdPlusInherits(AdCustomEventManager);
AdGenericVideoBanner.prototype._hookHtmlElements=function (){
	var me=this;
	if(!this._htmlDiv&&this._htmlParentDiv){
		this._htmlDiv=document.createElement("div");	
		this._htmlDiv.className="AdGenericVideoBanner";
		this._htmlDivObject=document.createElement("div");	
		this._htmlDivPictureInPicture=document.createElement("div");	
		this._htmlSpanPictureInPicture=document.createElement("span");	
		var spPlay=document.createElement("span");
		spPlay.className="PlayIcon";
		spPlay.innerHTML="play";
		this._htmlSpanPictureInPicture.appendChild(spPlay);
		this._htmlImgPictureInPicture=document.createElement("img");

		this._htmlImgPictureInPicture.setAttribute("src",this.adPictureInPictureImage.get());
		this._htmlDivObject.className="AdGenericVideoBannerObjectContainer";
		this._htmlDivPictureInPicture.className="AdGenericVideoBannerImageContainer";		
		this._htmlObject=document.createElement("object");
		this._htmlEmbed=document.createElement("embed");
		this._htmlObject.setAttribute('TYPE','application/x-oleobject');
		this._htmlObject.setAttribute("CLASSID","CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6");
		this._htmlObject.setAttribute("width",this.adVideoPlayerWidth.get());
		this._htmlObject.setAttribute("height",this.adVideoPlayerHeight.get());		
		this._htmlEmbed.setAttribute('TYPE','application/x-oleobject');
		this._htmlEmbed.setAttribute("CLASSID","CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6");
		this._htmlEmbed.setAttribute("width",this.adVideoPlayerWidth.get());
		this._htmlEmbed.setAttribute("height",this.adVideoPlayerHeight.get());				
		this._htmlObject.appendChild(this._makeHtmlParams("autoStart","true"));
		this._htmlObject.appendChild(this._makeHtmlParams("uiMode","none"));
		this._htmlObject.appendChild(this._makeHtmlParams("showControls","false"));
		this._htmlObject.appendChild(this._makeHtmlParams("windowlessVideo","true"));
		this._htmlObject.appendChild(this._makeHtmlParams("URL",this.adUrl.get()));
		this._htmlObject.appendChild(this._makeHtmlParams("width",this.adVideoPlayerWidth.get()));
		this._htmlObject.appendChild(this._makeHtmlParams("height",this.adVideoPlayerHeight.get()));
		this._htmlEmbed.setAttribute("autoStart","true");
		this._htmlEmbed.setAttribute("uiMode","none");
		this._htmlEmbed.setAttribute("showControls","false");
		this._htmlEmbed.setAttribute("windowlessVideo","false");
		this._htmlEmbed.setAttribute("src",this.adUrl.get());
		this._htmlDivPictureInPicture.appendChild(this._htmlImgPictureInPicture);
		this._htmlDivPictureInPicture.appendChild(this._htmlSpanPictureInPicture);
		if(this._htmlObject.outerHTML){
			this._htmlDivObject.innerHTML=this._htmlObject.outerHTML;
			this._htmlObject=this._htmlDivObject.getElementsByTagName("object")[0];
		}else{
			this._htmlDivObject.appendChild(this._htmlEmbed);
		}			
		this._htmlDiv.appendChild(this._htmlDivObject);
		this._htmlDiv.appendChild(this._htmlDivPictureInPicture);
		this._htmlParentDiv.appendChild(this._htmlDiv);		
	}else{
		//this._htmlObject.url=this.adUrl.get();
		//this._htmlImgPictureInPicture.setAttribute("src",this.adPictureInPictureImage.get());
		//this._htmlSpanPictureInPicture.innerHTML="<span class='PlayIcon'>play</span>";
	}
	if(window.attachEvent){
		this._htmlObject.attachEvent("playStateChange",function (){
			me._playStateChange.apply(me, Array.prototype.slice.call(arguments));
		});
	}else if(this._htmlObject.addEventListener){
		this._htmlObject.addEventListener("playStateChange",function (){
			me._playStateChange.apply(me, Array.prototype.slice.call(arguments));
		},false);		
	}
	this._htmlDivPictureInPicture.onclick=function (event){
		me._eventPreventDefault(event);
		me._click("close");
	};			
	this._htmlDivObject.onclick=function (event){
		me._eventPreventDefault(event);
		me._click("tag");
	};
	this._htmlDiv.onclick=function (event){
		me._eventPreventDefault(event);
		me._click("tag");
	};			
	if(this.adTimeWaitForButton.get()){
		this._adTimerWaitForButton=new AdTimer(this.adTimeWaitForButton.get(),"timeout").addEventListener("onTick",function (){
			//alert("adTimeWaitForButton Timer tick");
			me._htmlSpanPictureInPicture.innerHTML=me._htmlSpanPictureInPicture.innerHTML.replace(/play/,"play enabled");
			me._adTimerWaitForButton.stop();
			me._enableCloseableDiv();
			me.onCloseEnabled.fireEvent(me);
		});
		this._adTimerWaitForButton.start();
	}	
	this.adIsVisible.set(true);
};
AdGenericVideoBanner.prototype._playStateChange=function (newState){
	switch (newState) {
		case 8: //MediaEnded
			this.adIsVisible.set(false);
			this.onClosed.fireEvent(this);
	}
};
AdGenericVideoBanner.prototype._click=function (what){
	if(what=="close"){
		if(this._htmlDiv){
			if(this._htmlDiv.className.search("AdCloseEnabled")!=-1){
				this.adIsVisible.set(false);
				this.onClosed.fireEvent(this);
			}
		}
	}else{
		if(this.adClickTag.get()){
			window.open(this.adClickTag.get(),"_blank");
		}
	}	
};
AdGenericVideoBanner.prototype._eventPreventDefault=function (what){
	if(event){
		event.cancelBubble =true;
		if(event.stopPropagation)
			event.stopPropagation();
	}else if(window.event){
		window.event.cancelBubble =true;
	}
};
AdGenericVideoBanner.prototype._enableCloseableDiv=function (){
	if(this._htmlDiv){
		this._htmlDiv.className="AdGenericVideoBanner AdCloseEnabled";		
	}
};
AdGenericVideoBanner.prototype._makeHtmlParams=function (name,val){
	var oParam=document.createElement("param");	
	oParam.setAttribute("name",name);
	oParam.setAttribute("value",val);
	return oParam;	
};
/*******************************
 *AdDefaults Object
 */
AdDefaults={
	adArea: "demo",
	adSite: "cl_vod",
	adKeywords: "",
	adProvider: "",
	adRefreshTimeoutWait: "40",
	adExpiration: ""
};

function AdDebug(str){
	var $top=window;	
	try{
		var pruebaLocation=window.top.location.toString();
		$top=window.top;
	}catch(e){
		try{
			var pruebaLocation=window.parent.location.toString();		
			$top=window.parent;
		}catch(e){
			$top=window;
		}	
	} 
	if($top&&$top.location.toString().search("debugad")!=-1){
		alert(str);
	}
}
/***************************************************
 *PRUEBAS - eventos
 ***************************************************/
/*
function MyObjeto(){	
	AdObject.AdPlusInherits(this,AdCustomEventManager);
	this.className="MyObjeto";
	

};
MyObjeto.AdPlusInherits(AdCustomEventManager)
MyObjeto.prototype.onLoaded=new AdCustomEvent();
MyObjeto.prototype.onPrueba=new AdCustomEvent();



var objeto=new MyObjeto();

function myeventHandler(){
	alert("otra ivnocacion 2"+this.className);
}

function otroObjeto(){
	this.className="otroObjeto";
	this.mimetodo=function (){
		alert("invocacion desde otroObjeto invocacion 3"+this.className);
	}
};
otroObjeto.staticoHandler=function (){
	alert("invocacion desde otroObjeto.staticoHandler 4"+this.className);
};


///////////

objeto.onLoaded.addListener(function (){ alert("invocacion 1"+this.className);});
objeto.onLoaded.addListener(myeventHandler);
objeto.onLoaded.addListener(new otroObjeto().mimetodo);
objeto.onLoaded.addListener(function (){new otroObjeto().mimetodo();});
objeto.onLoaded.addListener(otroObjeto.staticoHandler);
objeto.onLoaded.addListener(function (){otroObjeto.staticoHandler();});
objeto.onLoaded.fireEvent(objeto);
objeto.onLoaded.clear();
objeto.onLoaded.addListener(myeventHandler);
objeto.onLoaded.fireEvent(objeto);
objeto.onLoaded.removeListener(myeventHandler);
objeto.onLoaded.fireEvent(objeto);

objeto.addListener("onLoaded",function (sender){ alert("invocacion 1"+sender.className);});
objeto.fireEvent("onLoaded");
alert(1);
*/


