var users = {

	menu:function(){

		
		$('div.menus a.more').each(function(){
			var id = $(this).attr('id');
			var w = $('#'+id+'s').width();
			var left = parseInt($(this).offset().left-$('#menubox').offset().left)+10;
			if(left+w>$('#menubox').width()){
				left -= w - parseInt($(this).parent().width());
			}
			$('#'+id+'s').css({'left':left+'px','top':(parseInt($(this).offset().top + $(this).parent().height()-$('#midData').offset().top))+'px'});		
		});	
	

		
		$('div.menus a.more').bind('mouseover',function(){			
			if($(this).attr('id')!='searchTop'){
				if($('#searchTops').css('display')!='none'){$('#searchTops').hide();users.unsetSA();}
				var id = $(this).attr('id');				
				if(users.sm_menuName.length>0 && users.sm_menuName!=id){
					$('#' + users.sm_menuName + 's').hide();	
				}				
				users.sm_menuName = id;				
				users.sm_menuDD(id,1);				
			}
		});		
		
			
		$('#searchTop').bind('click',function(){
				$('#searchTops').show();
				$('#qsearch').focus();
				users.setSA('qsearch','autoAS');
				$('#qsearch').bind('blur',users.unsetSA);
		});
		
		$('div.menus a.more').bind('mouseout',function(){
				var id = $(this).attr('id');
				if(id!='searchTop'){
					setTimeout(function(){users.sm_menuDD(id,0);},100);
				}				
		});		
		$('div.menus div.t_menu').bind('mouseover',function(){
				var id = $(this).attr('id').substr(0,$(this).attr('id').length-1);
				if(id!='searchTop'){
					users.sm_menuDD(id,1);	
				}
		});
		$('div.menus div.t_menu').bind('mouseout',function(){
				var id = $(this).attr('id').substr(0,$(this).attr('id').length-1);
				if(id!='searchTop'){
					setTimeout(function(){users.sm_menuDD(id,0);},100);	
				}
		});
		
		
		$('div.menubox a.more').each(function(){
			var id = $(this).attr('id');
			var w = $('#'+id+'s').width();
			var left = parseInt($(this).offset().left-$('#menubox').offset().left)+10;
			var className = 'radius7tr';
			if(left+w>$('#menubox').width()){
				left -= w - parseInt($(this).parent().width());
				className = 'radius7tl';
			}			
			$('#'+id+'s,#'+id+'s a.b').addClass(className);
			$('#'+id+'s').css({'left':left+'px','top':(parseInt($(this).offset().top + $(this).parent().height()-$('#midData').offset().top))+'px'});		
		});		
				
		$('div.menubox a.more').bind('mouseover',function(){
				if($('#searchTops').css('display')!='none'){$('#searchTops').hide();users.unsetSA();}
				var id = $(this).attr('id');
				
				if(users.menuName.length>0 && users.menuName!=id){
					$('#' + users.menuName).removeClass('mores radius0bl radius0br');					
					$('#' + users.menuName + 's').hide();	
				}				
				users.menuName = id;
				
				users.menuDD(id,1);
				users.calcWidth(id+'s');				
		});
		$('div.menubox a.more').bind('mouseout',function(){				
				var id = $(this).attr('id');
				setTimeout(function(){users.menuDD(id,0);},100);
		});		
		$('div.menubox div.t_menu').bind('mouseover',function(){		
				var id = $(this).attr('id').substr(0,$(this).attr('id').length-1);
				users.menuDD(id,1);
		});
		$('div.menubox div.t_menu').bind('mouseout',function(){		
				var id = $(this).attr('id').substr(0,$(this).attr('id').length-1);
				setTimeout(function(){users.menuDD(id,0);},100);
		});	

	},
	
	menuActiv:0,
	menuName:'',
	menuDD:function(id,c){	
		if(parseInt(c)==1){
			$('#'+id).addClass('mores radius0bl radius0br');					
			$('#'+id+'s').show();
			users.menuActiv += 1;
		}else{
			users.menuActiv -= 1;			
			if(users.menuActiv==0){
				$('#'+id).removeClass('mores radius0bl radius0br');					
				$('#'+id+'s').hide();
			}
		}
	},
	sm_menuActiv:0,
	sm_menuName:'',
	sm_menuDD:function(id,c){	
		if(parseInt(c)==1){
			$('#'+id+'s').show();
			users.sm_menuActiv += 1;
		}else{
			users.sm_menuActiv -= 1;			
			if(users.sm_menuActiv==0){
				$('#'+id+'s').hide();
			}
		}
	},
	
	slideMenu:function(id){main.slideMenu(id);},
	
	calcExist:{},
	calcWidth:function(id){		
		var w,wA,l,t;		
		if(typeof users.calcExist[id] == 'undefined'){		
			w = $('#'+id).width();		
			$('#'+id+' span.txt').each(function(){			
				wA = $(this).width();				
				if(wA>240){
					t = $(this).html();
					l = t.length;
					while(wA>240){
						$(this).html($(this).html().substr(0,l-1)+'...');
						l -= 1;
						wA = $(this).width();
					}
					//Tooltip, ja nepietiek vietas => $(this).parent().attr({'title':t});main.toolz($(this).parent());
				}
			});
			users.calcExist[id] = 1;
		}
	},
	
	set:function(){
		users.menu();		
	},
	
	url_txt:'',
	name_txt:'',	
	editFavUrl:function(txt,pid){

		var url = '';
		if(pid==0){
			url = document.location.href;
			var name = document.title;
		
			var hash = document.location.hash;
				hash = hash.substr(1);
			if(hash.length!=0){	
				if(hash.substr(0,1)!='/'){hash = '/'+hash;}
				url = main.domain + hash;
			}
			
			url = '&url='+encodeURIComponent(url)+'&name='+encodeURIComponent(name);
		}else{
			url = '&pid='+pid;
		}

		popbox.box('/popbox/adress/page/?tab=1'+url,1,0,0,1,txt);
	
		return false;
	},
	
	statUpdate:function(i,a,t){
		$('#stattops_').html(i);
		i = parseInt(i)?parseInt(i):'';
		a = parseInt(a)?parseInt(a):0;			
		if(a!=0){a = ' '+i+'['+a+']';}else{a = '';}
		a = t+a;
		$('#stattops').attr({"title":a});
		main.toolz('#stattops');
	},
	
	/* Serach */
	
	setSA:function(f,r){	
		$('#'+f).bind('keydown',{'f':f,'r':r},users.setSAData);
	},
	
	unsetSA:function(){	
		setTimeout(function(){
			$('#qsearch').unbind('keydown',users.setSAData).unbind('blur',users.unsetSA);
			$('#searchTops').hide();
		},300);
	},
	setSAData:function(e){
		f = e.data.f;
		r = e.data.r;
		setTimeout(function(){
			var input = $('#'+f);			
			var value = $.trim(input.val());				
			if(value.length>0){
				input.addClass('loading');
				$.get(main.domain+'/search/small.php',{'search':1,'q':value},function(data){
					$('#'+r).html(data);								
					input.removeClass('loading');
				});						
			}else{
				$('#'+r).html('');
			}
		},1000);		
	},
	
	
	inv_opt:function(tab,sub,pg,pid,opt){	
	
		switch(opt){
				case 'delete':
					main.shadow('inv'+pid);
					$('#inv'+pid).load(main.domain+'/inv/inv/?tab='+tab+'&pg='+pg+'&sub='+sub+'&pid='+pid+'&js&'+opt,function(){main.shadow('inv'+pid,1);});		
				break;
				case 'accept':
					main.shadow('inv'+pid);					
					var accept = {};
					accept[pid] = pid;					
					$.post(main.domain+'/inv/inv/?tab='+tab+'&pg='+pg+'&sub='+sub+'&pid='+pid+'&js',{'accept':accept},function(data){
						main.shadow('inv'+pid,1);
						$('#inv'+pid).html(data);	
					});					
				break;

			}
	
		return false;
	
	},
	invCount:function(i,ia,o,oa){
		$('#invIN_friendship').html(i);
		$('#invIN_all').html(ia);
		$('#invOUT_friendship').html(o);
		$('#invOUT_all').html(oa);	
	},
	
	optChange:function(url,id){
		main.shadow(id);
		$('#'+id).load(main.domain+url,function(){main.toolz('#'+id+' a');main.shadow(id,1);});	
		return false;
	},
	
	topVoteResult:function(id){	
		$('#voteResultChange').html($('#'+id).html());
		main.toolz('#voteResultChange a');
	},
	
	quiz:function(url,c){	
		if(parseInt(c)==1){
			var a = 0;
			$('input[name="answers"]').each(function(){
				if($(this).attr('checked')==true){
					a = $(this).val();
				}
			});
			if(a!=0){
				users.optChange(url+'&a='+a,'quizBegins');
			}
		}else{
			$('#answer_ready').show();		
		}	
		return false;
	},
	
	
	gifts:function(id,c){
	
		main.shadow('gift_preview');
		$.get(main.domain+'/profile/one_gift.php',{'pid':id,'edit':c},function(data){main.shadow('gift_preview',1);$('#gift_preview').html(data);})
		
		return false;
	},
	
	gifts_p:function(id,c){
	
		main.shadow('midFull');
		$.get(main.domain+'/profile/page/',{'tab':3,'pid':id,'edit':c},function(data){main.shadow('midFull',1);$('#midFull').html(data);})
		
		return false;
	},
	
	users_opt:0,
	users_opt_on:0,
	
	users_opt_time:30,
	users_opt_data:{msg:0,stat:0,invis:0},
	
	users_option_c:function(){
		var w = $(window);
		$('#fixed_options').css({'top':w.height()+'px','width':(w.width()-10)+'px'});
	},
	
	u_opt_change:function(c){	
		switch(c){
			case 'msg':
				users.users_opt_data.msg = users.users_opt_data.msg?0:1;
			break;
			case 'stat':
				users.users_opt_data.stat = users.users_opt_data.stat?0:1;		
			break;
			case 'invis':
				users.users_opt_data.invis = users.users_opt_data.invis?0:1;
			break;
		}
		$.get(main.domain+'/profile/options.php',users.users_opt_data);
	},
	
	users_opt_msg:0,
	users_opt_msgID:0,
	users_opt_stat:0,
	users_opt_statID:0,
	users_opt_title:0,
	
	count_e:{stat:0,msg:0},
	
	u_opt_changes:function(msg,stat){
		msg = parseInt(msg);
		stat = parseInt(stat);
		var play = 0;
		
		if(msg>0 && users.users_opt_msg==0){
			users.count_e.msg = msg;
			play = 1;
			$('#fixed_options').append(
				$('<div/>').attr({'id':'msg_options','class':'my_options'})
				.html(
					$('<div/>').attr({'class':'in_options radius3tr radius3tl'})
					.html(
						$('<div/>').attr({'class':'bg_options radius3tr radius3tl'})
						.html(
							$('<div/>').attr({'class':'msg_options'}).html(users.u_opt_name.msgin + ' [<span>' + msg + '</span>]')
						)
					)
				)
			)
			users.users_opt_msg = 1;
			$('#msg_options div.bg_options').click(function(){			
				window.open(main.domain+'/msg/','_self');
			});
			
			var pos_msg = -30;
			document.title = users.u_opt_name.msgin + ' [' + msg + ']';
			users.users_opt_msgID = setInterval(function(){
				if(pos_msg==-30){
					pos_msg = 0;
				}else{
					pos_msg = -30;
				}
				$('#msg_options div.bg_options').css('background-position','left '+pos_msg+'px');
			},1000);
			
		}else if(msg>0){		
			if(users.count_e.msg < msg){
				users.count_e.msg = msg;
				play = 1;
			}			
			$('#msg_options div.msg_options').html(users.u_opt_name.msgin + ' [<span>' + msg + '</span>]');
			document.title = users.u_opt_name.msgin + ' [' + msg + ']';
		
		}else if(users.users_opt_msg==1){
			users.count_e.msg = 0;
			$('#msg_options').remove();
			users.users_opt_msg = 0;
			clearInterval(users.users_opt_msgID);
			document.title = users.doc_title;
		}
		
		if(stat>0 && users.users_opt_stat==0){
			users.count_e.stat = stat;
			play = 1;
			$('#fixed_options').append(
				$('<div/>').attr({'id':'stat_options','class':'my_options'})
				.html(
					$('<div/>').attr({'class':'in_options radius3tr radius3tl'})
					.html(
						$('<div/>').attr({'class':'bg_options radius3tr radius3tl'})
						.html(
							$('<div/>').attr({'class':'stat_options'}).html(users.u_opt_name.statin + ' [<span>' + stat + '</span>]')
						)
					)
				)
			)
			users.users_opt_stat = 1;
			$('#stat_options div.bg_options').click(function(){			
				window.open(main.domain+'/stat/','_self');
			});
			
			var pos_stat = -30;
			document.title = users.u_opt_name.statin + ' [' + stat + ']';
			users.users_opt_statID = setInterval(function(){
				if(pos_stat==-30){
					pos_stat = 0;
				}else{
					pos_stat = -30;					
				}
				$('#stat_options div.bg_options').css('background-position','left '+pos_stat+'px');
			},1000);
			
		}else if(stat>0){
			if(users.count_e.stat < stat){
				users.count_e.stat = stat;
				play = 1;
			}	
			$('#stat_options div.stat_options').html(users.u_opt_name.statin + ' [<span>' + stat + '</span>]');
			document.title = users.u_opt_name.statin + ' [' + stat + ']';
		
		}else if(users.users_opt_stat==1){
			users.count_e.stat = 0;
			$('#stat_options').remove();
			users.users_opt_stat = 0;
			clearInterval(users.users_opt_statID);
			document.title = users.doc_title;
		}
		
		if(play==1){

		}
	},
	
	u_opt_name:{},
	doc_title:'',
	
	users_option:function(t,msg,stat,invis){
		users.u_opt_name = t;
		users.doc_title = document.title;

		$('body').append(
			$('<div/>').attr({'id':'fixed_options'}).html(
				$('<div/>').attr({'id':'my_options','class':'my_options'})
				.html(
					$('<div/>').attr({'class':'in_options radius3tr radius3tl'})
					.html(
						$('<div/>').attr({'class':'bg_options radius3tr radius3tl'})
						.html(
							$('<div/>').attr({'class':'txt_options'}).html($('<img/>').attr({'src':main.domain+'/img/dels.png'}))
							.append(t.options)
						)
					)
				)
			).append($('<div/>').attr({'id':'my_options_load','class':'hidden'}).html(''))
		);

		users.users_option_c();
		$(window).bind('resize',users.users_option_c);
		
		var set_h = 0;
		var units = 'px';
		
		users.users_opt_data.msg = parseInt(msg);
		users.users_opt_data.stat = parseInt(stat);
		users.users_opt_data.invis = parseInt(invis);
		
		$('#my_options div.bg_options').click(function(){
			if(users.users_opt==0){			
				$('#my_options div.in_options').append(					
					$('<div/>').attr({'class':'c_options hidden','id':'set_options'}).html(
						$('<div/>').attr({'class':'load_options'}).html($('<img/>').attr({'border':'0','src':main.domain+'/img/load.gif'}))
					)
				);
				users.users_opt = 1;
				set_h = $('#set_options').height();				
				$('#set_options').load(main.domain+'/profile/options.php?options',function(){
					set_h = $('#set_options').height();
					if(users.users_opt_on==1){users.users_option_fly(set_h,1);}
				});

			}
			
			users.users_option_fly(set_h,0);
		});
		
		setInterval(function(){
			if(users.users_opt_data.msg==1 || users.users_opt_data.stat==1){
				$('#my_options_load').load(main.domain+'/profile/options.php?info');
			}			
		},1000 * users.users_opt_time);
		
	},
	
	users_option_fly:function(set_h,c){
	
			if(users.users_opt_on==0 || c==1){
				users.users_opt_on = 1;
				$('#set_options').show();
				$('#my_options').animate({'top':-30-set_h},function(){$('#my_options div.txt_options img').css('visibility','visible');});
			}else{
				users.users_opt_on = 0;				
				$('#my_options').animate({'top':-30},function(){$('#set_options').hide();$('#my_options div.txt_options img').css('visibility','hidden');});
			}
	
	}

}

