﻿$(document).ready(function () {

	var mailRegex = /^[a-zA-Z0-9_'+*/^&=?~{}\-](\.?[a-zA-Z0-9_'+*/^&=?~{}\-])*\@((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\:\d{1,3})?)|(((([a-zA-Z0-9][a-zA-Z0-9\-]+[a-zA-Z0-9])|([a-zA-Z0-9]{1,2}))[\.]{1})+([a-zA-Z]{2,6})))$/;

	if( $("#subscribe_mail_input").toggleVal )
		$("#subscribe_mail_input").toggleVal();
    if( $(".name_search .form .text_input").toggleVal )
		$(".name_search .form .text_input").toggleVal();
    if( $(".get_name .text_input").toggleVal )
		$(".get_name .text_input").toggleVal();

	$(".subscribe_submit input").click( function(e) {
		e.preventDefault();
		if ( !$("#subscribe_mail_input").val().match( mailRegex ) ) alert("Неправильный email");
		else if ( UfologSite.includes.sidebar.SubscribeBlock.Subscribe( $("#subscribe_mail_input").val() ) )
			$(".popup_subscribe_messages").addClass("popup_active");
	} );

	$("a.popup_login_link").click(function () {
		$(document).scrollTop(0);
		$(".popup_login").toggleClass("popup_active");
	});

	$("a.popup_close").click(function () {
		$(this).parents(".popup").removeClass("popup_active");
	});

	$(".poll_list .result_link a").click(function () {
		$(this).parents(".poll_options").siblings(".poll_results_block").toggleClass("poll_results_block_active");
	});

	if( $('.period_list:visible').length )
		$('.period_list').jcarousel({});
	if( $('.big_photo_carusel').length )
		$('.big_photo_carusel').jcarousel({ scroll: 1 });

	$("a.filter").toggle(function () { $(this).text("скрыть фильтр"); }, function () { $(this).text("показать фильтр"); });

	$("a.filter").click(function () {
		$('div.theme_sect_block, div.period_select_block').toggle();
		if (!$('.period_list').data('jcarousel')) $('.period_list').jcarousel({});
	});

	$("#search").keypress(function (e) {
		if (e.which == 13) {
			e.preventDefault();
			location.href = "/search?text=" + $("#search").val();
		}
	});

	$("input.submit_button").click(function (e) {
		e.preventDefault();
		location.href = "/search?text=" + $("#search").val();
	});

	if( $.Lightbox ) {
		label = "Нажмите, чтобы увеличить";
		imgLabelForLightbox(label); 
		setTitleForLightbox();
		lightboxConstruct(); 

		$('div.big_photo_block .prew a').lightbox();
	}

	function imgLabelForLightbox(label) {
	  LIGHTBOX_IMGS_PATTERN = 'div.big_photo_block .prew a img'; 

	  label_code = '<div class="preview-image-caption">' + label + '</div>';

	  $(LIGHTBOX_IMGS_PATTERN).parent().after(label_code);

	  $(LIGHTBOX_IMGS_PATTERN).mouseover(function() {
		$(this).parent().parent().find(".preview-image-caption").css('visibility','visible');
	  });
	  $(LIGHTBOX_IMGS_PATTERN).mouseout(function() {
		$(this).parent().parent().find(".preview-image-caption").css('visibility','hidden');
	  });
	}

	function setTitleForLightbox() {
	  LIGHTBOX_TITLE_PATTERN = 'div.big_photo_block .prew a img';

	  EMPTY_TITLE = 'без заголовка';
  
	  $(LIGHTBOX_TITLE_PATTERN).each( function () {
		newTitle = EMPTY_TITLE
		title = $(this).attr('title');
		exifData = $(this).parent().find(".exif_data").text();
		titleData = $(this).parent().find(".title_data").text();
		if (title != "") newTitle = title;
		else if (exifData != "") newTitle = exifData;
		else if (titleData != "") newTitle = titleData;
    
		$(this).attr('title', newTitle);
	  });
	}

	function lightboxConstruct() {
	  $.Lightbox.construct({
		show_helper_text: false,
		show_extended_info: false,
		download_link: false,
		files: {			
			images: {
				prev: 'images/prev.ru.gif',
				next: 'images/next.ru.gif'
			}
		},
		text: {
		  image: 'Фото',
		  of: 'из',
		  close: 'закрыть',
		  closeInfo: '',
		  about: {
			text: '',
			link: ''
		  }
		}
	  })
	}

});
















