var totalMessages = 0;
var lastQuoteLinkBlock = true;
var selection = '';

function QuoteText(num)
{
	if (selection == '')
	{
		selection = false;
	}
	if (!selection && num == totalMessages && lastQuoteLinkBlock)
	{
		document.getElementById('message_li_' + num).innerHTML += '<br /><br /><span class="sm" style="color: #990000">' + _text['QUOTE_MSG'] + '</span>';
		lastQuoteLinkBlock = false;
		return true;
	}
	if (!selection)
	{
		var mText = document.getElementById('message_text_' + num).innerHTML;
		mText = mText.replace(/<\/?em[^>]*>/ig, '').replace(/<\/?u>/ig, '').replace(/<\/?a[^>]*>/ig, '').replace(/<\/?span[^>]*>/ig, '');
		mText = mText.replace(/&gt;/ig, '>').replace(/&lt;/ig, '<').replace(/&quote;/ig, '"').replace(/&amp;/ig, '&');
		var mTextArray = mText.split(/<br\s*\/?>\s*/i);
	}
	else
	{
		var mTextArray = selection.split('\n');
	}
	var mText2 = '';
	for (var i = 0; i < mTextArray.length; ++i)
	{
		mText2 += '> ' + mTextArray[i] + '\r\n';
	}
	if (document.f1.x9ett)
	{
		var insertText = 'Цитата (' + document.getElementById('message_author_' + num).innerHTML + ', ' + document.getElementById('message_date_' + num).innerHTML + '):\r\n' + mText2 + '\r\n';
		if (document.f1.x9ett.value == '')
		{
			document.f1.x9ett.value = insertText;
		}
		else
		{
			document.f1.x9ett.value += '\r\n' + insertText;
		}
		document.f1.x9ett.focus();
	}
}

function catchSelection()
{
	if (window.getSelection)
	{
		selection = window.getSelection().toString();
	}
	else if (document.getSelection)
	{
		selection = document.getSelection();
	}
	else if (document.selection)
	{
		selection = document.selection.createRange().text;
	}
}

function vote(pid, val)
{
	if (val != 0 && val != 1)
	{
		alert('Hacking attempt detected.');
		return;
	}

	$('b'+val).className = ' voted';
	$('b'+val).innerHTML = _text['VOTE_WAIT'];
	$('rating').className = 'loading';

	JsHttpRequest.query(
		'/ajax.php?ajax-action=photo-vote',
		{ 'pid' : pid, 'val' : val, 'type' : 'b' },
		function (result, errors)
		{
			if (errors) alert('Error catched: \n' + errors);
			if (result)
			{				var divs, signs, txt = '', cnt, rating = 0;
				divs = new Array('votes_pro', 'votes_con');
				signs = new Array('+', '&ndash;');

				for (var i = 0; i < 2; i++)
				{
					if (result[i] && result[i].length != 0)
					{
						txt += '<div style="padding:3px 5px" class="s' + (i == 0 ? 2 : 5) + '">';

						for (aid in result[i])
						{
							txt += '[' + signs[i] + '] <a href="/author/' + aid + '/">' + result[i][aid] + '</a><br />';

							if (i == 0)
								 rating++;
							else rating--;
						}

						txt += '</div><br />';
					}
				}

				$('votes').innerHTML = txt;

				if (rating > 0) $('rating').innerHTML = '+' + rating; else
				if (rating < 0) $('rating').innerHTML = '&ndash;' + parseInt(-rating);
						   else $('rating').innerHTML = '0';

				$('b1').className = (result[2][1] == 1) ? ' voted' : '';
				$('b0').className = (result[2][0] == 1) ? ' voted' : '';
			}
			else $('b'+val).className = '';

			$('rating').className = '';
			$('b1').innerHTML = _text['VOTE_B1'];
			$('b0').innerHTML = _text['VOTE_B0'];
		},
		true
	);
}

