var cur_wide;
var old_wide;
var min_bs = 100;
var max_bs = 110;
  
function show_alert(text, button_caption, button_onclick){
	var bc = button_caption || 'Close';
	var bo = button_onclick || 'hide_alert();';
	$('#reg_form').hide();
	$('#login_form').hide();
	$('#alert').html('<div onclick=\"hide_alert();\" class=\"close_button\">&times;</div><center><br>'+text+'<br>'+'<div style=\'position: absolute; left: 10px; text-align: center; width: 400px; bottom: 10px;\'><input type=button value=\''+bc+'\' onclick=\''+bo+'\'></div>');
	var w = document.getElementById('all_waiting');
	var r = document.getElementById('alert');
	set_fullscreen(w);
	set_element_top_middle(r);
}
  
function hide_alert(){
	var w = document.getElementById('all_waiting');
	var r = document.getElementById('alert');
	w.style.display = 'none';
	r.style.display = 'none';
}
 
function start_waiting(){
	if (document.getElementById('waiting')){
		return;
	}
	var e = document.createElement('div');
	var b = document.body;
	b.appendChild(e);
	try {e.style.opacity = 0.8;} catch(e){}
	try {e.style.MozOpacity = 0.8;} catch(e){}
	try {e.style.filter = 'alpha(opacity='+Math.round(0.8 * 100)+')';} catch(e){}
	try {e.style.KhtmlOpacity = 0.8;} catch(e){}
	e.id = 'waiting';
	e.style.backgroundColor = '#FCC';
	set_fullscreen(e);
	set_element_top_middle(document.getElementById('ds'));
}
  
function stop_waiting(){
	if (document.getElementById('waiting')){
		document.body.removeChild(document.getElementById('waiting'));
	}
}
  
function set_fullscreen(elem){
	var b = document.body;
	elem.style.position = 'absolute';
	var left = b.offsetLeft;
	var top = b.offsetTop;
	var width = b.offsetWidth;
	var height = b.offsetHeight;
	var l = b._loading;
	elem.style.left = left + 'px';
	elem.style.top = top + 'px';
	elem.style.width = width + 'px';
	elem.style.height = height + 'px';
	elem.style.display = 'inline';
}
  
function set_element_top_middle(elem){
	elem.style.top = '50%';
	elem.style.left = '50%';
	elem.style.display = 'block';
	elem.style.marginLeft = -Math.round(elem.offsetWidth / 2) + 'px';
	elem.style.marginTop = -Math.round(elem.offsetHeight / 2) + 'px';
}
  
function hide_descriptions(){
	for (var i = min_bs; i < max_bs; i++){
		if (document.getElementById('desc' + i)){
			document.getElementById('desc' + i).style.display = 'none';
		}
	}
}
  
function go_to_null(id){
	var sc = '' +
	'' +
	'$(document.getElementById(\'b' + id + '\')).animate({' +
	'width: 50' +
	'}, 300);' +
	'$(document.getElementById(\'desc' + id + '\')).fadeOut("fast");' +
	'';
	eval(sc);
}
  
function go_to_wide(id){
	if (id == cur_wide){
		return;
	}
	if (document.getElementById('b' + cur_wide)){
		var sc = '' +
		'$(document.getElementById(\'desc' + cur_wide + '\')).fadeOut();' +
		'$(document.getElementById(\'b' + cur_wide + '\')).animate({' +
		'width: 50' +
		'}, 300);' +
		'' +
		'';
		eval(sc);
	}
	cur_wide = id;
	$(document.getElementById('b'+id)).animate({width: 250}, 300, function(){
		$(document.getElementById('desc'+id)).toggle();
	});
}
  
function vote_make_plus(id){
	if (!loged_in){
		show_alert('Чтобы проголосовать за, необходимо войти в систему', 'Войти', 'hide_alert();show_login_form();');
		return false;
	}
	if (!id){
		show_alert('Вы уже проголосовали за эту закладку', 'Ну и ладно', 'hide_alert();');
		return false;
	}
	up_el = document.getElementById('j_votes_up_' + id);
	dn_el = document.getElementById('j_votes_down_' + id);
	$.post("ajax_processor.php", {action : "vote_make_plus", id : id});
	up_el.innerHTML = (parseInt(up_el.innerHTML) + 1);
	up_el.className = 'inf_vup_n';
	dn_el.className = 'inf_vdown_n';
	$(up_el).one("click");
	$(up_el).unbind("click");
	$(dn_el).one("click");
	$(dn_el).unbind("click");
	return true;
}
  
function vote_make_minus(id){
	if (!loged_in){
		show_alert('Чтобы проголосовать против, необходимо войти в систему', 'Войти', 'hide_alert();show_login_form();');
		return false;
	}
	if (!id){
		show_alert('Вы уже проголосовали за эту закладку', 'Ну и ладно', 'hide_alert();');
		return false;
	}
	up_el = document.getElementById('j_votes_up_' + id);
	dn_el = document.getElementById('j_votes_down_' + id);
	$.post("ajax_processor.php", {action : "vote_make_minus", id : id});
	dn_el.innerHTML = (parseInt(dn_el.innerHTML) + 1);
	up_el.className = 'inf_vup_n';
	dn_el.className = 'inf_vdown_n';
	$(up_el).one("click");
	$(up_el).unbind("click");
	$(dn_el).one("click");
	$(dn_el).unbind("click");
	return true;
}
  
function post_comment(id, vtext){
	if (!loged_in){
		show_alert('Чтобы оставить комментарий, необходимо войти в систему', 'Войти', 'hide_alert();show_login_form();');
		return false;
	}
	$.post("ajax_processor.php", {action : "post_comment", text : vtext, id: id}, function(data){
		document.getElementById('comments_block').innerHTML = data;
	});
	document.getElementById("comment_text").value = "";
	return true;
}

function remote_call(action, params, callback){
	$.ajax({
		url : 'ajax.php',
		type : 'POST',
		cache : false,
		data : JSON.stringify({'action' : action, 'params' : params}),
		success : function(response){
			callback(JSON.parse(response));
		}
	});
}

function link(url){
	return '<a href="' + url + '" target="_blank">' + url + '</a>';
}

function pbworks_link(name){
	return link('http://' + name + '.pbworks.com/');
}

