function new_album ()
{
    create_dialog('Новый фотоальбом', 500, 230);
    $('.dialog-content').append('<div class="new-album-form"><label class="txt-grey99 font-1_4" style="width:128px;">Название:</label><div style="margin-left:150px;"><input type="text" name="new_album_name" style="width:90%;" maxlength="200" id="new_album_name"/><span class="font-1_2 txt-grey99 i input-info">Не стоит писать очень длинные названия, также лучше не употреблять ненормативную лексику.</span><div style="padding-top:15px;"><input type="button" name="new_album_submit" value="Создать" onclick="create_album()"/></div></div></div>');
    dialog_load_success();
}



function new_org_album (org_id)
{
    create_dialog('Новый объект', 500, 430);
    $('.dialog-content').append('<div class="new-album-form"><label class="txt-grey99 font-1_4" style="width:128px;">Название:</label><div style="margin-left:150px;"><input type="text" name="new_album_name" style="width:90%;" maxlength="200" id="new_album_name"/><span class="font-1_2 line_12 txt-grey99 i input-info">Не стоит писать очень длинные названия, также лучше не употреблять ненормативную лексику.</span></div><div style="clear:both;"><label class="txt-grey99 font-1_4" style="width:128px;">Описание:</label><div style="margin-left:150px;"><textarea type="text" name="new_album_name" style="width:90%;" maxlength="200" id="new_album_desc"></textarea></div><div style="padding-top:15px;"><div style="margin-left:150px;"><input type="button" name="new_album_submit" value="Создать" onclick="create_org_album('+org_id+')"/></div></div></div>');
    dialog_load_success();
}



function create_album ()
{
    if ($('#new_album_name').val() == '') { $('#new_album_name').focus(); return false; }

    $('.dialog-content .ajax-load-request').removeClass('hidden');

    $.post('/ajax/create_user_album', { album_name: $('#new_album_name').val() }, function(data) {
        if (data.status)
        {
            Alert.show('', 'Альбом создан', 'message');
            window.location.href = base_url+'/people/'+data.user_id+'/photos/'+data.album_id+'/edit';
        }
        $('.dialog-content .ajax-load-request').addClass('hidden');
    }, "json");
}



function create_org_album (org_id)
{
    if ($('#new_album_name').val() == '') { $('#new_album_name').focus(); return false; }

    $('.dialog-content .ajax-load-request').removeClass('hidden');

    $.post('/ajax/create_org_object_album', { org_id: org_id, album_name: $('#new_album_name').val(), album_desc: $('#new_album_desc').val() }, function(data) {
        if (data.status)
        {
            Alert.show('', 'Объект создан', 'message');
            window.location.href = base_url+'organizations/'+org_id+'/objects/'+data.album_id+'/edit';
        }
        $('.dialog-content .ajax-load-request').addClass('hidden');
    }, "json");
}



function save_album_name (album_id, album_type)
{
    var album_name = $('#album_edit_name').val();
    if (trim(album_name) == '') { $('#album_edit_name').focus(); return false; }

    $('.album-edit .loader').removeClass('hidden');
    $.post('/ajax/edit_album_title', { album_id: album_id, album_name: album_name, album_type: album_type }, function(data) {
        if (data.status)
        {
            Alert.show('', 'Название альбома сохранено', 'message');
            $('#album_edit_name').val(data.album_new_name);
            $('#album_title').html(data.album_new_name);
            $('.album-edit .loader').addClass('hidden');
        }
    }, "json");    
}


function save_album_desc (album_id)
{
    var album_desc = $('#album_edit_desc').val();
    if (trim(album_desc) == '') { $('#album_edit_desc').focus(); return false; }

    $('.album-edit .loader').removeClass('hidden');
    $.post('/ajax/edit_album_desc', { album_id: album_id, album_desc: album_desc }, function(data) {
        if (data.status)
        {
            Alert.show('', 'Описание объекта сохранено', 'message');
            $('#album_edit_desc').val(data.album_new_desc);
            $('.album-edit .loader').addClass('hidden');
        }
    }, "json");
}



function show_album_del (photo, show)
{
    if (show) { $(photo).parent().parent().find('.del').removeClass('hidden'); }
    else { $(photo).parent().parent().find('.del').addClass('hidden'); }
}



