var ey,ex;
var pw_shown=0;
var profile_cache = new Array();

jQuery.noConflict();

var ey,ex;
var pw_shown=0;
var profile_cache = new Array();

jQuery(function() {

	jQuery("body").bind("mousemove", function(event){
	 	cx = event.clientX;
		cy = event.clientY;
		if(document.all) ey = event.y+(document.body.scrollTop||document.documentElement.scrollTop);
		else           ey = event.pageY;
		if(document.all) ex = event.x+(document.body.scrollLeft||document.documentElement.scrollLeft);
		else           ex = event.pageX;
	});

	jQuery("body").bind("click", function(){
	  if(!click_inside)
	  {
	  	cancelProfileWindow();
	  	closeProfileWindow();
	  }
	  click_inside=0;
	});

	bindRels(); 
});

function bindRels()
{
	jQuery("a[rel='userprofile']").each(function (i)
	{
    	this.onmouseover = function()
		{
			showProfileWindow(this);
		};

    	this.onmouseout = function()
		{
			cancelProfileWindow();
		};
	});
}

var timer_id=0;
var clearer_id=0;
var click_inside=0;
var cancel_load=0;

function showProfileWindow(that)
{
	var url = that.href.split('/');
	url = url[url.length-1];
	id = url.substr(0,url.indexOf('.'));
	cancelCloseProfileWindow();
	timer_id = setTimeout('show_user_profile('+id+')', 1000);
	cancel_load=0;
}

function cancelProfileWindow()
{
	if(timer_id)
	{
		clearTimeout(timer_id);
		timer_id=0;
	}
	cancel_load=1;
}

function cancelCloseProfileWindow()
{
	if(clearer_id)
	{
		clearTimeout(clearer_id);
		clearer_id=0;
	}
	cancel_load=1;
}

function closeProfileWindow(time)
{	
	if(jQuery.browser.msie || jQuery.browser.opera)
	{
		jQuery('#profilewindow').hide();
	}
	else
	{
		jQuery('#profilewindow').fadeOut(400);
	}
}

function profileOver()
{
	if(clearer_id)
	{
		clearTimeout(clearer_id);
		clearer_id=0;
	}
	
	cancel_load=1;
}

function profileOut()
{
	clearer_id = setTimeout("closeProfileWindow()", 3000);
	cancel_load=1;
}

function showMsg(msg)
{
 	jQuery('#profilewindow').html(msg);
 	w=192;
 	h=230;
 	x=ex;
 	y=ey;
 	
 	wx = jQuery(window).width();
 	wy = jQuery(window).height()
 	    	
 	if (wx - cx < w)
		x = x - w -5;
	else
		x = x - 5;
	
	if (wy - cy < h)
		y = y - h -5;
	else
		y = y - 5;
	
 	jQuery('#profilewindow').css('left',x).css('top',y);
	jQuery('#profilewindow').show();
}

function show_user_profile(id)
{
	cancelCloseProfileWindow();

	jQuery.post("/profilewindow", { user_id: id },
	function(data)
	{
		showMsg(data);

	}, 'html');
}

function addFriend(id) {
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = "/profile/addfriend";

	ajaxObjects[ajaxIndex].setVar('id', id);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAddFriend(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}

function showAddFriend(ajaxIndex) {
	eval("var result = " + ajaxObjects[ajaxIndex].response);
	if(result != undefined) {
		switch (result.status) {
			case 0:
				msg("Вы добавили в друзья " + result.nickname);
				break;
			case 1: 
				msg('Гм.. не ясно с кем вы хотите дружить!');
				break;
			case 2: 
				msg(result.nickname + " уже Ваш друг!");
				break;
			case 3: 
				msg('Для того чтобы подружиться необходимо сначала зарегистрироваться на сайте!');
				break;			
			default:
				msg('Неизвестная ошибка!');
				break;
		}
		
		closeProfileWindow();
		
	} else {
		alert('Ошибка при обращении к серверу');
	}
	ajaxObjects[ajaxIndex] = false;
}