// JavaScript Document
	function follow_link(link)
	{
		document.location = link;
	}



$(document).ready(function()
{

		
	
	$("#logo").click(function(){
		location.href = "/";
	});
	
	$("#news_own").click(function(){
		location.href = "/news/own.htm";
	});
	
	$("#news_partners").click(function(){
		location.href = "/news/partners.htm";
	});
	
	$("#news_business").click(function(){
		location.href = "/news/business.htm";
	});
	
	//$("input:file").uniform({fileBtnText: '', fileDefaultText: 'Пожалуйста, выберите файл'});	
	
	/* sort buttons */
	$("#sort_unixstamp").click(function(){

		var type = $("#home_news_type").val();

		$.post("/home_tabs.php", "type="+type+"&sort=unixstamp",
				function(data){
					if (data != '')
					{
						$("#sort_own_container").css('height', 'auto');
						$("#sort_own_container").html(data);
						$("#sort_unixstamp").attr('class', 'home_news_sort_date_selected');
						$("#sort_rating").attr('class', 'home_news_sort_rating');
						$("#sort_views").attr('class', 'home_news_sort_comments');
					}
				});
	});
	$("#sort_rating").click(function(){

		var type = $("#home_news_type").val();

		$.post("/home_tabs.php", "type="+type+"&sort=rating",
				function(data){
					if (data != '')
					{
						$("#sort_own_container").css('height', 'auto');
						$("#sort_own_container").html(data);
						$("#sort_unixstamp").attr('class', 'home_news_sort_date');
						$("#sort_rating").attr('class', 'home_news_sort_rating_selected');
						$("#sort_views").attr('class', 'home_news_sort_comments');
					}
				});
	});
	$("#sort_views").click(function(){

		var type = $("#home_news_type").val();

		$.post("/home_tabs.php", "type="+type+"&sort=views",
				function(data){
					if (data != '')
					{
						$("#sort_own_container").css('height', 'auto');
						$("#sort_own_container").html(data);
						$("#sort_unixstamp").attr('class', 'home_news_sort_date');
						$("#sort_rating").attr('class', 'home_news_sort_rating');
						$("#sort_views").attr('class', 'home_news_sort_comments_selected');
					}
				});
	});

	/* home news tabs */

	$("#home_news_business").click(function (){

		$.post("/home_tabs.php", "type=business",
				function(data){
					if (data != '')
					{
						$("#sort_own_container").css('height', 'auto');
						$("#sort_own_container").html(data);
						$("#sort_unixstamp").attr('class', 'home_news_sort_date_selected');
						$("#sort_rating").attr('class', 'home_news_sort_rating');
						$("#sort_views").attr('class', 'home_news_sort_comments');
						
						$("#home_news_type").val('business');
						$("#home_news_partners").attr('class', 'home_news_tab');
						$("#home_news_own").attr('class', 'home_news_tab');
						$("#home_news_business").attr('class', 'home_news_tab_selected');
					}
				});
	});
	$("#home_news_own").click(function (){
		
		$.post("/home_tabs.php", "type=own",
				function(data){
					if (data != '')
					{
						$("#sort_own_container").css('height', 'auto');
						$("#sort_own_container").html(data);
						$("#sort_unixstamp").attr('class', 'home_news_sort_date_selected');
						$("#sort_rating").attr('class', 'home_news_sort_rating');
						$("#sort_views").attr('class', 'home_news_sort_comments');

						$("#home_news_type").val('own');
						$("#home_news_partners").attr('class', 'home_news_tab');
						$("#home_news_business").attr('class', 'home_news_tab');
						$("#home_news_own").attr('class', 'home_news_tab_selected');
					}
				});
				
				
	});
	$("#home_news_partners").click(function (){
		$.post("/home_tabs.php", "type=partners",
				function(data){
					if (data != '')
					{
						$("#sort_own_container").css('height', 'auto');
						$("#sort_own_container").html(data);
						$("#sort_unixstamp").attr('class', 'home_news_sort_date_selected');
						$("#sort_rating").attr('class', 'home_news_sort_rating');
						$("#sort_views").attr('class', 'home_news_sort_comments');
						
						$("#home_news_type").val('partners');
						$("#home_news_partners").attr('class', 'home_news_tab_selected');
						$("#home_news_business").attr('class', 'home_news_tab');
						$("#home_news_own").attr('class', 'home_news_tab');
					}
				});
	});
	
	// add comments
	
	$("#comment_submit").click(function(){
		var comment = $("#comments_area").val();
		if (comment != '')
		{
			document.comment_form.submit();
		}
		else
							$.fancybox({
							'transitionIn'	:	'fade',
							'transitionOut'	:	'fade',
							'speedIn'		:	200, 
							'speedOut'		:	200, 
							'width'			:	200,
							'height'		:	100,
							'autoDimensions':	false,
							'overlayShow'	:	true,
							'overlayColor'	:	'#666',
							'overlayOpacity':	0.6,
							'titlePosition' :	'inside',
							'hideOnContentClick': true,
							'content'		:	'Напишите что-нибудь, Вы пытаетесь отправить пустой комментарий...'
							});
	});
	

	/* news calendar */

	$("#calendar_arrows_prev").click(function(){
		
		var cur_time = $("#cur_time").val();
		var type = $("#calendar_type").val();
		
		$.post("/ajax_calendar.php?prev", "cur_time="+cur_time+"&type="+type+"&css=frontend",
				function(data){
					if (data != '')
					{
						$("#calendar").html(data);
					}
				});
				
		$.post("/ajax_calendar.php?prev_setup_time", "cur_time="+cur_time+"&type="+type+"&css=frontend",
				function(data){
					if (data != '')
					{
						$("#cur_time").val(data);
					}
				});
	});
	$("#calendar_arrows_next").click(function(){
		
		var cur_time = $("#cur_time").val();
		var type = $("#calendar_type").val();
		
		$.post("/ajax_calendar.php?next", "cur_time="+cur_time+"&type="+type+"&css=frontend",
				function(data){
					if (data != '')
					{
						$("#calendar").html(data);
					}
				});
				
		$.post("/ajax_calendar.php?next_setup_time", "cur_time="+cur_time+"&type="+type+"&css=frontend",
				function(data){
					if (data != '')
					{
						$("#cur_time").val(data);
					}
				});
	});
	
	/* user popups */
	$("#reg").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'width'			:	470,
			'height'		:	400,
			'overlayShow'	:	true,
			'overlayOpacity':	0.6,
			'titlePosition' :	'inside',
			'hideOnContentClick': false,
			'padding'		: 	0,
			'margin'		:	0,
			'modal'			: 	true,
			'type'			:	'iframe',
			'href'			:	'/registration.htm',
			'onClosed'		:	function() {
														//document.reload();
														location.reload();
											}
	});
	$("#auth").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'width'			:	470,
			'height'		:	300,
			'overlayShow'	:	true,
			'overlayOpacity':	0.6,
			'titlePosition' :	'inside',
			'hideOnContentClick': false,
			'padding'		: 	0,
			'margin'		:	0,
			'modal'			: 	true,
			'type'			:	'iframe',
			'href'			:	'/login.htm',
			'onClosed'		:	function() {
														//document.reload();
														location.reload();
											}
	});
	
	$(".reg").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'width'			:	470,
			'height'		:	400,
			'overlayShow'	:	true,
			'overlayOpacity':	0.6,
			'titlePosition' :	'inside',
			'hideOnContentClick': false,
			'padding'		: 	0,
			'margin'		:	0,
			'modal'			: 	true,
			'type'			:	'iframe',
			'href'			:	'/registration.htm',
			'onClosed'		:	function() {
														//document.reload();
														location.reload();
											}
	});
	$(".auth").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'width'			:	470,
			'height'		:	300,
			'overlayShow'	:	true,
			'overlayOpacity':	0.6,
			'titlePosition' :	'inside',
			'hideOnContentClick': false,
			'padding'		: 	0,
			'margin'		:	0,
			'modal'			: 	true,
			'type'			:	'iframe',
			'href'			:	'/login.htm',
			'onClosed'		:	function() {
														//document.reload();
														location.reload();
											}
	});
	
	$("#header_sms_news").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'width'			:	470,
			'height'		:	400,
			'overlayShow'	:	true,
			'overlayOpacity':	0.6,
			'titlePosition' :	'inside',
			'hideOnContentClick': false,
			'padding'		: 	0,
			'margin'		:	0,
			'modal'			: 	true,
			'type'			:	'iframe',
			'href'			:	'/sms_subscribe.htm'
	});
	
	$("#header_voice").click(function(){
		location.href = "/send_mail.htm";
	});
