// Смена изображений проверки полей
(function($) {
	$.fn.SetInfo = function( ImageName ){

		if ( ImageName == undefined || ImageName == '' ) return this;
		
		if ( $(this).next( 'img' ).length ) $(this).next( 'img' ).attr( 'src', '/tpl/images/' + ImageName + '.jpg' );
		else if ( $(this).next().next( 'img' ).length ) $(this).next( ).next( 'img' ).attr( 'src', '/tpl/images/' + ImageName + '.jpg' );
		else if ( $(this).parent().next( 'td' ).children( 'img' ).length ) $(this).parent().next( 'td' ).children( 'img' ).attr( 'src', '/tpl/images/' + ImageName + '.jpg' );

		return this;
	};
})(jQuery);
// Смена изображений проверки полей

$(window).ready(function()
{
	$('#slide').jcarousel({	
		auto: 4,
		scroll: 1,
		wrap: 'circular'
	});

    $('#mycarousel').jcarousel({ 
		start: 5,
		scroll: 1
	});
	
	$( '#AllPic > div > img' ).click(function () {

		ID = $( this ).attr( 'alt' );

		Pic( );
		
		$( '#ShowPic' ).css(
		{
			top : $(window).scrollTop() + 'px',
			left : '103px',
			position : 'absolute',
			zIndex : 1005
		}).slideDown( 'slow' );
		
		$( '#overlay' ).fadeTo( 'slow', 0.8 );
	});

	$(".kalend > ul > li > a").live( 'click', function () {
		$(this).next( 'div.date_pop' ).show( 'slow' );
		$(this).parent().mouseleave( function() {$(this).children( 'div.date_pop' ).hide( 'slow' );});
	});
	
	$('#OrderDate, #ReserveDate').datetimepicker();
	
	$( ".captcha" ).click(function () { 
		$( this ).attr( "src", "/tpl/captcha/index.php?load=" + Math.random() );
	});
	
	$( '#overlay' ).click(function () {
		ClosePanel( 'OrderDinner', 1 );
		ClosePanel( 'ALLPictures', 1 );
		ClosePanel( 'AddPhone', 1 );
		ClosePic();
		CloseReserve();
	});
	
// Проверка правильности заполения форм

	$("#OrderDate, #ReserveDate").change( function( ) {
		if ( /^[0-9]{2}\-[0-9]{2}\-[0-9]{4} [0-9]{2}:[0-9]{2}$/.test( $(this).val( ) ) ) $(this).SetInfo( 'yes1' );
		else $(this).SetInfo( 'no1' );
	});

	$("#OrderNum, #ReserveNum").keyup( function( ) {
		if ( $(this).val( ).length > 0 && $(this).val( ).length < 4 && /\d+/.test( $(this).val( ) ) ) $(this).SetInfo( 'yes1' );
		else $(this).SetInfo( 'no1' );
	});

	$("#OrderMail, #PicMail, #ComMail, #ReserveMail").keyup( function( ) {
		if ( /^([a-z0-9]([\-\_\.]*[a-z0-9])*)+@([a-z0-9]([\-]*[a-z0-9])*\.)+[a-z]{2,6}$/.test( $(this).val( ) ) ) $(this).SetInfo( 'yes1' );
		else $(this).SetInfo( 'no1' );
	});

	$("#OrderText, #PicComment, #ComComment").keyup( function( ) {
		if ( $(this).val( ).length > 5 ) $(this).SetInfo( 'yes2' );
		else $(this).SetInfo( 'no2' );
	});

	$("#PicCaptcha, #ComCaptcha").keyup( function( ) {

		if ( $(this).val( ).length == 3 && /^[a-z0-9]{3}$/i.test( $(this).val( ) ) ) $(this).SetInfo( 'yes1' );
		else $(this).SetInfo( 'no1' );
	});
	
	$("#OrderFIO, #PhoneName, #PicName, #ComName, #ReserveFIO").keyup( function( ) {
	
		if ( $( this ).attr( 'id' ) == 'PhoneName' )
		{
			if ( $(this).val( ).length > 3 ) $(this).SetInfo( 'yes' );
			else $(this).SetInfo( 'no' );
		} else
		{
			if ( $(this).val( ).length > 3 ) $(this).SetInfo( 'yes1' );
			else $(this).SetInfo( 'no1' );
		}
	});

	$("#Phone, #PhoneDel").keydown( function( event ) {
		if ( event.keyCode == 9 ) return;
		
		if ( $(this).val( ).length > 12 && event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39 ) return false;
	
		if ( event.keyCode != 8 && ( /^[0-9]{3}$/.test( $(this).val( ) ) ||
		/^[0-9]{3}\-[0-9]{3}$/.test( $(this).val( ) ) ||
		/^[0-9]{3}\-[0-9]{3}\-[0-9]{2}$/.test( $(this).val( ) ) ) ) $(this).val( $(this).val( ) + '-' );
	});

	$("#Phone, #PhoneDel").keyup( function( event ) {
		if ( $(this).attr( 'id' ) == 'Phone' )
		{
			if ( /^[0-9]{3}\-?[0-9]{3}\-?[0-9]{2}\-?[0-9]{2}$/.test( $(this).val( ) ) ) $(this).SetInfo( 'yes' );
			else $(this).SetInfo( 'no' );
		} else
		{
			if ( /^[0-9]{3}\-?[0-9]{3}\-?[0-9]{2}\-?[0-9]{2}$/.test( $(this).val( ) ) ) $(this).SetInfo( 'yes1' );
			else $(this).SetInfo( 'no1' );
		}
	});

	$("#OrderPhone, #ReservePhone").keydown( function( event ) {
		if ( event.keyCode == 9 ) return;
		
		if ( $(this).val( ).length > 12 && event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39 ) return false;
	
		if ( event.keyCode != 8 && ( /^[0-9]{3}$/.test( $(this).val( ) ) ||
		/^[0-9]{3}\-[0-9]{3}$/.test( $(this).val( ) ) ||
		/^[0-9]{3}\-[0-9]{3}\-[0-9]{2}$/.test( $(this).val( ) ) ) ) $(this).val( $(this).val( ) + '-' );
	});

	$("#OrderPhone, #ReservePhone").keyup( function( event ) {
		if ( /^[0-9]{3}\-?[0-9]{3}[\-0-9]+$/.test( $(this).val( ) ) ) $(this).SetInfo( 'yes1' );
		else $(this).SetInfo( 'no1' );
	});
	
	$("#ReserveDesk").keyup( function( event ) {
		if ( /^[0-9]{1,2} ?((, ?[0-9]{1,2})+)?$/.test( $(this).val( ) ) ) $(this).SetInfo( 'yes1' );
		else $(this).SetInfo( 'no1' );
	});

// Проверка правильности заполения форм
	
// Кэшируем изображения в браузер
	$( '<img />' ).attr( 'src', '/tpl/images/pop_close_hover.png' );
	$( '<img />' ).attr( 'src', '/tpl/images/yes.jpg' );
	$( '<img />' ).attr( 'src', '/tpl/images/yes1.jpg' );
	$( '<img />' ).attr( 'src', '/tpl/images/yes2.jpg' );
// Кэшируем изображения в браузер

});

