var photo = {

    init: function()
    {
        if ($$('ul.gallery'))
        $$('ul.gallery').each(function(elem)
        {
            elem.getElements('a').each(function(a)
            {
                a.addEvent('click', photo.initButton);
            });
            photo.hideSurplus(elem);
        });

        if ($('content').getElement('li.gallery > a')) $('content').getElement('li.gallery > a').addEvent('click', photo.galleryList );
        photo.anchor();
    },
    
    
    hideSurplus: function(gallery)
    {
        var i = 1;
        var elements = gallery.getElements('li[class!=nxt]');
        
        if (elements.length < 7)
        {
            if (gallery.getElement('li.prv')) gallery.getElement('li.prv').toggleClass('hidden');
            if (gallery.getElement('li.nxt')) gallery.getElement('li.nxt').toggleClass('hidden');
        }
        else
        {
            elements[0].toggleClass('prv_passive');
            elements.each(function(elem)
            {
                if (i >= 7) elem.toggleClass('hidden');
                i++;     
            });
        }
    },


    editGallery: function(source, source_id, gallery_id)
    {
        win.init('photoGalleryEditor', 'Фотогалерея', 700);
        var frame = new IFrame({
            src: '/ajax.php?do=photoGalleryEditor&source='+ source +'&source_id='+ source_id +'&gallery_id='+ gallery_id,
            styles: {
                width: '99.7%',
                height: 415,
                border: '1px solid #ccc'
            }
        });

        $(frame).inject(win.content('photoGalleryEditor'));
        win.gotoCenter('photoGalleryEditor');
    },


    initButton: function(e)
    {
        e.stop();
        // var selected = $('content').getElement('ul.gallery > li.selected');

        if (this.get('href') != '#') 
        {
            var id = $(this).get('href').match(/\b\d+\b/)[0].toInt();
            photo.select(id);
        }
        else 
        {
            var li = $(this).getParent();
            var ul = li.getParent();
            
            if (li.hasClass('nxt') && li.getPrevious().hasClass('hidden'))
            {
                var marker = 'hide';
                ul.getElements('li[id]').each(function(elem)
                {     
                    if (!elem.hasClass('hidden') && marker == 'hide') { elem.toggleClass('hidden'); marker = ''; }
                    if (!elem.hasClass('hidden')) marker = 'view';
                    if (elem.hasClass('hidden') && marker == 'view') 
                    {     
                        elem.toggleClass('hidden'); marker = ''; 
                        if (elem.getNext().hasClass('nxt')) elem.getNext().toggleClass('nxt_passive');
                    }
                });
                ul.getElement('li.prv').removeClass('prv_passive');
            }
            if (li.hasClass('prv') && li.getNext().hasClass('hidden'))
            {                        
                var marker = 'view';
                ul.getElements('li[id]').each(function(elem)
                {
                    if (!elem.hasClass('hidden') && marker == 'view') 
                    { 
                        elem.getPrevious().toggleClass('hidden'); marker = 'hide'; 
                    }

                    if ((elem.getNext().hasClass('hidden') || elem.getNext().hasClass('nxt')) && marker == 'hide') 
                    { 
                        elem.toggleClass('hidden'); marker = ''; 
                    }

                });
                if (!ul.getElement('li.prv').getNext().hasClass('hidden')) ul.getElement('li.prv').toggleClass('prv_passive');
                ul.getElement('li.nxt').removeClass('nxt_passive');
            }
        }
    },


    select: function(id)
    {
        new Request.HTML({
            url: '/ajax.php?do=getPhoto',
            update: $('gallery'),
            onComplete: function(response) {
                $('content').getElement('ul.gallery > li.selected').toggleClass('selected');
                $('photo'+ id).toggleClass('selected');
                document.location.href = document.location.pathname + '#photo/' + id;
                comment.init();
            }
        }).get({'id': id});
    },


    setGalleryAvatar: function(gallery, photo)
    {
        new Request({method: 'get', url: '/ajax.php'}).send('do=setGalleryAvatar&gallery='+ gallery +'&photo='+ photo);
    },


    galleryList: function(e)
    {
        if (user.id)
        {
            e.stop();
            win.init('galleryList', 'Мои фотогалереи', 370);
            win.gotoThis('galleryList', $(this), 'bottom');
            win.content('galleryList').load('/ajax.php?do=userGalleryListEditable&id='+ user.id);
        }
        else user.drawUnregInfo.attempt(e, this);
    },


    insertGallery: function(id)
    {
        var title = win.content('galleryList').getElement('input').value;
        win.content('galleryList').load('/ajax.php?do=userGalleryListEditable&id='+ user.id +'&title='+ title);
    },


    deleteGallery: function(id)
    {
        if (confirm('Точно удалить?'))
        new Request.JSON({
            url: '/ajax.php?do=deleteGallery',
            onComplete: function(response)
            {
                if (response) $('content').getElement('h2').set('style','text-decoration:line-through;');
            }
        }).post({'id': id});
    },


    anchor: function()
    {
        var hash = document.location.hash;
        if (hash.contains('photo'))
        {
            var id = hash.match(/\b\d+\b/)[0].toInt();
            photo.select(id);
        }
    },


    transferDialog: function(id)
    {
        win.init('transferPhoto', 'Перенос фото', 430);
        win.gotoCenter('transferPhoto');
        win.content('transferPhoto').load('/ajax.php?do=transferPhotoLocation&id='+ id);
    },


    transfer: function(this_)
    {
        var source = $(this_).get('name');
        var source_id = $(this_).getParent().getPrevious().getChildren()[0].value;
        var photo_id = $('transferPhoto').getElement('input').value;

        if (source_id && photo_id && source)
        win.content('transferPhoto').load('/ajax.php?do=transferPhoto&source='+ source +'&source_id='+ source_id +'&photo_id='+ photo_id);
    }

}
