function perms_changed(id,val)
{
	if (val==3)	{ jQuery('#'+id).show(); } else	{ jQuery('#'+id).hide(); }
}
function OpenPopup(url,w,h)
{
	window.open(url, '', 'scrollbars=yes,resizable=yes,width='+w+',height='+h+',screenX=100,screenY=100');
	return(false);
}
function dsp(id,disp)
{
	d=document.getElementById(id);
	if (!d) return;
	d.style.display=disp;
}
function dspb(id,b)
{
	d=document.getElementById(id);
	if (!d) return;
	if (b) d.style.display='block';
	else d.style.display='none';
}
function dspi(id,b)
{
	d=document.getElementById(id);
	if (!d) return;
	if (b) d.style.display='inline';
	else d.style.display='none';
}
function dsp_swap(id)
{
	d=document.getElementById(id);
	if (!d) return;
	if (d.style.display=='none') d.style.display='block';
	else d.style.display='none';
}
function _getFormAction(e)
{
    for (var i = 0; i < e.attributes.length; i++) 
    {
    	var a = e.attributes[i];
        var k = a.nodeName;
        if (k == 'action') 	
	{
        	return(a.value);
       	}
    }
    return(false);
}
function _setFormAction(e,action)
{
    for (var i = 0; i < e.attributes.length; i++) 
    {
    	var a = e.attributes[i];
        var k = a.nodeName;
        if (k == 'action') 	
	{
		a.value=action;
        	return(true);
       	}
    }
    return(false);
}
function swap_ssl(c)
{
	/*var f=document.getElementById('login_form');
	if (!f) return;
	var action=_getFormAction(f);
	if (c>0) 
	{
		_setFormAction(f, action.replace('http://', 'https://'));
	}
	else _setFormAction(f, action.replace('https://', 'http://'));
	*/
	var action=jQuery('#login_form').attr('action');
	if (c>0) jQuery('#login_form').attr('action',action.replace('http://', 'https://'));
	else jQuery('#login_form').attr('action',action.replace('http://', 'https://'));
}
function load_contacts(group_id)
{
	dsp_swap('contacts_'+group_id);
	var d=$('#contacts_'+group_id);
	if (d.html()=='')
	{
		xajax_contacts(group_id);
	}
	return false;
}
function form_check_all(form,name,mark)
{
  for (i=0;i<form.elements.length; i++)
   {
    var item=form.elements[i];
    if (item.name==name)
	item.checked=mark;
   }
}
/*function addslashes(s)
{
    return s.split('\\').join('\\\\').split('"').join('\\"');
}*/
function open_chat_window(url,label)
{
	window.open(url, label, 'location=0,directories=0,menubar=0,status=0,dependent=1,width=700,height=400,screenX=200,screenY=300,toolbar=0,titlebar=0,resizable=1,scrollbars=1,hotkeys=0,maximize=0');
	return false;
}

events_checked=1;
function check_events(user_id,update_news)
{
	if (events_checked==0) return;
	events_checked=0;
	xajax_check_events(user_id,update_news);
}
function display_event(html,h,f)
{
	$.jGrowl(html, { header: h, sticky: true, beforeClose: f });
	playSound('/sound2');
	process_user_link(jQuery('#jGrowl'));
}

var xajaxRequestUri="/index.php";
var xajaxDebug=false;
var xajaxStatusMessages=false;
var xajaxWaitCursor=true;
var xajaxDefinedGet=0;
var xajaxDefinedPost=1;
var xajaxLoaded=false;
var selected_user=0;

function align_user_menu(menu)
{
	var card_width = 290;
	var card_height = menu.height();
	var left=parseInt(menu.css('left'));
	var top=parseInt(menu.css('top'));
	if (left+card_width > document.documentElement.clientWidth-10)
	{
		//menu.css('left', (document.documentElement.clientWidth - card_width - 20)+'px');
		menu.css('left', (left-card_width-10)+'px');
	}	
	if (top+card_height > (jQuery(window).scrollTop() + document.documentElement.clientHeight - 10))
	{
		//menu.css('top', (jQuery(window).scrollTop() + document.documentElement.clientHeight - card_height - 20)+'px');
		menu.css('top', (top-card_height-10)+'px');
	}
}

function align_meetings(menu)
{
	//if (!menu) return;
	var card_width = 300;
    var imgOfs=menu.offset();
    var left=imgOfs.left;
	
	
	var w = $(".Day").width();
	if (left+card_width > document.documentElement.clientWidth-10)
	{
		menu.css('left', (left-card_width+w)+'px');
	}
}

