function ControlProcessor(controlId)
{
	this.controlId = controlId;
}
ControlProcessor.prototype.getControl = function()
{
	return document.getElementById(this.controlId);
}
ControlProcessor.prototype.getFlashObj = function() {
    if (this.controlId != undefined) {
        return (navigator.appName.indexOf('Microsoft') != -1)
		? document.getElementById(this.controlId)
		: document[this.controlId];
    }
}
ControlProcessor.prototype.SetFocusSafe = function()
{
	var control = this.getFlashObj();
	if (control) { control.focus(); }
}
ControlProcessor.prototype.ShowByStyleDisplay = function()
{
	var control = this.getControl();
	if (control) { control.style.display = 'block'; }
}
ControlProcessor.prototype.HideByStyleDisplay = function()
{
	var control = this.getControl();
	if (control) { control.style.display = 'none'; }
}
ControlProcessor.prototype.ShowByVisibilityHeight = function()
{
	var control = this.getControl();
	if (control)
	{
		control.style.visibility = 'visible';
		control.style.height = '';
		if (/MSIE/.test(navigator.userAgent)) { control.style.display = 'block'; }
	}
}
ControlProcessor.prototype.HideByVisibilityHeight = function() {
    var control = this.getControl();
    if (control) {
        control.style.visibility = 'hidden';
        control.style.height = '0px';
        if (/MSIE/.test(navigator.userAgent)) { control.style.display = 'none'; }
    }
}

function RollAds(
	timer, /*seconds*/
	htmlTimerContentFormat,
	divTimerID,
	divAdsID,
	divGameID, //auto-height required
	flashObjID //auto-height required
	)
{
	this.timer = timer;
	this.timeout = null;
	this.htmlTimerContentFormat = htmlTimerContentFormat;

	this.divTimer = new ControlProcessor(divTimerID);
	this.divAds = new ControlProcessor(divAdsID);
	this.divGame = new ControlProcessor(divGameID);
	this.flashObj = new ControlProcessor(flashObjID);
	
	this.stuck = 0; //[moudrick] not used!! for quick remove
}

RollAds.prototype.SetTimeout = function(func)
{
	if (this.timeout != null)
	{
		clearTimeout(this.timeout);
	}
	this.timeout = setTimeout(func, 1000);
}

RollAds.prototype.StopTimer = function()
{
	this.timer = 0;
	if (this.timeout != null)
	{
		clearTimeout(this.timeout);
	}
	this.timeout = null;
}

RollAds.prototype.SetTimer = function(tValue)
{
	this.StopTimer();
	this.timer = tValue;
	this.ShowTimeWithCM();
	//this.SetTimeout(function() { that.ShowTimeWithCM() });
}
RollAds.prototype.LoadGame = function()
{
	this.divTimer.HideByStyleDisplay();
	this.divAds.HideByStyleDisplay();
	this.divGame.ShowByStyleDisplay();
	this.divGame.ShowByVisibilityHeight();
	this.flashObj.ShowByVisibilityHeight();
	this.flashObj.SetFocusSafe();
}
RollAds.prototype.PostRoll = function()
{
	this.divGame.HideByVisibilityHeight();
	this.flashObj.HideByVisibilityHeight();

	this.divTimer.ShowByStyleDisplay();
	this.divAds.ShowByStyleDisplay();

	this.ShowTime();
}

RollAds.prototype.ShowTimeWithCM = function()
{
	this.divTimer.getControl().innerHTML = this.htmlTimerContentFormat.replace(/\{0\}/g, this.timer);
	this.timer--;
	if (this.timer < 0)
	{
		OnAdsEnd();
		this.divTimer.HideByStyleDisplay();
		this.divAds.HideByStyleDisplay();
		this.divGame.ShowByStyleDisplay();
		this.divGame.ShowByVisibilityHeight();
		this.flashObj.ShowByVisibilityHeight();
		this.flashObj.SetFocusSafe();
	}
	else
	{
		var that = this;
		this.SetTimeout(function() { that.ShowTimeWithCM() });
	}
}
RollAds.prototype.ShowTime = function()
{
	this.divTimer.getControl().innerHTML = this.htmlTimerContentFormat.replace(/\{0\}/g, this.timer);
	this.timer--;
	if (this.timer < 0)
	{
		this.divTimer.HideByStyleDisplay();
		this.divAds.HideByStyleDisplay();
		this.divGame.ShowByStyleDisplay();
		this.divGame.ShowByVisibilityHeight();
		this.flashObj.ShowByVisibilityHeight();
		this.flashObj.SetFocusSafe();
		
	}
	else
	{
		var that = this;
		this.SetTimeout(function() { that.ShowTime() });
	}
}