function vote_konk(pid, val)
{
	if (val != 0 && val != 1)
	{
		alert('Hacking attempt detected.');
		return;
	}

	$('k'+val).className = ' voted';
	$('k'+val).innerHTML = _text['VOTE_WAIT'];

	JsHttpRequest.query(
		'/ajax.php?ajax-action=photo-vote',
		{ 'pid' : pid, 'val' : val, 'type' : 'k' },
		function (result, errors)
		{
			if (errors) alert(errors);
			if (result)
			{				if (result[0] != '')
					 $('star').innerHTML = '<img src="/img/star_' + result[0] + '.gif" width="16" height="15" border="0" align="top" />';
				else $('star').innerHTML = '';

				$('k1').className = (result[1][1] == 1) ? ' voted' : '';
				$('k0').className = (result[1][0] == 1) ? ' voted' : '';
			}
			else $('k'+val).className = '';

			$('k1').innerHTML = _text['VOTE_K1'];
			$('k0').innerHTML = _text['VOTE_K0'];
		},
		true
	);
}


var map, baseIcon;


function showInfoWindow(html)
{
	$('photo_from_map').innerHTML = html;
	$('pm').style.display = 'block';
}


function hideInfoWindow()
{	if ($('pm') && $('pm').style.display != 'none')
	{
		$('pm').style.display = 'none';
		$('photo_from_map').innerHTML = '';
	}
}


function initialize()
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map_canvas"));
		var center = new GLatLng(lat, lng);
		map.setCenter(center, 14);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setMapType(G_HYBRID_MAP);

		baseIcon = new GIcon();
		baseIcon.iconSize = new GSize(32, 32);
		baseIcon.iconAnchor = new GPoint(16, 32);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 36);

		GEvent.addListener(
			map,
			"moveend",
			function()
			{
				refreshMarkers();
			});

		refreshMarkers();
	}
}

function add_marker(z)
{
	var center = new GLatLng(z['lat'], z['lng']);
	var photoicon = new GIcon(baseIcon);
	photoicon.image = "/img/foto_" + z['heading'] + ".png";
	var marker = new GMarker(center, {draggable: false, icon: photoicon });

	GEvent.addListener(
		marker,
		"click",
		function()
		{
			JsHttpRequest.query(
				'/ajax.php?ajax-action=get-photo-data',
				{ 'pid' : z['pid'] },
				function (result, errors)
				{
					if (errors) showInfoWindow(errors);
					if (result) showInfoWindow(result);
				},
			false);
		});

	map.addOverlay(marker);
}

function add_centerphoto_marker()
{
	var cnt_square = 0.001;

	var center = new GLatLng(lat, lng);
	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(64/2, 71/2);
	baseIcon.iconAnchor = new GPoint(32/2, 71/2);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);

	var boundaries = new GLatLngBounds(
		new GLatLng(lat-0.0005, lng-0.0008),
		new GLatLng(lat+0.0005, lng+0.0008));

	var center_red_mark = new GGroundOverlay("/img/central_mark.png", boundaries);

	var photoicon = new GIcon(baseIcon);
	photoicon.image = "/img/foto_" + heading + ".png";

	var marker = new GMarker(center, {draggable: false, icon: photoicon });
	GEvent.addListener(
		marker,
		"click",
		function()
		{
			showInfoWindow('<strong>' + _text['P_CURRENT'] + '</strong>');
		}
	);

	map.addOverlay(center_red_mark);
	map.addOverlay(marker);
}

function refreshMarkers()
{
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();

	map.clearOverlays();

	var _arr = new Array();
	_arr['pid'] = pid;
	_arr['NElat'] = northEast.lat();
	_arr['NElng'] = northEast.lng();
	_arr['SWlat'] = southWest.lat();
	_arr['SWlng'] = southWest.lng();

	JsHttpRequest.query(
		'/ajax.php?ajax-action=get-photos-around',
		_arr,
		function (result, errors)
		{
			if (errors)
			{
				$('photo_from_map').innerHTML = errors;
				$('photo_from_map').style.display = 'block';
			}
   			if (result)
			{
				for (var i = 0; i < result.length; i++)
					add_marker(result[i]);
			}
		},
		false
	);

	add_centerphoto_marker();
}

document.onclick = function(e)
{
	e = e || window.event;
	E = e.target || e.srcElement;
	if (E.id != 'photo_from_map' && E.parentNode.id != 'photo_from_map')
		hideInfoWindow();
}

function showMap()
{	$('map').style.display = 'block';
	$('showmap').style.display = 'none';
	initialize();
}