var umt=null;
var pageX, pageY;
function process_user_link(link_obj)
{
	link_obj.find('a.UserLink').bind('mouseover', function(e){ selected_user=jQuery(this).attr('uid');window.clearTimeout(umt);umt=window.setTimeout('display_user_menu('+selected_user+');',1000);$(document).one('click', function(){ selected_user=0;window.clearTimeout(umt); }); }).bind('mouseout', function(e){ selected_user=0;window.clearTimeout(umt); }).bind('mousemove', function(e){ pageX=e.pageX; pageY=e.pageY; });
}

function display_user_menu(id)
{
	var menu=jQuery('#UserMenu');
    menu.hide();
    menu.css({'left':pageX+3,'top':pageY+3});
	
	var m=false;
	eval('if (typeof(user_menu'+id+')!="undefined") m=user_menu'+id+';');
	if (typeof(m)!='undefined' && m!=false) 
	{
		menu.html(m);
		align_user_menu(menu);
		menu.fadeIn(200);
		//menu.one('mouseout', function(e){ var m=jQuery(e.target);if(m.attr('id')=='UserMenu')m.hide(); });
	}
	else 
	{
		menu.html('<img src="/image/ajax-loader.gif" width="16" height="16">');
		if (typeof(community_id)!='undefined') {xajax_user_menu(id,community_id,''); }
		else if (typeof(meeting_id)!='undefined') xajax_user_menu(id,'',meeting_id);
		else xajax_user_menu(id,'','');
		menu.show();
	}


    $(document).one('click', function(){ menu.fadeOut(200); });
	//menu.draggable({ handle: '.card_placeholder .name' });
	
}

function xajax_user_menu(){xajaxRequestUri='/index.php';return xajax.call("user_menu",arguments,1);}
function xajax_user_news(){xajaxRequestUri='/index.php';return xajax.call("user_news",arguments,1);}
function xajax_send_im(){xajaxRequestUri='/index.php';return xajax.call("send_im",arguments,1);}
function xajax_community_news(){xajaxRequestUri='/community_news.php';return xajax.call("community_news",arguments,1);}
function set_user_menu(id,html)
{
	eval('user_menu'+id+'=html;');
	var um=jQuery('#UserMenu');
	um.hide();
	um.html(html);
	align_user_menu(um);
	um.bind('click', function(e) {e.stopPropagation();});

	/*if (typeof(shadow2)=='undefined') shadow2 = jQuery('<div></div>')
                 .css({backgroundColor:'#000',position:'absolute',opacity:0.3,zIndex:499})
                 .appendTo('body');
    shadow2.hide();*/
	
	um.fadeIn(200/*, function(){ shadow2.css({width:um.width()+13+'px',height:um.height()+13+'px',left:parseInt(um.css('left'))+5+'px',top:parseInt(um.css('top'))+5+'px'}).show(); }*/);
	//um.one('mouseout', function(){ var m=jQuery(this);if(m.attr('id')=='UserMenu')m.hide(); });
}

function im_sent(event_id,user_id)
{
	jQuery('#div_im_'+event_id).hide();		
	jQuery('#div_im_'+event_id+' #minisend').val('');		
	
	jQuery('.minisend_card').hide();
	jQuery('#im_'+event_id).val('');
//	jQuery('#form_co_'+event_id).remove();
//	jQuery('#form_im_'+event_id).remove();

//	jQuery('#form_uc_'+user_id).hide();
//	jQuery('#whisper_'+user_id).show();
}

/*function im_read(event_id)
{
	jQuery('#im_read_'+event_id).hide();
}*/

function change_online_status()
{
	var c=jQuery('#ChangeStatusBlock');
	if (c.is(':visible')) c.slideUp(200);
	else c.slideDown(200);
	return(false);
}

function status_msg_history()
{
	var h=jQuery('#status_msg_history');
	if (h.is(':visible'))
	{
//		h.hide();
	}
	else
	{
		jQuery('#events_history').hide();
		if (h.html().length==0)
		{
			xajax_my_status_msg_history();
		}
		else
		{
			h.show();	
		}
	}
	return(false);
}

function relations_history()
{
	var h=jQuery('#relations_content');
	if (h.html().length==0)
	{
		xajax_relations();
	}
	return(false);
}

function events_history()
{
	var h=jQuery('#events_history');
	if (h.is(':visible'))
	{
		//h.hide();
	}
	else
	{
		jQuery('#status_msg_history').hide();
		if (h.html().length==0)
		{
			xajax_user_news();
		}
		else
		{
			h.show();
		}		
	}
	return(false);
}

