//СОБЫТИЯ
imgSrc='';
$(function(){
    
    $(".choise_img").click(function(){
        var id_img = $(this).attr("id_imgs");
        
        $.get(ajaxFile, {go:'change_img', id_img:id_img}, function(html){
			$("#header_img").html(html);
			
		});          
        
    });
	$('a.unselect').click(function(){
		$(this).next('.subul').slideToggle('normal');
		return false;
	});
    $('.list_link').hover(
		function() {
			$(this).find('ul').fadeIn('fast');
		},
		function() {
			$(this).find('ul').fadeOut('fast');
		}
	);
    $(".map_tour_block a").hover(function() {
		$(this).find('em').stop(true, true);
        $(this).find("em").animate({opacity: "show", top: "-85"}, "normal");
    }, function() {
        $(this).find("em").animate({opacity: "hide", top: "-105"}, "fast");
    });
   
    
	$("#header_img a").fancybox({
		"speedIn"			: 600, 
		"speedOut"			: 200, 
		"overlayShow"		: true,
		"centerOnScroll"	: true,
		"titlePosition"		: "over"
	});
    
	$('.fancy').fancybox();
	
	$.datepicker.setDefaults({
		firstDay: 1,
		dateFormat: 'yy-mm-dd',
		dayNames: ['Воскресенье', 'Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота'],
		dayNamesMin: ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'],
		monthNames: ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'],
		duration: 'fast',
		changeYear: true
		
	});

	$("#anyClass2").jCarouselLite({
        btnNext: ".bounceout .next2",
        btnPrev: ".bounceout .prev2",
		speed: 800,
		easing: "bounceout",
		visible: 6
    });	
	
    $('.list_link').hover(
        function() {
        $(this).find('ul').fadeIn('fast');
        },
        function() {
        $(this).find('ul').fadeOut('fast');
    });
    
    $(".map_tour_block a").hover(function() {
        $(this).find('em').stop(true, true);
        $(this).find("em").animate({opacity: "show", top: "-85"}, "normal");
        }, function() {
        $(this).find("em").animate({opacity: "hide", top: "-105"}, "fast");
    });

	// FEEDBACK
	$("#feedback").fancybox({
		'scrolling'  : 'no',
		'titleShow'  : false
	});
	$('#subs').click(
		function() {
			$('#subs_main').is(':hidden') ? $('#subs_main').fadeIn('fast') : $('#subs_main').fadeOut('fast');
			return false;
		}
	);
	//SUBSCRIBE
	$('#subscribe-add').click(function(){
		var vars = {
			ru : {
				errors : {
					emptyMail : 'Введите e-mail',
					wrongMail : 'E-mail некорректен'
				},
				loading : 'Загрузка',
				enter : 'Подписаться'
			},
			en : {
				errors : {
					emptyMail : 'Enter e-mail',
					wrongMail : 'E-mail is incorrect'
				},
				loading : 'Loading',
				enter : 'Subscribe'
			}
		};
		var that = this;
		var el = $('#subscribe-mail');
		var val = el.val();
		var msg = [];
		if( val=='' || val==el.attr('title') ) msg.push(vars[_LANG_].errors.emptyMail);
		else if( !val.match(/^[\d\w\.-]+@([\d\w-]+)((\.[\w\d-]+)+)?\.\w{2,6}$/) ) msg.push(vars[_LANG_].errors.wrongMail);
		
		if(msg.length>0){
			alert(msg.join("\n"));
			return false;
		}
		$(this).text(vars[_LANG_].loading).attr('disabled', 'disabled');
		$.get(ajaxFile, {go:'subscribe_add', mail:val}, function(html){
			$(that).text(vars[_LANG_].enter).removeAttr('disabled');
			alert(html);
		});
	});
	
	
	$('select#period').change(function(){
		$("select#period option:selected").each(function(){
			//$('.report').hide().html('Загрузка...').css('color', 'green').fadeIn('fast');
			$.get(ajaxFile, {go:'getNights', period:$(this).val()}, function(html){
				if (html == 'nodata'){
					$('.report').hide().html('Нет данных').css('color', 'red').fadeIn('fast');
				} else if (html == 'noid'){
					$('.report').hide().html('Не передан ID').css('color', 'red').fadeIn('fast');
				} else {
					// alert($('select#period').parent().html());
					// return false();
					$('#nights').html(html);
					$('.report').hide();
					// $("select#nights option").live('click', function(){
						// $(this).attr('selected', 'selected');
					// });
					$("select#nights :first").attr("selected", "selected").change();
				}
			});
		});
	});
	
	$('select#nights').change(function(){
		$("select#nights option:selected").each(function(){
			//$('.report').hide().html('Загрузка...').css('color', 'green').fadeIn('fast');
			$.get(ajaxFile, {go:'getTables', table:$(this).val()}, function(html){
				if (html == 'nodata'){
					//$('.report').hide().html('Нет данных').css('color', 'red').fadeIn('fast');
				} else if (html == 'noid'){
					//$('.report').hide().html('Не передан ID').css('color', 'red').fadeIn('fast');
				} else {
					// alert($('select#period').parent().html());
					// return false();
					$('#contenttable').html(html);
					//$('.report').hide();
				}
			});
		});
	});
	
	$('select#period :first').attr("selected", "selected").change();
	
	$('.list_link').hover(
		function() {
			$(this).find('ul').fadeIn('fast');
		},
		function() {
			$(this).find('ul').fadeOut('fast');
		}
	);
    $(".map_tour_block a").hover(function() {
		$(this).find('em').stop(true, true);
        $(this).find("em").animate({opacity: "show", top: "-85"}, "normal");
    }, function() {
        $(this).find("em").animate({opacity: "hide", top: "-105"}, "fast");
    });

	/*new js*/
    $(".accordion_list h4:first").addClass("active");
    $(".accordion_list div.divaccord:not(:first)").hide();
 
    $(".accordion_list h4").click(function(){
		$(this).next("div.divaccord").slideToggle("slow")
		.siblings("div.divaccord:visible").slideUp("slow");
		$(this).toggleClass("active");
		$(this).siblings("h4").removeClass("active");
	});
});

$(function(){
	$('.needClear')
		 .focus(function(){
			  if(this.value==this.title) this.value='';
		 })
		 .blur(function(){
			  if(this.value=='') this.value=this.title;
		 });
});

function checkSearchForm(f){
	var msg = [];
	
	if(f['what'].value=='Поиск по сайту' || f['what'].value=='') msg.push('Введите слово для поиска');
	else if(f['what'].value.length<3) msg.push('Слово для поиска должно быть длиннее 3-х символов');
	
	if(msg.length>0){
		alert(msg.join("\n"));
		return false;
	}
	return true;
}

function voteIt(btn, voting_id, answer_id){
	var btn = $(btn).attr({disabled:'disabled'});
	$.get(ajaxFile, {vote:voting_id, a_id:answer_id}, function(html){
		$('#voting-backup').html( $('#voting-screen').html() );
		$('#voting-screen').html( html );
		btn.removeAttr('disabled');
		if(answer_id==false) $('#back-to-vote').show();
	});
	return false;
}

function showVoting(){
	$('#voting-screen').html( $('#voting-backup').html() );
	$('#back-to-vote').hide();
	return false;
}
