var coll;
var preview;
var shadow;

$(function() {
    /* lang switcher */

    var langEl = $('#lang');
    
    var langHtml = '';
    if (lang == 'ru') langHtml = '<a href="#en" id="lang-en">eng</a> <strong>rus</strong>';
    else if (lang == 'en') langHtml = '<strong>eng</strong> <a href="#ru" id="lang-ru">rus</a>';
    else langHtml = '<a href="#en" id="lang-en">eng</a> <a href="#ru" id="lang-ru">rus</a>';
    langEl.html(langHtml);
    
    $('a', langEl).click(function() {
        langEl.append('<input type="hidden" name="language" value="' + $(this).attr('href').substr(1) + '" />');
        langEl.get(0).submit();
        return false;
    });
    
    
    /* randomize */
    
    var rand = $('#random');
    if (rand.length) {
        colls = $('p', rand);
        var speed = 25;
        var defStartSpeed = 5 * speed;
        
        $('#rand a:first').click(function() {
            colls.each(function() {
                var coll = $(this);
                var newImage = Math.floor((Math.random() * 5));
                var currentImage = coll.data('currentImage');
                var startSpeed = (coll.data('currentImage')) ? speed * (5 - currentImage) : defStartSpeed;
                
                for (var i = 0; i < 7; i++) {
                    coll.animate({marginTop: '-1000px'}, startSpeed)
                        .animate({marginTop: '0'}, 1);
                    
                    if (i == 0) startSpeed = defStartSpeed;
                }
                coll.animate({marginTop: '-'+ (200 * newImage) +'px'}, speed * newImage);
                
                coll.data('currentImage', newImage);
            });
            
            return false;
        });
    }    
    
    /* preview */
    
    coll = $('#collection');
    if (coll.length) $('ul a', coll).click(openPreview);
    if (location.hash) {
        var id = null;
        var hashPos = location.hash.indexOf('#');
        if (hashPos != -1) id = location.hash.substring(hashPos + 1);
        if (id) $('#' + id + ' a:first').click();
    }
    
    
    /* upload form */
    
    $('#show_form').click(function() {
        $('#upl form:first').toggleClass('hidden');
        return false;
    });
});


function openPreview() {
    var t = $(this.parentNode);
    
    var url = $('img:first', t).attr('src').replace(/small/, 'big');
    var name = $('span.name:first', t).html();
    var location = $('span.location:first', t).html();
    var web = $('span.web:first', t).html();
    
    var arrows = '';
    if (t.prev().length) arrows += '<span class="prev">&#8592;</span>';
    if (t.next().length) arrows += '<span class="next">&#8594;</span>';
    if (arrows) arrows = '<span class="arrows">' + arrows + '</span>';
    
    if (preview) preview.remove();
    
    preview = $(
        '<div id="preview">' +
        '<img src="' + url + '" width="400" height="400" alt="" style="visibility: hidden;" />' +
        '<span class="author"><strong>' + name + '</strong><em>' + location + '</em>' +
        '<a href="' + web + '" class="web">' + web.replace('http://', '').replace(/\/$/, '') + '</a></span>' +
        arrows +
        '</div>'
    );
    
    $('img:first', preview).load(function() {
        $(this).css('visibility', 'visible');
    });
    
    if (!shadow) shadow = $('<div id="shadow"></div>');
    
    shadow.prependTo($('body:first'));
    if (window.donkey) {
        preview.css({
            top: (parseInt($('#head').get(0).offsetHeight) + 189) + 'px',
            left: '50%',
            width: '1002px',
            marginLeft: '-501px'
        });
        preview.appendTo($('body:first'));
    }
    else preview.appendTo(coll);

    shadow.add(preview).click(closePreview);
    
    $('.prev:first', preview).click(function() {
        t.prev().children('a:first').click();
        return false;
    });
    
    $('.next:first', preview).click(function() {
        t.next().children('a:first').click();
        return false;
    });
    
    $('.pages:first').css('visibility', 'hidden');
    
    return false;
}

function closePreview(e) {
    if (e.target.className == 'web') return true;

    preview.remove();
    shadow.remove();
    $('.pages:first').css('visibility', 'visible');
}