function community_events_history(id)
{
	var h=jQuery('#events_history');
	if (h.is(':visible'))
	{
		h.hide();
	}
	else
	{
		if (h.html().length==0)
		{
			xajax_community_news(id);
		}
		else
		{
			h.show();
		}
	}
	return(false);
}

function findPosX(obj)
{
  var currleft = 0;
  if (obj.offsetParent)
    while (obj.offsetParent) {
      currleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  else if (obj.x) currleft += obj.x;
  return currleft;
}

function findPosY(obj)
{
  var currtop = 0;
  if (obj.offsetParent)
    while (obj.offsetParent) {
      currtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
  else if (obj.y) currtop += obj.y;
  return currtop;
}

var selected_user=0;
jQuery(document).ready(function() {

	jQuery('a').easyTooltip();
	jQuery.localScroll({ lazy:1, hash:true });
	
	jQuery('.block_settings_button').hide();
	
	process_user_link(jQuery(document));
});

function getCaretPos(obj) 
{
  //var obj = document.getElementById(objName);
  obj.focus();
  if (document.selection) { // IE
    var sel = document.selection.createRange();
    var clone = sel.duplicate();
    sel.collapse(true);
    clone.moveToElementText(obj);
    clone.setEndPoint('EndToEnd', sel);
    return clone.text.length;
  } else if (obj.selectionStart!==false) return obj.selectionStart; // Gecko
  else return 0;
}

function addslashes(str) 
{
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}

function set_status(status_html)
{
	if (status_html.length>0)
	{
		$('#my_status_msg').html(status_html);
		$('#status_msg_updated').html('только что');
		$('#status_msg_updated_block').show();
	}
	else
	{
		$('#status_msg_updated_block').hide();
		$('#my_status_msg').html('<i>нет статуса</i>');
	}
	tb_remove();
}

function do_apply_status()
{
	jQuery('#status_form input[name=status_msg]').val($('#my_status_msg_input').val());
	xajax_my_status_msg(xajax.getFormValues($('#status_form')[0]));
	return(false);
}

function set_layout(num) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == num) a.disabled = false;
    }
  }
}

function fix_layout_height()
{
	num = $('#layout_id').val();
	$('.connectWith').css('height', 'auto');	
	
	if(num==1)
	{
		h2 = $('.c2').height();			
		h3 = $('.c3').height();
		maxh = 0;
		if(h2>maxh) maxh = h2;
		if(h3>maxh) maxh = h3;	
		
		$('.c2').height(maxh);
		$('.c3').height(maxh);	
	}
	if(num==2)
	{	
		h1 = $('.c1').height();
		h2 = $('.c2').height();			
		h3 = $('.c3').height();
		h4 = $('.c4').height();	
		maxh = 0;
		if(h3>maxh) maxh = h3;	
		if(h4>maxh) maxh = h4;			
		$('.c3').height(maxh);
		$('.c4').height(maxh);
		if(h2+maxh > h1) $('.c1').height(h2+maxh+44);	
	}
	if(num==3)
	{		
		h2 = $('.c2').height();			
		h3 = $('.c3').height();
		h4 = $('.c4').height();	
		maxh = 0;
		if(h2>maxh) maxh = h2;
		if(h3>maxh) maxh = h3;	
		if(h4>maxh) maxh = h4;			
		$('.c2').height(maxh);
		$('.c3').height(maxh);
		$('.c4').height(maxh);	
	}
	if(num==4)
	{		
		h1 = $('.c1').height();
		h2 = $('.c2').height();			
		h3 = $('.c3').height();
		h4 = $('.c4').height();	
		maxh = 0;
		if(h3>maxh) maxh = h3;	
		if(h4>maxh) maxh = h4;			
		$('.c3').height(maxh);
		$('.c4').height(maxh);
		if(h2+maxh > h1) $('.c1').height(h2+maxh+44);	
	}
	if(num==5)
	{		
		h2 = $('.c2').height();			
		h3 = $('.c3').height();
		maxh = 0;
		if(h2>maxh) maxh = h2;
		if(h3>maxh) maxh = h3;	
		$('.c2').height(maxh);
		$('.c3').height(maxh);
	}
}
	
