$(document).ready(function(){

    $('.button').live('click', function(){
        var form;
        var url = $(this).attr('href');

        if (url.length == 0) {
            form = $(this).parents('form');
            if (form.length == 1) {
                form.submit();
                return false;
            }
        }
        return true;
    });


    $('.new_window').click(function(){
        if ($(this).attr('href')) {
            window.open($(this).attr('href'), '', "width=800,height=600,toolbar=no");
            return false;
        }
    });
    $('.new_window_full').click(function(){
        if ($(this).attr('href')) {
            window.open($(this).attr('href'), '', "width=900,height=500,scrollbars=yes");
            return false;
        }
    });


    $('.main_menu a').hover(function(){
        var id = $(this).attr('id').substr(5);
        if (top_hints[id] !== 'undefined') {
            $('#top_hint').text(top_hints[id]);
        }
    }, function(){
        $('#top_hint').text(top_hints['default']);
    });


    $('.delete_article').click(function(){
        if (!confirm('Удалить статью?')) {
            return false;
        }
        var self = $(this);
        var article = $(self.parents('.article_short')[0]);
        article.css('opacity', 0.5);
        $.ajax({
           url: self.attr('href'),
           success: function(data) {
               article.animate({ height: 17 }, 500).html(data).addClass('removed').animate({ opacity: 1, height: 20 }, 200);
           }
        });

        return false;
    });

    if ($('.photo_form').length > 0) {
        $('.photo_form').ajaxForm({
            resetForm: true,
            dataType: 'json',
            success: function(data){
                if (!data['error']) {
                    if (data.img.old) {
                        $('#photo_title_' + data.img.id).val(data.img.title);
                        return false;
                    } else {
                        var block = $('<div class="photo"></div>');
                        var form = $('<form method="post" action="/blog/gallery/photo-save/id/' + data.img.article_id + '" class="photo_form"></form>');
                        block.append($('<a href="' + data.img.big + '" title="' + data.img.title + '"><img src="' + data.img.path + '" /></a>'));
                        form.append($('<input type="hidden" name="photo_id" value="' + data.img.id + '" />'));
                        var title_field = $('<input type="text" name="title" value="' + data.img.title + '" id="photo_title_' + data.img.id + '" />');
                        form.append(title_field);
                        title_field.blur(function(){
                            form.submit();
                            return false;
                        });
                        form.append($('<a href="/blog/gallery/photo-del/id/' + data.img.article_id + '/photo_id/' + data.img.id + '" class="delete_simple"></a>'));
                        block.append(form);
                        form.ajaxForm({
                            resetForm: true,
                            dataType: 'json',
                            success: function(data){
                                if (!data['error']) {
                                    if (data.img.old) {
                                        $('#photo_title_' + data.img.id).val(data.img.title);
                                        return false;
                                    }
                                } else {
                                    var block = $('<div class="errors">' + data.error + '</div>');
                                    $('.photos_list').append(block);
                                }
                            }
                        });
                    }
                } else {
                    var block = $('<div class="errors">' + data.error + '</div>');
                }
                $('.photos_list').append(block);

            }
        });

        $('.photo_form [name=title]').blur(function(){
            $(this).parents('form').submit();
            return false;
        });

        $('.photo_form [type=file]').change(function(){
            $(this).parents('form').submit();
            return false;
        });


        $('.photo_form .delete_simple').live('click', function(){
            var self = $(this);
            $.ajax({
                url: self.attr('href'),
                dataType: 'json',
                success: function(data){
                    var removed = $('<div>' + 'Фото удалено' + '</div>').addClass('removed');
                    var photo = self.parents('.photo');
                    photo.append(removed);
                    photo.find(':input,.delete_simple').remove();
                    photo.find('img').animate({ opacity: 0.3 }, 100).css('margin-bottom', '22px');
                }
            });
            return false;
        });

    }


    if ($('.article_short .photos_list a').length > 0) {
        $('.article_short .photos_list a').lightBox({
            overlayOpacity: 0.4,
            containerResizeSpeed: 100,
            txtImage: 'фото',
            txtOf: 'из',
            imageBlank: '/images/lightbox-blank.gif',
            imageBtnPrev: '/images/gallery_arrow_left.png',
            imageBtnNext: '/images/gallery_arrow_right.png',
            imageBtnClose: '/images/gallery_close.png'
        });
    }
    if ($('#article_full .photos_list a').length > 0) {
        $('#article_full .photos_list a').lightBox({
            overlayOpacity: 0.4,
            containerResizeSpeed: 100,
            txtImage: 'фото',
            txtOf: 'из',
            imageBlank: '/images/lightbox-blank.gif',
            imageBtnPrev: '/images/gallery_arrow_left.png',
            imageBtnNext: '/images/gallery_arrow_right.png',
            imageBtnClose: '/images/gallery_close.png'
        });
    }


});