var ssTime = 10000;
var isGallery = false;
var isFrontPage = true;
var intID = 0
var nextSlide = 986;
var sum = 0;
var sumMemory = 0;
var imgW = 58;
var imgs = 17;
var max;
var nr;
var nrPrev;
var block = false;
var isMain = true;
var isNav = true;
var isMenu = false;
var eUser = 'pawezka';
var eM = '@';
var eHost = 'twojfotograf.net';
var isGalleryFC = true;

$(document).ready(function(){
    $("#content").find('img').batchImageLoad({
        loadingCompleteCallback:
            function(){
                $('#content').fadeIn();
            },
        imageLoadedCallback:
            function(elementsLoadedCount, totalImagesCount){}
    }); 
    $("#thumbs").find('img').batchImageLoad({
        loadingCompleteCallback:
            function(){
                $('#thumbs').fadeIn();
            },
        imageLoadedCallback:
            function(elementsLoadedCount, totalImagesCount){}
    });

    var thumbsOpacity = '0.3';
    var menuOpacity = '0.5';

    var mNr;
    var mNrPrev;

    $('.thumb').live('click', function(e){
        isMenu = false;
        var link = $(this).attr('href');
        nrPrev = nr;
        nr = $(this).find('img').attr('id');

        if (!isGallery)
        {
            $('#'+mNr).animate(
                { opacity: menuOpacity },
                500
            );
            mNr = null;
            mNrPrev = null;
        }

        $('#'+nrPrev).animate(
            { opacity: thumbsOpacity },
            500,
            function(){

            }
        );


        if (!isGallery)
        {
            $('#submenu').fadeOut();
            $('#submenu').html('');
        }

        $('#content').stop(true, true).fadeOut('normal', function(){
            $('#content').html('');
            $('#content').load(link, function() {
                $("#content").find('img').batchImageLoad({
                    loadingCompleteCallback:
                        function(){
                            $('#content').fadeIn();
                        },
                    imageLoadedCallback:
                        function(elementsLoadedCount, totalImagesCount){}
                });                    
            });
        });

        sumMemory = sum;

        e.preventDefault();
    });


    $('.thumb').live('mouseover mouseout', function(event) {
        if (event.type == 'mouseover')
        {
            $(this).find('img').stop(true, true).animate(
                { opacity: 1 },
                500
            );
        }
        else
        {
            var o;
            //alert(isMenu);
            if (nr == $(this).find('img').attr('id') && !isMenu)
            {
                o = '1';
            }
            else
            {
                o = thumbsOpacity;
            }
            $(this).find('img').stop(true, true).animate(
                { opacity: o },
                500
            );
        }
    });

    $('#menu li').hover(function(){
        $(this).stop(true, true).animate(
            { opacity: 1 },
            500
        );
    }, function(){
        var o;
        if (mNr == $(this).attr('id'))
        {
            o = '1';
        }
        else
        {
            o = menuOpacity;
        }

        $(this).stop(true, true).animate(
            { opacity: o },
            500
        );
    });


    $('#menu li').click(function(){
        isMenu = true;
        isFrontPage = false;
        clearInterval(intID);

        if ( $(this).attr('id') != 'me3')
        {
            isGallery = false;
            if (!isMain)
            {
                block = true;
                $('#thumbsC').stop(true, true).fadeOut('normal', function(){
                    $('#thumbsC').html('');
                    $('#thumbsC').load('galeria-0.html', function(){
                        $('#thumbsC').fadeIn();
                    });
                });
                isMain = true;
            }
        }

        if ( $(this).attr('id') == 'me3')
        {
            isMenu = false;
            isMain = false;
            block = false;
            isGalleryFC = true;
            $('#thumbsC').stop(true, true).fadeOut('normal', function(){
                $('#thumbsC').html('');
                $('#thumbsC').load('galeria-1.html', function(){
                    $('#thumbsC').fadeIn();
                });
            });
        }
 
        mNrPrev = mNr;
        mNr = $(this).attr('id');

        $('#'+mNrPrev).animate(
            { opacity: menuOpacity },
            500
        );

        $('#'+nr).animate(
            { opacity: thumbsOpacity },
            500
        );
    });

    $('#menu a.site').click(function(e){
        var link = $(this).attr('href');

        $('#content').stop(true, true).fadeOut('normal', function(){
            $('#content').html('');
            $('#content').load(link, function(){
                $("#content").find('img').batchImageLoad({
                    loadingCompleteCallback:
                        function(){
                            $('#content').fadeIn();
                        },
                    imageLoadedCallback:
                        function(elementsLoadedCount, totalImagesCount){}
                }); 
            });
        });

        $('#submenu').fadeOut('normal', function(){
            $('#submenu').html('');
        });
        
        e.preventDefault();
    });

    $('#menu a.submenu').click(function(e){
        var link = $(this).attr('href');
        var thisID = $(this).attr('id');

        $('#submenu').stop(true, true).fadeOut('normal', function(){
            $('#submenu').html('');
            $('#submenu').load(link, function(){
                $('#subSiteFI').css('color', '#b20000');
                $('#submenu').fadeIn('normal', function(){
                    if (thisID == 'me2a')
                    {
                        $('#subSiteFI').trigger('click');
                    }
                });
            });
        });

        e.preventDefault();
    });

    $('.galleryLink').live('click', function(e){
        isMenu = false;
        isMain = false;
        block = false;
        isGalleryFC = false;
        var link = $(this).attr('href'); 

        $('#thumbsC').stop(true, true).fadeOut('normal', function(){
            $('#thumbsC').html('');
            $('#thumbsC').load(link, function(){
                $('#thumbsC').fadeIn();
            });
        });

        $('.galleryLink').css('color', '#fff');
        $(this).css('color', '#b20000');

        e.preventDefault();
    });

    $('#navRight').live('click', function(){ if (isNav) {
        sum += nextSlide;
        
        if (sum < ( max*imgW ) )
        {
            $('#tInner').stop(true, true).animate(
                { left: '-='+nextSlide+'px' },
                500
            );
        }
        else
        {
            sum = 0;
            $('#tInner').stop(true, true).animate(
                { left: '0' },
                500
            );
        }
    }});

    $('#navLeft').live('click', function(){ if (isNav) {
        if (sum <= 0)
        {
            var iw = (max*imgW)%nextSlide;
            sum = Math.floor((max*imgW)/nextSlide)*nextSlide;
            if ( iw == 0 )
                sum -= nextSlide;
            $('#tInner').stop(true, true).animate(
                { left: '-'+sum+'px' },
                500
            );
        }
        else
        {
            $('#tInner').stop(true, true).animate(
                { left: '+='+nextSlide+'px' },
                500
            );
            sum -= nextSlide;
        }
    }});



    $('#aFoto').live('click', function(e){
        var link = $(this).attr('href');

        if ( sumMemory != sum )
        {
            sum = sumMemory;
            $('#tInner').stop(true, true).animate(
                { left: '-'+sum+'px' },
                500
            );
        }

        nrPrev = nr;
        nr = $(this).find('img').attr('alt');

        if (($('#'+nr).attr('alt')%imgs) == 0)
        {
            sum += nextSlide;
        
            if (sum < ( max*imgW ) )
            {
                $('#tInner').stop(true, true).animate(
                    { left: '-='+nextSlide+'px' },
                    500
                );
            }
            else
            {
                sum = 0;
                $('#tInner').stop(true, true).animate(
                    { left: '0' },
                    500
                );
            }
            sumMemory = sum;
        }

        $('#'+nrPrev).animate(
            { opacity: thumbsOpacity },
            500
        );
        $('#'+nr).animate(
            { opacity: 1 },
            500
        );

        $('#content').stop(true, true).fadeOut('normal', function(){
            $('#content').html('');
            $('#content').load(link, function(){
                $("#content").find('img').batchImageLoad({
                    loadingCompleteCallback:
                        function(){
                            $('#content').fadeIn();
                        },
                    imageLoadedCallback:
                        function(elementsLoadedCount, totalImagesCount){}
                }); 
            });
        });

        e.preventDefault();
    });    
    

    $('.pytanie span').live('mouseover mouseout', function(event) {
        if (event.type == 'mouseover')
        {
            $('#' + $(this).attr('title')).stop(true,true).slideDown();
        }
        else
        {
            $('#' + $(this).attr('title')).stop(true,true).slideUp();
        }
    });

    $('a.subSite').live('click', function(e){
        var link = $(this).attr('href');
        
        $('a.subSite').css({'color': '#fff'});
        $(this).css({'color': '#b20000'});

        $('#content').stop(true, true).fadeOut('normal', function(){
            $('#content').html('');
            $('#content').load(link, function(){
                $("#content").find('img').batchImageLoad({
                    loadingCompleteCallback:
                        function(){
                            $('#content').fadeIn();
                        },
                    imageLoadedCallback:
                        function(elementsLoadedCount, totalImagesCount){}
                }); 
            });
        });

        e.preventDefault();
    });

    $('#faq a').live('click', function(e){
        var link = $(this).attr('href');
        
        $('#content').stop(true, true).fadeOut('normal', function(){
            $('#content').html('');
            $('#content').load(link, function(){
                $("#content").find('img').batchImageLoad({
                    loadingCompleteCallback:
                        function(){
                            $('#content').fadeIn();
                        },
                    imageLoadedCallback:
                        function(elementsLoadedCount, totalImagesCount){}
                }); 
            });
        });

        e.preventDefault();
    });

});

