var Calendar = {};

Calendar.showInfo = function(el){
    $('.wrap').remove();
    //прячем все попапы
    $('.event').hide();
    //слой для скрытия
    var new_width = $(window).width();
    var new_height = $('#main').height();
    $('body').append('<div class="wrap" style="width:' + new_width + 'px; height:' + new_height + 'px;">&nbsp;</div>');
    //как звать героя
    var cur_name = $(el).attr('name');
    var cur_event = $(el).parents('span').siblings('div[name="' + cur_name + '"]');
    //второй, третий
    if( cur_name == 'second' ) {
        cur_event.css({'margin-top':'-71px'});
    }
    if( cur_name == 'third' ) {
        cur_event.css({'margin-top':'-55px'});
    }
    //верхний
    if( $(el).parents('tr').hasClass('first') ) {
        cur_event.css({'margin-top':'-3px'});
        cur_event.children('.arrow').css({'top':'22px'});
        if( cur_name == 'second' ) {
            cur_event.children('.arrow').css({'top':'39px'});
        }
        if( cur_name == 'third' ) {
            cur_event.children('.arrow').css({'top':'56px'});
        }
    }
    //нижний
    if( $(el).parents('tr').hasClass('last') ) {
        cur_event.css({'margin-top':'-140px'});
        cur_event.children('.arrow').css({'top':'159px'});
        if( cur_name == 'second' ) {
            cur_event.children('.arrow').css({'top':'176px'});
        }
        if( cur_name == 'third' ) {
            cur_event.children('.arrow').css({'top':'193px'});
        }
    }
    //в конце недели
    if( $(el).parents('td').hasClass('weekEnd') ) {
        cur_event.css({'margin-left':'-292px'});
        cur_event.children('.arrow').css({'background':'url(/static/i/arrow-rev.gif)', 'left':'291px'});
    }
    //чтоб красиво было
    cur_event.children('*').siblings('[class!="arrow"]').css({display:'none'});
    if( $.browser.msie) {
        cur_event.children('[class="bgie"]').css({display:'block'});
    }
    cur_event.show().css({'width':'0px'});
    cur_event.animate({width:'279px', paddingLeft:'38px'}, 'fast');
    setTimeout(function(){
        cur_event.children('[class!="form"]').fadeIn();
        cur_event.children('form').hide();
    }, 100);
    //Тыкаем на запись
    var flag = 0;
    var fio_regexp = /[^а-яА-ЯёЁ\s\-]/g;
    var phone_regexp = /[^0-9\-\s]/g;
    var email_regexp = /^[-a-z0-9!#$%&'*+/=?^_`{|}~]+(?:\.[-a-z0-9!#$%&'*+/=?^_`{|}~]+)*@(?:[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?\.)*(?:aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z][a-z])$/;
    cur_event.children('.order').click(function(){
        if( flag == 0 ) {
            if( $.browser.msie) {
                cur_event.children('[class!="bgie"]').fadeOut();
            } else {
                cur_event.children('*').siblings('[class!="arrow"]').fadeOut();
            }
            setTimeout(function(){
                cur_event.animate({width:'20px', paddingLeft:'0px'}, 'fast', function(){
                    cur_event.animate({width:'279px', paddingLeft:'38px'}, 'fast', function(){
                        cur_event.children('[class!="text"]').fadeIn();
                    });
                });
            }, 250);
            flag = 1;
        } else {
            var err = 0;
            if( $(this).parents('.event').children('form').children('.text[name="fio"]').val() == '' ) {
                $(this).parents('.event').children('form').children('.text[name="fio"]').addClass('error');
                err=1;
            }
            if( fio_regexp.test( $(this).parents('.event').children('form').children('.text[name="fio"]').val()) === true ) {
                $(this).parents('.event').children('form').children('.text[name="fio"]').addClass('error');
                err=1;
            }
            if( $(this).parents('.event').children('form').children('.text[name="phone"]').val() == '' ) {
                $(this).parents('.event').children('form').children('.text[name="phone"]').addClass('error');
                err=1;
            }
            if( phone_regexp.test($('.text[name="phone"]').val()) === true ) {
                $('.text[name="phone"]').addClass('error');
                err=1;
            }
            if( $(this).parents('.event').children('form').children('.text[name="email"]').val() != '' && email_regexp.test($('.text[name="email"]').val()) === false ) {
                $(this).parents('.event').children('form').children('.text[name="email"]').addClass('error');
                err=1;
            }
            if( err == 0 ) {
                $.ajax({
                    type: "POST",
                    url: '/register/',
                    data: $(this).parents('.event').children('form').serialize()
                });
                $(this).parents('.event').children('form').children('*').hide();
                $(this).parents('.event').children('.form').text('Сообщение отправленно');
            }
        }
    });
    //прятаем
    $('.wrap').click(function(){
        $(this).remove();
        cur_event.hide();
    });
    $('.form .text').focus(function(){
        $(this).removeClass('error');
    });
};

Calendar.change = function(el) {
    $('a.selected.calendar-switcher').removeClass('selected');
    $(el).addClass('selected');
    $.ajax({ url: '/courses/' + $(el).attr('date').replace("-", "/"),
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        success: function(data){
            $('#calendar-wrap').html(data);
            $('.calendar .stripe').click(function(){Calendar.showInfo(this)});
        }
    });
    return false;
};

$(document).ready(function(){
    $('.calendar-switcher').click(function(){Calendar.change(this)});
    $('.calendar .stripe').click(function(){Calendar.showInfo(this)});
    //empty links
    $('a[href=#]').click(function(){
        return false;
    });
});

