/*
* Este objeto permite el manejo de tandas publicitarias por intervalos de tiempo
*/

/**
* Constructor
* @
*/

ADMARKMANAGER_STATE_STOP = 0;
ADMARKMANAGER_STATE_START = 1; 

function AdMarkManager(node) {
	this.tickInterval = 300;
	this.intervalTime = null;
	this.intervalPosition = "";
	this.fixedMarks = new Array();
	this.maxFixedTime = 0;
	this.hndlInterval = null;
	this.lastTimeProc = -1;
	
	this.config = null;
	this.state = ADMARKMANAGER_STATE_STOP;
	
	this.start = start;
	this.stop = stop;	
	this._loadConfig = _loadConfig;
	this._node = node;
	
	this._loadConfig();
}

/**
* Inicializa el timer
*/
function start(oPlayer) {
	oConfig = this.config;
	if (oConfig) {
		oInstance = this;
		this.hndlInterval = window.setInterval(function () {adMarkManager_tick(oPlayer,oConfig,oInstance)},this.tickInterval);
	}
}

function stop() {
	if (this.hndlInterval) {
		window.clearInterval(this.hndlInterval);
	}
}

function adMarkManager_tick(oPlayer,oConfig,oInstance) {
	if (!gbReproduciendoPublicidad) { //Publicidad vieja del player
		if (oPlayer && oConfig) {
			curPos = Math.floor(oPlayer.controls.currentPosition);		
			node = oConfig.selectSingleNode("/admarks/node[@name=\""+oInstance._node+"\"]/mark[type=\"Fixed\" and time=\""+curPos+"\"]");
			if (node) {
				if (oInstance.lastTimeProc != curPos) {
					oInstance.lastTimeProc = curPos;
					AdRefreshTanda(gsAdUrl,node.selectSingleNode("position").text);
				}
			} else {
				if (curPos > 0 && (curPos - oInstance.maxFixedTime) > 0 && (curPos - oInstance.maxFixedTime) % oInstance.intervalTime == 0) {
					if (oInstance.lastTimeProc != curPos) {
						oInstance.lastTimeProc = curPos;
						AdRefreshTanda(gsAdUrl,this.intervalPosition);
					}
				} else {
					oInstance.lastTimeProc = -1;
				}
			}
		}	
	}	
}

function _loadConfig() {
	dom = new ActiveXObject("Microsoft.XMLDOM");
	dom.async = false;
	dom.load("/scripts/app/framework.php?APP=player_contenidos&API=ad_setup");
	var useNode = "";
	if (dom.parseError.errorCode == 0) {
		//Obtengo los nodos
		count = dom.selectNodes("/admarks/node[@name=\""+this._node+"\"]/mark[type=\"Fixed\"]");
		if (count.length == 0) {
			//Obtengo los defaults
			count = dom.selectNodes("/admarks/node[@name=\"default\"]/mark[type=\"Fixed\"]");
			this._node = "default";
		}
		if (count.length > 0) {
			for(var i = 0; i < count.length; i++) {
				time = count[i].selectSingleNode("time");
				if (time && parseInt(time.text) > parseInt(this.maxFixedTime)) {
					this.maxFixedTime = parseInt(time.text);
				}
			}			
		}

		//Averiguo el interval
		count = dom.selectSingleNode("/admarks/node[@name=\""+this._node+"\"]/mark[type=\"Interval\"]");
		if (count) {
			this.intervalTime = parseInt(count.selectSingleNode("time").text);
			this.intervalPosition = count.selectSingleNode("position").text;
			
		} 
		this.config = dom;
	}
}

window.AdMarkManager = AdMarkManager;
window.adMarkManager_tick = adMarkManager_tick; 