$(function() {
    $('a.email').each(function() {
        $(this).attr('href', 'mailto:' + $(this).attr('href').substring(1).replace('.gav.', '@'));
    });
    $('input.button').mouseover(function() {
        $(this).addClass('hover');
    }).mouseout(function() {
        $(this).removeClass('hover');
    }).attr('disabled', '');
    $('input.text, textarea').focus(function() {
        if (this.value == this.defaultValue) {
            this.value = '';
        }
        $(this).addClass('focus');
    }).blur(function() {
        if (this.value == '') {
            this.value = this.defaultValue;
        }
        $(this).removeClass('focus');
    });
    $('form').submit(function() {
        var check_form = true;
        $('input.text, textarea').each(function() {
            if ($(this).hasClass('notblank') && (this.value == '' || this.value == this.defaultValue)) {
                alert('Необходимо ввести данные в обязательное к заполнению поле');
                $(this).focus();
                check_form = false;
            }
        });
        if (! check_form) {
            return false;
        }
        $('input.text, textarea').each(function() {
            if (this.value == this.defaultValue) {
                this.value = '';
            }
        });
    });
    $('form').attr('action', 'send.xbl').show();
    $('div.hr').show();
    if ($('form').length > 0) {
        $(window).resize(function() {
            setHeight();
        });
        setHeight();
    }
    $('a.preview').click(function() {
        //$(this).hide();
        if ($(this).parent().children('div').length > 0) {
            $(this).parent().find('div img').animate({width:1, height:1}, function() {
                $(this).parent().remove();
            });
        } else {
            $(this).parent().append('<div><img class="preview" src="'+$(this).attr('href')+'" width="1" height="1" title="Кликните по картинке для изменения пропорций"></div>');
            $(this).parent().find('div img').animate({width:512, height:384}).click(function() {
                $(this).animate({height: $(this).height() > 300 ? 256 : 384});
            });
        }
        return false;
    });
    $(document).keydown(function(e) {
        if (! e.altKey && ! e.ctrlKey) {
            var s1000 = $('#s1000').html();
            if (s1000 == null) s1000 = '';
            var c = String.fromCharCode(e.which).toUpperCase();
            if (e.which >= 32 && e.which <= 122) {
                $('#s1000').html(s1000 + c);
            }
            if (e.which == 8) {
                $('#s1000').html(s1000.substring(0, s1000.length - 1));
                if (s1000 != '') {
                    return false;
                }
            }
            if (e.which == 13) {
                $('#s1000').html('');
                $('#s1000').parent().before('<p>? '+s1000+'</p>');
                if (s1000.toLowerCase() == 's1000') {
                    $('#s1000').parent().before('<p>программа не загружена в память</p>');
                }
            }
        }
    });
});
function setHeight() {
    var form_height = 320;
    if ($('form.form_title').length > 0) {
        form_height = 340;
    }
    var height = $(window).height() - form_height;
    if (height < 200) {
        height = 200;
    }
    $('ul.list').css('height', height + 'px');
}
