var Overlay = {
	options:{
		'appear':{duration:0.4, to:0.7},
		'fade':{duration:0.4}
	},
	resize: function(){
		var coords = document.viewport.getDimensions();
		var scroll = document.viewport.getScrollOffsets();
		$('overlay').style.width = parseInt(scroll.left + coords.width) + 'px';
		$('overlay').style.height = parseInt(scroll.top + coords.height) + 'px';
	},
	on: function(){
		Overlay.resize();
		Effect.Appear('overlay', Overlay.options.appear);
		Event.observe(window, 'resize', Overlay.resize);
		Event.observe(window, 'scroll', Overlay.resize);
	},
	off: function(){
		Event.stopObserving(window, 'resize', Overlay.resize);
		Event.stopObserving(window, 'scroll', Overlay.resize);
		Effect.Fade('overlay', Overlay.options.fade);
	}
}
var PopupBox = {
	id: 'loginbox',
	position: function(init){
		var sizes = document.viewport.getDimensions();
		var scroll = document.viewport.getScrollOffsets();
		var newx = parseInt((sizes.width - $(PopupBox.id).style.width)/2 + scroll.left);
		var newy = parseInt((sizes.height - $(PopupBox.id).style.height)/2 + scroll.top);
		
		if (init == true){
			$(PopupBox.id).style.top = newy + 'px';
			$(PopupBox.id).style.left = newx + 'px';
		}else{
			new Effect.Move(PopupBox.id, {x: newx, y: newy, mode: 'absolute', duration:0.4});
		}
	},
	show: function(){
		PopupBox.position(true);
		$(PopupBox.id).show();
		$('popup_login').focus();
		Event.observe(window, 'resize', PopupBox.position);
		Event.observe(window, 'scroll', PopupBox.position);
	},
	hide: function(){
		Event.stopObserving(window, 'resize', PopupBox.position);
		Event.stopObserving(window, 'scroll', PopupBox.position);
		$(PopupBox.id).hide();
	}
}
function memberAction(requestFunction){
	if (isLogged) requestFunction();
	else{
		onLogin = requestFunction;
		showLoginBox();
	}
}
function showLoginBox(){
	Overlay.options.appear.afterFinish = function(){PopupBox.show();};
	Overlay.on();
}
function hideLoginBox(){
	PopupBox.hide();
	Overlay.off();
}
function onLogin(){}
var ToolTip = {
	options:{
		'appear':{duration:0.4, to:0.9},
		'fade':{duration:0.4}
	},
	show: function(id){
		Effect.Appear(id, ToolTip.options.appear);
	},
	hide: function(id){
		Effect.Fade(id, ToolTip.options.fade);
	}
}
function addFriend(id,text) {
    var new_text = $('friendAction_' + id).innerHTML;
    new_text.replace("'","\'");
    $('friendAction_' + id).onclick = function() { memberAction(function(){ajaxRequest('friends/delFriend', {method:'post',postBody:'id=' + id, onSuccess:function(){delFriend(id,new_text)}})})};
    $('friendAction_' + id).innerHTML = text;
}
function delFriend(id,text) {
    var new_text = $('friendAction_' + id).innerHTML;
    new_text.replace("'","\'");
    $('friendAction_' + id).onclick = function() { memberAction(function(){ajaxRequest('friends/addFriend', {method:'post',postBody:'id=' + id, onSuccess:function(){addFriend(id,new_text)}})})};
    $('friendAction_' + id).innerHTML = text;
}