$(document).ready(parse);

var lock = 0;
var lockSlideshow = false;
var lastSlide = 0;
function parse()
{
    $('ul#projects li.main ul').hide();
    $('div#data').hide();
    $('div#submenu .inner').hide();
    

    $('a[@rel=inner]').click( function(){
        lockSlideshow = true;
        if(lock)
        {
            return false;
        }
        else
        {
            lockRandom = 1;
            lock = 1;
            hash = this.hash.substring(1);
            $('#content div.proj').toggleClass('projd');
            $('#content').prepend('<div class="proj">'+$('div#data #'+hash).html()+'</div>');

            $('#content a[@rel=cnt]').removeClass('active');
            $('#content a[@rel=cnt]:first').addClass('active');

            

            $('#content div.projd').fadeOut("slow", function(){
                $('#content img').remove(".projd");
                lock = 0;
                return false;
            });
            $('#content a[@rel=cnt]').click(changeCounterImage);
            return false;
        }
    });
    $('#projects li.main').children('a').click( function(){
        if($(this).parent('li').children('ul').css('display') == 'block')
        {
            $(this).children('img').attr('src', '_gfx/szczalka.png');
            $(this).parent('li').children('ul').slideUp();
        }
        else
        {
            $('ul#projects li.main img').attr('src', '_gfx/szczalka.png');
            $('ul#projects li.main ul').slideUp();
            $(this).children('img').attr('src', '_gfx/szczalka_dol.png');
            $(this).parent('li').children('ul').slideDown();
        }
        return false;
    });
    $('h4.menu').click( function(){
        $('.inner').slideUp();
        $('h4.menu img').attr('src', '_gfx/szczala.png');
        if($(this).parent('div').children('.inner').css('display') == 'block')
        {
            $(this).parent('div').children('.inner').slideUp();
            $(this).children('img').attr('src', '_gfx/szczala.png');
        }
        else
        {
            $(this).parent('div').children('.inner').slideDown();
            $(this).children('img').attr('src', '_gfx/szczala_dol.png');
        }
    });
  
 
    $('div#slideshow .proj').hide();
    $('div#slideshow .proj').eq(0).show();
    setTimeout(slideshow, 6000);
}

function changeCounterImage()
{
    $('a').removeClass('active');
    $(this).addClass('active');

    $("#loader").show();

    hash_local = this.hash.substring(1);
    photo_div = $(this).parent().parent().parent().children('div.photoInside');
    image = photo_div.children('img');
    addr = image.attr('src');
    image_folder = addr.substr(0, addr.lastIndexOf('/')+1);

    new_image = photo_div.prepend('<img alt="" />').children('img').eq(0);

    new_image.load(function(){
        image.fadeOut(function(){
            $(this).remove();
        });
        $("#loader").hide();
    });
    
    new_image.attr('src', image_folder + hash_local+'.jpg');
    
    return false;

}

function slideshow()
{
    if(lockSlideshow) return false;
    slide = (lastSlide + 1)%($('div#slideshow img').size());
    if(slide != lastSlide)
    {
        $('div#slideshow .proj').eq(lastSlide).fadeOut(1500);
        $('div#slideshow .proj').eq(slide).fadeIn(1500);
    }
    lastSlide = slide;
    if(lastSlide == ($('div#slideshow img').size() -1))
    {
        $('#loader').hide();
    }
    setTimeout(slideshow, 6000);
    return 0;
}


$(window).load(function(){
    $('#loader').hide();
});
