function slideshow()
{
    $('div.next a').click();
    setTimeout("slideshow()", 4000);
}

$(function() {

/*** ACCORDION NAV ***/
/*$('ul.acc a').click(function() {
    var $next = $(this).next('ul.acc');
    if ($next.hasClass('open'))
    {
        $next.slideUp('fast', function() {
            $next.removeClass('open');        
            // also close open children        
            $next.find('ul.acc').each(function(i, e) {
                $(e).hide();
                $(e).removeClass('open');                
            });            
        });
    }
    else
    {        
        if ($next.length == 1)
        {
            $next.slideDown('fast');
            $next.addClass('open');
        }        
    }

    return ($next.length == 1) ? false : true;
});
*/

/*** CLIENTS: PRINT ***/
if ($('div.clients').is(':visible'))
{
    $('div.clients a.print').click(function() {
        var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
        if (is_chrome)
        {
            if (confirm('Pastebėjome, kad jūs naudojate Google Chrome.\n\nJūsų pasirinkta nuotrauka atsidarys naujame lange, kur tereikės surinkti klaviatūros kombinaciją Ctrl+P, kad ją atsispausdinti.'))
            {
                window.open($(this).attr('rel'), '_blank');
            }
        }
        else
        {
            alert('Dėmesio!\n\nPrieš sutikdami spausdinti nuotrauką, palaukite kol jūsų nuotrauka bus pilnai užkrauta.');
            
            var print_window = window.open($(this).attr('rel'), '_blank');
            setTimeout(print_window.print(), 5000);
        }
    });
}

/*** WORKS ***/
if ($('div.works, div.about, div.clients').is(':visible'))
{
    // set total photos
    $('div.overlay div.title span.total_photos').each(function(i, e) {
        $(e).text($(e).parent('div.title').parent('div.overlay').parent('div').find('img.pic').size());
    });

    // refresh titles
    function refresh_titles()
    {
        $('div.overlay div.title div.name span').each(function(i, e) {
            $(e).text($(e).parent('div.name').parent('div.title').parent('div.overlay').parent('div').find('img.pic:visible').attr('title'));
        });  
    }
    if ($('div.clients').is(':visible'))
    {        
        refresh_titles();
    }

    // next
    $('div.overlay div.next a').click(function() {        
        var $current_visible_img = $(this)
                                   .parent('div.next')
                                   .parent('div.overlay')
                                   .parent('div')
                                   .children('a')
                                   .children('img.pic:visible');

        var $first_img = $(this)
                         .parent('div.next')
                         .parent('div.overlay')
                         .parent('div')
                         .children('a')
                         .children('img.pic:first');

        var $count = $(this)
                     .parent('div.next')
                     .parent('div.overlay')
                     .children('div.title')
                     .children('span.current_photo');
                    
        var $next = $current_visible_img.parent('a').next('a').children('img.pic');        

        if ($next.size() !== 0) // if next exists
        {            
            $current_visible_img.fadeOut('fast', function() {
                $next.fadeIn('fast');
                $count.text(eval($count.text()) + 1); // increment image count

                if ($('div.clients').is(':visible')) refresh_titles();
                $next.parent().parent().children('a:last').attr('href',$next.parent().attr('href'));
            });
        }
        else // if next doesn't exist, jump to first
        {            
            $current_visible_img.fadeOut('fast', function() {
                $first_img.fadeIn('fast');
                $count.text(1); // set image count to 1

                if ($('div.clients').is(':visible')) refresh_titles();
                $first_img.parent().parent().children('a:last').attr('href',$first_img.parent().attr('href'));
            });

        }
        return false;
    });

    // prev
    $('div.overlay div.prev a').click(function() {
        var $current_visible_img = $(this)
                                   .parent('div.prev')
                                   .parent('div.overlay')
                                   .parent('div')
                                   .children('a')
                                   .children('img.pic:visible');

        var $last_img = $(this)
                         .parent('div.prev')
                         .parent('div.overlay')
                         .parent('div')
                         .children('a')
                         .children('img.pic:last');


        var $count = $(this)
                     .parent('div.prev')
                     .parent('div.overlay')
                     .children('div.title')
                     .children('span.current_photo');

        var $prev = $current_visible_img.parent('a').prev('a').children('img.pic');

        if ($prev.size() !== 0) // if prev exists
        {            
            $current_visible_img.fadeOut('fast', function() {
                $prev.fadeIn('fast');
                $count.text(eval($count.text()) - 1); // decrement image count

                if ($('div.clients').is(':visible')) refresh_titles();
            });
        }
        else // if prev doesn't exist, jump to last
        {
            $current_visible_img.fadeOut('fast', function() {
                $last_img.fadeIn('fast');
                $count.text($count.siblings('span.total_photos').text()); // set image count to total

                if ($('div.clients').is(':visible')) refresh_titles();
            });
        }

        return false;
    });
}

if ($('div.restricted').is(':visible'))
{
    $('div.restricted a img').click(function() {
        $('form[name="client"]').submit();
    });
}

});
