$(document).ready(function()
{
	var selected_rating=0;
	var orig_header_search='';

	$('.header .menu a').mouseover(function()
	{
		$(this).addClass('hover');
	});
	$('.header .menu a').mouseout(function()
	{
		$(this).removeClass('hover');
	});
	
	// включаем кнопку AddThis
	if ( typeof(addthis) != 'undefined')
	{
		addthis.button("#addthis-button");
	}
	
	function formatItem(row)
	{
		if (row[1])
		{
			return row[0] + " - " + row[1];
		}
		else
		{
			return false;
		}
	}
	function formatResult(row)
	{
		str = row[0];
		return str;
	}
	
	function selectResult(data)
	{
		if (data.extra && data.extra[1])
		{
			document.location=data.extra[1];
		}
	}
	// подключаем suggest (автозаполнение) для поиска похожих файлов
	// @see http://plugins.jquery.com/project/jq-autocomplete
	$('.search-input').autocomplete(
		'/api/search.php?o=suggest', {
		width			: 300,
		multiple		: false,
		matchContains	: true,
		maxItemsToShow	: 10,
		minChars		: 3,
		formatItem		: formatItem,
		formatResult	: formatResult,
		onItemSelect	: selectResult
	});

			
	
	
	if ( document.location.toString().match(/\#comment\-add/) )
	{
		$('#comment-add-form').show();
		document.location='#comment-add';
	}
	
	if ( $('#message-ok').attr('show')=='true')
	{
		$('#message-ok').show();
		document.location='#comments';
	}
	
	if ( $('#comment-add-form').attr('show')=='true')
	{
		$('#message-ok').hide();
		$('#comment-add-form').show();
		document.location='#comment-add';
	}
	
	/**
	 * click on 'explorer.exe' from search panel
	 */
	$('#search-example').click(function()
	{
		t=$(this).html();
		$('#search-input').val( t );
		return false;
	});

	/**
	 * click on 'Add you comment'
	 */
	$('a.comment-add').click(function()
	{
		$('#comment-add-form').show();
		document.location='#comment-add';
		return false;
	});
	
	/**
	 * captcha refresh
	 */
	$('a.captcha-refresh').click(function()
	{
		s=$('img#captcha').attr('src').replace(/\?.*?$/,"");
		s+='?r='+Math.floor(Math.random()*1000);
		$('img#captcha').attr('src', s);
		return false;
	});
	
	/**
	 * onfocus()  on input field from site header
	 */
	$('#header-search').focus(function()
	{
		if (!orig_header_search)
		{
			orig_header_search=$('#header-search').val();
		}
		if ( $('#header-search').val()==orig_header_search )
		{
			$('#header-search').val('');
			$('#header-search').removeClass('example');
		}
	});
	/**
	 * onclick() on submit "mini-search" (pages: /files/, /fileinfo/..., etc)
	 */
	$('div.header input[type=submit]').click(function()
	{
		if ( $('#header-search').val()=='Type in a process name')
		{
			$('#header-search').val('');
		}
	});
	
	/**
	 * blur input field from site header
	 */
	$('#header-search').blur(function()
	{
		if ( $('#header-search').val()=='' )
		{
			$('#header-search').val( orig_header_search );
			$('#header-search').addClass('example');
		};
	});
	
	/**
	 * on over rating stars
	 */
	$('.rating a').mouseover(function()
	{
		oRating=$(this).parent();
		oRating.removeClass('rating-green');
		oRating.removeClass('rating-yellow');
		oRating.removeClass('rating-red');
		
		rating=$(this).attr('r');
		if (rating==1)
		{
			oRating.addClass('rating-green');
			$('div.title',oRating).html('safe file');
		}
		else if (rating==2)
		{
			oRating.addClass('rating-yellow');
			$('div.title',oRating).html('unsafe file');
		}
		else if (rating==3)
		{
			oRating.addClass('rating-red');
			$('div.title',oRating).html('dangerous file');
		}
	});
	
	/**
	 * on out rating stars
	 */
	$('.rating a').mouseout(function()
	{
		oRating=$(this).parent();
		rating=new Number(oRating.attr('r'));
		if (rating == 0)
		{
			$('div.title',oRating).html('unrated file');
		}
		if (rating!=1)
		{
			oRating.removeClass('rating-green');
		}
		else
		{
			$('div.title',oRating).html('safe file');
			oRating.addClass('rating-green');
		}
		if (rating!=2)
		{
			oRating.removeClass('rating-yellow');
		}
		else
		{
			$('div.title',oRating).html('unsafe file');
			oRating.addClass('rating-yellow');
		}
		if (rating!=3)
		{
			oRating.removeClass('rating-red');
		}
		else
		{
			$('div.title',oRating).html('dangerous file');
			oRating.addClass('rating-red');
		}
	});
	
	/**
	 * click on rating stars
	 */
	$('.rating a').click(function()
	{
		rating=$(this).attr('r');
		oRating=$(this).parent();
		if (rating==oRating.attr('r') && !oRating.parent().hasClass('content') )
		{
			rating=0;
			$('.rating div.title').html('unrated file');
		}
	
		$('.rating a').each(function()
		{
			oRating=$(this).parent();
			oRating.removeClass('rating-green');
			oRating.removeClass('rating-yellow');
			oRating.removeClass('rating-red');
			
			if (rating==1)
			{
				oRating.addClass('rating-green');
				$('div.title',oRating).html('safe file ');
			}
			else if (rating==2)
			{
				oRating.addClass('rating-yellow');
				$('div.title',oRating).html('unsafe file');
			}
			else if (rating==3)
			{
				oRating.addClass('rating-red');
				$('div.title',oRating).html('dangerous file');
			}
			$('rating').attr( 'r', rating );
			oRating.attr( 'r', rating );
		});
		
		$('#rating-field').val( rating );
		
		return false;
	});
	
	$('.content .rating a').click(function()
	{
		$('#comment-add-form').show();
		document.location='#comment-add';
		return false;
	});
	
	// @TODO ajax send form
	$('#comment-add-form form').submit(function()
	{
		//return false;
	});
	
	$('a#blog-this').click(function()
	{
		$('div.into-blog').show();
		$(this).hide();
		$('.panel').css('height','100%');
		return false;
	});
	
	$('a#blog-how-look').click(function()
	{
		$('div.blog-how-look').toggle();
		$('.panel').css('height','100%');
		return false;
	});
	
	$('a#blog-copy').click(function()
	{
		if ($.browser.msie)
		{
			range=$('#blog-copy-text').get(0).createTextRange();
			range.execCommand('Copy');
			return false;
		}
	});
	
	if (!$.browser.msie)
	{
		$('a#blog-copy').hide();
		$('label#blog-label').show();
	}
	else
	{
		$('label#blog-label').hide();
		$('a#blog-copy').show();
	}
	
	VotesEvent();
	
});


function VotesEvent()
{
	$('.comment .votes a').click(function()
	{
		obj=$(this);
		url='/fileinfo/vote.php?cid='+$(this).parent().attr('cid')+'&vote='+$(this).attr('href').substr(1);
		$.ajax({
			type:'GET',
			url:url,
			dataType:'json',
			success:function(data)
			{
				if (data.msg)
				{
					$( 'em', obj.parent() ).html(data.msg);
					$( 'em', obj.parent() ).css('display','inline');
					$( 'em', obj.parent() ).fadeOut(2500);					
				}
				obj.html(data.new_value);
			}
		});
		return false;
	});
}