function layout(num)
{
	
	$('.layout_variant').removeClass('selected');
		
	$('#layout_'+num).addClass('selected');
		
	$('.connectWith').css('height', 'auto');	
	if(num==1)
	{
		$('#first_column').css('clear', 'none'); 
		$('#first_column').css('margin', '0px');
		$('#first_column').css('width', 'auto');
		$('#first_column').css('float', 'none');
		$('#second_column').css('clear', 'none'); 
		$('#second_column').css('margin', '0px');
		$('#second_column').css('width', 'auto');
		$('#second_column').css('float', 'none');
		$('#third_column').css('clear', 'none'); 
		$('#third_column').css('margin', '0px');
		$('#third_column').css('width', 'auto');
		$('#third_column').css('float', 'none');
		$('#fourth_column').css('clear', 'none'); 
		$('#fourth_column').css('margin', '0px');
		$('#fourth_column').css('width', 'auto');
		$('#fourth_column').css('float', 'none');
		
		$('#second_column').css('float', 'left');
		$('#second_column').css('width', '33.9%');

		$('#third_column').css('float', 'left');
		$('#third_column').css('width', '66%');
		$('#fourth_column').css('clear', 'both');
		
		$('#layout_id').val(1);
	}
	if(num==2)
	{
		$('#first_column').css('clear', 'none'); 
		$('#first_column').css('margin', '0px');
		$('#first_column').css('width', 'auto');
		$('#first_column').css('float', 'none');
		$('#second_column').css('clear', 'none'); 
		$('#second_column').css('margin', '0px');
		$('#second_column').css('width', 'auto');
		$('#second_column').css('float', 'none');
		$('#third_column').css('clear', 'none'); 
		$('#third_column').css('margin', '0px');
		$('#third_column').css('width', 'auto');
		$('#third_column').css('float', 'none');
		$('#fourth_column').css('clear', 'none'); 
		$('#fourth_column').css('margin', '0px');
		$('#fourth_column').css('width', 'auto');
		$('#fourth_column').css('float', 'none');

		$('#first_column').css('float', 'left');
		$('#first_column').css('width', '33.9%');
		
		$('#second_column').css('float', 'left');
		$('#second_column').css('width', '66%');		

		$('#third_column').css('float', 'right');
		$('#third_column').css('width', '33%');
		
		$('#fourth_column').css('float', 'right');
		$('#fourth_column').css('width', '33%');
		
		$('#layout_id').val(2);
	}
	if(num==3)
	{
		$('#first_column').css('clear', 'none'); 
		$('#first_column').css('margin', '0px');
		$('#first_column').css('width', 'auto');
		$('#first_column').css('float', 'none');
		$('#second_column').css('clear', 'none'); 
		$('#second_column').css('margin', '0px');
		$('#second_column').css('width', 'auto');
		$('#second_column').css('float', 'none');
		$('#third_column').css('clear', 'none'); 
		$('#third_column').css('margin', '0px');
		$('#third_column').css('width', 'auto');
		$('#third_column').css('float', 'none');
		$('#fourth_column').css('clear', 'none'); 
		$('#fourth_column').css('margin', '0px');
		$('#fourth_column').css('width', 'auto');
		$('#fourth_column').css('float', 'none');
		
		$('#second_column').css('float', 'left');
		$('#second_column').css('width', '33.9%');

		$('#third_column').css('float', 'left');
		$('#third_column').css('width', '33%');

		$('#fourth_column').css('float', 'left');
		$('#fourth_column').css('width', '33%');
		
		$('#layout_id').val(3);	
	}
	if(num==4)
	{
		$('#first_column').css('clear', 'none'); 
		$('#first_column').css('margin', '0px');
		$('#first_column').css('width', 'auto');
		$('#first_column').css('float', 'none');
		$('#second_column').css('clear', 'none'); 
		$('#second_column').css('margin', '0px');
		$('#second_column').css('width', 'auto');
		$('#second_column').css('float', 'none');
		$('#third_column').css('clear', 'none'); 
		$('#third_column').css('margin', '0px');
		$('#third_column').css('width', 'auto');
		$('#third_column').css('float', 'none');
		$('#fourth_column').css('clear', 'none'); 
		$('#fourth_column').css('margin', '0px');
		$('#fourth_column').css('width', 'auto');
		$('#fourth_column').css('float', 'none');

		$('#first_column').css('float', 'right');
		$('#first_column').css('width', '33%');
		
		$('#second_column').css('float', 'right');
		$('#second_column').css('width', '66.9%');		

		$('#third_column').css('float', 'left');
		$('#third_column').css('width', '33.9%');
		
		$('#fourth_column').css('float', 'left');
		$('#fourth_column').css('width', '33%');
		
		$('#layout_id').val(4);
	}
	if(num==5)
	{
		$('#first_column').css('clear', 'none'); 
		$('#first_column').css('margin', '0px');
		$('#first_column').css('width', 'auto');
		$('#first_column').css('float', 'none');
		$('#second_column').css('clear', 'none'); 
		$('#second_column').css('margin', '0px');
		$('#second_column').css('width', 'auto');
		$('#second_column').css('float', 'none');
		$('#third_column').css('clear', 'none'); 
		$('#third_column').css('margin', '0px');
		$('#third_column').css('width', 'auto');
		$('#third_column').css('float', 'none');
		$('#fourth_column').css('clear', 'none'); 
		$('#fourth_column').css('margin', '0px');
		$('#fourth_column').css('width', 'auto');
		$('#fourth_column').css('float', 'none');
		
		$('#second_column').css('float', 'left');
		$('#second_column').css('width', '66%');

		$('#third_column').css('float', 'left');
		$('#third_column').css('width', '33.9%');

		$('#fourth_column').css('clear', 'both');
		
		$('#layout_id').val(5);	
	}
	fix_layout_height();
	return(false);
}