function ShowHeader()
{
	$( 'html, body' ).animate( { scrollTop: 300 }, 1000 );
}

// Открытие и закрытие окон
function ShowPanel( ID, overlay )
{
	if ( ID == 'Loading' ) ZIndex = '2000';
	else ZIndex = '1000';
	
	$( "#" + ID ).css(
	{
		left : ( $( window ).width() - $( "#" + ID ).width() ) / 2 + "px",
		top : ( $( window ).height() - $( "#" + ID ).height() ) / 2 + "px",
		position : 'fixed',
		zIndex : ZIndex
	}).fadeIn( 'slow' );
	
	if ( overlay != undefined && overlay ) $( '#overlay' ).fadeTo( 'slow', 0.8 );
	
	return;
}

function ShowMaps( )
{
	$( "#Maps" ).css(
	{
		top : $( window ).scrollTop() + 'px',
		left : '200px',
		position : 'absolute',
		zIndex : '1000'
	}).fadeIn( 'slow' );
	
	$( '#overlay' ).fadeTo( 'slow', 0.8 );
	
	return;
}

function ClosePanel( ID, overlay )
{
	if ( ID == 'Loading' ) setTimeout(function(){$( "#Loading" ).fadeOut( 'slow' );}, 1500 );
	else
	{
		$( "#" + ID ).fadeOut( 'slow' );
	
		if ( overlay != undefined && overlay ) $( '#overlay' ).fadeOut( 'slow' );
	}
	
	return;
}

function ClosePic( )
{
	ClosePanel( 'ShowPic', 1 );
	
	$( 'img.bord' ).slideUp( 'slow', function(){$(this).attr( 'src', '' );});
}