/* sendmail */

	$("#sendmail_submit").click(function()
	{
		var mail_text = $("#sendmail_area").val();
		var name = $("#sendmail_name").val();
		var email = $("#sendmail_email").val();
		var recaptcha = $("#recaptcha_response_field").val();



		$("#sendmail_name").attr('class', 'table_input1');
		$("#sendmail_email").attr('class', 'table_input1');
		
		jQuery("div.sendmail_error").each(function (index, value){
			$(value).html('');
			});
		jQuery("#sendmail_error_recaptcha").html('');	

		function isValidEmail (email)
		{
			return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9_\-.]*\.)+[a-z]{2,4}$/i).test(email);
		}


		if (name == '')
		{
			jQuery("#sendmail_error_name").html('Представьтесь, пожалуйста');	
			jQuery("#sendmail_name").attr('class', 'table_input1_error');
		}

		if (recaptcha == '')
		{
			jQuery("#sendmail_error_recaptcha").html('Введите код на картинке слева!');	
		}

		if (email == '')
		{
			jQuery("#sendmail_error_email").html('Введите e-mail для обратной связи');	
			jQuery("#sendmail_email").attr('class', 'table_input1_error');
		}
		
		if ((email != '') && (!isValidEmail(email)))
		{
			jQuery("#sendmail_error_email").html('Некорректный e-mail');	
			jQuery("#sendmail_email").attr('class', 'table_input1_error');
		}

		if (mail_text == '')
		{
							$.fancybox({
							'transitionIn'	:	'fade',
							'transitionOut'	:	'fade',
							'speedIn'		:	200, 
							'speedOut'		:	200, 
							'width'			:	200,
							'height'		:	100,
							'autoDimensions':	false,
							'overlayShow'	:	true,
							'overlayColor'	:	'#666',
							'overlayOpacity':	0.6,
							'titlePosition' :	'inside',
							'hideOnContentClick': true,
							'content'		:	'Напишите что-нибудь, Вы пытаетесь отправить пустое сообщение...'
							});
		}
		if ((email != '') && isValidEmail(email) && (mail_text != '') && (name != '') && (recaptcha != ''))
		{
			document.sendmail_form.submit();
		}
	});

