var tapeta;
$(document).ready(function()
        {
            loadPage(0);
            $('#menu #butt div').css('left', 0);

            $('#slideDown').click(function()
            {
                $('#content').slideUp('fast');
            });

            $('#menu #butt div').click(function()
            {
                $('#content').slideUp('fast');

                if( jQuery('#siteWrapper table.imgs').width() != null )
                {
                    jQuery('#siteWrapper').html('<div id="content"><div id="contentTop"><div id="leftCorner"></div><div id="rest"><div id="slideDown"></div></div></div><div id="contentBottom"></div></div>');
                    jQuery('body').css('background', tapeta);
                    jQuery('#siteFooter').html('');
                }

                jQuery('#imageContainer').hide();

                var leftSite;

                if( $(this).attr('id') == 'home' )
                {
                    leftSite = 0 - parseInt($('#checker').css('left'));
                }
                else if( $(this).attr('id') == 'blog' )
                {
                    leftSite = 47 - parseInt($('#checker').css('left'));
                }
                else if( $(this).attr('id') == 'zdjecia' )
                {
                    leftSite = 104 - parseInt($('#checker').css('left'));
                }
                else if( $(this).attr('id') == 'onas' )
                {
                    leftSite = 164 - parseInt($('#checker').css('left'));
                }
                else if( $(this).attr('id') == 'oferta' )
                {
                    leftSite = 217 - parseInt($('#checker').css('left'));
                }
                else if( $(this).attr('id') == 'terminy' )
                {
                    leftSite = 279 - parseInt($('#checker').css('left'));
                }
                else if( $(this).attr('id') == 'kontakt' )
                {
                    leftSite = 347 - parseInt($('#checker').css('left'));
                }


                $('#checker').animate(
                {
                    left: '+=' + leftSite
                }, 300, function() {});



            });



            //$('#rest').width( window.innerWidth-40
            setTimeout('setRandomBg();', 3000);
            setTimeout('checkHash();', 3500);

            

            



        });

        function checkHash()
        {
            var hash = document.location.hash;
            if( hash.length > 1 )
            {
                hash = hash.substr(1);

                if( hash != 'dzieciece' && hash != 'portrety' && hash != 'slubne' && hash != 'magdaitomek' )
                {
                        jQuery( '#' + hash + ' a' ).click();
                        if( hash == 'zdjecia' )
                        {
                            loadDialog();
                        }
                }
                else
                {
                    if(hash == 'slubne')
                    {
                        loadGallery(0);
                    }
                    else if( hash == 'dzieciece' )
                    {
                        loadGallery(1);
                    }
                    else if( hash == 'portrety' )
                    {
                        loadGallery(2);
                    }
					else if(hash == 'magdaitomek')
					{
						loadGallery(3);
					}
                }
            }
        }

        function schowaj()
        {
            alert('sdsd');
            $('#content').slideUp('slow');

        }

		function hideText()
		{
			$('#content').slideUp('slow');
		}

        function loadPage( page )
        {

			jQuery.ajax(
			{
				url: 'content.php?cont=' + page,
				async: true,
				dataType: 'text',
				error: function(){},
				success: function(src)
				{
					
					$("#content").slideDown("slow");
                                        jQuery("#contentBottom").html(src);
				}
			});
        }

        function loadDialog()
        {
            jQuery.ajax(
			{
				url: 'content.php?cont=1',
				async: true,
				dataType: 'text',
				error: function(){},
				success: function(src)
				{

                                        jQuery('body').append('<div id="zdjeciaDialog"></div>')
                                        jQuery( '#zdjeciaDialog' ).html(src);
                                        jQuery('#zdjeciaDialog').fadeIn('fast');

                                        jQuery('#zdjeciaDialog').mouseleave(function()
                                        {
                                            setTimeout('hideDialog();', 1000);

                                        });
				}
			});

                        
        }

        function deleteDialog()
        {
            
            jQuery('#zdjeciaDialog').remove();
        }

        function hideDialog()
        {
            jQuery('#zdjeciaDialog').fadeOut('slow');

            setTimeout('deleteDialog();',2000);
        }

        function loadGallery( gallery )
        {
            jQuery('body').css('background', '#666768' );
            jQuery('#content').slideUp('slow');
            var wysokosc;
            var iRozmiar = 0;

            if(jQuery.browser.msie )
            {
                wysokosc = parseInt(document.body.clientHeight) - 155;
                if(wysokosc<0)
                {
                    wysokosc = parseInt(document.documentElement.clientHeight) - 155;
                }

            }
            else
            {
                wysokosc = parseInt(window.innerHeight) - 155;
            }

            iRozmiar = getVerticalSize(wysokosc);




            jQuery.ajax(
			{
				url: 'gallery.php?galeria=' + gallery + '&rozmiar=' + iRozmiar,
				async: true,
				dataType: 'text',
				error: function(){},
				success: function(src)
				{
					var aZdjecia = src.split('||');
                                        var iIloscZdjec = aZdjecia.length-1;
                                        szerokosc = 0;
                                        jQuery('#siteFooter').html('<div id="mini"></div>');
                                        jQuery('#siteWrapper').html('<table class="imgs"><tr></tr></table>');

                                        for(var i = 0; i < iIloscZdjec; i++)
                                        {
                                            jQuery('#siteWrapper table tr').append( '<td><div class="photo"><img src="' + aZdjecia[i] + '" /></div></td>' );
                                            jQuery('#mini').append('<div class="miniPhoto"><a href="javascript:void(0);" onclick="scrollToImage(' + i + ');">' + (i + 1) +  '</a></div>');
                                            setTimeout('wait();', 1000);
                                        }

                                        jQuery('#siteWrapper div.photo').click(function()
                                        {
                                            $.scrollTo(this, 1000, {offset:-20});
                                        });

                                        jQuery('#mini div.miniPhoto').mouseenter(function(e)
                                        {
                                            var iNr = parseInt(jQuery(this).children('a').html()) - 1;
                                            var stosunek = parseInt(jQuery('#siteWrapper table img').eq(iNr).width())/parseInt(jQuery('#siteWrapper table img').eq(iNr).height());
                                            jQuery('#miniature').html( '<img src="' + aZdjecia[iNr] + '" width="' + parseInt(stosunek * 50) + '" height="50" />' );
                             
                                            jQuery('#miniature').css('left', e.pageX - 30);
                                            jQuery('#miniature').fadeIn(200);

                                        });

                                        jQuery('#mini div.miniPhoto').mouseleave(function()
                                        {
                                            jQuery('#miniature').hide();

                                        });
				}
			});




        }

        function checkHeight()
        {
            var szer;
            if(jQuery.browser.msie )
            {
                $('#content').width( document.body.clientWidth);
                szer = document.body.clientWidth;
            }
            else
            {
                $('#content').width( window.innerWidth);
                szer = window.innerWidth;
            }

            return szer;
        }

        function getSize( szerokosc )
        {
            var iRozmiar = 1024;

            if( szerokosc > 1100 )
            {
                iRozmiar = 1152;
            }

            if( szerokosc > 1230 )
            {
                iRozmiar = 1280;
            }

            if( szerokosc > 1310 )
            {
                iRozmiar = 1360;
            }

            if( szerokosc > 1390 )
            {
                iRozmiar = 1440;
            }

            if( szerokosc > 1550 )
            {
                iRozmiar = 1600;
            }

            if( szerokosc > 1630 )
            {
                iRozmiar = 1680;
            }

            if( szerokosc > 1870 )
            {
                iRozmiar = 1920;
            }

            return iRozmiar;
        }

        function getVerticalSize( wysokosc )
        {
            var iRozmiar = 0;

            if( wysokosc > 430 )
            {
                iRozmiar = 1;
            }

            if( wysokosc > 530 )
            {
                iRozmiar = 2;
            }

            if( wysokosc > 630 )
            {
                iRozmiar = 3;
            }

            if( wysokosc > 730 )
            {
                iRozmiar = 4;
            }

            if( wysokosc > 830 )
            {
                iRozmiar = 5;
            }

            if( wysokosc > 930 )
            {
                iRozmiar = 6;
            }

            return iRozmiar;
        }


        function setRandomBg()
        {
            var szerokosc = checkHeight();
            var iRozmiar = getSize(szerokosc);
            var iTlo = Math.floor(Math.random()*100)%13;
            var background = 'url(img/tlo/' + iRozmiar + '/' + iTlo + '.jpg) #fff center no-repeat fixed ';
            tapeta = background;
            jQuery('body').css('background', background);
        }

        function scrollToImage(nr)
        {
            var element = $('#siteWrapper div.photo').eq(nr);
            jQuery('#miniature').hide();
            $.scrollTo(element,1000, {offset:-20});

        }

        function wait()
        {

        }