mousex = 0;
mousey = 0;


document.onkeydown = NavigateThrough;
function NavigateThrough (event)
{
	if (!document.getElementById) return;

	if (window.event) event = window.event;

	if (event.ctrlKey)
	{
		var link = null;
		var href = null;
		switch (event.keyCode ? event.keyCode : event.which ? event.which : null)
		{
			case 0x25:
				link = document.getElementById ('NextLink');
				break;
			case 0x27:
				link = document.getElementById ('PrevLink');
				break;
			case 0x26:
				link = document.getElementById ('UpLink');
				break;
			case 0x28:
				link = document.getElementById ('DownLink');
				break;
			case 0x24:
				href = '/';
				break;
		}

		if (link && link.href) document.location = link.href;
		if (href) document.location = href;
	}			
}

function stopEvent(e)
{
	if(!e) var e = window.event;
	if (e)
	{		
		// e.cancelBubble is supported by IE - this will kill the bubbling process.
		e.cancelBubble = true;
		e.returnValue = false;

		//e.stopPropagation works only in Firefox.
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
	}
	return false;
}

function showCities()
{
	var el = $('citiesList');
	if (! el.visible)
	{
		Element.show(el);	
		el.visible = true;
	}
}

function hideCities()
{
	var el = $('citiesList');
	if (el.visible)
	{
		Element.hide(el);
		el.visible = false;
	}
}

function showCatSearch()
{
	var el = $('CatSearchList');
	if (! el.visible)
	{
		Element.show(el);	
		el.visible = true;
	}
}

function CatSearchClick(indx,indxname)
{
	var lnk = $('link');
	lnk.value = indx;	
	var lnk = $('CatSearchName');
	lnk.innerHTML = indxname;
}

function hideCatSearch()
{
	var el = $('CatSearchList');
	if (el.visible)
	{
		Element.hide(el);
		el.visible = false;
	}
}

//MM_preloadImages('/_images/nav/nav-news_over.gif', '/_images/nav/nav-photos_over.gif', '/_images/nav/nav-talk_over.gif', '/_images/nav/nav-board_over.gif', '/_images/nav/nav-info_over.gif', '/_images/nav/nav-my_over.gif');

function complainClick(e)
{
	window.open(this.href, null, 'height=300, width=300, location=no, menubar=no, resizable=yes, toolbar=no');
	e.preventDefault();
}

addEvent(window, 'load', function(e){
			
	/* Mouse Moves */
	addEvent(document, 'mousemove', function(e){
		if(isMSIE){
			mousex=e.clientX+document.body.scrollLeft;
			mousey=e.clientY+document.body.scrollTop;
			return true;
		}else if(isOpera){
			mousex=e.clientX;
			mousey=e.clientY;
			return true;
		}else if(isMozilla){
			mousex = e.pageX;
			mousey = e.pageY;
			return true;
		}
	});
	
	
	/* City picker */
  	if ($('cityName'))
  	{
		hideCities();
		
		addEvent(document, 'click', function(e){ 
			hideCities();
		});
	
		addEvent($('cityName'), 'click', function(e){ 
			var el = $('citiesList');
			if (el.visible)
				hideCities();
			else
				showCities();
			e.preventDefault(); 
			e.stopPropagation();		
		});
	
  	}		
  	
  	/* CatSearch picker */
  	if ($('CatSearchName'))
  	{
		hideCatSearch();
		
		addEvent(document, 'click', function(e){ 
			hideCatSearch();
		});
	
		addEvent($('CatSearchName'), 'click', function(e){ 
			var el = $('CatSearchList');
			if (el.visible)
				hideCatSearch();
			else
				showCatSearch();
			e.preventDefault(); 
			e.stopPropagation();		
		});
	
  	}	

	/* Starred links */
  	var links = document.getElementsByTagName('a');
	var linksS = [];

	for(var i = 0; i < links.length; i++)
	{
		var link = links[i];
		if (Element.hasClassName(link, 'starredInd'))
			linksS.push(link);
		else if (Element.hasClassName(link, 'subscrInd'))
			linksS.push(link);
		else if (Element.hasClassName(link, 'starredIndBig'))
			linksS.push(link);
		else if (Element.hasClassName(link, 'complainLink'))
			addEvent(link, 'click', complainClick);
	}
	
	for(var i = 0; i < linksS.length; i++)
	{
		var link = linksS[i];

		Starred(link).setTitle();

		addEvent(link, 'focus', 	function(){ this.blur(); });
		addEvent(link, 'mousedown', function(){
			var a = Starred(this);
			
			new ajax(this.href+'&ajax=1&random='+Math.random(), {
				onError: function(request) {
					alert('Произошла ошибка: ' + request.responseText);
					
				},
				
				onComplete: function(request) {
					var text = request.responseText;
					
					if(request.responseText == '1')
					{
						a.mark();
						a.object.parentNode.href = a.object.parentNode.href.replace('old=0', 'old=1')
					}
					else if (request.responseText == '0')
					{
						a.unmark();
						a.object.parentNode.href = a.object.parentNode.href.replace('old=1', 'old=0')
					}
					else if (request.responseText == 'notlogged')
					{
						if (confirm('Данная возможность доступна зарегистрированным пользователям.\nХотите перейти на страницу авторизации и регистрации?'))
						{
							window.location.href = '/user/login/?return='+escape(window.location.href);
						}
					}
					else
						alert('!'+ request.responseText+'!');
				}
			});
		});

		addEvent(link, 'click', function(e){
			e.preventDefault();
		});

	}
});