/* profile */
	$("#profile_account_submit").click(function()
	{
		var login = $("#login").val();
		var pwd = $("#pwd").val();
		var email = $("#email").val();

		$("#login").attr('class', 'table_input1');
		$("#email").attr('class', 'table_input1');
		$("#pwd").attr('class', 'table_input1');
		
		$("div.sendmail_error").each(function (index, value){
			$(value).html('');
			});

		function isValidEmail (email)
		{
			return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9_\-.]*\.)+[a-z]{2,4}$/i).test(email);
		}


		if (login == '')
		{
			$("#login_error").html('Введите логин');	
			$("#login").attr('class', 'table_input1_error');
		}

		if (pwd == '')
		{
			$("#pwd_error").html('Введите пароль');	
			$("#pwd").attr('class', 'table_input1_error');
		}

		if (email == '')
		{
			$("#email_error").html('Введите e-mail для обратной связи с Вами');	
			$("#email").attr('class', 'table_input1_error');
		}
		
		if ((email != '') && (!isValidEmail(email)))
		{
			$("#email_error").html('Некорректный e-mail');	
			$("#email").attr('class', 'table_input1_error');
		}

		if ((email != '') && isValidEmail(email) && (pwd != '') && (login != ''))
		{
			document.profile_account.submit();
		}
	});

	$("#profile_maininfo_submit").click(function()
	{
			document.profile_maininfo.submit();
	});

	$("#profile_addinfo_submit").click(function()
	{
			document.profile_addinfo.submit();
	});



	/* search */	
	$("#header_search_submit").click(function(){
		
		var search_query = $("#header_search_input").val();
		
		if (search_query != '' && search_query != 'Введите поисковый запрос')
		{
			document.search_form.submit();
		}
		else
			$.fancybox({
							'transitionIn'	:	'fade',
							'transitionOut'	:	'fade',
							'speedIn'		:	200, 
							'speedOut'		:	200, 
							'width'			:	200,
							'height'		:	100,
							'autoDimensions':	false,
							'overlayShow'	:	true,
							'overlayColor'	:	'#666',
							'overlayOpacity':	0.6,
							'titlePosition' :	'inside',
							'hideOnContentClick': true,
							'content'		:	'Вы не ввели слова для поиска. Введите поисковый запрос и попробуйте еще раз.'
			});
	});

	$("#ext_search_submit").click(function(){
		
		var search_query = $("#search_ext").val();
		
		if (search_query != '' && search_query != 'Введите поисковый запрос')
		{
			document.search_ext_form.submit();
		}
		else
			$.fancybox({
							'transitionIn'	:	'fade',
							'transitionOut'	:	'fade',
							'speedIn'		:	200, 
							'speedOut'		:	200, 
							'width'			:	200,
							'height'		:	100,
							'autoDimensions':	false,
							'overlayShow'	:	true,
							'overlayColor'	:	'#666',
							'overlayOpacity':	0.6,
							'titlePosition' :	'inside',
							'hideOnContentClick': true,
							'content'		:	'Вы не ввели слова для поиска. Введите поисковый запрос и попробуйте еще раз.'
			});
	});
	
	/* rating functions */
	$("#articles_stars_submit").click(function()
	{
		//alert ('star');
		var user_id = $("#user_id").val();
		var about_id = $("#about_id").val();
		var type = $("#type").val();
		var rating = $("input[name='rating']:checked").val();
		//alert (user_id);
		//return false;
		if ((user_id == 'undefined') || (user_id == ''))
		{
			$.fancybox({
							'transitionIn'	:	'fade',
							'transitionOut'	:	'fade',
							'speedIn'		:	200, 
							'speedOut'		:	200, 
							'width'			:	200,
							'height'		:	100,
							'autoDimensions':	false,
							'overlayShow'	:	true,
							'overlayColor'	:	'#666',
							'overlayOpacity':	0.6,
							'titlePosition' :	'inside',
							'hideOnContentClick': true,
							'content'		:	'Только для зарегистрированных пользователей. Зарегистрируйтесь или войдите на сайт под своим логином и паролем.'
				
				});
		}
		else
		if (rating > 1)
		{
				jQuery.post("/voting.php", "user_id="+user_id+"&about_id="+about_id+"&type="+type+"&rating="+rating,
					function(data){
						if (data == '1')
						{
							$.fancybox({
							'transitionIn'	:	'fade',
							'transitionOut'	:	'fade',
							'speedIn'		:	200, 
							'speedOut'		:	200, 
							'width'			:	200,
							'height'		:	100,
							'autoDimensions':	false,
							'overlayShow'	:	true,
							'overlayColor'	:	'#666',
							'overlayOpacity':	0.6,
							'titlePosition' :	'inside',
							'hideOnContentClick': true,
							'content'		:	'Ваша оценка учтена в общем рейтинге! Спасибо :)'
							});
						}
						else
						if (data == '0')
						{
							$.fancybox({
							'transitionIn'	:	'fade',
							'transitionOut'	:	'fade',
							'speedIn'		:	200, 
							'speedOut'		:	200, 
							'width'			:	200,
							'height'		:	100,
							'autoDimensions':	false,
							'overlayShow'	:	true,
							'overlayColor'	:	'#666',
							'overlayOpacity':	0.6,
							'titlePosition' :	'inside',
							'hideOnContentClick': true,
							'content'		:	'Ваш голос уже был учтен ранее. Один пользователь может голосовать только 1 раз за одну новость или статью'
							});
						}
					});
		}
		else
							$.fancybox({
							'transitionIn'	:	'fade',
							'transitionOut'	:	'fade',
							'speedIn'		:	200, 
							'speedOut'		:	200, 
							'width'			:	400,
							'height'		:	200,
							'autoDimensions':	false,
							'overlayShow'	:	true,
							'overlayColor'	:	'#666',
							'overlayOpacity':	0.6,
							'titlePosition' :	'inside',
							'hideOnContentClick': true,
							'content'		:	'Поставьте оценку, нажав на соответствующую звездочку'
							});
	});
	
	// comments_rating
	$(".comment_rate_plus").click(function()
	{
		var parts = jQuery(this).attr('id').split('_');
		
		var comment_id = parts[1];
		var user_id = $("#user_id").val();
		var about_id = $("#about_id").val();
		var vote = '1';
		//alert (user_id);
		//return false;

		if ((user_id == 'undefined') || (user_id == ''))
		{
			$.fancybox({
							'transitionIn'	:	'fade','transitionOut'	:	'fade',	'speedIn'		:	200, 'speedOut'		:	200, 'width'			:	200,'height'		:	100,'autoDimensions':	false,
							'overlayShow'	:	true,'overlayColor'	:	'#666','overlayOpacity':	0.6,'titlePosition' :	'inside','hideOnContentClick': true,
							'content'		:	'Только для зарегистрированных пользователей. Зарегистрируйтесь или войдите на сайт под своим логином и паролем.'
				
				});
		}
		else
		{
				jQuery.post("/voting.php", "user_id="+user_id+"&comment_id="+comment_id+"&rating_comments="+vote,
					function(data){
						if (data == '1')
						{
							$.post("/voting.php", "get_rating="+comment_id,
							function(data){
								if (data >= 0)
								$("#com_rate_container_"+comment_id).attr('class', 'comment_rate_container');
								else if (data > 0)
								$("#com_rate_container_"+comment_id).attr('class', 'comment_rate_container_red');

								$("#com_rate_container_"+comment_id).text(data);
								});
							$.fancybox({
							'transitionIn'	:	'fade','transitionOut'	:	'fade',	'speedIn'		:	200, 'speedOut'		:	200, 'width'			:	200,'height'		:	100,'autoDimensions':	false,
							'overlayShow'	:	true,'overlayColor'	:	'#666','overlayOpacity':	0.6,'titlePosition' :	'inside','hideOnContentClick': true,
							'content'		:	'Ваша оценка учтена в общем рейтинге комментария! Спасибо :)'
							});
						}
						else
						if (data == '0')
						{
							$.fancybox({
							'transitionIn'	:	'fade','transitionOut'	:	'fade',	'speedIn'		:	200, 'speedOut'		:	200, 'width'			:	200,'height'		:	100,'autoDimensions':	false,
							'overlayShow'	:	true,'overlayColor'	:	'#666','overlayOpacity':	0.6,'titlePosition' :	'inside','hideOnContentClick': true,
							'content'		:	'Ваш голос уже был учтен ранее. Один пользователь может голосовать единожды за один комментарий'
							});
						}
					});
		}
	});

	$(".comment_rate_minus").click(function()
	{
		var parts = jQuery(this).attr('id').split('_');
		
		var comment_id = parts[1];
		var user_id = $("#user_id").val();
		var about_id = $("#about_id").val();
		var vote = '-1';
		//alert (user_id);
		//return false;

		if ((user_id == 'undefined') || (user_id == ''))
		{
			$.fancybox({
							'transitionIn'	:	'fade','transitionOut'	:	'fade',	'speedIn'		:	200, 'speedOut'		:	200, 'width'			:	200,'height'		:	100,'autoDimensions':	false,
							'overlayShow'	:	true,'overlayColor'	:	'#666','overlayOpacity':	0.6,'titlePosition' :	'inside','hideOnContentClick': true,
							'content'		:	'Только для зарегистрированных пользователей. Зарегистрируйтесь или войдите на сайт под своим логином и паролем.'
				
				});
		}
		else
		{
				jQuery.post("/voting.php", "user_id="+user_id+"&comment_id="+comment_id+"&rating_comments="+vote,
					function(data){
						if (data == '1')
						{
							$.post("/voting.php", "get_rating="+comment_id,
							function(data){
								if (data >= 0)
								$("#com_rate_container_"+comment_id).attr('class', 'comment_rate_container');
								else if (data < 0)
								$("#com_rate_container_"+comment_id).attr('class', 'comment_rate_container_red');

								$("#com_rate_container_"+comment_id).text(data);
								});
							$.fancybox({
							'transitionIn'	:	'fade','transitionOut'	:	'fade',	'speedIn'		:	200, 'speedOut'		:	200, 'width'			:	200,'height'		:	100,'autoDimensions':	false,
							'overlayShow'	:	true,'overlayColor'	:	'#666','overlayOpacity':	0.6,'titlePosition' :	'inside','hideOnContentClick': true,
							'content'		:	'Ваша оценка учтена в общем рейтинге комментария! Спасибо :)'
							});
						}
						else
						if (data == '0')
						{
							$.fancybox({
							'transitionIn'	:	'fade','transitionOut'	:	'fade',	'speedIn'		:	200, 'speedOut'		:	200, 'width'			:	200,'height'		:	100,'autoDimensions':	false,
							'overlayShow'	:	true,'overlayColor'	:	'#666','overlayOpacity':	0.6,'titlePosition' :	'inside','hideOnContentClick': true,
							'content'		:	'Ваш голос уже был учтен ранее. Один пользователь может голосовать единожды за один комментарий'
							});
						}
					});
		}
	});
}); 