if(jQuery) (function(){
	
	$.extend($.fn, {
		
		rightClick: function(handler) {
			$(this).each( function() {
				$(this).mousedown( function(e) {
					var evt = e;
					$(this).mouseup( function() {
						$(this).unbind('mouseup');
						if( evt.button == 2 ) {
							handler.call( $(this), evt );
							return false;
						} else {
							return true;
						}
					});
				});
				$(this)[0].oncontextmenu = function() {
					return false;
				}
			});
			return $(this);
		},		
		
		rightMouseDown: function(handler) {
			$(this).each( function() {
				$(this).mousedown( function(e) {
					if( e.button == 2 ) {
						handler.call( $(this), e );
						return false;
					} else {
						return true;
					}
				});
				$(this)[0].oncontextmenu = function() {
					return false;
				}
			});
			return $(this);
		},
		
		rightMouseUp: function(handler) {
			$(this).each( function() {
				$(this).mouseup( function(e) {
					if( e.button == 2 ) {
						handler.call( $(this), e );
						return false;
					} else {
						return true;
					}
				});
				$(this)[0].oncontextmenu = function() {
					return false;
				}
			});
			return $(this);
		},
		
		noContext: function() {
			$(this).each( function() {
				$(this)[0].oncontextmenu = function() {
					return false;
				}
			});
			return $(this);
		}
		
	});
	
})(jQuery);	

/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/
function slideSwitch() {
    var $active = jQuery('#slideshow DIV.active');

    if ($active.length == 0) $active=jQuery('#slideshow DIV:last');

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : jQuery('#slideshow DIV:first');

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

    $active.addClass('last-active');

    //$active.animate({opacity: 0}, 500);

    var $next_img=$next.find('img');
    if ($next_img.attr('src')=='')
    {
    	$next_img.attr('src',$next_img.attr('alt'));
    	$next_img.load(function(){
    		var $next=jQuery(this).parent().parent();
			$next.css({opacity: 0.0})
			        .addClass('active').show()
			        .animate({opacity: 1.0}, 800, function() {
			            $active.removeClass('active last-active').hide();
			        });    		
    	});
    	setTimeout("slideSwitch()", 3000);
    	return;
    }
    
    $next.css({opacity: 0.0})
        .addClass('active').show()
        .animate({opacity: 1.0}, 800, function() {
            $active.removeClass('active last-active').hide();
        });
   	setTimeout("slideSwitch()", 3000);
}

jQuery(document).ready(function() {
	if (jQuery('#slideshow div').length>0)
	{
    	setTimeout("slideSwitch()", 3000);
	}
});

function edit_contact(id)
{
	$('#contact_properties').html('');
	$('#edit_contact_id').val(id);
	//$.blockUI({ message: $('#edit_contact_block'), css: { cursor: 'normal' } });
	xajax_edit_contact(id);
}
function contact_properties(id,html)
{
	$('#contact_properties').html(html);
}

function xajax_edit_contact(){xajaxRequestUri="/contacts";return xajax.call("edit_contact", arguments, 1);}
function xajax_my_status_msg_history(){xajaxRequestUri='/index.php';return xajax.call("my_status_msg_history",arguments,1);}
function xajax_relations(){xajaxRequestUri='/index.php';return xajax.call("relations",arguments,1);}
function rate_object(obj_type,obj_id,token,act,url){xajaxRequestUri=url;return xajax.call("rate",arguments,1);}