function delete_album (album_id, in_org)
{
    var current_albums_count = parseInt($('.albums-count').html());
    $.post('/ajax/delete_album', { album_id: album_id, in_org: in_org, current_albums_count: current_albums_count }, function(data) {
        if (data.status)
        {
            Alert.show('', data.result_msg, 'message');
            $('#album-'+album_id).fadeOut('slow');
            current_albums_count == current_albums_count--;
            $('.albums-count').html(current_albums_count);
            if (in_org) $('.sub-header').html(data.albums_count_str);
            else $('.user-inner-header').html(data.albums_count_str);
        }
    }, "json");
}



var album_photo_selected = 0;
function select_album_thumb_to_edit (img_id)
{
    if (img_id == -1)
    {
        if ( $('#album-edit-photos-list li:first').size() > 0 )
            img_id = parseInt ( $('#album-edit-photos-list li:first').attr('class').replace('album-thumb-', '') );
        else
        {
            $('.album-photo-info').addClass('hidden');
            return false;
        }
    }
    $('.album-photo-info').removeClass('hidden');
    $('.album-photo-info textarea').val($('.album-thumb-'+img_id+' .photo-text').html());
    $('#album-edit-photos-list li').removeClass('thumb-active');
    $('.album-thumb-'+img_id).addClass('thumb-active');
    album_photo_selected = img_id;

}


function save_img_desc ()
{
    $('.album-edit .loader').removeClass('hidden');
    var img_desc = $('.album-photo-info textarea').val();
    $.post('/ajax/save_album_img_desc', { img_id: album_photo_selected, img_desc: img_desc  }, function(data) {
        if (data.status)
        {
            $('.album-thumb-'+album_photo_selected+' .photo-text').html(data.img_desc);
            Alert.show('', 'Описание фотографии сохранено', 'message');
            $('.album-edit .loader').addClass('hidden');
        }
    }, "json");    
}


function make_album_cover (album_id, album_type)
{
    if (album_photo_selected == 0) return false;
    $('.album-edit .loader').removeClass('hidden');
    $.post('/ajax/make_album_cover', { img_id: album_photo_selected, album_id: album_id, album_type: album_type  }, function(data) {
        if (data.status)
        {
            //$('.album-thumb-'+album_photo_selected+' .photo-text').html(data.img_desc);
            Alert.show('', 'Обложка сохранена', 'message');
            $('.album-edit .loader').addClass('hidden');
        }
    }, "json");    
}


function delete_album_img (img_id, album_id, album_type)
{
    $('.album-edit .loader').removeClass('hidden');
    $.post('/ajax/delete_album_img', { img_id: img_id, album_id: album_id, album_type: album_type  }, function(data) {
        if (data.status)
        {
            Alert.show('', 'Фотография удалена', 'message');
            $('.album-thumb-'+img_id).fadeOut('slow', function () { $('.album-thumb-'+img_id).remove(); select_album_thumb_to_edit(-1); });
            $('.album-edit .loader').addClass('hidden');
            $('.album-photos-title span').html(data.photos_count_str);
        }
    }, "json");
}


function rotate_album_img (img_id, album_id, album_type, thumb_width)
{
    $('.album-edit .loader').removeClass('hidden');
    $.post('/ajax/rotate_album_img', { img_id: img_id, album_id: album_id, album_type: album_type  }, function(data) {
        if (data.status)
        {
            Alert.show('', 'Фотография повернута', 'message');
            //$('.album-thumb-'+img_id).fadeOut('slow', function () { $('.album-thumb-'+img_id).remove(); select_album_thumb_to_edit(-1); });
            //$('.album-edit .loader').addClass('hidden');
            //$('.album-photos-title span').html(data.photos_count_str);
            
            var new_margin = Math.round(data.new_thumb_width / 2);
            var new_del_icon_margin = Math.round(data.new_thumb_width / 2 - 18);
            var new_rotate_icon_margin = Math.round(data.new_thumb_width / 2 - 3);
            $('.album-thumb-'+img_id+' .thumb').css('margin-left', '-'+new_margin+'px');
            $('.album-thumb-'+img_id+' .del-icon').css('margin-left', new_del_icon_margin+'px');
            $('.album-thumb-'+img_id+' .rotate-icon').css('margin-left', '-'+new_rotate_icon_margin+'px');
            $('.album-thumb-'+img_id+' .thumb').attr('src', data.new_src);

        } else alert(data.msg);
        $('.album-edit .loader').addClass('hidden');
    }, "json");
}