// Открытие и закрытие окон

// Закрытие окна бронирования столиков
function CloseReserve( )
{
	ClosePanel( 'Reserve', 1 );
	
	$( 'div[id^=t]' ).each(function(){
		$( this ).children( 'div' ).css( 'visibility', 'hidden' );
		$( this ).css( 'background-position', '0px 0px' );
	});
}
// Закрытие окна бронирования столиков


//Загрузка календаря

function LoadCalendar( Month, Year )
{
	$( '#Loading > span' ).text( 'Загрузка календаря' );
	ShowPanel( 'Loading' );
	
	$.post( "/ajax/Calendar.php", { month: Month, year: Year }, function(data){
		$("#Calendar").fadeOut( 'slow', function() {$(this).html(data).fadeIn( 'slow' );});
	});
	
	ClosePanel( 'Loading' );
}
//Загрузка календаря


//Подписка СМС
function SendSMS()
{
	$( '#Loading > span' ).text( 'Отправка данных' );
	ShowPanel( 'Loading' );
	
	$.post( "/ajax/SendSMS.php", { phone: $( "#Phone" ).val( ), name: $( "#PhoneName" ).val( ) }, function( data )
	{
		if ( data == 0 )
		{
			ClosePanel( 'AddPhone', 1 );
			
			$( '#Loading > span' ).text( 'Телефон успешно добавлен' );
			
			ClosePanel( 'Loading' );

			return;
		}

		if ( data == 1 ) data = 'Телефон уже существует в базе данных';
		else if ( data == -1 ) data = 'Телефон введен не верно';
		else if ( data == -2 ) data = 'Имя не введено';
		else if ( data == -3 ) data = 'Имя введено не верно';
		else if ( data == -4 ) data = 'Имя должно быть меньше 50 символов';
		else data = 'Неизвестная ошибка';
		
		$( '#Loading > span' ).text( data );
	});
	
	ClosePanel( 'Loading' );
}

//Подписка СМС


//Отказ от подписки
function DelSMS()
{
	$( '#Loading > span' ).text( 'Выполняется удаление' );
	ShowPanel( 'Loading' );
	
	$.post( "/ajax/SendSMS.php", { id: $( "#PhoneDel" ).val( ) }, function( data )
	{
		if ( data == 0 )
		{
			ClosePanel( 'AddPhone', 1 );
			
			$( '#Loading > span' ).text( 'Телефон удален!' );
			
			ClosePanel( 'Loading' );

			return;
		}
		
		else if ( data == -1 ) data = 'Телефон введен не верно';
		else data = 'Неизвестная ошибка';
		
		$( '#Loading > span' ).text( data );
	});
	
	ClosePanel( 'Loading' );
}
//Отказ от подписки


// Заказ банкета
function SendOrder()
{
	$( '#Loading > span' ).text( 'Отправка данных' );
	ShowPanel( 'Loading' );
	
	$.post( "/ajax/SendOrder.php", { date: $( "#OrderDate" ).val( ), num: $( "#OrderNum" ).val( ), name: $( "#OrderFIO" ).val( ), phone : $( "#OrderPhone" ).val( ), mail : $( "#OrderMail" ).val( ), text : $( "#OrderText" ).val( ) }, function( data )
	{
		if ( data == 0 )
		{
			ClosePanel( 'OrderDinner', 1 );
			
			$( '#Loading > span' ).text( 'Заказ принят' );
			
			ClosePanel( 'Loading' );

			return;
		}

		$( '#Loading > span' ).html( data );
	});

	ClosePanel( 'Loading' );
}

// Заказ банкета

// Перезагрузка комментариев
function ReComment( )
{
	$( '#Loading > span' ).text( 'Обновление комментариев' );
	ShowPanel( 'Loading' );
	
	$.post( "/ajax/AddComment.php", { action : 'show' }, function(data){
		$("#AllComment").fadeOut( 'slow', function() {$(this).html(data).fadeIn( 'slow' );});
	});
	
	ClosePanel( 'Loading' );
}
// Перезагрузка комментариев

