﻿$(document).ready(function () {
    function show_result(s) {
        $('#form_error').html(s);
        $('#form_error').show();
    }
    if ($('#main_form') != null) $('#main_form .submit').click(function () {
        $.ajax({
            type: "POST",
            url: "/form/",
            data: $("#main_form form").serialize(),
            success: function (html) {
                try {
                    //	alert(html);
                    eval(html);
                    if (ret.ok) {
                        show_result(ret.mes);
                    } else {
                        show_result(ret.mes);
                    };
                } catch (e) {
                    show_result('Ошибка формата ответа.');
                };
            },
            error: function () {
                show_result('Ошибка соединения.');
            }
        });
        return false;
    });
    //Описание товара. Смена фотографий
    if ($('.page_numbers') != null) $('.page_numbers a').click(function () {
        var $previews = $('a', this.parentNode);
        var i = $previews.index(this);
        var $cur_el = $('.photo_production>div.active');
        var $new_el = $('.photo_production>div').eq(i);
        if ($cur_el.get(0) !== $new_el.get(0)) {
            $new_el.addClass('active');
            $cur_el.removeClass('active');
            $previews.removeClass('active');
            $(this).addClass('active');
        }
        return false;
    });
    //Форма заказа билета
    $('.zakazat_knopka').click(function () {
        var window_height = $(window).height();
        var window_scroll = $(window).scrollTop();
        var size_chart_height;
        $('<div id="curtain"></div><div id="size_chart"><div id="close_size_chart"></div><div id="size_chart_content"><div class="afisha_form_heading">Форма заказа билета на мероприятие:</div><form name="ticket_order" action="" method="post" id="afisha_form"><div class="afisha_order_left_side"><textarea name="dop_info" cols="" rows="" class="afisha_textarea">Дополнительная информация</textarea></div><div><input name="fio" type="text" class="afisha_input" title="Имя Фамилия" /></div><div><input name="event" type="text" class="afisha_input" title="Название мероприятия" /></div><div><input name="phone" type="text" class="afisha_input" title="Контактный телефон" /></div><div><input name="email" type="text" class="afisha_input" title="Адрес электронной почты" /></div><div><input name="date" type="text" class="afisha_input" title="Дата" /></div><div  class="afisha_button_box"><input name="" type="image" src="/img/afisha_send_button.jpg" class="afisha_button" /></div></form></div></div>').appendTo('body');
        var curtain = $('#curtain');
        var size_chart = $('#size_chart');
        open_size_chart();
        afisha_input = $('.afisha_input');
        afisha_input.each(function (index) {
            $(this).attr('value', $(this).attr('title'));
        });
        afisha_input.blur(function () {
            var thisvalue = $(this).attr('value');
            if (thisvalue == '') {
                $(this).attr('value', $(this).attr('title'));
                return false;
            }
        });
        afisha_input.focus(function () {
            $(this).attr('value', '');
        });
				$('.afisha_button').click(function () {
            
            //if ((document.forms.ticket_order.fio.value !== "Имя Фамилия" && document.forms.ticket_order.fio.value.length > 3) && (document.forms.ticket_order.event.value !== "Название Мероприятия" && document.forms.ticket_order.event.value.length > 3) && (document.forms.ticket_order.phone.value !== "Контактный телефон" && document.forms.ticket_order.phone.value.length > 3) && (document.forms.ticket_order.email.value !== "Адрес электронной почты" && document.forms.ticket_order.email.value.length > 3) && (document.forms.ticket_order.date.value !== "Дата" && document.forms.ticket_order.date.value.length > 3)) {
            $('#afisha_form').css('display','none');
            $.ajax({
               type: "POST",
               url: "/order_mail.php",
               data: "info="+document.forms.ticket_order.dop_info.value+"&fio="+document.forms.ticket_order.fio.value+"&event="+document.forms.ticket_order.event.value+"&phone="+document.forms.ticket_order.phone.value+"&email="+document.forms.ticket_order.email.value+"&date="+document.forms.ticket_order.date.value,
               success: function(msg){
                 $('<div class="message_sending"><strong>Спасибо, ваш заказ получен.</strong> Наш менеджер свяжется с вами в самое ближайшее время. </div>').appendTo('#size_chart_content');
               }
             });
						 //}
						 /*else {
              alert("Все поля обязательны к заполнению!");
             }*/
						
						return false;
        });

        function open_size_chart() {
            size_chart_height = size_chart.height();
            curtain.css({
                'opacity': '0.1'
            });
            if (size_chart_height > window_height) {} else {
                curtain.fadeTo(100, 0.9);
                size_chart.css('top', window_scroll + ((window_height - size_chart_height) / 2)).fadeTo(100, 1);
            }
        }

        function close_size_chart() {
            curtain.fadeOut(function () {
                curtain.remove();
            });
            size_chart.fadeOut(function () {
                size_chart.remove();
            });
        }

        $('#close_size_chart').click(function () {
            close_size_chart();
        });

    });
// Лайтбокс афиши
$('.r a').lightBox();
});