var inv_users = {

	binded:'',
	txt_txt:'',
	inv_email_txt:'',
	submited:0,
	mail:/^[_a-zA-Z0-9-]+((\.|\+)[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,6})$/,

	check:function(){
	
		$('#_form input,#_form textarea').bind('change keyup keydown',function(){
			var id = $(this).attr('id');
			if(inv_users.submited==1){inv_users.check_fields(id);}
			if(inv_users.binded.search('#' + id + '#') == -1){
				$('#'+id).bind('change keyup keydown',function(){setTimeout('inv_users.check_fields("'+id+'")',100);});
				inv_users.binded = inv_users.binded + '#' + id + '#';
			}
		});

	},
	
	check_fields:function(field){
		if(!field){field='';}
		var er_count = 0;
		
		if(field=='inv_email' || field==''){
			var inv_email = $.trim($('#inv_email').val());			
			if(inv_email.length==0){
				er_count = er_count + 1;
				$('#inv_email_err').addClass('red');
				$('#inv_email_err span.infoicon').show().attr({'title':inv_users.inv_email_txt});
				main.toolz('#inv_email_err span.infoicon');
			}else{
				var count_email = 0;
				inv_email = inv_email.split(',');				
				for(var i=0;i<inv_email.length;i++){
					if(inv_users.mail.test(inv_email[i])){
					count_email += 1;
					}
				}
				if(count_email>0){
					$('#inv_email_err').removeClass('red');
					$('#inv_email_err span.infoicon').hide();
				}else{
					er_count = er_count + 1;
					$('#inv_email_err').addClass('red');
					$('#inv_email_err span.infoicon').show().attr({'title':inv_users.inv_email_txt});
					main.toolz('#inv_email_err span.infoicon');
				}
			}
		}
	
		if(field=='' && er_count==0){	

			var btn_text = 'Wait...';
			if(typeof lang[btn_text] != 'undefined'){
				btn_text = lang[btn_text];
			}	
			$('#send').val(btn_text);			
			return true;	
		
		}else if(field==''){
			inv_users.submited = 1;		
		}
		return false;
	}	
}