//Добаление комментария
function AddComment()
{
	$( '#Loading > span' ).text( 'Добавление комментария' );
	ShowPanel( 'Loading' );
	
	$.post( "/ajax/AddComment.php", { name: $( "#ComName" ).val( ), mail: $( "#ComMail" ).val( ), comment : $( "#ComComment" ).val( ), captcha : $( "#ComCaptcha" ).val( ) }, function(data){

		if ( data == 0 )
		{
			ClosePanel( 'AddComment' );
			
			$( '#Loading > span' ).text( 'Комментарий успешно добавлен!' );
			$( ".captcha" ).attr( "src", "/tpl/captcha/index.php?load=" + Math.random() );
			
			$( "#ComCaptcha" ).val( '' );
			$( "#ComComment" ).val( '' );
			
			ClosePanel( 'Loading' );
			
			ReComment( );
			return;
		}
		
		$( '#Loading > span' ).html( data );
		
		$( ".captcha" ).attr( "src", "/tpl/captcha/index.php?load=" + Math.random() );
		$( "#ComCaptcha" ).val( '' );
	});
	
	ClosePanel( 'Loading' );
}

//Добаление комментария

// Показ формы ответа на комментарий
function ShowResponse( ID )
{
	$( '#Loading > span' ).text( 'Получение данных' );
	ShowPanel( 'Loading' );
	
	$.post( "/ajax/EditComment.php", { id: ID, action : 'showresponse' }, function(data){
		
		$("#EditComment").fadeOut( 'slow', function() {$(this).html(data).css(
		{
			left : ( $( window ).width()  - $( this ).width() ) / 2 + "px",
			top : ( $( window ).height() - $( this ).height() ) / 2 + "px",
			position : 'fixed',
			zIndex : '1000'
		}).fadeIn( 'slow' );});
	});
	
	ClosePanel( 'Loading' );
}
// Показ формы ответа на комментарий


// Отправка ответа на комментарий
function SendResponse( ID )
{
	$( '#Loading > span' ).text( 'Отправка ответа на ID: ' . ID );
	ShowPanel( 'Loading' );
	
	$.post( "/ajax/EditComment.php", { id: ID, action : 'response', response : $( "#response" ).val( ) }, function(data){
		
		if ( data == 0 )
		{
			ClosePanel( 'EditComment' );
			
			$( '#Loading > span' ).text( 'Ответ на отзыв успешно добавлен!' );

			ClosePanel( 'Loading' );
			
			ReComment( );
			
			return;
		}
		
		$( '#Loading > span' ).html( data );
	});
	
	ClosePanel( 'Loading' );
}
// Отправка ответа на комментарий


// Показ формы редактирования комментария
function ShowComment( ID )
{
	$( '#Loading > span' ).text( 'Получение данных' );
	ShowPanel( 'Loading' );
	
	$.post( "/ajax/EditComment.php", { id: ID }, function(data){
		
		$( "#EditComment" ).fadeOut( 'slow', function() {$(this).html(data).css(
		{
			left : ( $( window ).width()  - $( this ).width() ) / 2 + "px",
			top : ( $( window ).height() - $( this ).height() ) / 2 + "px",
			position : 'fixed',
			zIndex : '1000'
		}).fadeIn( 'slow' );});
	});
	
	ClosePanel( 'Loading' );
}
// Показ формы редактирования комментария


// Отправка комментария
function SendComment( ID )
{
	$( '#Loading > span' ).text( 'Отправка Отзыва' );
	ShowPanel( 'Loading' );

	var Epprove = $( "#Eapprove" ).prop( 'checked' );
	
	if ( ! Epprove ) Epprove = 0;
	else Epprove = 1;
	
	$.post( "/ajax/EditComment.php", { id : ID, action : "edit", name: $( "#Ename" ).val( ), mail: $( "#Email" ).val( ), comment : $( "#Ecomment" ).val( ), approve : Epprove }, function(data){

		if ( data == 0 )
		{
			ClosePanel( 'EditComment' );
			
			$( '#Loading > span' ).text( 'Отзыв успешно отредактирован!' );

			ClosePanel( 'Loading' );

			ReComment( );
			
			return;
		}
		
		$( '#Loading > span' ).html( data );
	});
	
	ClosePanel( 'Loading' );
}
// Отправка комментария