var current_photoalbum_large_photo = 0;
var current_photoalbum_large_photo_num = 1;
function show_large_photo (album_name, album_id, photo_name, photo_id, photo_width, photo_height, album_type)
{
    var server_folder = '';
    if (album_type == 'user')
    {
        var str_title = $('#album_title').html();
        create_dialog('Фотоальбом &laquo;'+str_title+'&raquo;', 770, 650);
        server_folder = 'photoalbums';
    }
    else if (album_type == 'org')
    {
        var str_title = $('.sub-header span').html();
        create_dialog('Объект &laquo;'+str_title+'&raquo;', 770, 650);
        server_folder = 'org_objects';
    }

    $('.dialog-content').append('<div class="dialog-album-photo"><div class="bg"></div></div>');
    var margin_left = photo_width / 2; var margin_top = photo_height / 2;
    if (photo_height > 500)
    {
        margin_left = Math.round( (photo_width / photo_height * 500) / 2 );
        margin_top = 250;
        $('.dialog-content .bg').append('<a href="javascript:;" onclick="change_album_large_photo('+"'right'"+')"><img src="/images/modules/'+server_folder+'/'+album_id+'/gallery/'+photo_name+'" alt="" style="margin-left:-'+margin_left+'px; margin-top:-'+margin_top+'px; height:500px;"/>');
    }
    else
        $('.dialog-content .bg').append('<a href="javascript:;" onclick="change_album_large_photo('+"'right'"+')"><img src="/images/modules/'+server_folder+'/'+album_id+'/gallery/'+photo_name+'" alt="" style="margin-left:-'+margin_left+'px; margin-top:-'+margin_top+'px;"/>');
    var photo_text = $('.album-thumb-'+photo_id+' .photo-text').html();
    $('.dialog-content .dialog-album-photo').append('<div class="photo-info"><div class="text i txt-grey66 font-1_2">'+photo_text+'</div><div class="nav"><div class="left"><a href="javascript:;" class="js font-1_2" onclick="change_album_large_photo('+"'left'"+')">Предыдущая</a></div><div class="right"><a href="javascript:;" class="js font-1_2" onclick="change_album_large_photo('+"'right'"+')">Следующая</a></div></div></div>');

    //if ($('li.album-thumb-'+photo_id).hasClass('first')) $('.dialog-content .dialog-album-photo .nav .left').addClass('disabled');
    //if ($('li.album-thumb-'+photo_id).hasClass('last')) $('.dialog-content .dialog-album-photo .nav .right').addClass('disabled');

    dialog_load_success();
    current_photoalbum_large_photo = photo_id;
    current_photoalbum_large_photo_num = parseInt($('.album-thumb-'+photo_id+' .photo-num').html());
}



