jQuery.cookie = function(name, value) {
    if (typeof value != 'undefined') {
        if (value === null) {
            value = '';
        }
        var date;
        date = new Date();
        date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
        var expires = '; expires=' + date.toUTCString();
        document.cookie = [name, '=', encodeURIComponent(value), expires, '; path=/'].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

$(function() {
    $('a.email').each(function() {
        $(this).attr('href', 'mailto:' + $(this).attr('href').substring(1).replace('.gav.', '@')).html($(this).html().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');
    });
    if ($.cookie('bk0010_name')) $('input[name=name]').val($.cookie('bk0010_name'));
    if ($.cookie('bk0010_email')) $('input[name=email]').val($.cookie('bk0010_email'));
    $('form').submit(function() {
        if ($('input[name=name]').val() == $('input[name=name]').get(0).defaultValue)
            $('input[name=name]').val('');
        if ($('input[name=email]').val() == $('input[name=email]').get(0).defaultValue)
            $('input[name=email]').val('');
        $.cookie('bk0010_name', $('input[name=name]').val());
        $.cookie('bk0010_email', $('input[name=email]').val());
        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', './').show().append('<input type="hidden" name="mode" value="save"/>');
    $('div.hr').show();
    if ($('form').length > 0 && $(document).width() > 800) {
        $(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') {
                    var rand = Math.floor(Math.random() * 3);
                    if (rand == 0) {
                        $('#s1000').parent().before('<p>БЕЙСИК (ВИЛЬНЮС,1986.07.24)</p>');
                    } else if (rand == 1) {
                        $('#s1000').parent().before('<p>CAGLRCCC.R</p>');
                    } else {
                        $('#s1000').parent().before('<p>?00 AT  0.00<br/>ГОТОВНОСТЬ К РАБОТЕ<br/>*</p>');
                    }
                    window.scrollTo(0, 1000000);
                }
            }
        }
    });
});
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');
}