//Удаление комментария
function DeleteComment( ID, ok )
{
	if ( ok != undefined && ok )
	{
		$( '#Loading > span' ).text( 'Удаление' );
		ShowPanel( 'Loading' );
		
		$.post( "/ajax/EditComment.php", { id: ID, action : "delete" }, function(data){
			
			if ( data == 0 )
			{
				ClosePanel( 'EditComment' );
				
				$( '#Loading > span' ).text( 'Отзыв успешно удален!' );

				ClosePanel( 'Loading' );

				ReComment( );
				
				return;
			}
			
			$( '#Loading > span' ).text( 'Ошибка удаления!' );
		});
		
		ClosePanel( 'Loading' );
	} else
	{
		$( '#EditComment' ).fadeOut( 'slow', function() {$(this).html( "Удалить отзыв?<br/><a href=\"javascript:DeleteComment( '" + ID + "', true );\">Да</a> <a href=\"javascript:ClosePanel( 'EditComment' );\">Нет</a>" ).css(
		{
			left : ( $( window ).width()  - $( this ).width() ) / 2 + "px",
			top : ( $( window ).height() - $( this ).height() ) / 2 + "px",
			position : 'fixed',
			zIndex : '1000'
		}).fadeIn( 'slow' );});
	}
}
//Удаление комментария

//Публикация комментария
function ApproveComment( ID )
{
	$( '#Loading > span' ).text( 'Отправка запроса' );
	ShowPanel( 'Loading' );
	
	$.post( "/ajax/EditComment.php", { id: ID, action : "approve" }, function(data){
		if ( data == 0 )
		{
			$( '#Loading > span' ).text( 'Отзыв успешно опубликован!' );

			ClosePanel( 'Loading' );

			ReComment( );
			
			return;
		}
		
		$( '#Loading > span' ).text( 'Ошибка публикации!' );
	});
	
	ClosePanel( 'Loading' );
}
//Публикация комментария

//Голосование
function SendVote( ID, id )
{
	$.post( "/ajax/Poll.php", { id: ID, votes: id }, function(data){

		if ( data == 1 ) {alert( 'Вы уже голосовали' ); return;}
		else if ( data == -1 ) { alert( 'Ошибка в передаваемых данных' ); return;}
		
		$("#Poll").fadeOut( 'slow', function() {$(this).html(data).fadeIn( 'slow' );});
	});
}

//Голосование

//Вывод комментариев к фотографиям
function ShowPicCom( )
{
	$.post( "/ajax/Pictures.php", { action : 'ShowCom', id : $( "#PicID" ).val( ) }, function(data){
		$( "#CommentPic" ).fadeOut( 'slow', function() {$(this).html(data).fadeIn( 'slow' );});
	});
}
//Вывод комментариев к фотографиям


//Добаление комментария к фотографиям
function AddPicCom()
{
	$( '#Loading > span' ).text( 'Добавление комментария' );
	ShowPanel( 'Loading' );
	
	$.post( "/ajax/Pictures.php", { action : 'AddCom', id : $( "#PicID" ).val( ), name: $( "#PicName" ).val( ), mail: $( "#PicMail" ).val( ), comment : $( "#PicComment" ).val( ), captcha : $( "#PicCaptcha" ).val( ) }, function(data){

		if ( data == 0 )
		{
			$( '#Loading > span' ).text( 'Комментарий успешно добавлен!' );
			
			$( "#PicCaptcha" ).val( '' );
			$( "#PicComment" ).val( '' );
			
			$( ".captcha" ).attr( "src", "/tpl/captcha/index.php?load=" + Math.random() );

			ShowPicCom();
			ClosePanel( 'Loading' );

			return;
		}
		
		$( '#Loading > span' ).html( data );

		$( ".captcha" ).attr( "src", "/tpl/captcha/index.php?load=" + Math.random() );
		$( "#captcha" ).val( '' );
	});

	ClosePanel( 'Loading' );
}
//Добаление комментария к фотографиям

// Бронирование столиков
function ReserveSend()
{
	$( '#Loading > span' ).text( 'Отправка данных' );
	ShowPanel( 'Loading' );
	
	$.post( "/ajax/Reserve.php", { desk : $("#ReserveDesk").val(), date: $( "#ReserveDate" ).val( ), num: $( "#ReserveNum" ).val( ), name: $( "#ReserveFIO" ).val( ), phone : $( "#ReservePhone" ).val( ), mail : $( "#ReserveMail" ).val( ) }, function( data )
	{
		if ( data == 0 )
		{
			ClosePanel( 'Reserve', 1 );
			
			$( '#Loading > span' ).text( 'Заявка на бронирования успешно отправилена' ).parent('div').css(
			{
				left : ( $( window ).width() - $( '#Loading' ).width() ) / 2 + "px",
				top : ( $( window ).height() - $( '#Loading' ).height() ) / 2 + "px",
			});

			CloseReserve();
			ClosePanel( 'Loading' );
			
			return;
		}

		$( '#Loading > span' ).html( data );
	});

	ClosePanel( 'Loading' );
}

// Бронирование столиков
