function objTp() {
	this.isAuth = false;
	this.isCheck = false;

	this.tpAuthExpTime = 0;
	this.tpSidExpTime = 0;
	this.tpRemCheckExpTime = 0;
	this.tpCookieSidName = '';
	this.tpCookieAuthName = 'TP_AUTH';
	this.tpCookieUserName = 'TP_USER';
	this.tpCookieRemCheckName = 'tp_auth_check';
	this.cookieHost = '';

	this.urlCheckAuth = '/travelpassport.php';
	this.urlTpCheckAuth = '';

	this.userId = 0;
	this.userData = {};
	this.sid = '';

	this.funcEx = {};

	this.init = function(){
		this.sid = '';
		this.userId = 0;
		this.userData = {};
		this.isAuth = false;
		this.isCheck = false;
		this.funcEx = {};
		this.funcEx.oncheck = [];

		this.tpAuthExpTime = TP_AUTH_EXPIRE_TIME;
		this.tpSidExpTime = TP_SID_EXPIRE_TIME;
		this.tpRemCheckExpTime = TP_AUTH_EXPIRE_TIME;
		this.tpCookieSidName = TP_SID_COOKIE_NAME;
		this.cookieHost = COOKIE_HOST;

		this.urlTpCheckAuth = 'http://'+TP_HOST+'/auth_check.php?user_data=1';
	}

	this._checkAuth = function(){
		this.isCheck = false;
		this.sid = jQuery.cookie(this.tpCookieSidName);
		if(this.sid!==null && this.sid.length>0){
			var auth = jQuery.cookie(this.tpCookieAuthName);
			if (auth!==null && auth.length>0) {
				var checkDate = new Date(auth);
				var currentDate = new Date();
				if (Math.round((currentDate.getTime()-checkDate.getTime())/1000)>this.tpAuthExpTime) {
					var objTmp1 = this;
					jQuery.getJSON(this.urlCheckAuth, {rnd: Math.random()}, function(data){
						objTmp1._afterCheck(data);
						objTmp1._oncheck();
					});
				} else {
					this._oncheck();
				}
			} else {
				var objTmp2 = this;
				jQuery.getJSON(this.urlCheckAuth, {rnd: Math.random()}, function(data){
					objTmp2._afterCheck(data);
					objTmp2._oncheck();
				});
			}
		}else{
			this._logout();
			if (!jQuery.cookie(this.tpCookieRemCheckName)) {
				var dateCookie = new Date();
				jQuery.cookie(this.tpCookieRemCheckName, '1', {
					expires: new Date(dateCookie.getTime()+this.tpRemCheckExpTime*1000),
					path: '/',
					domain: this.cookieHost
				});
				var objTmp3 = this;
				jQuery.getScript(this.urlTpCheckAuth, function(){
					objTmp3._oncheck();
				});
			} else {
				this._oncheck();
			}
		}
	}

	this._afterCheck = function(data){
		if (data.auth==1) {
			var checkDate = new Date();
			var dateExp = new Date(checkDate.getTime()+this.tpAuthExpTime*1000);
			jQuery.cookie(this.tpCookieAuthName, checkDate.toGMTString(), { expires: dateExp, path: '/', domain: this.cookieHost });
			if ((typeof(data.userData)!="undefined")&&(data.userData.length>0)) {
				jQuery.cookie(this.tpCookieUserName, data.userData, { expires: dateExp, path: '/', domain: this.cookieHost });
			}
		} else {
			this._logout();
			window.setTimeout(function(){ window.location.reload(); }, 1);
		}
	}

	this._oncheck = function(){
		this.isCheck = true;
		this.sid = jQuery.cookie(this.tpCookieSidName);
		if(this.sid!==null && this.sid.length>0){
			var auth = jQuery.cookie(this.tpCookieAuthName);
			if(auth!==null && auth.length>0){
				var checkDate = new Date(auth);
				var currentDate = new Date();
				if (Math.round((currentDate.getTime()-checkDate.getTime())/1000)>this.tpAuthExpTime) {
					this._logout();
					this._runExFunc('oncheck');
				} else {
					this.isAuth = true;
					var userData = jQuery.cookie(this.tpCookieUserName);
					if((userData!==null)&&(userData.length>0)){
						userData = userData.split('%|%');
						this.userId = userData[0];
						this.userData.name = userData[1] + ' ' + userData[2];
					}
					this._runExFunc('oncheck');
				}
			}else{
				this._logout();
				this._runExFunc('oncheck');
			}
		}else{
			this._logout();
			this._runExFunc('oncheck');
		}
	}

	this._runExFunc = function(listener){
		if(this.funcEx[listener].length>0){
			for(i=0;i<this.funcEx[listener].length;i++){
				this.funcEx[listener][i]();
			}
		}
	}

	this._logout = function(){
		this.sid = '';
		this.userId = 0;
		this.userData = {};
		this.isAuth = false;
		jQuery.cookie(this.tpCookieSidName, null, { path: '/', domain: this.cookieHost });
		jQuery.cookie(this.tpCookieAuthName, null, { path: '/', domain: this.cookieHost });
		jQuery.cookie(this.tpCookieUserName, null, { path: '/', domain: this.cookieHost });
	}

	this.checkAuth = function(){
		this._checkAuth();
	}

	this.addExFunc = function(listener, func){
		if(typeof(func) == 'function'){
			this.funcEx[listener][this.funcEx[listener].length] = func;
		}else{
			alert('Не верный тип функции "' + func + '"!');
		}
	}

	this.tpIsAuth = function(){
		return this.isAuth;
	}

	this.tpIsCheck = function(){
		return this.isCheck;
	}

	this.tpGetUserId = function(){
		return this.userId;
	}

	this.tpGetUserData = function(){
		return this.userData;
	}
}

clsTp = new objTp();
clsTp.init();

function remoteAuthDone(done, SID, SIDName, userData) {
	if (done===true) {
		cookie = jQuery.cookie(SIDName);
		if (cookie===false||cookie!=SID) {
			var dateCookie = new Date();
			var dateExp = new Date(dateCookie.getTime()+clsTp.tpAuthExpTime*1000);
			jQuery.cookie(SIDName, SID, { expires: new Date(dateCookie.getTime()+clsTp.tpSidExpTime*1000), path: '/', domain: clsTp.cookieHost });
			if (jQuery.cookie(SIDName)==SID) {
				var checkDate = new Date();
				jQuery.cookie(clsTp.tpCookieAuthName, checkDate.toGMTString(), { expires: dateExp, path: '/', domain: clsTp.cookieHost });
				if ((typeof(userData)!="undefined")&&(userData.length>0)) {
					jQuery.cookie(clsTp.tpCookieUserName, userData, { expires: dateExp, path: '/', domain: clsTp.cookieHost });
				}

				jQuery.getJSON(clsTp.urlCheckAuth, {rnd: Math.random()}, function(data){
					clsTp._afterCheck(data);
					window.setTimeout(function(){ window.location.reload(); }, 1);
				});
			}
		}
	}
}

clsTp.addExFunc('oncheck', function(){
	if (clsTp.tpIsAuth()) {
		userData = clsTp.tpGetUserData();
		jQuery("#tpEntryBlock, #login_block, #about_block").css("display", "none");
		jQuery("#tpUserBlock #userName").html(userData.name);
		jQuery("#tpUserBlock").css("display", "block");
	} else {
		jQuery("#tpUserBlock, #login_block, #about_block").css("display", "none");
		jQuery("#tpEntryBlock").css("display", "block");
	}
});

jQuery(document).ready(function(){
	clsTp.checkAuth();
});