function change_album_large_photo (direction)
{
    //if ( $(a).parent().hasClass('disabled')) return false;
    if (direction == 'left')
    {
        $('.dialog-content .dialog-album-photo .bg img').animate({
           opacity: 0.01
         }, 500, function() {

            if (current_photoalbum_large_photo_num <= 1)
            {
                current_photoalbum_large_photo_num = $('#album-view-photos-list li').size();
            }
            else
                current_photoalbum_large_photo_num--;

            //$('.dialog-content .dialog-album-photo .bg img').attr('src', $('.album-thumb-'+current_photoalbum_large_photo).prev().find('.photo-filename').html());

            // NEW
            $('.dialog-content .dialog-album-photo .bg img').remove();
            $('.dialog-content .dialog-album-photo .bg').html('<a href="javascript:;" onclick="change_album_large_photo('+"'right'"+')"><img src="" alt="" class="hidden"/></a>');

            $('.dialog-content .dialog-album-photo .photo-info .text').html( $('.thumb-num-'+current_photoalbum_large_photo_num).find('.photo-text').html());
            var margin_left = parseInt($('.thumb-num-'+current_photoalbum_large_photo_num).find('.photo-width').html()) / 2;
            var margin_top = parseInt($('.thumb-num-'+current_photoalbum_large_photo_num).find('.photo-height').html()) / 2;
            $('.dialog-content .dialog-album-photo .bg img').css('margin-left', '-'+margin_left+'px').css('margin-top', '-'+margin_top+'px');

            // NEW
            $('.dialog-content .dialog-album-photo .bg img').attr('src', $('.thumb-num-'+current_photoalbum_large_photo_num).find('.photo-filename').html());
            $('.dialog-content .dialog-album-photo .bg img').removeClass('hidden');

            //alert(current_photoalbum_large_photo_num);
            //$('.dialog-content .dialog-album-photo .bg img').animate({ opacity: 1 }, 500);
            //current_photoalbum_large_photo = $('.album-thumb-'+current_photoalbum_large_photo).prev().attr('class').replace('album-thumb-', '').replace(' first', '');


        });
    }
    else if (direction == 'right')
    {
        $('.dialog-content .dialog-album-photo .bg img').animate({
           opacity: 0
         }, 500, function() {
            //$('.dialog-content .dialog-album-photo .bg img').attr('src', $('.album-thumb-'+current_photoalbum_large_photo).next().find('.photo-filename').html());

            // номер фото для просмотра
            if (current_photoalbum_large_photo_num >= $('#album-view-photos-list li').size())
            {
                current_photoalbum_large_photo_num = 1;
            }
            else
                current_photoalbum_large_photo_num++;

            // NEW
            $('.dialog-content .dialog-album-photo .bg img').remove();
            $('.dialog-content .dialog-album-photo .bg').html('<a href="javascript:;" onclick="change_album_large_photo('+"'right'"+')"><img src="" alt="" class="hidden"/></a>');


            $('.dialog-content .dialog-album-photo .photo-info .text').html( $('.thumb-num-'+current_photoalbum_large_photo_num).find('.photo-text').html());
            var margin_left = parseInt($('.thumb-num-'+current_photoalbum_large_photo_num).find('.photo-width').html()) / 2;
            var margin_top = parseInt($('.thumb-num-'+current_photoalbum_large_photo_num).find('.photo-height').html()) / 2;
            $('.dialog-content .dialog-album-photo .bg img').css('margin-left', '-'+margin_left+'px').css('margin-top', '-'+margin_top+'px');

            // NEW
            $('.dialog-content .dialog-album-photo .bg img').attr('src', $('.thumb-num-'+current_photoalbum_large_photo_num).find('.photo-filename').html());
            $('.dialog-content .dialog-album-photo .bg img').removeClass('hidden');

            //$('.dialog-content .dialog-album-photo .bg img').animate({ opacity: 1 }, 500);
            //current_photoalbum_large_photo = $('.album-thumb-'+current_photoalbum_large_photo).attr('class').replace('album-thumb-', '').replace(' last', '');
         });

    }
}



function open_insert_gallery_img_in_post_dialog ()
{
    // создаем окно
    create_dialog('Фотография из альбома', 750, 500);
    // получаем с сервера список альбомов
    $.post('/ajax/get_albums_list_for_dropdown', { a: 'a'  }, function(data) {
        if (data.status)
        {
            $('.dialog-content').append('<div class="dialog-insert-gallery-photo"><div class="select"><label class="txt-grey99 font-1_4">Фотоальбом:</label><select name="album_id" onchange="insert_gallery_img_in_post_change_album(this)"><option value="0">...</option></select></div></div>');
            $('.dialog-content .dialog-insert-gallery-photo').append('<div class="thumbs"></div>');
            $.each(data.albums, function (index, value) { $('.dialog-insert-gallery-photo select').append('<option value="'+value.m_item_id+'">'+value.m_item_title+'</option>') });
        }
    }, "json");
    // выключаем загрузчик
    dialog_load_success();
}


function insert_gallery_img_in_post_change_album (select)
{
    if ($(select).val() != 0)
    {
        $('.dialog-content .dialog-insert-gallery-photo .thumbs').html('');
        dialog_load_turn_on();
        $.post('/ajax/get_albums_thumbs_to_insert', { album_id: $(select).val()  }, function(data) {
            if (data.status)
            {
                dialog_load_success();
                $('.dialog-content .dialog-insert-gallery-photo .thumbs').append(data.thumbs_list);
            }
        }, "json");
    }
}


function insert_gallery_img_in_post (album_id, img_name)
{
    if( $('#post-add-content').size() > 0 )
        var ts = document.getElementById('post-add-content');
    else if ( $('#question-add-content').size() > 0 )
        var ts = document.getElementById('question-add-content');
    else if ( $('#news-add-content').size() > 0 )
        var ts = document.getElementById('news-add-content');
    else if ( $('#news-edit-content').size() > 0 )
        var ts = document.getElementById('news-edit-content');
    DocumentSelection.wrapSelection(ts, "#photo-"+album_id+"-"+img_name+"#","");
    close_dialog();
}
