
var _MT_FLYSCREEN = false;

FlyScreenRun();

function FlyScreenRun() {
	if (! window._rbns) {
		setTimeout(FlyScreenRun, 100);
		return;
	}
	if (typeof(window._mtfs_run) != 'undefined') {
		return;
	}

	window._mtfs_run = true;
	_MT_FLYSCREEN = new FlyScreenObj(window._rbns);
	if (document._rbns != null && document._rbns.v != null) {
		if (document._rbns.v.position != null)
			_MT_FLYSCREEN.position = document._rbns.v.position;
	}
	_MT_FLYSCREEN.run();
}

function _mtfs_ev(ev) {
	ad = _MT_FLYSCREEN.ad;
	switch (ev) {
		case 'click':
		case 'close':
			_MT_FLYSCREEN.hideBanner(ad);
			break;
		case 'over':
			if (ad && ad.evo) {
				_MT_FLYSCREEN.cacheFile(ad.evo);
				ad.evo = false;
			}
			if (typeof(ad.bplayer_url) == 'undefined' || ad.expanded)
				break;
			_MT_FLYSCREEN.expandBanner(ad);
			ad.expanded = true;
			break;
		case 'ready':
			if (ad.expanded)
				break;
			if (ad && _MT_FLYSCREEN.scrlcb == false) {
				if (!_MT_FLYSCREEN.ad.trace) {
					if (ad.trace_url)
						_MT_FLYSCREEN.cacheFile(ad.trace_url+'&ul='+_MT_FLYSCREEN.location);
					if (ad.tracing_url && ad.tracing_url.length > 0)
						_MT_FLYSCREEN.cacheFile(ad.tracing_url);
					_MT_FLYSCREEN.ad.trace = true;
				}
			}
			break;
		case 'quarter':
			if (ad && ad.evq) {
				_MT_FLYSCREEN.cacheFile(ad.evq);
			}
			break;
		case 'half':
			if (ad && ad.evh) {
				_MT_FLYSCREEN.cacheFile(ad.evh);
			}
			break;
		case 'threequarters':
			if (ad && ad.evtq) {
				_MT_FLYSCREEN.cacheFile(ad.evtq);
			}
			break;
		case 'complete':
			if (ad && ad.evc)
				_MT_FLYSCREEN.cacheFile(ad.evc);
			this.scrlcb = setInterval(
				function() {
					_MT_FLYSCREEN.hideBanner(ad);
				},
				3000
			);
			break;
	}
}

