var Global = new Class({
	initialize: function(){
		this.call_to_database=1;
		this.call_to_convert_icon=2;
		this.call_to_cookie =0.20;
		this.last_date;
		this.remote_path = "http://filer.tusa.co.il/uploads/";
		this.domain="http://"+document.domain;
		this.member_id = 'null';
		this.is_blocked = '?';
		this.tm_user = new Array();
		
	   	window.addEvent('domready', function(){
			global.setTopMenu();
			global.UpdateStatusPics();
	   	});
	},
		
     setTopMenu: function(){
		if($('my_tusa') && $('login_menu')) return;		
	
		var menu_obj = {
			0: {'title': 'Моя TUSA', 'link': this.domain + '/profile/main.html'},
			1: {'title': 'Мой профайл', 'link': this.domain + '/profile/edit.html'},
		    2: {'title': 'Мои друзья', 'link': this.domain + '/friends/' + this.member_id['user_id'] + '-show.html'},
			3: {'title': 'Моя почта', 'link': this.domain + '/inmail/inbox.html'},
			4: {'title': 'Мой блог', 'link': this.domain + '/blog/panel.html'},
			5: {'title': 'Мой фотоальбом', 'link': this.domain + '/photo/edit.html'},
			6: {'title': 'Моё видео', 'link': this.domain + '/video/edit.html'},
			7: {'title': 'Мои уведомления', 'link': this.domain + '/subscribe/edit.html'},
			8: {'title': 'Стили страниц', 'link': this.domain + '/style/edit.html'},
			9: {'title': 'Гостевая книга', 'link': this.domain + '/user/' + this.member_id['user_id'] + '-guestbook.html'}
		};
		
		try{ 
			global.tm_user['my_tusa'] = new tusaMenu({'new_id': 'my_tusa', 'id_el': 'login_menu', 'data': menu_obj, 'width': 120}); 
		}
		catch(err){}
	},
		
	openPopUp: function(url,pop_width,pop_height){
	    newWin = window.open(url,'','width='+pop_width+',height='+pop_height+',resizable=no,scrollbars=no'); 
	    newWin.moveTo((screen.width-pop_width)/2,(screen.height-pop_height)/2);
        newWin.focus();
	},
	
	showBanners: function(){ 
		new Ajax('/public/php/banner.php?banner_id=top_banner&tmp='+$time(),{method:'get',evalScripts: true,update:'top_banner'}).request();
		//new Ajax('/public/php/banner.php?banner_id=left_banner&tmp='+$time(),{method:'get',evalScripts: true,update:'left_banner'}).request();
		//new Ajax('/public/php/banner.php?banner_id=bottom_banner&tmp='+$time(),{method:'get',evalScripts: true,update:'bottom_banner'}).request();
	
	},
	
	setUserMenu: function(obj){
		if(obj.id.contains('tm_')) return;
		var u_ex = obj.src.split('/');
		var user_id = u_ex[u_ex.length - 3] + u_ex[u_ex.length - 2];
		
		var menu_obj = {
			0: {'main': obj.alt},
			1: {'title': 'Написать сообщение',	'link': global.domain + '/inmail/sendwin/' + user_id + '.html', 'onclick': 'mess.SendWindow(' + user_id + ', false)'},
			2: {'title': 'Фотоальбом',	'link': global.domain + '/photo/' + user_id + '-album.html'},
			3: {'title': 'Показать переписку',	'link': global.domain + '/inmail/correspondence/' + user_id + '.html'},
			4: {'title': 'Гостевая книга', 'link': global.domain + '/user/' + user_id + '-guestbook.html'},
			5: {'title': 'Друзья', 'link': global.domain + '/friends/' + user_id + '-show.html'}
		};
		
		var el_id = 'tm_' + $time();
		var new_id = 'new_' + el_id;
		obj.id = el_id;
		
		global.tm_user[new_id] = new tusaMenu({
			'new_id': new_id, 'id_el': el_id, 'data': menu_obj, 'width': 150, 
			'vertical': 'center', 'horizontal': 'center'
		});
		global.tm_user[new_id].open();
	},
		
	showLogin: function(exe_func, page){
		new Ajax('/ajax.php', {
			method: 'get', data: {'do': 'login'},
			evalScripts: true,
			onComplete: function(str){
				if(page == 'upage') var str_logo = '<a href="/sf/index.html" id="logo">tusa.co.il</a><div id="header-img"></div>';
				else var str_logo = '<a href="/sf/index.html" id="logo">tusa.co.il<b></b></a>';
				$('header').innerHTML = str_logo + str;
				
				global.getUserData();
				
				if(exe_func != "") {
					try{eval(exe_func);}catch(err){}
				}
    		}
		}).request();
	},
	
	getUserData: function(){
		try{
			var obj = $('uavatar');
			if(obj){
				var arr = new Array();
				arr['user_id'] = obj.lang;
				arr['foto'] = obj.src;
				arr['name'] = obj.title;
				arr['path'] = obj.src.replace(/avatar.+([a-zA-Z])/g, '');
			}
			else var arr = false;
		}
		catch(err){ var arr = false; }
		this.member_id = arr;
		
		return arr;
	},
	
	UpdateStatusPics: function(){
		var arr = $$('.status_class');
       	var time = $time();	

       	for (var i=0;i<arr.length;i++) {
	    	arr[i].src+="?"+time;
		} 
	},
	
	isBlocked: function(user_id, check_id){
		if(this.is_blocked != '?') return this.is_blocked;
		else{
			new Ajax('/ajax.php', {
				method: 'post', 
				data: {'do': 'is_blocked', 'owner_id': user_id, 'user_id': check_id},
				onComplete: function(str){
					if(str.toString() == 'true') var is_blocked = true;
					else var is_blocked = false;

					global.setBlocked(is_blocked);
	    		}
			}).request();
		}
	},
	
	setBlocked: function(param){
		this.is_blocked = param;
	},
	
	blockUser: function(uid){
		var alert = new Alert();
		if(alert.confirm('Вы уверены что хотите добавить этого пользователя в черный список?')){
			new Ajax('/ajax.php?do=friends&op=block', {
				data: {user_id: uid}, method: 'post'
			}).request();
		}
	},

	CheckCookieTime: function(){
		if(global.member_id['user_id']){
			this.last_date=Cookie.get('last_date_'+global.member_id['user_id']);
			var last_time=$time()-(this.call_to_database*60000);
			if(!this.last_date || this.last_date<last_time) {
				return true;
			}
			else return false;
		}
	},

	UpdateStatus: function(){
		return;
		if(this.CheckCookieTime()){
			var time=$time()-(this.call_to_convert_icon*60000);
			if(!this.last_date || this.last_date<time) var icon=1;
			else var icon=0;
		
			new Ajax(global.domain+"/ajax.php?do=online_status&op=online&icon="+icon+"&tmp="+$time(), {
		    		method: 'get',
		    		  	onComplete: function(){
		    	    	global.SetCookie('last_date_'+global.member_id['user_id'],$time(), null, "/");
				}}).request();
		}
		window.setTimeout('global.UpdateStatus();',this.call_to_cookie*60000);
	},
	
	ShowLoading: function(){
		$('loading-layer').setStyle('display', 'block');
	},

	HideLoading: function(){
		$('loading-layer').setStyle('display', 'none');
	},

	ShowLayer: function() {
		$('busy_layer').setStyles({
			'visibility': 'visible',
			'width': getScrollWidth() + "px",
			'height': getScrollHeight() + "px"
		});
	},

	HideLayer: function() {
		$('busy_layer').setStyles({
			'visibility': 'hidden',
			'width': "0px",
			'height': "0px"
		});
	},

	LoadJS: function(source, func){
		var id = 'js' + Math.round(100*Math.random());
		var element = new Element('script', {
			type: 'text/javascript',
			src: source,
			id: id
		}).set({
			load: function(){
				eval(func);
			},
			onreadystatechange: function(){
				if(element.readyState == 'complete' || element.readyState == 'loaded'){
					eval(func);
				}
			}
		}).inject(document.head);
		if(element.readyState) return true;
		else return false;
	},

	reloadCaptcha: function() {
		var rndval = new Date().getTime();

		$('dle-captcha').innerHTML = '<img src="/engine/modules/antibot.php?rndval=' + rndval + '" border="0" width="120" height="50"><br /><a onclick="global.reloadCaptcha(); return false;" href="#">обновить код</a>';
	},

	encodeVAR: function(url){
		url = url.toString();
		var regcheck = url.match(/[\x90-\xFF]/g);

		if (regcheck) {
			for (var i = 0; i < i.length; i++){
				url = url.replace(regcheck[i], '%u00' + (regcheck[i].charCodeAt(0) & 0xFF).toString(16).toUpperCase());
			}
		}

		return escape(url).replace(/\+/g, "%2B");
	},

	getToppos: function(obj){
		var top = obj.offsetTop;
		
		while( (obj = obj.offsetParent) != null ) {
			top += obj.offsetTop;
		}
	},
	
	getUpContainer: function(id){
		var post_box_top  = this.getToppos($(id));
		if (post_box_top) scroll(0, post_box_top);
	},

	SetCookie: function(name,value,expires,path,domain,secure){
		document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
	},
	
	explode: function( delimiter, string ) {
	    var emptyArray = { 0: '' };
	 
	    if ( arguments.length != 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined' ) return null;
	    if ( delimiter === '' || delimiter === false || delimiter === null ) return false;
	 
	    if ( 
	    	typeof delimiter == 'function' || typeof delimiter == 'object' || 
	    	typeof string == 'function' || typeof string == 'object' 
	    ) return emptyArray;
	 
	    if ( delimiter === true ) delimiter = '1';
	 
	    return string.toString().split ( delimiter.toString() );
	},
	
	$_GET: function (name) {
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  	var regexS = "[\\?&]"+name+"=([^&#]*)";
	  	var regex = new RegExp( regexS );
	  	var results = regex.exec( window.location.href );
	  	
	  	if( results == null ) return false;
	  	else return results[1];
	}
});

var global = new Global();