if ((typeof LOGINZA == "undefined") || !LOGINZA) {
	// инициализация объекта LOGINZA
    var LOGINZA = {
    	'loaded': false,
        'token_url': null,
        'selected_provider': null,
        'providers_set': null,
        'service_host': 'https://loginza.ru'
    };
}
// показать форму
LOGINZA.show = function () {
	// пред выбор провайдера
	LOGINZA.selected_provider = LOGINZA.getQueryStringValue(this, 'provider');
	// набор провайдеров в виджете
	LOGINZA.providers_set = LOGINZA.getQueryStringValue(this, 'providers_set');
	// получение token
	LOGINZA.token_url = LOGINZA.getQueryStringValue(this, 'token_url');

	if (!LOGINZA.loaded) {
		var cldDiv = document.createElement("div");
		cldDiv.setAttribute('id', 'loginza_auth_form');
		cldDiv.setAttribute('style', 'overflow: visible; z-index: 10000; background-color: transparent; position: fixed; top: 0px; display: block; text-align: center; left: 0px; height: 878px; width: 1247px; padding-top: 125px; background-image: url('+LOGINZA.service_host+'/img/widget/overlay.png);');
		cldDiv.innerHTML = '<div style="position:relative;display:inline;overflow:visible;"><img style="position:relative;left:348px;top:-332px;cursor:hand;" onclick="LOGINZA.close();" src="'+
			LOGINZA.service_host+'/img/widget/close.gif" width="7" height="7" alt="X" border="0"/></div>'+
			'<iframe id="loginza_main_ifr" width="359" height="350" scrolling="no" frameborder="0" src="javascript:\'<html><body style=background-color:transparent><h1>...</h1></body></html>\'"></iframe>';
		try {
			cldDiv.style.paddingTop = (window.innerHeight-350)/2 + 'px';
		} catch (e) {
			cldDiv.style.paddingTop = '100px';
		}
		cldDiv.style.paddingLeft = 0;
		cldDiv.style.height = '2000px';
		cldDiv.style.width = document.body.clientWidth + 50 + 'px';
		// создание контейнера для формы
		document.body.appendChild(cldDiv);
		// форма загружена
		LOGINZA.loaded = true;
	}
	if (!LOGINZA.token_url) {
		alert('Error token_url value!');
	} else {
		// загрузка формы
		document.getElementById('loginza_main_ifr').setAttribute('src', LOGINZA.service_host+'/api/widget.php?overlay=true&w='+document.body.clientWidth+'&token_url='+encodeURIComponent(LOGINZA.token_url)+'&provider='+encodeURIComponent(LOGINZA.selected_provider)+'&providers_set='+encodeURIComponent(LOGINZA.providers_set));
	}
	// показать форму
	document.getElementById('loginza_auth_form').style.display = '';
	return false;
}
LOGINZA.close = function () {
	document.getElementById('loginza_auth_form').style.display = 'none';
}
// изменение размеров окна браузера
LOGINZA.resize = function () {
	var frm = document.getElementById('loginza_auth_form');
	if (frm) {
		frm.style.width = document.body.clientWidth + 50 + 'px';
		try {
			frm.style.paddingTop = (window.innerHeight-350)/2 + 'px';
		} catch (e) {
			frm.style.paddingTop = '100px';
		}
	}
}
// получение параметра из ссылки
LOGINZA.getQueryStringValue = function (link, key) {
	var url_str = link.href;
    var match = null;
    var query_str = url_str.match(/^[^?]*(?:\?([^#]*))?(?:$|#.*$)/)[1]
    var _query_regex = new RegExp("([^=]+)=([^&]*)&?", "g");
    while ((match = _query_regex.exec(query_str)) != null)
    {
        if (decodeURIComponent(match[1]) == key) {
            return decodeURIComponent(match[2]);
        }
    }
    return null;
}
LOGINZA.findClass = function (str, node) {
	if(document.getElementsByClassName) return (node || document).getElementsByClassName(str);
	else {
		var node = node || document, list = node.getElementsByTagName('*'), length = list.length, Class = str.split(/\s+/), classes = Class.length, array = [], i, j, key;
		for(i = 0; i < length; i++) {
			key = true;
			for(j = 0; j < classes; j++) if(list[i].className.search('\\b' + Class[j] + '\\b') == -1) key = false;
			if(key) array.push(list[i]);
		}
		return array;
	}
}
LOGINZA.addEvent = function (obj, type, fn){
	if (obj.addEventListener){
	      obj.addEventListener( type, fn, false);
	} else if(obj.attachEvent) {
	      obj.attachEvent( "on"+type, fn );
	} else {
	      obj["on"+type] = fn;
	}
}
LOGINZA.init = function () {
	// обработчик на открытие формы
	if (document.getElementById('loginza')) {
		document.getElementById('loginza').onclick = LOGINZA.show;
	}
	var i, list = LOGINZA.findClass('loginza'), length = list.length;
	for(i = 0; i < length; i++) list[i].onclick = LOGINZA.show;
	// прочие обработчики
	LOGINZA.addEvent(window, 'resize', LOGINZA.resize);
	LOGINZA.addEvent(document, 'keydown', function(e) {
		e = e || window.event;
		if (e.keyCode == 27) {
			LOGINZA.close();
		}
		return true;
	});
}
LOGINZA.addEvent(window, 'load', LOGINZA.init);