function FlyScreenObj(ad) {
	this.ad = ad;
	this.ad.expanded = false;
	this.params = {
		'info_url' : 'http://videoclick.ru/'
	};
	this.location = escape(window.location.href);
	this.scrlcb = false;

	this.run = function () {
		var b = document.getElementsByTagName("body")[0];

		var o = document.createElement("div");
		o.setAttribute('id', '_mt_fs_div');

		b.appendChild(o);

		this.showBanner(o);

		if (typeof(this.ad.bplayer_url) == 'undefined')
			return;

		o = document.createElement("div");
		o.setAttribute('id', '_mt_fs_div_big');

		b.appendChild(o);

		this.prepareBigPlayer(o);
	}

	this.showBanner = function(ct) {
		var o, v;
		var align_left = this.position && this.position == "left";
		this.ad.trace = false;

		v = 'clickTAG=';
		v += escape(this.ad.click_url + '&ul=' + this.location);
		v += '&fileURL=' + escape(this.ad.video_url);
		v += '&vendorLink=' + escape(this.params.info_url);
		v += '&statScript=' + escape("javascript:_mtfs_ev('{event}')");
		v += '&infinite=false&fullScreen=false';

		for (var k in this.ad.flash_vars) {
			v += "&"+k+"="+this.ad.flash_vars[k];
		}

		v += '&autoPlay=true';
		v += "&userAgent=" + escape(navigator.userAgent);

		if (this.ad.cwidth && this.ad.cwidth != "0")
			this.ad.width = this.ad.cwidth;
		if (this.ad.cheight && this.ad.cheight != "0")
			this.ad.height = this.ad.cheight;

		o = '<object id="_mtfs_flash_" width="' + this.ad.width + '"  height="' + this.ad.height + '" data="' + this.ad.player_url + '" type="application/x-shockwave-flash">';
		o += '<param name="movie" value="' + this.ad.player_url + '"/>';
		o += '<param name="quality" value="high"/>';
		o += '<param name="wmode" value="opaque"/>';
		o += '<param name="bgcolor" value="#ffffff"/>';
		o += '<param name="scale" value="exactfit"/>';
		o += '<param name="swliveconnect" value="true"/>';
		o += '<param name="allowscriptaccess" value="always"/>';
		o += '<param name="allowFullScreen" value="true"/>';
		o += '<param name="flashvars" value=\'' + v + '\'/>';
		o += '</object>';

		ct.innerHTML = o;
		ct.style.width = this.ad.width + 'px';
		ct.style.height = this.ad.height + 'px';

		ct.style.display = 'block';
		ct.style.margin = '10px';
		ct.style.padding = '0px';

		ct.style.position = 'fixed';
		ct.style.bottom = '0px';

		if (align_left)
			ct.style.left = '0px';
		else
			ct.style.left = (this.getPageWidth() - this.ad.width - 40) + 'px';

		ct.style.zIndex = 99999;
	}

	this.prepareBigPlayer = function(ct) {
		var o, v;
		var align_left = this.position && this.position == "left";
		this.ad.trace = false;

		v = 'clickTAG=';
		v += escape(this.ad.click_url + '&ul=' + this.location);
		v += '&fileURL=' + escape(this.ad.bvideo_url);
		v += '&vendorLink=' + escape(this.params.info_url);
		v += '&statScript=' + escape("javascript:_mtfs_ev('{event}')");
		v += '&infinite=false&fullScreen=false';

		for (var k in this.ad.flash_vars) {
			v += "&"+k+"="+this.ad.flash_vars[k];
		}

		v += '&autoPlay=true';
		v += "&userAgent=" + escape(navigator.userAgent);

		if (this.ad.cwidth && this.ad.cwidth != "0")
			this.ad.width = this.ad.cwidth;
		if (this.ad.cheight && this.ad.cheight != "0")
			this.ad.height = this.ad.cheight;

		o = '<object id="_mtfs_flash_big_" width="' + this.ad.bwidth + '"  height="' + this.ad.bheight + '" data="' + this.ad.bplayer_url + '" type="application/x-shockwave-flash">';
		o += '<param name="movie" value="' + this.ad.bplayer_url + '"/>';
		o += '<param name="quality" value="high"/>';
		o += '<param name="wmode" value="opaque"/>';
		o += '<param name="bgcolor" value="#ffffff"/>';
		o += '<param name="scale" value="exactfit"/>';
		o += '<param name="swliveconnect" value="true"/>';
		o += '<param name="allowscriptaccess" value="always"/>';
		o += '<param name="allowFullScreen" value="true"/>';
		o += '<param name="flashvars" value=\'' + v + '\'/>';
		o += '</object>';

		ct.innerHTML = o;
		ct.style.width = this.ad.bwidth + 'px';
		ct.style.height = this.ad.bheight + 'px';

		ct.style.display = 'none';
		ct.style.margin = '10px';
		ct.style.padding = '0px';

		ct.style.position = 'fixed';
		ct.style.bottom = '0px';

		if (align_left)
			ct.style.left = '0px';
		else
			ct.style.left = (this.getPageWidth() - this.ad.bwidth - 40) + 'px';

		ct.style.zIndex = 99999;
    }

	this.expandBanner = function(ad) {
		var bd = document.getElementById('_mt_fs_div_big');
		if (! bd)
			return;
		var bo = document.getElementById('_mtfs_flash_big_');
		if (! bo)
			return;

		this.hideBanner(ad);

		bd.style.display = 'block';
		bo.callback = 'play';
	}

	this.hideBanner = function(ad) {
		var body = document.getElementsByTagName('body')[0];
		if (! body)
			return;

		var ename = ad.expanded ? '_mt_fs_div_big' : '_mt_fs_div';
		var o = document.getElementById(ename);
		if (! o)
			return;
		body.removeChild(o);

		o.style.display = 'none';
    }

	this.cacheFile = function(u) {
		(new Image(10, 10)).src = u.replace("{rid}", Math.random()); 
	}

	this.getPosY = function(o) {
		var y = 0;
		if (o.offsetParent) {
			while (o) {
				y += o.offsetTop;
				o = o.offsetParent;
			}
		} else {
			if (o.y) {
				y = o.y;
			}
		}
		return y;
	}

	this.getPageHeight = function () {
		var y;
		if (typeof(window.innerHeight) == 'number') {
			y = window.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			y = document.documentElement.clientHeight;
		} else if (document.body.clientHeight) {
			y = document.body.clientHeight;
		}
		return y;
	}

	this.getPageWidth = function () {
		var y;
		if (typeof(window.innerWidth) == 'number') {
			y = window.innerWidth;
		} else if (document.documentElement && document.documentElement.clientWidth) {
			y = document.documentElement.clientWidth;
		} else if (document.body.clientWidth) {
			y = document.body.clientWidth;
		}
		return y;
	}

	this.getScrollY = function() {
		var y = 0;
		if (typeof(window.pageYOffset) == 'number') {
			y = window.pageYOffset;
		} else if (document.body && document.body.scrollTop) {
			y = document.body.scrollTop;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			y = document.documentElement.scrollTop;
		}
		return y;
	}
}