/* =Stared --------------------------------------------------*/

function StarredHandler(object) {
	if (object.tagName == 'A') 	this.object = object.firstChild;
	else						this.object = object;
	
	if (Element.hasClassName(object, 'subscrInd'))
		this.isSubscr = true;
	
	if (Element.hasClassName(object, 'starredIndBig'))
		this.isStarredBig = true;
}

StarredHandler.prototype = {
	object: null,
	isSubscr: false,

	is: function() 
	{
		if (this.isSubscr)
			return this.object.src.indexOf('subscr-1') > -1;
		
		else if (this.isStarredBig)
			return this.object.src.indexOf('starBig-1') > -1;
		
		else
			return this.object.src.indexOf('star-1') > -1;
	},

	flip: function() 
	{
		if (this.is()) 
			this.unmark();
		else 
			this.mark();

		this.setTitle();
	},

	mark: function()
	{ 
		if (this.isSubscr)
			this.object.src = '/_images/subscr-1.gif'; 
		
		else if (this.isStarredBig)
		{
			this.object.src = '/_images/starBig-1.gif'; 
			
			var id = this.object.parentNode.id.replace('-big', '');
			if ($(id))
				$(id).firstChild.src = '/_images/star-1.gif';
		}	
		else
		{
			this.object.src = '/_images/star-1.gif'; 
			
			var id = this.object.parentNode.id;
			if ($(id+'-big'))
				$(id+'-big').firstChild.src = '/_images/starBig-1.gif';
		}
		
		this.setTitle();
	},

	unmark: function()
	{ 
		if (this.isSubscr)
		{
			this.object.src = '/_images/subscr-0.gif'; 
		}	
		else if (this.isStarredBig)
		{
			this.object.src = '/_images/starBig-0.gif'; 
			
			var id = this.object.parentNode.id.replace('-big', '');
			if ($(id))
				$(id).firstChild.src = '/_images/star-0.gif';
		}	
		else
		{
			this.object.src = '/_images/star-0.gif'; 
			
			var id = this.object.parentNode.id;
			if ($(id+'-big'))
				$(id+'-big').firstChild.src = '/_images/starBig-0.gif';	
		}	
		this.setTitle();
	},

	setTitle: function()
	{
		if (this.isSubscr)
		{
			this.object.parentNode.title = 'Присылать новые комментарии по e-mail';
		}
		else
		{
			if (this.is())
				this.object.parentNode.title = 'Убрать из закладок';
			else 			
				this.object.parentNode.title = 'Добавить в закладки';
		}
	}
}

function Starred(object) {return new StarredHandler(object) }

function voteForEntity(btn, Vote, entityType, entityID, onCompleteText) {
	new ajax(btn.form.action+'?a=vote&ajax=1&random='+Math.random()+'&vote=' + Vote + '&entity_id='+entityID + '&entity_type='+entityType, {
		onError: function(request) {
			alert('Proizoshla oshibka: ' + request.responseText + '\n Proizoshla oshibka, poprobuyte povtorit pozdnee.');
		},
		
		onComplete: function(request) {
			var text = request.responseText;
			$('voteResults-'+entityType+'-'+entityID).innerHTML = text;
			if (onCompleteText != null) {
				$('voteForm-'+entityType+'-'+entityID).innerHTML = onCompleteText;
			}
		}
	});
}