/* Maps interface functions */

// Переключение картинок
function swap_img(mid)
{
	if($('pic_'+mid).className == 'hidden')
	{
		$('pic_'+mid).className = 'visible';
		$('upic_'+mid).className = 'hidden';
		$('swap_up_'+mid).innerHTML = 'поверхность';
		$('swap_down_'+mid).innerHTML = '<a href="#" onclick="return swap_img(' + mid + ');">подземелье</a>';
	}
	else
	{
		$('pic_'+mid).className = 'hidden';
		$('upic_'+mid).className = 'visible';
		$('swap_up_'+mid).innerHTML = '<a href="#" onclick="return swap_img(' + mid + ');">поверхность</a>';
		$('swap_down_'+mid).innerHTML = 'подземелье';
	}
	return false;
}

// Функция вывода реплеев по пользователю
function maps_by_user()
{
	var msize = $('msize').value;
	if(msize == 0)
	{
		location.href = '/homm5/maps/' + $('splayer').value + '/';
	}
	else
	{
		location.href = '/homm5/maps/' + $('splayer').value + '/' + msize + '/';
	}
	return false;
}

// Функция сортировки
function maps_sort(value)
{
  	document.cookie="msort=" + value + "; path=/; expires=Mon, 01-Jan-2091 00:00:00 GMT";
  	location.href = location.href;
	return false;
}

// Функция обсчёта голоса
function vote_map(rid)
{
	location.href = '/homm5/maps/vote/m' + rid + '/' + $('vote['+rid+']').value + '/';
	return false;
}

// Показ комментариев
function show_comments(rid)
{
	$('shadow').className = 'visible';
	$('shadow').style.height = document.body.scrollHeight + 'px';
	$('maps_tbl').className = 'hidden';
	$('rcomments').className = 'visible';
	$('sort').className = 'hidden';
	$('rcomments').style.left = Math.round((screen.width - 725)/2) + 'px';
	$('rcomments').style.top = (document.body.parentNode.scrollTop + 100) + 'px';
	$('record_id').value = rid;
	$('rctext').value = '';
	$('rcsave').disabled = false;
	$('posting').className = 'hidden';
	load_comments(rid);
	return false;
}

function send_message(hash)
{
	var msg = $('rctext').value;
	if(!msg)
	{
		alert('Пустые сообщения отправлять смысла нет :-)');
		return false;
	}
	if(!hash)
	{
		alert('Комментировать могут только зарегистрированные пользователи');
		return false;
	}
	$('rctext').value = '';
	$('rcsave').disabled = true;
	$('posting').className = 'visible';

	new Ajax.Request(
		'/lib/comments.php', 
		{   
			method: 'post',   
			onSuccess: function(transport)
			{
				$('posting').className = 'hidden';
				$('rcsave').disabled = false;
				load_comments($('record_id').value);
				return true;
			},
			parameters: 'action=post_comments&type=2&rid=' + $('record_id').value + '&msg=' + encodeURI(msg) + '&hash=' + hash
		}
	); 	
	return false;
}

// Загрузка комментариев
function load_comments(rid)
{
	$('rc_container').innerHTML = '';
	$('loading_c').className = 'visible';
	new Ajax.Request(
		'/lib/comments.php', 
		{   
			method: 'post',   
			onSuccess: function(transport)
			{
				if(transport.responseText == '0')
				{
					print_comment(new Array('Комментариев пока нет', '', ''), 1);
				}
				else
				{
					var cdata = transport.responseText.split('~!~');
					for(i in cdata)
					{
						var cdetails = cdata[i].split('^&^');
						print_comment(cdetails, i);
					}
				}
				//alert(regdata);
				//$('rc_container').innerHTML = transport.responseText;
				$('loading_c').className = 'hidden';
				return true;
			},
			parameters: 'action=get_comments&type=2&rid=' + rid + '&rnd = ' + Math.random()*1000000000
		}
	); 	
	return false;
}

// Функция отображения комментария
function print_comment(cdetail, index)
{
	eComment = document.createElement("div");
	eText = document.createElement("p");
	eDate = document.createElement("div");
	eAuthor = document.createElement("span");
			
	eComment.setAttribute("id", "comment"+index);
	eDate.setAttribute("id", "date"+index);
	eAuthor.setAttribute("id", "author"+index);
				
	eText.appendChild(document.createTextNode(cdetail[2]));
	eAuthor.appendChild(document.createTextNode(cdetail[0]));
	$("rc_container").appendChild(eComment);
	$("comment"+index).appendChild(eDate);
	$("date"+index).appendChild(eAuthor);
	eDate.appendChild(document.createTextNode(cdetail[1]));
	$("comment"+index).appendChild(eText);
				
	$('comment'+index).className = 'comment';
	$('date'+index).className = 'cdate';
	$('author'+index).className = 'cauthor';
	
	return true;
}

// Сокрытие комментариев
function close_comments()
{
	$('rcomments').className = 'hidden';
	$('shadow').className = 'hidden';
	$('sort').className = 'visible_inline';
	$('maps_tbl').className = 'maps_tbl';
	return false;
}