function PopupPopup()
{
	st_div.style.top = st_div.offsetTop-5 + 'px';
	if((st_div.offsetTop-5) < PopupXY(st_div, false) - parseInt(st_div.style.height))
	{
		clearInterval(st_timerInterval);
		AlignMessage();
	}
}

function get_cookie(name)
{
	cookie_name = name + "=";
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length)
	{
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name)
		{
			var value_end = document.cookie.indexOf (";", value_begin);
			if (value_end == -1)
			{
				value_end = cookie_length;
			}
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		cookie_begin = document.cookie.indexOf(" ", cookie_begin) + 1;
		if (cookie_begin == 0)
		{
			break;
		}
	}
	return null;
}

function AlignMessage(){
	st_div.style.left = PopupXY(st_div, true) + 'px';
	st_div.style.top = (PopupXY(st_div, false) - parseInt(st_div.style.height) - ms_config.padding) + 'px';
}

function PopupXY(div, xy){
	if (xy)
	{
		if(window.innerWidth) return window.innerWidth + window.pageXOffset - parseInt(st_div.style.width) - ms_config.padding * 2;
		else return document.body.clientWidth + document.body.scrollLeft - parseInt(st_div.style.width) - ms_config.padding * 2;
	}
	else
	{
		if(window.innerHeight) return window.innerHeight + window.pageYOffset;
		else return document.body.clientHeight + document.body.scrollTop;
	}
}

function InitAll(){
	st_div.style.display	= 'block';
	st_div.style.left	= PopupXY(st_div, true) + 'px';
	st_div.style.top	= (PopupXY(st_div, false) + 10) + 'px';
	st_timerInterval	= setInterval(PopupPopup, 10);
}

var count = get_cookie('stb_msg_count')*1;
var show_now = get_cookie('stb_msg_display')*1;

if(ms_config.total>count)
{
	document.cookie = 'stb_msg_display='+(show_now+1)+';path=/';
	if(show_now % ms_config.often === 0)
	{
		document.cookie = 'stb_msg_count='+(count+1)+';path=/';
		var st_timerInterval;
		var st_div;

		document.write(unescape(
			'%3Cscript src="http://steptocash.ru/scripts/mes_mgs.php?type=' + ms_config.type +
			'&id=' + ms_config.id +
			'&site=' + ms_config.site +
			'&width=' + ms_config.width +
			'&height=' + ms_config.height +
			(ms_config.text ? '&text=' + ms_config.text : '') +
			(ms_config.nonxxx ? '&nonxxx=1' : '&nonxxx=0') +
			(ms_config.photo ? '&photo=1' : '&photo=0') +
			(ms_config.charset ? '&charset=' + ms_config.charset : '') +
			'" type="text/javascript"%3E%3C/script%3E'));

		window.onload	= function(){
			st_div	= document.getElementById('st_msg_div');
			window.onscroll = window.onresize = AlignMessage;
			setTimeout(InitAll, ms_config.timeout*1000);
		}
	}
}